aboutsummaryrefslogtreecommitdiffstats
path: root/config-application-package/src/test/java/com/yahoo/config/model/application/provider/FilesApplicationPackageTest.java
blob: c93e1a1672623c3930bbed43a4efede27aab5830 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.application.provider;

import com.yahoo.config.application.TestBase;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.Zone;
import com.yahoo.io.IOUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;

import static com.yahoo.config.model.application.provider.FilesApplicationPackage.applicationFile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author Ulf Lilleengen
 */
public class FilesApplicationPackageTest {

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Test
    public void testPreprocessing() throws IOException {
        File appDir = temporaryFolder.newFolder();
        IOUtils.copyDirectory(new File("src/test/resources/multienvapp"), appDir);
        assertTrue(new File(appDir, "services.xml").exists());
        assertTrue(new File(appDir, "hosts.xml").exists());
        FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir);

        ApplicationPackage processed = app.preprocess(new Zone(Environment.dev, RegionName.defaultName()),
                                                      new BaseDeployLogger());
        assertTrue(new File(appDir, ".preprocessed").exists());
        String expectedServices = """
                                  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                                  <!-- Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -->
                                  <services xmlns:deploy="vespa" xmlns:preprocess="properties" version="1.0">
                                      <admin version="2.0">
                                          <adminserver hostalias="node0"/>
                                      </admin>
                                      <content id="foo" version="1.0">
                                        <redundancy>1</redundancy>
                                        <documents>
                                          <document mode="index" type="music.sd"/>
                                        </documents>
                                        <nodes>
                                          <node distribution-key="0" hostalias="node0"/>
                                        </nodes>
                                      </content>
                                      <container id="stateless" version="1.0">
                                        <search/>
                                        <component bundle="foobundle" class="MyFoo" id="foo"/>
                                        <component bundle="foobundle" class="TestBar" id="bar"/>
                                        <nodes>
                                          <node hostalias="node0" baseport="5000"/>
                                        </nodes>
                                      </container>
                                  </services>""";
        TestBase.assertDocument(expectedServices, processed.getServices());
        String expectedHosts = """
                               <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                               <!-- Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -->
                               <hosts xmlns:deploy="vespa" xmlns:preprocess="properties">
                                   <host name="bar.yahoo.com">
                                       <alias>node1</alias>
                                   </host>
                               </hosts>""";
        TestBase.assertDocument(expectedHosts, processed.getHosts());
    }

    @Test
    public void testDeploymentXmlNotAvailable()  {
        File appDir = new File("src/test/resources/multienvapp");
        assertFalse(new File(appDir, "deployment.xml").exists());
        FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir);
        assertFalse(app.getDeployment().isPresent());
        assertTrue(app.getDeploymentSpec().isEmpty());
    }

    @Test
    public void testDeploymentXml() throws IOException {
        File appDir = new File("src/test/resources/app-with-deployment");
        final File deployment = new File(appDir, "deployment.xml");
        assertTrue(deployment.exists());
        FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir);
        assertTrue(app.getDeployment().isPresent());
        assertFalse(app.getDeploymentSpec().isEmpty());
        assertFalse(app.getMajorVersion().isPresent());
        assertEquals(IOUtils.readAll(app.getDeployment().get()), IOUtils.readAll(new FileReader(deployment)));
    }

    @Test
    public void testPinningMajorVersion() throws IOException {
        File appDir = new File("src/test/resources/app-pinning-major-version");
        final File deployment = new File(appDir, "deployment.xml");
        assertTrue(deployment.exists());
        FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir);
        assertTrue(app.getDeployment().isPresent());
        assertTrue(app.getMajorVersion().isPresent());
        assertEquals(6, (int)app.getMajorVersion().get());
        assertEquals(IOUtils.readAll(app.getDeployment().get()), IOUtils.readAll(new FileReader(deployment)));
    }

    @Test
    public void testLegacyOverrides() {
        File appDir = new File("src/test/resources/app-legacy-overrides");
        ApplicationPackage app = FilesApplicationPackage.fromFile(appDir);
        var overrides = app.legacyOverrides();
        assertEquals(2, overrides.size());
        assertEquals("something here", overrides.get("foo-bar"));
        assertEquals("false", overrides.get("v7-geo-positions"));
    }

    @Test
    public void failOnMissingServicesXml() throws IOException {
        File appDir = temporaryFolder.newFolder();
        IOUtils.copyDirectory(new File("src/test/resources/multienvapp"), appDir);
        Files.delete(new File(appDir, "services.xml").toPath());
        FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir);
        var exception = assertThrows(IllegalArgumentException.class,
                                     () -> app.preprocess(new Zone(Environment.dev, RegionName.defaultName()), new BaseDeployLogger()));
        assertEquals("services.xml does not exist in application package", exception.getMessage());
    }

    @Test
    public void testApplicationFile() {
        applicationFile(new File("foo"), "");
        applicationFile(new File("foo"), "bar");
        applicationFile(new File(new File(""), ""), "");
        assertEquals("/ is not a child of ",
                     assertThrows(IllegalArgumentException.class,
                                  () -> applicationFile(new File(""), ""))
                             .getMessage());
        assertEquals("'..' is not allowed in path",
                     assertThrows(IllegalArgumentException.class,
                                  () -> applicationFile(new File("foo"), ".."))
                             .getMessage());
    }

    @Test
    public void testValidFileExtensions() {
        File appDir = new File("src/test/resources/app-with-deployment");
        FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir);
        app.validateFileExtensions();
    }

    @Test
    public void testInvalidFileExtensions() {
        File appDir = new File("src/test/resources/app-with-invalid-files-in-subdir");
        FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir);
        try {
            app.validateFileExtensions();
            fail("expected an exception");
        } catch (IllegalArgumentException e) {
            assertEquals("File in application package with unknown extension: search/query-profiles/file-with-invalid.extension, " +
                                 "please delete or move file to another directory.",
                         e.getMessage());
        }
    }

    @Test
    public void testInvalidFileExtensionInSubDirOfSubDir() {
        File appDir = new File("src/test/resources/app-with-files-with-invalid-extension-in-subdir-of-subdir/");
        FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir);
        try {
            app.validateFileExtensions();
            fail("expected an exception");
        } catch (IllegalArgumentException e) {
            assertEquals("File in application package with unknown extension: schemas/foo/bar.junk, " +
                                 "please delete or move file to another directory.",
                         e.getMessage());
        }
    }

}