/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */

package org.elasticsearch.xpack.autoscaling.action;

import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.monitor.os.OsProbe;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.xpack.autoscaling.AutoscalingIntegTestCase;
import org.elasticsearch.xpack.autoscaling.capacity.AutoscalingCapacity;
import org.hamcrest.Matchers;

import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.greaterThan;

@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0)
public class TransportGetAutoscalingCapacityActionIT extends AutoscalingIntegTestCase {

    @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/67089")
    public void testCurrentCapacity() throws Exception {
        assertThat(capacity().results().keySet(), Matchers.empty());
        long memory = OsProbe.getInstance().getTotalPhysicalMemorySize();
        long storage = internalCluster().getInstance(NodeEnvironment.class).nodePaths()[0].fileStore.getTotalSpace();
        assertThat(memory, greaterThan(0L));
        assertThat(storage, greaterThan(0L));
        putAutoscalingPolicy("test");
        assertCurrentCapacity(0, 0, 0);

        int nodes = between(1, 5);
        internalCluster().startDataOnlyNodes(nodes);

        assertBusy(() -> { assertCurrentCapacity(memory, storage, nodes); });
    }

    public void assertCurrentCapacity(long memory, long storage, int nodes) {
        GetAutoscalingCapacityAction.Response capacity = capacity();
        AutoscalingCapacity currentCapacity = capacity.results().get("test").currentCapacity();
        assertThat(currentCapacity.node().memory().getBytes(), Matchers.equalTo(memory));
        assertThat(currentCapacity.total().memory().getBytes(), Matchers.equalTo(memory * nodes));
        assertThat(currentCapacity.node().storage().getBytes(), Matchers.equalTo(storage));
        assertThat(currentCapacity.total().storage().getBytes(), Matchers.equalTo(storage * nodes));
    }

    public GetAutoscalingCapacityAction.Response capacity() {
        GetAutoscalingCapacityAction.Request request = new GetAutoscalingCapacityAction.Request();
        GetAutoscalingCapacityAction.Response response = client().execute(GetAutoscalingCapacityAction.INSTANCE, request).actionGet();
        return response;
    }

    private void putAutoscalingPolicy(String policyName) {
        final PutAutoscalingPolicyAction.Request request = new PutAutoscalingPolicyAction.Request(
            policyName,
            new TreeSet<>(Set.of("data")),
            new TreeMap<>()
        );
        assertAcked(client().execute(PutAutoscalingPolicyAction.INSTANCE, request).actionGet());
    }

}

@@ -318,27 +318,194 @@
 rch.
-env.NodeEnvironment
+action.admin.cluster.node.info.NodesInfoResponse;%0Aimport org.elasticsearch.bootstrap.JavaVersion;%0Aimport org.elasticsearch.env.NodeEnvironment;%0Aimport org.elasticsearch.monitor.os.OsInfo
 ;%0Aim
@@ -1156,141 +1156,652 @@
     
-@AwaitsFix(bugUrl = %22https://github.com/elastic/elasticsearch/issues/67089%22)%0A    public void testCurrentCapacity() throws Exception %7B
+public void testCurrentCapacity() throws Exception %7B%0A        final NodesInfoResponse response = client().admin().cluster().prepareNodesInfo().execute().actionGet();%0A        final boolean anyDebian8Nodes = response.getNodes()%0A            .stream()%0A            .anyMatch(ni -%3E ni.getInfo(OsInfo.class).getPrettyName().equals(%22Debian GNU/Linux 8 (jessie)%22));%0A        boolean java15Plus = JavaVersion.current().compareTo(JavaVersion.parse(%2215%22)) %3E= 0;%0A        // see: https://github.com/elastic/elasticsearch/issues/67089#issuecomment-756114654%0A        assumeTrue(%22cannot run on debian 8 prior to java 15%22, java15Plus %7C%7C anyDebian8Nodes == false);%0A
 %0A   

Disable autoscaling capacity test on debian 8 (#67159)