aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/application/validation/BundleValidatorTest.java
blob: 0e98225aba5245bcb78dca39915b9695277fbbe6 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.model.deploy.DeployState;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;

import static com.yahoo.yolean.Exceptions.uncheck;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class BundleValidatorTest {
    @TempDir
    public File tempDir;

    @Test
    void basicBundleValidation() throws Exception {
        // Valid jar file
        JarFile ok = createTemporaryJarFile(tempDir, "ok");
        BundleValidator bundleValidator = new BundleValidator();
        bundleValidator.validateJarFile(DeployState.createTestState(), ok);

        // No manifest
        validateWithException("nomanifest", "Non-existing or invalid manifest in nomanifest.jar");
    }

    private void validateWithException(String jarName, String exceptionMessage) throws IOException {
        try {
            JarFile jarFile = createTemporaryJarFile(tempDir, jarName);
            BundleValidator bundleValidator = new BundleValidator();
            bundleValidator.validateJarFile(DeployState.createTestState(), jarFile);
            assert (false);
        } catch (IllegalArgumentException e) {
            assertEquals(exceptionMessage, e.getMessage());
        }
    }

    @Test
    void require_that_deploying_snapshot_bundle_gives_warning() throws IOException {
        final StringBuffer buffer = new StringBuffer();

        DeployState state = createDeployState(buffer);
        JarFile jarFile = createTemporaryJarFile(tempDir, "snapshot_bundle");
        new BundleValidator().validateJarFile(state, jarFile);
        assertTrue(buffer.toString().contains("Deploying snapshot bundle"));
    }

    @Test
    void outputs_deploy_warning_on_import_of_packages_from_deprecated_artifact() throws IOException {
        final StringBuffer buffer = new StringBuffer();
        DeployState state = createDeployState(buffer);
        BundleValidator validator = new BundleValidator();
        JarFile jarFile = createTemporaryJarFile(tempDir, "import-warnings");
        validator.validateJarFile(state, jarFile);
        String output = buffer.toString();
        assertTrue(output
                .contains("JAR file 'import-warnings.jar' imports the packages [org.json] from 'org.json:json'. \n" +
                        "This bundle is no longer provided on Vespa 8 - see https://docs.vespa.ai/en/vespa8-release-notes.html#container-runtime."));
        assertTrue(output
                .contains("JAR file 'import-warnings.jar' imports the packages [org.eclipse.jetty.client.api] from 'jetty'. \n" +
                        "The Jetty bundles are no longer provided on Vespa 8 - see https://docs.vespa.ai/en/vespa8-release-notes.html#container-runtime."));
    }

    static DeployState createDeployState(StringBuffer buffer) {
        DeployLogger logger = (__, message) -> buffer.append(message).append('\n');
        return DeployState.createTestState(logger);
    }

    static JarFile createTemporaryJarFile(File tempDir, String testArtifact) throws IOException {
        Path jarFile = Paths.get(tempDir.toString(), testArtifact + ".jar");
        Path artifactDirectory = Paths.get("src/test/cfg/application/validation/testjars/" + testArtifact);
        try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(jarFile))) {
            Files.walk(artifactDirectory).forEach(path -> {
                Path relativePath = artifactDirectory.relativize(path);
                String zipName = relativePath.toString();
                uncheck(() -> {
                    if (Files.isDirectory(path)) {
                        out.putNextEntry(new JarEntry(zipName + "/"));
                    } else {
                        out.putNextEntry(new JarEntry(zipName));
                        out.write(Files.readAllBytes(path));
                    }
                    out.closeEntry();
                });
            });
        }
        return new JarFile(jarFile.toFile());
    }

    private void verifyParsing(String text, List<String> expected) {
        TokenizeAndDeQuote tokenizer = new TokenizeAndDeQuote(";,=", "\"'");
        var words = tokenizer.tokenize(text);
        assertEquals(expected, words);
    }

    @Test
    void testImportPackagesParsing() {
        assertEquals(new ImportPackageInfo("com.yahoo.config;version=\"[1.0.0,2)\",com.yahoo.filedistribution.fileacquirer;version=\"[1.0.0,2)\"," +
                        "com.yahoo.jdisc,com.yahoo.jdisc.handler,java.io;version=\"[0.0.0,1)\",java.lang;version=\"[0.0.0,1)\"," +
                        "java.nio;version=\"[0.0.0,1)\",java.util.concurrent;version=\"[0.0.0,1)\",java.util;version=\"[0.0.0,1)\"").packages(),
                List.of("com.yahoo.config", "com.yahoo.filedistribution.fileacquirer", "com.yahoo.jdisc",
                        "com.yahoo.jdisc.handler", "java.io", "java.lang", "java.nio", "java.util.concurrent", "java.util"));
        assertEquals(new ImportPackageInfo("org.json;version=\"[0.0.0,1)\",org.eclipse.jetty.client.api;version=\"[9.4.46,10)").packages(),
                List.of("org.json", "org.eclipse.jetty.client.api"));
    }

    @Test
    void testQuotedTokenizer() {
        verifyParsing("org.json;version=\"[0.0.0,1)\",org.eclipse.jetty.client.api;version=\"[9.4.46,10)\"",
                List.of("org.json", "version", "[0.0.0,1)", "org.eclipse.jetty.client.api", "version", "[9.4.46,10)"));
        verifyParsing("org.json;version='[0.0.0,1)',org.eclipse.jetty.client.api;version='[9.4.46,10)'",
                List.of("org.json", "version", "[0.0.0,1)", "org.eclipse.jetty.client.api", "version", "[9.4.46,10)"));
    }


}