aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionTest.java
blob: df011dd9307c32855d25ea538207dce260bef84c (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
// 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.config.application.api.ApplicationFile;
import com.yahoo.config.provision.*;
import com.yahoo.path.Path;
import com.yahoo.config.model.application.provider.*;
import com.yahoo.slime.Slime;
import com.yahoo.vespa.config.server.*;
import com.yahoo.vespa.config.server.application.MemoryTenantApplications;
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.curator.Curator;
import com.yahoo.vespa.curator.mock.MockCurator;
import com.yahoo.vespa.config.server.zookeeper.ConfigCurator;

import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.time.Instant;
import java.util.*;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

/**
 * @author lulf
 * @since 5.1
 */
public class LocalSessionTest {

    private Path tenantPath = Path.createRoot();
    private Curator curator;
    private ConfigCurator configCurator;
    private TenantFileSystemDirs tenantFileSystemDirs;
    private SuperModelGenerationCounter superModelGenerationCounter;

    @Before
    public void setupTest() throws Exception {
        curator = new MockCurator();
        configCurator = ConfigCurator.create(curator);
        superModelGenerationCounter = new SuperModelGenerationCounter(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 {
        LocalSession session = createSession(TenantName.defaultName(), 3);
        assertTrue(configCurator.exists("/3"));
        assertTrue(new File(tenantFileSystemDirs.path(), "3").exists());
        long gen = superModelGenerationCounter.get();
        session.delete();
        assertThat(superModelGenerationCounter.get(), is(gen + 1));
        assertFalse(configCurator.exists("/3"));
        assertFalse(new File(tenantFileSystemDirs.path(), "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 {
        AllocatedHosts input = AllocatedHosts.withHosts(Collections.singleton(new HostSpec("myhost", Collections.<String>emptyList())));

        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())));
    }

    @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 {
        Path sessionPath = Path.fromString("/" + sessionId);
        SessionZooKeeperClient zkc = new MockSessionZKClient(curator, sessionPath, allocatedHosts);
        zkc.createWriteStatusTransaction(Session.Status.NEW).commit();
        ZooKeeperClient zkClient = new ZooKeeperClient(configCurator, new BaseDeployLogger(), false, sessionPath);
        if (allocatedHosts.isPresent()) {
            zkClient.write(allocatedHosts.get());
        }
        zkClient.write(Collections.singletonMap(Version.fromIntValues(0, 0, 0), new MockFileRegistry()));
        File sessionDir = new File(tenantFileSystemDirs.path(), String.valueOf(sessionId));
        sessionDir.createNewFile();
        return new LocalSession(tenant, sessionId, preparer, new SessionContext(FilesApplicationPackage.fromFile(testApp), zkc, sessionDir, new MemoryTenantApplications(), new HostRegistry<>(), superModelGenerationCounter));
    }

    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());
    }
}