summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionRepoTest.java
blob: a758698d3b5b52603dc5cd8e491ae1f549cab448 (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
// 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.yahoo.cloud.config.ConfigserverConfig;
import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.config.provision.TenantName;
import com.yahoo.io.IOUtils;
import com.yahoo.vespa.config.server.GlobalComponentRegistry;
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.host.HostRegistry;
import com.yahoo.vespa.config.server.http.SessionHandlerTest;
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.nio.file.Path;
import java.nio.file.Paths;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

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

    private File testApp = new File("src/test/apps/app");
    private LocalSessionRepo repo;
    private static final TenantName tenantName = TenantName.defaultName();

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Before
    public void setupSessions() throws Exception {
        setupSessions(tenantName, true);
    }

    private void setupSessions(TenantName tenantName, boolean createInitialSessions) throws Exception {
        File configserverDbDir = temporaryFolder.newFolder().getAbsoluteFile();
        if (createInitialSessions) {
            Path sessionsPath = Paths.get(configserverDbDir.getAbsolutePath(), "tenants", tenantName.value(), "sessions");
            IOUtils.copyDirectory(testApp, sessionsPath.resolve("1").toFile());
            IOUtils.copyDirectory(testApp, sessionsPath.resolve("2").toFile());
            IOUtils.copyDirectory(testApp, sessionsPath.resolve("3").toFile());
        }
        GlobalComponentRegistry globalComponentRegistry = new TestComponentRegistry.Builder()
                .curator(new MockCurator())
                .configServerConfig(new ConfigserverConfig.Builder()
                                            .configServerDBDir(configserverDbDir.getAbsolutePath())
                                            .configDefinitionsDir(temporaryFolder.newFolder().getAbsolutePath())
                                            .sessionLifetime(5)
                                            .build())
                .build();
        SessionFactory sessionFactory = new SessionFactory(globalComponentRegistry,
                                                           TenantApplications.create(globalComponentRegistry, tenantName),
                                                           new HostRegistry<>(),
                                                           tenantName);
        repo = new LocalSessionRepo(tenantName, globalComponentRegistry, sessionFactory);
    }

    @Test
    public void require_that_sessions_can_be_loaded_from_disk() {
        assertNotNull(repo.getSession(1L));
        assertNotNull(repo.getSession(2L));
        assertNotNull(repo.getSession(3L));
        assertNull(repo.getSession(4L));
    }

    @Test
    public void require_that_all_sessions_are_deleted() {
        repo.close();
        assertNull(repo.getSession(1L));
        assertNull(repo.getSession(2L));
        assertNull(repo.getSession(3L));
    }

    @Test
    public void require_that_sessions_belong_to_a_tenant() {
        // tenant is "default"
        assertNotNull(repo.getSession(1L));
        assertNotNull(repo.getSession(2L));
        assertNotNull(repo.getSession(3L));
        assertNull(repo.getSession(4L));

        // tenant is "newTenant"
        try {
            setupSessions(TenantName.from("newTenant"), false);
        } catch (Exception e) {
            fail();
        }
        assertNull(repo.getSession(1L));

        repo.addSession(new SessionHandlerTest.MockLocalSession(1L, FilesApplicationPackage.fromFile(testApp)));
        repo.addSession(new SessionHandlerTest.MockLocalSession(2L, FilesApplicationPackage.fromFile(testApp)));
        assertNotNull(repo.getSession(1L));
        assertNotNull(repo.getSession(2L));
        assertNull(repo.getSession(3L));
    }
}