aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/application/pkg/ApplicationPackageDiffTest.java
blob: fbbd199aa055d89204a8d81a199d8e63f3859e02 (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 Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.application.pkg;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static com.yahoo.vespa.hosted.controller.application.pkg.ApplicationPackageDiff.diff;
import static com.yahoo.vespa.hosted.controller.application.pkg.ApplicationPackageDiff.diffAgainstEmpty;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author freva
 */
public class ApplicationPackageDiffTest {
    private static final ApplicationPackage app1 = applicationPackage(Map.of("file1", "contents of the\nfirst file", "dir/myfile", "Second file", "dir/binary", "øøøø"));
    private static final ApplicationPackage app2 = applicationPackage(Map.of("file1", "updated contents\nof the\nfirst file\nafter some changes", "dir/myfile2", "Second file", "dir/binary", "øøøø"));

    @Test
    void no_diff() {
        assertEquals("No diff\n", new String(diff(app1, app1)));
    }

    @Test
    void diff_against_empty() {
        assertEquals("--- dir/binary\n" +
                "Diff skipped: File is binary (new file -> 8B)\n" +
                "\n" +
                "--- dir/myfile\n" +
                "@@ -1,0 +1,1 @@\n" +
                "+ Second file\n" +
                "\n" +
                "--- file1\n" +
                "@@ -1,0 +1,2 @@\n" +
                "+ contents of the\n" +
                "+ first file\n" +
                "\n", new String(diffAgainstEmpty(app1)));
    }

    @Test
    void full_diff() {
        // Even though dir/binary is binary file, we can see they are identical, so it should not print "Diff skipped"
        assertEquals("--- dir/myfile\n" +
                "@@ -1,1 +1,0 @@\n" +
                "- Second file\n" +
                "\n" +
                "--- dir/myfile2\n" +
                "@@ -1,0 +1,1 @@\n" +
                "+ Second file\n" +
                "\n" +
                "--- file1\n" +
                "@@ -1,2 +1,4 @@\n" +
                "+ updated contents\n" +
                "+ of the\n" +
                "- contents of the\n" +
                "  first file\n" +
                "+ after some changes\n" +
                "\n", new String(diff(app1, app2)));
    }

    @Test
    void skips_diff_for_too_large_files() {
        assertEquals("--- dir/myfile\n" +
                "@@ -1,1 +1,0 @@\n" +
                "- Second file\n" +
                "\n" +
                "--- dir/myfile2\n" +
                "@@ -1,0 +1,1 @@\n" +
                "+ Second file\n" +
                "\n" +
                "--- file1\n" +
                "Diff skipped: File too large (26B -> 53B)\n" +
                "\n", new String(diff(app1, app2, 12, 1000, 1000)));
    }

    @Test
    void skips_diff_if_file_diff_is_too_large() {
        assertEquals("--- dir/myfile\n" +
                "@@ -1,1 +1,0 @@\n" +
                "- Second file\n" +
                "\n" +
                "--- dir/myfile2\n" +
                "@@ -1,0 +1,1 @@\n" +
                "+ Second file\n" +
                "\n" +
                "--- file1\n" +
                "Diff skipped: Diff too large (96B)\n" +
                "\n", new String(diff(app1, app2, 1000, 50, 1000)));
    }

    @Test
    void skips_diff_if_total_diff_is_too_large() {
        assertEquals("--- dir/myfile\n" +
                "@@ -1,1 +1,0 @@\n" +
                "- Second file\n" +
                "\n" +
                "--- dir/myfile2\n" +
                "Diff skipped: Total diff size >20B)\n" +
                "\n" +
                "--- file1\n" +
                "Diff skipped: Total diff size >20B)\n" +
                "\n", new String(diff(app1, app2, 1000, 1000, 20)));
    }

    private static ApplicationPackage applicationPackage(Map<String, String> files) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try (ZipOutputStream out = new ZipOutputStream(baos)) {
            out.setLevel(Deflater.NO_COMPRESSION); // This is for testing purposes so we skip compression for performance
            for (Map.Entry<String, String> file : files.entrySet()) {
                ZipEntry entry = new ZipEntry(file.getKey());
                out.putNextEntry(entry);
                out.write(file.getValue().getBytes(UTF_8));
                out.closeEntry();
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        return new ApplicationPackage(baos.toByteArray());
    }
}