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

import com.yahoo.config.FileNode;
import com.yahoo.config.FileReference;
import com.yahoo.config.ModelReference;
import com.yahoo.config.UrlReference;
import com.yahoo.config.application.api.FileRegistry;
import com.yahoo.config.model.application.provider.BaseDeployLogger;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.config.model.producer.UserConfigRepo;
import com.yahoo.config.model.test.MockRoot;
import com.yahoo.vespa.config.ConfigDefinition;
import com.yahoo.vespa.config.ConfigDefinitionKey;
import com.yahoo.vespa.config.ConfigPayloadBuilder;
import com.yahoo.vespa.model.SimpleConfigProducer;
import com.yahoo.vespa.model.container.ApplicationContainerCluster;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

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

/**
 * @author Ulf Lilleengen
 * @author hmusum
 */
public class UserConfiguredFilesTest {

    private SimpleConfigProducer<?> producer;
    private ConfigPayloadBuilder builder;
    private final MyFileRegistry fileRegistry = new MyFileRegistry();
    private ConfigDefinition def;

    private static class MyFileRegistry implements FileRegistry {
        public Map<String, FileReference> pathToRef = new HashMap<>();
        @Override
        public FileReference addFile(String relativePath) {
            return pathToRef.get(relativePath);
        }

        @Override
        public FileReference addUri(String uri) {
            return null;
        }

        @Override
        public FileReference addBlob(String name, ByteBuffer blob) {
            return null;
        }

        @Override
        public List<Entry> export() {
            return pathToRef.entrySet().stream()
                    .map(e -> new Entry(e.getKey(), e.getValue()))
                    .toList();
        }

        @Override
        public String toString() { return export().toString(); }

    }

    private UserConfiguredFiles userConfiguredFiles() {
        return new UserConfiguredFiles(fileRegistry,
                                       new BaseDeployLogger(),
                                       new TestProperties(),
                                       new ApplicationContainerCluster.UserConfiguredUrls());
    }

    @BeforeEach
    public void setup() {
        MockRoot root = new MockRoot();
        producer = new SimpleConfigProducer<>(root, "test");
        ConfigDefinitionKey key = new ConfigDefinitionKey("myname", "mynamespace");
        def = new ConfigDefinition("myname", "mynamespace");
        builder = new ConfigPayloadBuilder(def);
        Map<ConfigDefinitionKey, ConfigPayloadBuilder> builderMap = new HashMap<>();
        builderMap.put(key, builder);
        UserConfigRepo testRepo = new UserConfigRepo(builderMap);
        producer.setUserConfigs(testRepo);
    }

    @Test
    void require_that_simple_file_fields_are_modified() {
        def.addFileDef("fileVal");
        def.addStringDef("stringVal");
        builder.setField("fileVal", "foo.txt");
        builder.setField("stringVal", "foo.txt");
        fileRegistry.pathToRef.put("foo.txt", new FileNode("fooshash").value());
        userConfiguredFiles().register(producer);
        assertEquals("fooshash", builder.getObject("fileVal").getValue());
        assertEquals("foo.txt", builder.getObject("stringVal").getValue());
    }

    @Test
    void require_that_simple_path_fields_are_modified() {
        def.addPathDef("fileVal");
        def.addStringDef("stringVal");
        builder.setField("fileVal", "foo.txt");
        builder.setField("stringVal", "foo.txt");
        fileRegistry.pathToRef.put("foo.txt", new FileNode("fooshash").value());
        userConfiguredFiles().register(producer);
        assertEquals("fooshash", builder.getObject("fileVal").getValue());
        assertEquals("foo.txt", builder.getObject("stringVal").getValue());
    }

    @Test
    void require_that_simple_model_field_with_just_path_is_modified() {
        var originalValue = ModelReference.unresolved(new FileReference("myModel.onnx"));
        def.addModelDef("modelVal");
        builder.setField("modelVal", originalValue.toString());
        assertFileSent("myModel.onnx", originalValue);
    }

    @Test
    void require_that_simple_model_field_with_path_and_url_is_modified() {
        var originalValue = ModelReference.unresolved(Optional.empty(),
                                                      Optional.of(new UrlReference("myUrl")),
                                                      Optional.of(new FileReference("myModel.onnx")));
        def.addModelDef("modelVal");
        builder.setField("modelVal", originalValue.toString());
        assertFileSent("myModel.onnx", originalValue);
    }

