summaryrefslogtreecommitdiffstats
path: root/config-application-package/src/test/java/com/yahoo/config/application/HostedOverrideProcessorTest.java
blob: 1a4dab019300d094c3de6cb3d746184a1edbf3ba (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.application;

import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.RegionName;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.io.StringReader;

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

    static {
        XMLUnit.setIgnoreWhitespace(true);
    }

    private static final String input =
            "<?xml version='1.0' encoding='UTF-8' standalone='no'?>" +
                    "<services xmlns:deploy='vespa' xmlns:preprocess='?' version='1.0'>" +
                    "  <container id='foo' version='1.0'>" +
                    "    <nodes count='1'/>" +
                    "    <nodes count='3' deploy:environment='perf'/>" +
                    "    <nodes deploy:environment='staging' count='2' required='true'/>" +
                    "    <nodes deploy:environment='prod' count='3' flavor='v-4-8-100'/>" +
                    "    <nodes deploy:environment='prod' deploy:region='us-west' count='4'/>" +
                    "    <nodes deploy:environment='prod' deploy:region='us-east-3' flavor='v-8-8-100' count='5'/>" +
                    "    <nodes deploy:instance='myinstance' deploy:environment='prod' deploy:region='us-west' count='1'/>" +
                    "  </container>" +
                    "</services>";


    @Test
    public void testParsingDefault() throws TransformerException {
        String expected = 
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
                "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" +
                "  <container id=\"foo\" version=\"1.0\">" +
                "    <nodes count='1'/>" +
                "  </container>" +
                "</services>";
        assertOverride(Environment.test, RegionName.defaultName(), expected);
    }

    @Test
    public void testParsingEnvironmentAndRegion() throws TransformerException {
        String expected = 
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
                "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" +
                "  <container id=\"foo\" version=\"1.0\">" +
                "    <nodes count='4' required='true'/>" +
                "  </container>" +
                "</services>";
        assertOverride(Environment.from("prod"), RegionName.from("us-west"), expected);
    }

    @Test
    public void testParsingInstance() throws TransformerException {
        String expected =
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
                "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" +
                "  <container id=\"foo\" version=\"1.0\">" +
                "    <nodes count='1' required='true'/>" +
                "  </container>" +
                "</services>";
        assertOverride(InstanceName.from("myinstance"), Environment.from("prod"), RegionName.from("us-west"), expected);
    }

    @Test
    public void testParsingEnvironmentAndRegion2() throws TransformerException {
        String expected =
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
                        "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" +
                        "  <container id=\"foo\" version=\"1.0\">" +
                        "    <nodes count='5' flavor='v-8-8-100' required='true'/>" +
                        "  </container>" +
                        "</services>";
        assertOverride(Environment.from("prod"), RegionName.from("us-east-3"), expected);
    }

    @Test
    public void testParsingEnvironmentAndRegion3() throws TransformerException {
        String expected =
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
                        "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" +
                        "  <container id=\"foo\" version=\"1.0\">" +
                        "    <nodes count='3' required='true'/>" +
                        "  </container>" +
                        "</services>";
        assertOverride(Environment.from("perf"), RegionName.from("us-east-3"), expected);
    }

    @Test
    public void testParsingEnvironmentUnknownRegion() throws TransformerException {
        String expected =
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
                "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" +
                "  <container id=\"foo\" version=\"1.0\">" +
                "    <nodes count='3' flavor='v-4-8-100' required='true'/>" +
                "  </container>" +
                "</services>";
        assertOverride(Environment.valueOf("prod"), RegionName.from("unknown"), expected);
    }

    @Test
    public void testParsingEnvironmentNoRegion() throws TransformerException {
        String expected =
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
                "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" +
                "  <container id=\"foo\" version=\"1.0\">" +
                "    <nodes count='3' flavor='v-4-8-100' required='true'/>" +
                "  </container>" +
                "</services>";
        assertOverride(Environment.from("prod"), RegionName.defaultName(), expected);
    }

    @Test
    public void testParsingUnknownEnvironment() throws TransformerException {
        String expected =
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
                "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" +
                "  <container id=\"foo\" version=\"1.0\">" +
                "    <nodes count='1'/>" +
                "  </container>" +
                "</services>";
        assertOverride(Environment.from("dev"), RegionName.defaultName(), expected);
    }

    @Test
    public void testParsingUnknownEnvironmentUnknownRegion() throws TransformerException {
        String expected =
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
                "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" +
                "  <container id=\"foo\" version=\"1.0\">" +
                "    <nodes count='1'/>" +
                "  </container>" +
                "</services>";
        assertOverride(Environment.from("test"), RegionName.from("us-west"), expected);
    }

    @Test
    public void testParsingInheritEnvironment() throws TransformerException {
        String expected =
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
                "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" +
                "  <container id=\"foo\" version=\"1.0\">" +
                "    <nodes count='2' required='true'/>" +
                "  </container>" +
                "</services>";
        assertOverride(Environment.from("staging"), RegionName.from("us-west"), expected);
    }

    private void assertOverride(Environment environment, RegionName region, String expected) throws TransformerException {
        assertOverride(InstanceName.from("default"), environment, region, expected);
    }

    private void assertOverride(InstanceName instance, Environment environment, RegionName region, String expected) throws TransformerException {
        Document inputDoc = Xml.getDocument(new StringReader(input));
        Document newDoc = new OverrideProcessor(instance, environment, region).process(inputDoc);
        TestBase.assertDocument(expected, newDoc);
    }

}