summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/zookeeper/ConfigCuratorTest.java
blob: 9a8aec72564621c2c9296204c9b250802613c8cf (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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.zookeeper;

import com.yahoo.text.Utf8;
import com.yahoo.vespa.curator.mock.MockCurator;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.*;

/**
 * Tests the ZKFacade using a curator mock.
 *
 * @author hmusum
 */
public class ConfigCuratorTest {

    private final String defKey1 = "attributes";

    private final String payload1 = "attribute[5]\n" +
            "attribute[0].name Popularity\n" +
            "attribute[0].datatype string\n" +
            "attribute[0].collectiontype single\n" +
            "attribute[0].removeifzero false\n" +
            "attribute[0].createifnonexistent false\n" +
            "attribute[0].loadtype \"always\"\n" +
            "attribute[0].uniqueonly false\n" +
            "attribute[0].sparse false\n" +
            "attribute[0].noupdate false\n" +
            "attribute[0].fastsearch false\n" +
            "attribute[0].fastaggregate false\n" +
            "attribute[0].fastersearch false\n" +
            "attribute[1].name atA\n" +
            "attribute[1].datatype string\n" +
            "attribute[1].collectiontype weightedset\n" +
            "attribute[1].removeifzero false\n" +
            "attribute[1].createifnonexistent false\n" +
            "attribute[1].loadtype \"always\"\n" +
            "attribute[1].uniqueonly false\n" +
            "attribute[1].sparse false\n" +
            "attribute[1].noupdate false\n" +
            "attribute[1].fastsearch true\n" +
            "attribute[1].fastaggregate false\n" +
            "attribute[1].fastersearch false\n" +
            "attribute[2].name default_fieldlength\n" +
            "attribute[2].datatype uint32\n" +
            "attribute[2].collectiontype single\n" +
            "attribute[2].removeifzero false\n" +
            "attribute[2].createifnonexistent false\n" +
            "attribute[2].loadtype \"always\"\n" +
            "attribute[2].uniqueonly false\n" +
            "attribute[2].sparse false\n" +
            "attribute[2].noupdate true\n" +
            "attribute[2].fastsearch false\n" +
            "attribute[2].fastaggregate false\n" +
            "attribute[2].fastersearch false\n" +
            "attribute[3].name default_literal_fieldlength\n" +
            "attribute[3].datatype uint32\n" +
            "attribute[3].collectiontype single\n" +
            "attribute[3].removeifzero false\n" +
            "attribute[3].createifnonexistent false\n" +
            "attribute[3].loadtype \"always\"\n" +
            "attribute[3].uniqueonly false\n" +
            "attribute[3].sparse false\n" +
            "attribute[3].noupdate true\n" +
            "attribute[3].fastsearch false\n" +
            "attribute[3].fastaggregate false\n" +
            "attribute[3].fastersearch false\n" +
            "attribute[4].name artist_fieldlength\n" +
            "attribute[4].datatype uint32\n" +
            "attribute[4].collectiontype single\n" +
            "attribute[4].removeifzero false\n" +
            "attribute[4].createifnonexistent false\n" +
            "attribute[4].loadtype \"always\"\n" +
            "attribute[4].uniqueonly false\n" +
            "attribute[4].sparse false\n" +
            "attribute[4].noupdate true\n" +
            "attribute[4].fastsearch false\n" +
            "attribute[4].fastaggregate false\n" +
            "attribute[4].fastersearch false\n";

    private final String payload3 = "attribute[5]\n" +
            "attribute[0].name Popularity\n" +
            "attribute[0].datatype String\n" +
            "attribute[0].collectiontype single\n" +
            "attribute[0].removeifzero false\n" +
            "attribute[0].createifnonexistent false\n" +
            "attribute[0].loadtype \"always\"\n" +
            "attribute[0].uniqueonly false\n" +
            "attribute[0].sparse false\n" +
            "attribute[0].noupdate false\n" +
            "attribute[0].fastsearch false\n" +
            "attribute[0].fastaggregate false\n" +
            "attribute[0].fastersearch false\n" +
            "attribute[1].name atA\n" +
            "attribute[1].datatype string\n" +
            "attribute[1].collectiontype weightedset\n" +
            "attribute[1].removeifzero false\n" +
            "attribute[1].createifnonexistent false\n" +
            "attribute[1].loadtype \"always\"\n" +
            "attribute[1].uniqueonly false\n" +
            "attribute[1].sparse false\n" +
            "attribute[1].noupdate false\n" +
            "attribute[1].fastsearch true\n" +
            "attribute[1].fastaggregate false\n" +
            "attribute[1].fastersearch false\n" +
            "attribute[2].name default_fieldlength\n" +
            "attribute[2].datatype uint32\n" +
            "attribute[2].collectiontype single\n" +
            "attribute[2].removeifzero false\n" +
            "attribute[2].createifnonexistent false\n" +
            "attribute[2].loadtype \"always\"\n" +
            "attribute[2].uniqueonly false\n" +
            "attribute[2].sparse false\n" +
            "attribute[2].noupdate true\n" +
            "attribute[2].fastsearch false\n" +
            "attribute[2].fastaggregate false\n" +
            "attribute[2].fastersearch false\n" +
            "attribute[3].name default_literal_fieldlength\n" +
            "attribute[3].datatype uint32\n" +
            "attribute[3].collectiontype single\n" +
            "attribute[3].removeifzero false\n" +
            "attribute[3].createifnonexistent false\n" +
            "attribute[3].loadtype \"always\"\n" +
            "attribute[3].uniqueonly false\n" +
            "attribute[3].sparse false\n" +
            "attribute[3].noupdate true\n" +
            "attribute[3].fastsearch false\n" +
            "attribute[3].fastaggregate false\n" +
            "attribute[3].fastersearch false\n" +
            "attribute[4].name artist_fieldlength\n" +
            "attribute[4].datatype uint32\n" +
            "attribute[4].collectiontype single\n" +
            "attribute[4].removeifzero false\n" +
            "attribute[4].createifnonexistent false\n" +
            "attribute[4].loadtype \"always\"\n" +
            "attribute[4].uniqueonly false\n" +
            "attribute[4].sparse false\n" +
            "attribute[4].noupdate true\n" +
            "attribute[4].fastsearch false\n" +
            "attribute[4].fastaggregate false\n" +
            "attribute[4].fastersearch false\n";

    private void initAndClearZK(ConfigCurator zkIf) {
        zkIf.initAndClear(ConfigCurator.DEFCONFIGS_ZK_SUBPATH);
        zkIf.initAndClear(ConfigCurator.USERAPP_ZK_SUBPATH);
    }

    private ConfigCurator deployApp() {
        ConfigCurator zkIf = create();
        initAndClearZK(zkIf);
        zkIf.putData(ConfigCurator.DEFCONFIGS_ZK_SUBPATH, defKey1, payload1);
        // zkIf.putData(ConfigCurator.USERCONFIGS_ZK_SUBPATH, cfgKey1, payload3);
        String partitionsDef = "version=7\\n" +
                "dataset[].id       int\\n" +
                "dataset[].partbits                      int default=6";
        zkIf.putData(ConfigCurator.DEFCONFIGS_ZK_SUBPATH, "partitions", partitionsDef);
        String partitionsUser = "dataset[0].partbits 8\\n";
        // zkIf.putData(ConfigCurator.USERCONFIGS_ZK_SUBPATH, "partitions", partitionsUser);
        return zkIf;
    }

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Test
    public void testZKInterface() {
        ConfigCurator zkIf = create();
        zkIf.putData("", "test", "foo");
        zkIf.putData("/test", "me", "bar");
        zkIf.putData("", "test;me;now,then", "baz");
        assertEquals(zkIf.getData("", "test"), "foo");
        assertEquals(zkIf.getData("/test", "me"), "bar");
        assertEquals(zkIf.getData("", "test;me;now,then"), "baz");
    }

    @Test
    public void testNonExistingPath() {
        ConfigCurator configCurator = create();

        expectedException.expect(IllegalArgumentException.class);
        expectedException.expectMessage("Cannot read data from path /non-existing, it does not exist");
        configCurator.getData("/non-existing");
    }

    @Test
    public void testWatcher() {
        ConfigCurator zkIf = create();

        zkIf.putData("", "test", "foo");
        String data = zkIf.getData("/test");
        assertEquals(data, "foo");
        zkIf.putData("", "/test", "bar");
        data = zkIf.getData("/test");
        assertEquals(data, "bar");

        zkIf.getChildren("/");
        zkIf.putData("", "test2", "foo2");
    }

    private ConfigCurator create() {
        return ConfigCurator.create(new MockCurator());
    }

    @Test
    public void testGetDeployedData() {
        ConfigCurator zkIf = deployApp();
        assertEquals(zkIf.getData(ConfigCurator.DEFCONFIGS_ZK_SUBPATH, defKey1), payload1);
    }

    @Test
    public void testEmptyData() {
        ConfigCurator zkIf = create();
        zkIf.createNode("/empty", "data");
        assertEquals("", zkIf.getData("/empty", "data"));
    }

    @Test
    public void testRecursiveDelete() {
        ConfigCurator configCurator = create();
        configCurator.putData("/foo", Utf8.toBytes("sadsdfsdfsdfsdf"));
        configCurator.putData("/foo/bar", Utf8.toBytes("dsfsdffds"));
        configCurator.putData("/foo/baz",
                Utf8.toBytes("sdf\u00F8l ksdfl skdflsk dflsdkfd welkr3k lkr e4kt4 54l4l353k l534klk3lk4l33k5l 353l4k l43k l4k"));
        configCurator.putData("/foo/bar/dill", Utf8.toBytes("sdfsfe 23 42 3 3 2342"));
        configCurator.putData("/foo", Utf8.toBytes("sdcfsdfsdf"));
        configCurator.putData("/foo", Utf8.toBytes("sdcfsd sdfdffsdf"));
        configCurator.deleteRecurse("/foo");
        assertFalse(configCurator.exists("/foo"));
        assertFalse(configCurator.exists("/foo/bar"));
        assertFalse(configCurator.exists("/foo/bar/dill"));
        assertFalse(configCurator.exists("/foo/bar/baz"));
        try {
            configCurator.getChildren("/foo");
            fail("Got children from nonexisting ZK path");
        } catch (RuntimeException e) {
            assertTrue(e.getCause().getMessage().matches(".*NoNode.*"));
        }
        configCurator.deleteRecurse("/nonexisting");
    }

}