aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/application/DeploymentQuotaCalculatorTest.java
blob: 0a262a41138ad1be4d8b5b1a38caf30120ce7060 (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
63
64
65
66
67
68
69
70
71
package com.yahoo.vespa.hosted.controller.application;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yahoo.config.application.api.DeploymentSpec;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.api.integration.billing.Quota;
import com.yahoo.vespa.hosted.controller.api.integration.noderepository.ApplicationData;
import org.junit.Test;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class DeploymentQuotaCalculatorTest {

    @Test
    public void quota_is_divided_among_prod_instances() {
        Quota calculated = DeploymentQuotaCalculator.calculate(Quota.unlimited().withBudget(10), List.of(), ApplicationId.defaultId(), ZoneId.defaultId(),
                DeploymentSpec.fromXml(
                        "<deployment version='1.0'>\n" +
                                "  <instance id='instance1'> \n" +
                                "    <test />\n" +
                                "    <staging />\n" +
                                "    <prod>\n" +
                                "      <region active=\"true\">us-east-1</region>\n" +
                                "      <region active=\"false\">us-west-1</region>\n" +
                                "    </prod>\n" +
                                "  </instance>\n" +
                                "  <instance id='instance2'>\n" +
                                "    <perf/>\n" +
                                "    <dev/>\n" +
                                "    <prod>\n" +
                                "      <region active=\"true\">us-north-1</region>\n" +
                                "    </prod>\n" +
                                "  </instance>\n" +
                                "</deployment>"));
        assertEquals(10d/3, calculated.budget().get().doubleValue(), 1e-5);
    }

    @Test
    public void unlimited_quota_remains_unlimited() {
        Quota calculated = DeploymentQuotaCalculator.calculate(Quota.unlimited(), List.of(), ApplicationId.defaultId(), ZoneId.defaultId(), DeploymentSpec.empty);
        assertTrue(calculated.isUnlimited());
    }

    @Test
    public void zero_quota_remains_zero() {
        Quota calculated = DeploymentQuotaCalculator.calculate(Quota.zero(), List.of(), ApplicationId.defaultId(), ZoneId.defaultId(), DeploymentSpec.empty);
        assertEquals(calculated.budget().get().doubleValue(), 0, 1e-5);
    }

    @Test
    public void using_highest_resource_use() throws Exception {
        var content = new String(Files.readAllBytes(Paths.get("src/test/java/com/yahoo/vespa/hosted/controller/application/response/application.json")));
        var mapper = new ObjectMapper();
        var application = mapper.readValue(content, ApplicationData.class).toApplication();
        var usage = DeploymentQuotaCalculator.calculateQuotaUsage(application);
        assertEquals(1.164, usage.rate(), 0.001);
    }

    @Test
    public void tenant_quota_in_pipeline() {
        var tenantQuota = Quota.unlimited().withBudget(42);
        var calculated = DeploymentQuotaCalculator.calculate(tenantQuota, List.of(), ApplicationId.defaultId(), ZoneId.from("test", "apac1"), DeploymentSpec.empty);
        assertEquals(tenantQuota, calculated);
    }
}