Skip to content

Commit

Permalink
Make pre-processing respect region for dev environments
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolden committed Jan 11, 2019
1 parent 25221db commit c60c773
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ private boolean matches(Set<Environment> elementEnvironments, Set<RegionName> el
}

if ( ! elementRegions.isEmpty()) { // match region
// match region in prod only
if ( environment.equals(Environment.prod) && ! elementRegions.contains(region)) return false;
// match region in multi-region environments only
if ( environment.isMultiRegion() && ! elementRegions.contains(region)) return false;

// explicit region implies prod
if ( ! environment.equals(Environment.prod) && elementEnvironments.isEmpty() ) return false;
// explicit region implies multi-region environment
if ( ! environment.isMultiRegion() && elementEnvironments.isEmpty() ) return false;
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void testParsingDev() throws TransformerException {
" </component>\n" +
" </container>\n" +
"</services>";
assertOverride(Environment.dev, RegionName.defaultName(), expected);
assertOverride(Environment.dev, RegionName.from("us-east-3"), expected);
}

@Test
Expand Down Expand Up @@ -118,7 +118,7 @@ public void testParsingDevWithIds() throws TransformerException {
" </component>\n" +
" </container>\n" +
"</services>";
assertOverrideWithIds(Environment.dev, RegionName.defaultName(), expected);
assertOverrideWithIds(Environment.dev, RegionName.from("us-east-3"), expected);
}

private void assertOverride(Environment environment, RegionName region, String expected) throws TransformerException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
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;

/**
Expand Down Expand Up @@ -42,6 +38,11 @@ public class OverrideProcessorTest {
" <document deploy:environment='prod' mode='index' type='music3'/>\n" +
" <document deploy:environment='prod' deploy:region='us-west' mode='index' type='music4'/>\n" +
" </documents>" +
" <documents deploy:environment='dev'>" +
" <document mode='store-only' type='music'/>\n" +
" <document type='music5' mode='streaming' />\n" +
" <document deploy:region='us-east-1' type='music6' mode='streaming' />\n" +
" </documents>" +
" <documents>" +
" <document mode='store-only' type='music'/>\n" +
" <document type='music2' mode='streaming' />\n" +
Expand Down Expand Up @@ -214,7 +215,7 @@ public void testParsingDevEnvironment() throws TransformerException {
" <redundancy>1</redundancy>" +
" <documents>" +
" <document mode=\"store-only\" type=\"music\"/>" +
" <document mode=\"streaming\" type=\"music2\"/>" +
" <document mode=\"streaming\" type=\"music5\"/>" +
" </documents>" +
" <nodes>" +
" <node distribution-key=\"0\" hostalias=\"node0\"/>" +
Expand All @@ -231,6 +232,35 @@ public void testParsingDevEnvironment() throws TransformerException {
assertOverride(Environment.from("dev"), RegionName.defaultName(), expected);
}

@Test
public void testParsingDevEnvironmentAndRegion() throws Exception {
String expected =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
"<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" +
" <admin version=\"2.0\">" +
" <adminserver hostalias=\"node0\"/>" +
" </admin>" +
" <content id=\"foo\" version=\"1.0\">" +
" <redundancy>1</redundancy>" +
" <documents>" +
" <document mode=\"streaming\" type=\"music6\"/>" +
" </documents>" +
" <nodes>" +
" <node distribution-key=\"0\" hostalias=\"node0\"/>" +
" </nodes>" +
" </content>" +
" <jdisc id=\"stateless\" version=\"1.0\">" +
" <search/>" +
" <component id=\"foo\" class=\"MyFoo\" bundle=\"foobundle\" />" +
" <nodes>" +
" <node hostalias=\"node0\"/>" +
" </nodes>" +
" </jdisc>" +
"</services>";

assertOverride(Environment.from("dev"), RegionName.from("us-east-1"), expected);
}

@Test
public void testParsingTestEnvironmentUnknownRegion() throws TransformerException {
String expected =
Expand Down Expand Up @@ -317,7 +347,7 @@ public void testParsingDifferentRegionInParentAndChild() throws TransformerExcep
}

private void assertOverride(Environment environment, RegionName region, String expected) throws TransformerException {
Document inputDoc = Xml.getDocument(new StringReader(input));
Document inputDoc = Xml.getDocument(new StringReader(OverrideProcessorTest.input));
Document newDoc = new OverrideProcessor(environment, region).process(inputDoc);
TestBase.assertDocument(expected, newDoc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public enum Environment {
/** Returns whether this environment is production (prod) */
public boolean isProduction() { return this == prod; }

/** Returns whether this environment can exist in multiple regions */
public boolean isMultiRegion() { return this == prod || this == dev; }

/** Returns the prod environment. This is useful for non-hosted properties where we just need any consistent value */
public static Environment defaultEnvironment() { return prod; }

Expand Down

0 comments on commit c60c773

Please sign in to comment.