aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/restapi/ApplicationPatcherTest.java
blob: ad870398420feab0ea8a6ab396f9770e65d42228 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.restapi;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Capacity;
import com.yahoo.config.provision.ClusterResources;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.vespa.hosted.provision.NodeRepositoryTester;
import com.yahoo.vespa.hosted.provision.applications.Application;
import com.yahoo.vespa.hosted.provision.applications.BcpGroupInfo;
import com.yahoo.vespa.hosted.provision.applications.Cluster;
import org.junit.Test;

import java.io.ByteArrayInputStream;

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

/**
 * @author bratseth
 */
public class ApplicationPatcherTest {

    @Test
    public void testPatching() {
        NodeRepositoryTester tester = new NodeRepositoryTester();
        Application application = Application.empty(ApplicationId.from("t1", "a1", "i1"));
        tester.nodeRepository().applications().put(application, tester.nodeRepository().applications().lock(application.id()));
        String patch = "{ \"currentReadShare\" :0.4, \"maxReadShare\": 1.0 }";
        ApplicationPatcher patcher = new ApplicationPatcher(new ByteArrayInputStream(patch.getBytes()),
                                                            application.id(),
                                                            tester.nodeRepository());
        Application patched = patcher.apply();
        assertEquals(0.4, patched.status().currentReadShare(), 0.0000001);
        assertEquals(1.0, patched.status().maxReadShare(), 0.0000001);
        patcher.close();
    }

    @Test
    public void testPatchingWithBcpGroupInfo() {
        var c1 = ClusterSpec.Id.from("c1");
        var c2 = ClusterSpec.Id.from("c2");
        var capacity = Capacity.from(new ClusterResources(10, 1, new NodeResources(1.0, 10.0, 100.0, 3.0)));
        NodeRepositoryTester tester = new NodeRepositoryTester();
        Application application = Application.empty(ApplicationId.from("t1", "a1", "i1"));
        application = application.with(Cluster.create(c1, false, capacity));
        application = application.with(Cluster.create(c2, false, capacity));
        tester.nodeRepository().applications().put(application, tester.nodeRepository().applications().lock(application.id()));

        String patch = """
        {
          "currentReadShare": 0.4,
          "maxReadShare": 1.0,
          "clusters": {
            "c1": {
              "bcpGroupInfo": {
                "queryRate": 0.1,
                "growthRateHeadroom": 0.2,
                "cpuCostPerQuery": 0.3
              }
            },
            "c2": {
              "bcpGroupInfo": {
                "queryRate": 1,
                "growthRateHeadroom": 2,
                "cpuCostPerQuery": 3
              }
            },
            "ignored": {
              "bcpGroupInfo": {
                "queryRate": 1,
                "growthRateHeadroom": 2,
                "cpuCostPerQuery": 3
              }
            }
          }
        }
        """;

        ApplicationPatcher patcher = new ApplicationPatcher(new ByteArrayInputStream(patch.getBytes()),
                                                            application.id(),
                                                            tester.nodeRepository());
        Application patched = patcher.apply();
        assertEquals(0.4, patched.status().currentReadShare(), 0.0000001);
        assertEquals(1.0, patched.status().maxReadShare(), 0.0000001);
        assertEquals(new BcpGroupInfo(0.1, 0.2, 0.3),
                     patched.cluster(c1).get().bcpGroupInfo());
        assertEquals(new BcpGroupInfo(1.0, 2.0, 3.0),
                     patched.cluster(c2).get().bcpGroupInfo());
        assertTrue(patched.cluster(ClusterSpec.Id.from("ignored")).isEmpty());
        patcher.close();
    }

}