// Copyright Vespa.ai. 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.Cloud;
import com.yahoo.config.provision.CloudName;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.Tags;
import org.junit.Test;
import org.w3c.dom.Document;
import javax.xml.transform.TransformerException;
import java.io.StringReader;
import static com.yahoo.config.provision.Tags.empty;
/**
* @author Ulf Lilleengen
*/
public class OverrideProcessorTest {
private static final String input =
"" +
"" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" 1" +
" " +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" " +
" " +
" \n" +
" \n" +
" \n" +
" " +
" " +
" \n" +
" \n" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
@Test
public void testParsingDefault() throws TransformerException {
String expected = "" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.test, RegionName.defaultName(), expected);
}
@Test
public void testParsingEnvironmentAndRegion() throws TransformerException {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.from("prod"), RegionName.from("us-west"), expected);
}
@Test
public void testParsingEnvironmentUnknownRegion() throws TransformerException {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" \n" +
" \n" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.valueOf("prod"), RegionName.from("unknown"), expected);
}
@Test
public void testParsingEnvironmentNoRegion() throws TransformerException {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" \n" +
" \n" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.from("prod"), RegionName.defaultName(), expected);
}
@Test
public void testParsingDevEnvironment() throws TransformerException {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.from("dev"), RegionName.defaultName(), expected);
}
@Test
public void testParsingDevEnvironmentAndRegion() throws Exception {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.from("dev"), RegionName.from("us-east-1"), expected);
}
@Test
public void testParsingTestEnvironmentUnknownRegion() throws TransformerException {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.from("test"), RegionName.from("us-west"), expected);
}
@Test(expected = IllegalArgumentException.class)
public void testParsingDifferentEnvInParentAndChild() throws TransformerException {
String in = "" +
"" +
" " +
" " +
" " +
"";
Document inputDoc = Xml.getDocument(new StringReader(in));
new OverrideProcessor(InstanceName.from("default"),
Environment.from("prod"),
RegionName.from("us-west"),
Cloud.defaultCloud().name(),
Tags.empty()).process(inputDoc);
}
@Test(expected = IllegalArgumentException.class)
public void testParsingDifferentRegionInParentAndChild() throws TransformerException {
String in = "" +
"" +
" " +
" " +
" " +
"";
Document inputDoc = Xml.getDocument(new StringReader(in));
new OverrideProcessor(InstanceName.from("default"),
Environment.defaultEnvironment(),
RegionName.from("us-west"),
Cloud.defaultCloud().name(),
Tags.empty()).process(inputDoc);
}
@Test
public void testImpliedRequired() throws TransformerException {
String input = "" +
"" +
" " +
" " +
" " +
" " +
" " +
" " +
"";
String expected = "" +
"" +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(input, Environment.dev, RegionName.defaultName(), expected);
}
@Test
public void testNodeElementCancelsImpliedRequired() throws TransformerException {
String input = "" +
"" +
" " +
" " +
" " +
" " +
" " +
"";
String expected = "" +
"" +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(input, Environment.dev, RegionName.defaultName(), expected);
}
/**
* Tests that searchers referred to with idref are overridden per cloud
* and that searchers not referred to with idref are not overridden.
*/
@Test
public void testSearchersReferredWithIdRefPerCloud() throws TransformerException {
String input =
"""
"""";
String expected =
"""
"""";
assertOverride(input, "aws", expected.formatted("AwsSearcher"));
assertOverride(input, "gcp", expected.formatted("GcpSearcher"));
}
private void assertOverride(Environment environment, RegionName region, String expected) throws TransformerException {
assertOverride(input, environment, region, expected);
}
private void assertOverride(String input, Environment environment, RegionName region, String expected) {
assertOverride(input, environment, region, Cloud.defaultCloud().name(), expected);
}
private void assertOverride(String input, String cloudName, String expected) {
assertOverride(input, Environment.defaultEnvironment(), RegionName.defaultName(), CloudName.from(cloudName), expected);
}
private void assertOverride(String input, Environment environment, RegionName region, CloudName cloudName, String expected) {
var inputDoc = Xml.getDocument(new StringReader(input));
try {
var newDoc = new OverrideProcessor(InstanceName.from("default"), environment, region, cloudName, Tags.empty())
.process(inputDoc);
TestBase.assertDocument(expected, newDoc);
} catch (TransformerException e) {
throw new RuntimeException(e);
}
}
}