    @Test
    void require_that_simple_model_field_with_just_url_is_not_modified() {
        var originalValue = ModelReference.unresolved(new UrlReference("myUrl"));
        def.addModelDef("modelVal");
        builder.setField("modelVal",originalValue.toString());
        userConfiguredFiles().register(producer);
        assertEquals(originalValue, ModelReference.valueOf(builder.getObject("modelVal").getValue()));
    }

    private void assertFileSent(String path, ModelReference originalValue) {
        fileRegistry.pathToRef.put(path, new FileNode("myModelHash").value());
        userConfiguredFiles().register(producer);
        var expected = ModelReference.unresolved(originalValue.modelId(),
                                                 originalValue.url(),
                                                 Optional.of(new FileReference("myModelHash")));
        assertEquals(expected, ModelReference.valueOf(builder.getObject("modelVal").getValue()));
    }

    @Test
    void require_that_fields_in_inner_arrays_are_modified() {
        def.innerArrayDef("inner").addFileDef("fileVal");
        def.innerArrayDef("inner").addStringDef("stringVal");
        ConfigPayloadBuilder inner = builder.getArray("inner").append();
        inner.setField("fileVal", "bar.txt");
        inner.setField("stringVal", "bar.txt");
        fileRegistry.pathToRef.put("bar.txt", new FileNode("barhash").value());
        userConfiguredFiles().register(producer);
        assertEquals("barhash", builder.getArray("inner").get(0).getObject("fileVal").getValue());
        assertEquals("bar.txt", builder.getArray("inner").get(0).getObject("stringVal").getValue());
    }

    @Test
    void require_that_paths_and_model_fields_are_modified() {
        def.arrayDef("fileArray").setTypeSpec(new ConfigDefinition.TypeSpec("fileArray", "file", null, null, null, null));
        def.arrayDef("pathArray").setTypeSpec(new ConfigDefinition.TypeSpec("pathArray", "path", null, null, null, null));
        def.arrayDef("stringArray").setTypeSpec(new ConfigDefinition.TypeSpec("stringArray", "string", null, null, null, null));
        builder.getArray("fileArray").append("foo.txt");
        builder.getArray("fileArray").append("bar.txt");
        builder.getArray("pathArray").append("path.txt");
        builder.getArray("stringArray").append("foo.txt");
        fileRegistry.pathToRef.put("foo.txt", new FileNode("foohash").value());
        fileRegistry.pathToRef.put("bar.txt", new FileNode("barhash").value());
        fileRegistry.pathToRef.put("path.txt", new FileNode("pathhash").value());
        userConfiguredFiles().register(producer);
        assertEquals("foohash", builder.getArray("fileArray").get(0).getValue());
        assertEquals("barhash", builder.getArray("fileArray").get(1).getValue());
        assertEquals("pathhash", builder.getArray("pathArray").get(0).getValue());
        assertEquals("foo.txt", builder.getArray("stringArray").get(0).getValue());
    }

    @Test
    void require_that_arrays_are_modified() {
        def.arrayDef("fileArray").setTypeSpec(new ConfigDefinition.TypeSpec("fileArray", "file", null, null, null, null));
        def.arrayDef("pathArray").setTypeSpec(new ConfigDefinition.TypeSpec("pathArray", "path", null, null, null, null));
        def.arrayDef("stringArray").setTypeSpec(new ConfigDefinition.TypeSpec("stringArray", "string", null, null, null, null));
        builder.getArray("fileArray").append("foo.txt");
        builder.getArray("fileArray").append("bar.txt");
        builder.getArray("pathArray").append("path.txt");
        builder.getArray("stringArray").append("foo.txt");
        fileRegistry.pathToRef.put("foo.txt", new FileNode("foohash").value());
        fileRegistry.pathToRef.put("bar.txt", new FileNode("barhash").value());
        fileRegistry.pathToRef.put("path.txt", new FileNode("pathhash").value());
        userConfiguredFiles().register(producer);
        assertEquals("foohash", builder.getArray("fileArray").get(0).getValue());
        assertEquals("barhash", builder.getArray("fileArray").get(1).getValue());
        assertEquals("pathhash", builder.getArray("pathArray").get(0).getValue());
        assertEquals("foo.txt", builder.getArray("stringArray").get(0).getValue());
    }

    @Test
    void require_that_structs_are_modified() {
        def.structDef("struct").addFileDef("fileVal");
        def.structDef("struct").addStringDef("stringVal");
        builder.getObject("struct").setField("fileVal", "foo.txt");
        builder.getObject("struct").setField("stringVal", "foo.txt");
        fileRegistry.pathToRef.put("foo.txt", new FileNode("foohash").value());
        userConfiguredFiles().register(producer);
        assertEquals("foohash", builder.getObject("struct").getObject("fileVal").getValue());
        assertEquals("foo.txt", builder.getObject("struct").getObject("stringVal").getValue());
    }

