summaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/ResourceMeterMaintainerTest.java
blob: 540efcba3b850bb1c4b8990514e923e4fa1b1182 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.maintenance;

import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.ControllerTester;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.vespa.hosted.controller.api.integration.noderepository.NodeRepositoryNode;
import com.yahoo.vespa.hosted.controller.api.integration.resource.ResourceSnapshot;
import com.yahoo.vespa.hosted.controller.api.integration.stubs.MockResourceSnapshotConsumer;
import com.yahoo.vespa.hosted.controller.integration.NodeRepositoryClientMock;
import com.yahoo.vespa.hosted.controller.integration.MetricsMock;
import com.yahoo.vespa.hosted.controller.integration.ZoneApiMock;
import org.junit.Test;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

/**
 * @author olaa
 */
public class ResourceMeterMaintainerTest {

    private final double DELTA = Double.MIN_VALUE;
    private NodeRepositoryClientMock nodeRepository = new NodeRepositoryClientMock();
    private MockResourceSnapshotConsumer snapshotConsumer = new MockResourceSnapshotConsumer();
    private MetricsMock metrics = new MetricsMock();

    @Test
    public void testMaintainer() {
        ControllerTester tester = new ControllerTester();
        tester.zoneRegistry().setZones(
                ZoneApiMock.newBuilder().withId("prod.us-east-3").build(),
                ZoneApiMock.newBuilder().withId("prod.us-west-1").build(),
                ZoneApiMock.newBuilder().withId("prod.us-central-1").build(),
                ZoneApiMock.newBuilder().withId("prod.aws-us-east-1").withCloud("aws").build());

        ResourceMeterMaintainer resourceMeterMaintainer = new ResourceMeterMaintainer(tester.controller(), Duration.ofMinutes(5), new JobControl(tester.curator()), nodeRepository, tester.clock(), metrics, snapshotConsumer);
        resourceMeterMaintainer.maintain();
        Map<ApplicationId, ResourceSnapshot> consumedResources = snapshotConsumer.consumedResources();

        assertEquals(2, consumedResources.size());

        ResourceSnapshot app1 = consumedResources.get(ApplicationId.from("tenant1", "app1", "default"));
        ResourceSnapshot app2 = consumedResources.get(ApplicationId.from("tenant2", "app2", "default"));

        assertEquals(24, app1.getResourceAllocation().getCpuCores(), DELTA);
        assertEquals(24, app1.getResourceAllocation().getMemoryGb(), DELTA);
        assertEquals(500, app1.getResourceAllocation().getDiskGb(), DELTA);

        assertEquals(40, app2.getResourceAllocation().getCpuCores(), DELTA);
        assertEquals(24, app2.getResourceAllocation().getMemoryGb(), DELTA);
        assertEquals(500, app2.getResourceAllocation().getDiskGb(), DELTA);

        assertEquals(tester.clock().millis()/1000, metrics.getMetric("metering_last_reported"));
        assertEquals(1112.0d, (Double) metrics.getMetric("metering_total_reported"), DELTA);
    }
}