aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionTest.java
blob: 96caff9b3a7541ca5b75c5c821cd47d5525978c5 (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
// 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.session;

import com.google.common.io.Files;
import com.yahoo.component.Version;
import com.yahoo.config.application.api.ApplicationFile;
import com.yahoo.config.model.application.provider.BaseDeployLogger;
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.config.provision.NetworkPorts;
import com.yahoo.config.provision.TenantName;
import com.yahoo.path.Path;
import com.yahoo.slime.Slime;
import com.yahoo.transaction.NestedTransaction;
import com.yahoo.vespa.config.server.MockReloadHandler;
import com.yahoo.vespa.config.server.TestComponentRegistry;
import com.yahoo.vespa.config.server.application.TenantApplications;
import com.yahoo.vespa.config.server.deploy.DeployHandlerLogger;
import com.yahoo.vespa.config.server.deploy.TenantFileSystemDirs;
import com.yahoo.vespa.config.server.deploy.ZooKeeperClient;
import com.yahoo.vespa.config.server.host.HostRegistry;
import com.yahoo.vespa.config.server.tenant.TenantRepository;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.mock.MockCurator;
import com.yahoo.vespa.config.server.zookeeper.ConfigCurator;

import com.yahoo.vespa.flags.InMemoryFlagSource;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * @author Ulf Lilleengen
 */
public class LocalSessionTest {

    private final InMemoryFlagSource flagSource = new InMemoryFlagSource();
    private Path tenantPath = Path.createRoot();
    private Curator curator;
    private ConfigCurator configCurator;
    private TenantFileSystemDirs tenantFileSystemDirs;

    @Before
    public void setupTest() {
        curator = new MockCurator();
        configCurator = ConfigCurator.create(curator);
        tenantFileSystemDirs = new TenantFileSystemDirs(Files.createTempDir(), TenantName.from("test_tenant"));
    }

    @Test
    public void require_that_session_is_initialized() throws Exception {
        LocalSession session = createSession(TenantName.defaultName(), 2);
        assertThat(session.getSessionId(), is(2l));
        session = createSession(TenantName.defaultName(), Long.MAX_VALUE);
        assertThat(session.getSessionId(), is(Long.MAX_VALUE));
        assertThat(session.getActiveSessionAtCreate(), is(0l));
    }

    @Test
    public void require_that_session_status_is_updated() throws Exception {
        LocalSession session = createSession(TenantName.defaultName(), 3);
        assertThat(session.getStatus(), is(Session.Status.NEW));
        doPrepare(session, Instant.now());
        assertThat(session.getStatus(), is(Session.Status.PREPARE));
        session.createActivateTransaction().commit();
        assertThat(session.getStatus(), is(Session.Status.ACTIVATE));
    }

    @Test
    public void require_that_marking_session_modified_changes_status_to_new() throws Exception {
        LocalSession session = createSession(TenantName.defaultName(), 3);
        doPrepare(session, Instant.now());
        assertThat(session.getStatus(), is(Session.Status.PREPARE));
        session.getApplicationFile(Path.createRoot(), LocalSession.Mode.READ);
        assertThat(session.getStatus(), is(Session.Status.PREPARE));
        session.getApplicationFile(Path.createRoot(), LocalSession.Mode.WRITE);
        assertThat(session.getStatus(), is(Session.Status.NEW));
    }

    @Test
    public void require_that_preparer_is_run() throws Exception {
        SessionTest.MockSessionPreparer preparer = new SessionTest.MockSessionPreparer();
        LocalSession session = createSession(TenantName.defaultName(), 3, preparer);
        assertFalse(preparer.isPrepared);
        doPrepare(session, Instant.now());
        assertTrue(preparer.isPrepared);
        assertThat(session.getStatus(), is(Session.Status.PREPARE));
    }

    @Test
    public void require_that_session_status_can_be_deactivated() throws Exception {
        SessionTest.MockSessionPreparer preparer = new SessionTest.MockSessionPreparer();
        LocalSession session = createSession(TenantName.defaultName(), 3, preparer);
        session.createDeactivateTransaction().commit();
        assertThat(session.getStatus(), is(Session.Status.DEACTIVATE));
    }

    private File testApp = new File("src/test/apps/app");

    @Test
    public void require_that_application_file_can_be_fetched() throws Exception {
        LocalSession session = createSession(TenantName.defaultName(), 3);
        ApplicationFile f1 = session.getApplicationFile(Path.fromString("services.xml"), LocalSession.Mode.READ);
        ApplicationFile f2 = session.getApplicationFile(Path.fromString("services2.xml"), LocalSession.Mode.READ);
        assertTrue(f1.exists());
        assertFalse(f2.exists());
    }

    @Test
    public void require_that_session_can_be_deleted() throws Exception {
        TenantName tenantName = TenantName.defaultName();
        LocalSession session = createSession(tenantName, 3);
        String sessionNode = TenantRepository.getSessionsPath(tenantName).append(String.valueOf(3)).getAbsolute();
        assertTrue(configCurator.exists(sessionNode));
        assertTrue(new File(tenantFileSystemDirs.sessionsPath(), "3").exists());
        NestedTransaction transaction = new NestedTransaction();
        session.delete(transaction);
        transaction.commit();
        assertFalse(configCurator.exists(sessionNode));
        assertFalse(new File(tenantFileSystemDirs.sessionsPath(), "3").exists());
    }

    @Test(expected = IllegalStateException.class)
    public void require_that_no_provision_info_throws_exception() throws Exception {
        createSession(TenantName.defaultName(), 3).getAllocatedHosts();
    }

    @Test
    public void require_that_provision_info_can_be_read() throws Exception {
        List<NetworkPorts.Allocation> list = new ArrayList<>();
        list.add(new NetworkPorts.Allocation(8080, "container", "default/0", "http"));
        list.add(new NetworkPorts.Allocation(19101, "searchnode", "other/1", "rpc"));
        NetworkPorts ports = new NetworkPorts(list);

        AllocatedHosts input = AllocatedHosts.withHosts(Collections.singleton(
                new HostSpec("myhost", Collections.<String>emptyList(),
                             Optional.empty(), Optional.empty(), Optional.empty(),
                             Optional.of(ports))));

        LocalSession session = createSession(TenantName.defaultName(), 3, new SessionTest.MockSessionPreparer(), Optional.of(input));
        ApplicationId origId = new ApplicationId.Builder()
                               .tenant("tenant")
                               .applicationName("foo").instanceName("quux").build();
        doPrepare(session, new PrepareParams.Builder().applicationId(origId).build(), Instant.now());
        AllocatedHosts info = session.getAllocatedHosts();
        assertNotNull(info);
        assertThat(info.getHosts().size(), is(1));
        assertTrue(info.getHosts().contains(new HostSpec("myhost", Collections.emptyList())));
        Optional<NetworkPorts> portsCopy = info.getHosts().iterator().next().networkPorts();
        assertTrue(portsCopy.isPresent());
        assertThat(portsCopy.get().allocations(), is(list));
    }

    @Test
    public void require_that_application_metadata_is_correct() throws Exception {
        LocalSession session = createSession(TenantName.defaultName(), 3);
        doPrepare(session, new PrepareParams.Builder().build(), Instant.now());
        assertThat(session.getMetaData().toString(), is("n/a, n/a, 0, 0, , 0"));
    }

    private LocalSession createSession(TenantName tenant, long sessionId) throws Exception {
        SessionTest.MockSessionPreparer preparer = new SessionTest.MockSessionPreparer();
        return createSession(tenant, sessionId, preparer);
    }

    private LocalSession createSession(TenantName tenant, long sessionId, SessionTest.MockSessionPreparer preparer) throws Exception {
        return createSession(tenant, sessionId, preparer, Optional.<AllocatedHosts>empty());
    }

    private LocalSession createSession(TenantName tenant, long sessionId, SessionTest.MockSessionPreparer preparer, Optional<AllocatedHosts> allocatedHosts) throws Exception {
        SessionZooKeeperClient zkc = new MockSessionZKClient(curator, tenant, sessionId, allocatedHosts);
        zkc.createWriteStatusTransaction(Session.Status.NEW).commit();
        ZooKeeperClient zkClient = new ZooKeeperClient(configCurator, new BaseDeployLogger(), false, TenantRepository.getSessionsPath(tenant).append(String.valueOf(sessionId)));
        if (allocatedHosts.isPresent()) {
            zkClient.write(allocatedHosts.get());
        }
        zkClient.write(Collections.singletonMap(new Version(0, 0, 0), new MockFileRegistry()));
        File sessionDir = new File(tenantFileSystemDirs.sessionsPath(), String.valueOf(sessionId));
        sessionDir.createNewFile();
        TenantApplications applications = TenantApplications.create(new TestComponentRegistry.Builder().curator(curator).build(), new MockReloadHandler(), tenant);
        applications.createApplication(zkc.readApplicationId());
        return new LocalSession(tenant, sessionId, preparer,
                new SessionContext(
                        FilesApplicationPackage.fromFile(testApp),
                        zkc,
                        sessionDir,
                        applications,
                        new HostRegistry<>(),
                        flagSource));
    }

    private void doPrepare(LocalSession session, Instant now) {
        doPrepare(session, new PrepareParams.Builder().build(), now);
    }

    private void doPrepare(LocalSession session, PrepareParams params, Instant now) {
        session.prepare(getLogger(false), params, Optional.empty(), tenantPath, now);
    }

    DeployHandlerLogger getLogger(boolean verbose) {
        return new DeployHandlerLogger(new Slime().get(), verbose,
                                       new ApplicationId.Builder().tenant("testtenant").applicationName("testapp").build());
    }

}