    @Test
    void require_that_leaf_maps_are_modified() {
        def.leafMapDef("fileMap").setTypeSpec(new ConfigDefinition.TypeSpec("fileMap", "file", null, null, null, null));
        def.leafMapDef("pathMap").setTypeSpec(new ConfigDefinition.TypeSpec("pathMap", "path", null, null, null, null));
        def.leafMapDef("stringMap").setTypeSpec(new ConfigDefinition.TypeSpec("stringMap", "string", null, null, null, null));
        builder.getMap("fileMap").put("foo", "foo.txt");
        builder.getMap("fileMap").put("bar", "bar.txt");
        builder.getMap("pathMap").put("path", "path.txt");
        builder.getMap("stringMap").put("bar", "bar.txt");
        fileRegistry.pathToRef.put("foo.txt", new FileNode("foohash").value());
        fileRegistry.pathToRef.put("bar.txt", new FileNode("barhash").value());
        fileRegistry.pathToRef.put("path.txt", new FileNode("pathhash").value());
        userConfiguredFiles().register(producer);
        assertEquals("foohash", builder.getMap("fileMap").get("foo").getValue());
        assertEquals("barhash", builder.getMap("fileMap").get("bar").getValue());
        assertEquals("pathhash", builder.getMap("pathMap").get("path").getValue());
        assertEquals("bar.txt", builder.getMap("stringMap").get("bar").getValue());
    }

    @Test
    void require_that_fields_in_inner_maps_are_modified() {
        def.structMapDef("inner").addFileDef("fileVal");
        def.structMapDef("inner").addStringDef("stringVal");
        ConfigPayloadBuilder inner = builder.getMap("inner").put("foo");
        inner.setField("fileVal", "bar.txt");
        inner.setField("stringVal", "bar.txt");
        fileRegistry.pathToRef.put("bar.txt", new FileNode("barhash").value());
        userConfiguredFiles().register(producer);
        assertEquals("barhash", builder.getMap("inner").get("foo").getObject("fileVal").getValue());
        assertEquals("bar.txt", builder.getMap("inner").get("foo").getObject("stringVal").getValue());
    }

    @Test
    void require_that_null_files_are_not_sent() {
        assertThrows(IllegalArgumentException.class, () -> {
            def.addFileDef("fileVal");
            fileRegistry.pathToRef.put("foo.txt", new FileNode("fooshash").value());
            userConfiguredFiles().register(producer);
        });
    }

    @Test
    void require_that_empty_optional_paths_are_not_sent() {
        def.addOptionalPathDef("optionalPathVal");
        userConfiguredFiles().register(producer);
    }

    @Test
    void require_that_missing_file_gives_sane_error_message() {
        try {
            def.addFileDef("fileVal");
            builder.setField("fileVal", "foo.txt");
            fileRegistry.pathToRef.put("bar.txt", new FileNode("barshash").value());
            userConfiguredFiles().register(producer);
            fail("Should have thrown exception");
        } catch (IllegalArgumentException e) {
            assertEquals("Invalid config in services.xml for 'mynamespace.myname': No such file or directory 'foo.txt'",
                         e.getMessage());
        }
    }

    @Test
    void require_that_using_dot_gives_sane_error_message() {
        try {
            def.addFileDef("fileVal");
            builder.setField("fileVal", ".");
            fileRegistry.pathToRef.put("bar.txt", new FileNode("barshash").value());
            userConfiguredFiles().register(producer);
            fail("Should have thrown exception");
        } catch (IllegalArgumentException e) {
            assertEquals("Invalid config in services.xml for 'mynamespace.myname': Invalid config value '.' for field 'fileVal",
                         e.getMessage());
        }
    }

    @Test
    void require_that_using_empty_dir_gives_sane_error_message(@TempDir Path tempDir) {
        String relativeTempDir = tempDir.toString().substring(tempDir.toString().lastIndexOf("target"));
        try {
            def.addPathDef("pathVal");
            builder.setField("pathVal", relativeTempDir);
            fileRegistry.pathToRef.put(relativeTempDir, new FileReference("bazshash"));
            userConfiguredFiles().register(producer);
            fail("Should have thrown exception");
        } catch (IllegalArgumentException e) {
            assertEquals("Invalid config in services.xml for 'mynamespace.myname': Directory '" + relativeTempDir + "' is empty",
                         e.getMessage());
        }
    }

}