summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/ZooKeeperClientTest.java
blob: e20363af4e98e64d40fefd574f0ca13a3fc591fe (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.deploy;

import com.google.common.collect.ImmutableSet;
import com.yahoo.component.Version;
import com.yahoo.config.application.api.ApplicationMetaData;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.application.api.FileRegistry;
import com.yahoo.config.model.application.provider.BaseDeployLogger;
import com.yahoo.config.model.application.provider.DeployData;
import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.config.model.application.provider.MockFileRegistry;
import com.yahoo.config.provision.AllocatedHosts;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.HostSpec;
import com.yahoo.path.Path;
import com.yahoo.vespa.config.server.zookeeper.ConfigCurator;
import com.yahoo.vespa.config.server.zookeeper.ZKApplicationPackage;
import com.yahoo.vespa.curator.mock.MockCurator;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.yahoo.config.provision.serialization.AllocatedHostsSerializer.fromJson;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * Unit tests for ZooKeeperClient.
 *
 * @author hmusum
 */
public class ZooKeeperClientTest {

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    private ConfigCurator zk;
    private final String appPath = "/1";

    @Before
    public void setupZK() throws IOException {
        zk = ConfigCurator.create(new MockCurator());
        ZooKeeperClient zkc = new ZooKeeperClient(zk, new BaseDeployLogger(), Path.fromString(appPath));
        ApplicationPackage app = FilesApplicationPackage.fromFileWithDeployData(new File("src/test/apps/zkfeed"),
                                                                                new DeployData("foo",
                                                                                               "/bar/baz",
                                                                                               ApplicationId.from("default", "appName", "default"),
                                                                                               1345L,
                                                                                               true,
                                                                                               3L,
                                                                                               2L));
        Map<Version, FileRegistry> fileRegistries = createFileRegistries();
        app.writeMetaData();
        zkc.initialize();
        zkc.writeApplicationPackage(app);
        zkc.write(fileRegistries);
    }

    private Map<Version, FileRegistry> createFileRegistries() {
        FileRegistry a = new MockFileRegistry();
        a.addFile("fileA");
        FileRegistry b = new MockFileRegistry();
        b.addFile("fileB");
        Map<Version, FileRegistry> registryMap = new HashMap<>();
        registryMap.put(new Version(1, 2, 3), a);
        registryMap.put(new Version(3, 2, 1), b);
        return registryMap;
    }

    @Test
    public void testInitZooKeeper() {
        ConfigCurator zk = ConfigCurator.create(new MockCurator());
        BaseDeployLogger logger = new BaseDeployLogger();
        long generation = 1L;
        ZooKeeperClient zooKeeperClient = new ZooKeeperClient(zk, logger, Path.fromString("/1"));
        zooKeeperClient.initialize();
        String appPath = "/";
        assertThat(zk.getChildren(appPath).size(), is(1));
        assertTrue(zk.exists("/" + generation));
        String currentAppPath = appPath + generation;
        assertTrue(zk.exists(currentAppPath, ConfigCurator.DEFCONFIGS_ZK_SUBPATH.replaceFirst("/", "")));
        assertThat(zk.getChildren(currentAppPath).size(), is(4));
    }

    @Test
    public void testFeedDefFilesToZooKeeper() {
        String defsPath = appPath + ConfigCurator.DEFCONFIGS_ZK_SUBPATH;
        assertTrue(zk.exists(appPath, ConfigCurator.DEFCONFIGS_ZK_SUBPATH.replaceFirst("/", "")));
        List<String> children = zk.getChildren(defsPath);
        assertEquals(defsPath + " children", 1, children.size());
        Collections.sort(children);
        assertThat(children.get(0), is("a.b.test2"));

        assertTrue(zk.exists(appPath, ConfigCurator.USER_DEFCONFIGS_ZK_SUBPATH.replaceFirst("/", "")));
        String userDefsPath = appPath + ConfigCurator.USER_DEFCONFIGS_ZK_SUBPATH;
        children = zk.getChildren(userDefsPath);
        assertThat(children.size(), is(1));
        Collections.sort(children);
        assertThat(children.get(0), is("a.b.test2"));
    }

    @Test
    public void testFeedAppMetaDataToZooKeeper() {
        assertTrue(zk.exists(appPath, ConfigCurator.META_ZK_PATH));
        ApplicationMetaData metaData = ApplicationMetaData.fromJsonString(zk.getData(appPath, ConfigCurator.META_ZK_PATH));
        assertTrue(metaData.getChecksum().length() > 0);
        assertTrue(metaData.isInternalRedeploy());
        assertThat(metaData.getDeployedByUser(), is("foo"));
        assertThat(metaData.getDeployPath(), is("/bar/baz"));
        assertThat(metaData.getDeployTimestamp(), is(1345L));
        assertThat(metaData.getGeneration(), is(3L));
        assertThat(metaData.getPreviousActiveGeneration(), is(2L));
    }

    @Test
    public void testVersionedFileRegistry() {
        String fileRegPath = appPath + "/" + ZKApplicationPackage.fileRegistryNode;
        assertTrue(zk.exists(fileRegPath));
        assertTrue(zk.exists(fileRegPath + "/1.2.3"));
        assertTrue(zk.exists(fileRegPath + "/3.2.1"));
        // assertNull("Data at " + fileRegPath, zk.getData(fileRegPath)); Not null any more .. hm
    }

    @Test
    public void include_dirs_are_written_to_ZK() {
        assertTrue(zk.exists(appPath + ConfigCurator.USERAPP_ZK_SUBPATH + "/" + "dir1", "default.xml"));
        assertTrue(zk.exists(appPath + ConfigCurator.USERAPP_ZK_SUBPATH + "/nested/" + "dir2", "chain2.xml"));
        assertTrue(zk.exists(appPath + ConfigCurator.USERAPP_ZK_SUBPATH + "/nested/" + "dir2", "chain3.xml"));
    }

    @Test
    public void search_chain_dir_written_to_ZK() {
        assertTrue(zk.exists(appPath().append("search").append("chains").append("dir1").append("default.xml").getAbsolute()));
        assertTrue(zk.exists(appPath().append("search").append("chains").append("dir2").append("chain2.xml").getAbsolute()));
        assertTrue(zk.exists(appPath().append("search").append("chains").append("dir2").append("chain3.xml").getAbsolute()));
    }

    private Path appPath() {
        return Path.fromString(appPath).append(ConfigCurator.USERAPP_ZK_SUBPATH);
    }

    @Test
    public void testWritingHostNamesToZooKeeper() throws IOException {
        ConfigCurator zk = ConfigCurator.create(new MockCurator());
        BaseDeployLogger logger = new BaseDeployLogger();
        Path app = Path.fromString("/1");
        ZooKeeperClient zooKeeperClient = new ZooKeeperClient(zk, logger, app);
        zooKeeperClient.initialize();
        HostSpec host1 = new HostSpec("host1.yahoo.com", Collections.emptyList(), Optional.empty());
        HostSpec host2 = new HostSpec("host2.yahoo.com", Collections.emptyList(), Optional.empty());
        ImmutableSet<HostSpec> hosts = ImmutableSet.of(host1, host2);
        zooKeeperClient.write(AllocatedHosts.withHosts(hosts));
        Path hostsPath = app.append(ZKApplicationPackage.allocatedHostsNode);
        assertTrue(zk.exists(hostsPath.getAbsolute()));
        
        AllocatedHosts deserialized = fromJson(zk.getBytes(hostsPath.getAbsolute()));
        assertEquals(hosts, deserialized.getHosts());
    }

}