aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/config/model/ApplicationDeployTest.java
blob: e975421002ef94386bd8ab635d2825fbaf9ad4d8 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model;

import com.yahoo.config.application.api.ApplicationMetaData;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.application.api.UnparsedConfigDefinition;
import com.yahoo.config.model.application.provider.Bundle;
import com.yahoo.config.model.application.provider.DeployData;
import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.document.DataType;
import com.yahoo.io.IOUtils;
import com.yahoo.path.Path;
import com.yahoo.schema.DocumentOnlySchema;
import com.yahoo.schema.Schema;
import com.yahoo.vespa.config.ConfigDefinition;
import com.yahoo.vespa.config.ConfigDefinitionKey;
import com.yahoo.vespa.model.VespaModel;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class ApplicationDeployTest {

    private static final String TESTDIR = "src/test/cfg/application/";
    private static final String TEST_SCHEMAS_DIR = TESTDIR + "app1/schemas/";

    @TempDir
    public File tmpFolder;

    @Test
    void testVespaModel() throws SAXException, IOException {
        ApplicationPackageTester tester = ApplicationPackageTester.create(TESTDIR + "app1");
        new VespaModel(tester.app());
        List<Schema> schemas = tester.getSchemas();
        assertEquals(schemas.size(), 5);
        for (Schema schema : schemas) {
            switch (schema.getName()) {
                case "music":
                case "laptop":
                case "pc":
                case "sock":
                    break;
                case "product":
                    assertTrue(schema instanceof DocumentOnlySchema);
                    assertEquals(DataType.STRING, schema.getDocument().getField("title").getDataType());
                    break;
                default:
                    fail();
            }
        }
        assertEquals(Set.of(new File(TEST_SCHEMAS_DIR + "laptop.sd"),
                        new File(TEST_SCHEMAS_DIR + "music.sd"),
                        new File(TEST_SCHEMAS_DIR + "pc.sd"),
                        new File(TEST_SCHEMAS_DIR + "product.sd"),
                        new File(TEST_SCHEMAS_DIR + "sock.sd")),
                new HashSet<>(tester.app().getSearchDefinitionFiles()));

        List<FilesApplicationPackage.Component> components = tester.app().getComponents();
        assertEquals(1, components.size());
        Map<String, Bundle.DefEntry> defEntriesByName = defEntries2map(components.get(0).getDefEntries());
        assertEquals(5, defEntriesByName.size());

        Bundle.DefEntry def1 = defEntriesByName.get("test-namespace");
        assertNotNull(def1);
        assertEquals("namespace=config\nintVal int default=0", def1.contents);

        Bundle.DefEntry def2 = defEntriesByName.get("namespace-in-filename");
        assertNotNull(def2);
        assertEquals("namespace=a.b\n\ndoubleVal double default=0.0", def2.contents);

        // Check that getFilename works
        ArrayList<String> sdFileNames = new ArrayList<>();
        for (Schema schema : schemas)
            sdFileNames.add(schema.getName() + ApplicationPackage.SD_NAME_SUFFIX);
        Collections.sort(sdFileNames);
        assertEquals("laptop.sd", sdFileNames.get(0));
        assertEquals("music.sd", sdFileNames.get(1));
        assertEquals("pc.sd", sdFileNames.get(2));
        assertEquals("product.sd", sdFileNames.get(3));
        assertEquals("sock.sd", sdFileNames.get(4));
    }

    @Test
    void testGetFile() throws IOException {
        ApplicationPackageTester tester = ApplicationPackageTester.create(TESTDIR + "app1");
        try (Reader foo = tester.app().getFile(Path.fromString("files/foo.json")).createReader()) {
            assertEquals(IOUtils.readAll(foo), "foo : foo\n");
        }
        try (Reader bar = tester.app().getFile(Path.fromString("files/sub/bar.json")).createReader()) {
            assertEquals(IOUtils.readAll(bar), "bar : bar\n");
        }
        assertTrue(tester.app().getFile(Path.createRoot()).exists());
        assertTrue(tester.app().getFile(Path.createRoot()).isDirectory());
    }

    /*
     * Put a list of def entries to a map, with the name as key. This is done because the order
     * of the def entries in the list cannot be guaranteed.
     */
    private Map<String, Bundle.DefEntry> defEntries2map(List<Bundle.DefEntry> defEntries) {
        Map<String, Bundle.DefEntry> ret = new HashMap<>();
        for (Bundle.DefEntry def : defEntries)
            ret.put(def.defName, def);
        return ret;
    }

    @Test
    void include_dirs_are_included() {
        ApplicationPackageTester tester = ApplicationPackageTester.create(TESTDIR + "include_dirs");

        Set<String> includeDirs = new HashSet<>(tester.app().getUserIncludeDirs());
        assertEquals(Set.of("jdisc_dir", "dir1", "dir2", "empty_dir"), includeDirs);
    }

    @Test
    void non_existent_include_dir_is_not_allowed() throws Exception {
        File appDir = newFolder(tmpFolder, "non-existent-include");
        String services =
                "<services version='1.0'>" +
                        "    <include dir='non-existent' />" +
                        "</services>\n";

        IOUtils.writeFile(new File(appDir, "services.xml"), services, false);
        try {
            FilesApplicationPackage.fromFile(appDir);
            fail("Expected exception due to non-existent include dir");
        } catch (IllegalArgumentException e) {
            assertEquals("Cannot include directory 'non-existent', as it does not exist. Directory must reside in application package, and path must be given relative to application package.",
                    e.getMessage());
        }
    }

    @Test
    void testThatModelIsRebuiltWhenSearchDefinitionIsAdded() throws IOException {
        File tmpDir = tmpFolder;
        IOUtils.copyDirectory(new File(TESTDIR, "app1"), tmpDir);
        ApplicationPackageTester tester = ApplicationPackageTester.create(tmpDir.getAbsolutePath());
        assertEquals(5, tester.getSchemas().size());
        File sdDir = new File(tmpDir, "schemas");
        File sd = new File(sdDir, "testfoo.sd");
        IOUtils.writeFile(sd, "search testfoo { document testfoo { field bar type string { } } }", false);
        assertEquals(6, tester.getSchemas().size());
    }

    @Test
    void testThatAppWithDeploymentXmlIsValid() throws IOException {
        File tmpDir = tmpFolder;
        IOUtils.copyDirectory(new File(TESTDIR, "app1"), tmpDir);
        ApplicationPackageTester.create(tmpDir.getAbsolutePath());
    }

    @Test
    void testThatAppWithIllegalDeploymentXmlIsNotValid() {
        assertThrows(IllegalArgumentException.class, () -> {
            File tmpDir = tmpFolder;
            IOUtils.copyDirectory(new File(TESTDIR, "app_invalid_deployment_xml"), tmpDir);
            ApplicationPackageTester.create(tmpDir.getAbsolutePath());
        });
    }

    @Test
    void testComplicatedDeploymentSpec() throws IOException {
        File tmpDir = tmpFolder;
        IOUtils.copyDirectory(new File(TESTDIR, "app_complicated_deployment_spec"), tmpDir);
        ApplicationPackageTester.create(tmpDir.getAbsolutePath());
    }

    @Test
    void testAppWithEmptyProdRegion() throws IOException {
        File tmpDir = tmpFolder;
        IOUtils.copyDirectory(new File(TESTDIR, "empty_prod_region_in_deployment_xml"), tmpDir);
        ApplicationPackageTester.create(tmpDir.getAbsolutePath());
    }

    @Test
    void testThatAppWithInvalidParallelDeploymentFails() throws IOException {
        String expectedMessage = """
                4:  <staging/>
                5:  <prod>
                6:    <parallel>
                7:      <instance id="hello" />
                8:    </parallel>
                9:  </prod>
                10:</deployment>
                """;
        File tmpDir = tmpFolder;
        IOUtils.copyDirectory(new File(TESTDIR, "invalid_parallel_deployment_xml"), tmpDir);
        try {
            ApplicationPackageTester.create(tmpDir.getAbsolutePath());
            fail("Expected exception");
        } catch (IllegalArgumentException e) {
            assertEquals("Invalid XML according to XML schema, error in deployment.xml: element \"instance\" not allowed here; expected the element end-tag or element \"delay\", \"region\", \"steps\" or \"test\" [7:30], input:\n" + expectedMessage,
                    e.getMessage());
        }
    }

    @Test
    void testConfigDefinitionsFromJars() {
        String appName = "src/test/cfg//application/app1";
        FilesApplicationPackage app = FilesApplicationPackage.fromFile(new File(appName), false);
        Map<ConfigDefinitionKey, UnparsedConfigDefinition> defs = app.getAllExistingConfigDefs();
        assertEquals(5, defs.size());
    }

    @Test
    void testMetaData() throws IOException {
        File tmp = tmpFolder;
        String appPkg = TESTDIR + "app1";
        IOUtils.copyDirectory(new File(appPkg), tmp);
        ApplicationId applicationId = ApplicationId.from("tenant1", "application1", "instance1");
        DeployData deployData = new DeployData("bar",
                                               applicationId,
                                               13L,
                                               false,
                                               1337L,
                                               3L);
        FilesApplicationPackage app = FilesApplicationPackage.fromFileWithDeployData(tmp, deployData);
        app.writeMetaData();
        FilesApplicationPackage newApp = FilesApplicationPackage.fromFileWithDeployData(tmp, deployData);
        ApplicationMetaData meta = newApp.getMetaData();
        assertEquals("bar", meta.getDeployPath());
        assertEquals(applicationId, meta.getApplicationId());
        assertEquals(13L, (long) meta.getDeployTimestamp());
        assertEquals(1337L, (long) meta.getGeneration());
        assertEquals(3L, meta.getPreviousActiveGeneration());
        String checksum = meta.getChecksum();
        assertNotNull(checksum);

        assertTrue((new File(tmp, "hosts.xml")).delete());
        FilesApplicationPackage app2 = FilesApplicationPackage.fromFileWithDeployData(tmp, deployData);
        String app2Checksum = app2.getMetaData().getChecksum();
        assertNotEquals(checksum, app2Checksum);

        assertTrue((new File(tmp, "files/foo.json")).delete());
        FilesApplicationPackage app3 = FilesApplicationPackage.fromFileWithDeployData(tmp, deployData);
        String app3Checksum = app3.getMetaData().getChecksum();
        assertNotEquals(app2Checksum, app3Checksum);
    }

    @Test
    void testGetJarEntryName() {
        JarEntry e = new JarEntry("/schemas/foo.sd");
        assertEquals(ApplicationPackage.getFileName(e), "foo.sd");
        e = new JarEntry("bar");
        assertEquals(ApplicationPackage.getFileName(e), "bar");
        e = new JarEntry("");
        assertEquals(ApplicationPackage.getFileName(e), "");
    }

    @Test
    void testGetJarEntryNameForLegacyPath() {
        JarEntry e = new JarEntry("/searchdefinitions/foo.sd");
        assertEquals(ApplicationPackage.getFileName(e), "foo.sd");
        e = new JarEntry("bar");
        assertEquals(ApplicationPackage.getFileName(e), "bar");
        e = new JarEntry("");
        assertEquals(ApplicationPackage.getFileName(e), "");
    }

    @AfterEach
    public void cleanDirs() {
        IOUtils.recursiveDeleteDir(new File(TESTDIR + "app1/myDir"));
        IOUtils.recursiveDeleteDir(new File(TESTDIR + "app1/searchdefinitions/myDir2"));
        IOUtils.recursiveDeleteDir(new File(TESTDIR + "app1/myDir3"));
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    @AfterEach
    public void cleanFiles() {
        new File(new File(TESTDIR + "app1"),"foo.txt").delete();
        new File(new File(TESTDIR + "app1"),"searchdefinitions/bar.text").delete();
        IOUtils.recursiveDeleteDir(new File(TESTDIR + "app1/mySubDir"));
    }

    /**
     * Tests that an invalid jar is identified as not being a jar file
     */
    @Test
    void testInvalidJar() {
        try {
            FilesApplicationPackage.getComponents(new File("src/test/cfg/application/validation/invalidjar_app"));
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Error opening jar file 'invalid.jar'. Please check that this is a valid jar file",
                    e.getMessage());
        }
    }

    /**
     * Tests that config definitions with namespace are treated properly when they have the format
     * as in the config definitions dir ($VESPA_HOME/share/vespa/configdefinitions on a machine
     * with Vespa packages installed) (does not test when read from user def files). Also tests a config
     * definition without version in file name
     */
    @Test
    void testConfigDefinitionsAndNamespaces() {
        final File appDir = new File("src/test/cfg/application/configdeftest");
        FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir);

        DeployState deployState = new DeployState.Builder().applicationPackage(app).build();

        ConfigDefinition def = deployState.getConfigDefinition(new ConfigDefinitionKey("baz", "xyzzy")).get();
        assertEquals("xyzzy", def.getNamespace());

        def = deployState.getConfigDefinition(new ConfigDefinitionKey("foo", "qux")).get();
        assertEquals("qux", def.getNamespace());

        // A config def without version in filename and version in file header
        def = deployState.getConfigDefinition(new ConfigDefinitionKey("bar", "xyzzy")).get();
        assertEquals("xyzzy", def.getNamespace());
        assertEquals("bar", def.getName());
    }

    @Test
    void testDifferentNameOfSdFileAndSearchName() {
        assertThrows(IllegalArgumentException.class, () -> {
            ApplicationPackageTester tester = ApplicationPackageTester.create(TESTDIR + "sdfilenametest");
            new DeployState.Builder().applicationPackage(tester.app()).build();
        });
    }

    private static File newFolder(File root, String... subDirs) throws IOException {
        String subFolder = String.join("/", subDirs);
        File result = new File(root, subFolder);
        if (!result.mkdirs()) {
            throw new IOException("Couldn't create folders " + root);
        }
        return result;
    }

}