summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/TenantRepositoryTest.java
blob: 6104d95b5c27d42786dfa63575f2f2c4b59e557c (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
// 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.tenant;

import com.yahoo.cloud.config.ConfigserverConfig;
import com.yahoo.component.Version;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationName;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.Zone;
import com.yahoo.vespa.config.server.GlobalComponentRegistry;
import com.yahoo.vespa.config.server.ServerCache;
import com.yahoo.vespa.config.server.TestComponentRegistry;
import com.yahoo.vespa.config.server.application.Application;
import com.yahoo.vespa.config.server.application.ApplicationSet;
import com.yahoo.vespa.config.server.application.TenantApplications;
import com.yahoo.vespa.config.server.application.TenantApplicationsTest;
import com.yahoo.vespa.config.server.monitoring.MetricUpdater;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.mock.MockCurator;
import com.yahoo.vespa.model.VespaModel;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class TenantRepositoryTest {
    private static final TenantName tenant1 = TenantName.from("tenant1");
    private static final TenantName tenant2 = TenantName.from("tenant2");
    private static final TenantName tenant3 = TenantName.from("tenant3");

    private TenantRepository tenantRepository;
    private TestComponentRegistry globalComponentRegistry;
    private TenantApplicationsTest.MockReloadListener listener;
    private MockTenantListener tenantListener;
    private Curator curator;

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

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Before
    public void setupSessions() {
        curator = new MockCurator();
        globalComponentRegistry = new TestComponentRegistry.Builder().curator(curator).build();
        listener = (TenantApplicationsTest.MockReloadListener)globalComponentRegistry.getReloadListener();
        tenantListener = (MockTenantListener)globalComponentRegistry.getTenantListener();
        assertFalse(tenantListener.tenantsLoaded);
        tenantRepository = new TenantRepository(globalComponentRegistry);
        assertTrue(tenantListener.tenantsLoaded);
        tenantRepository.addTenant(tenant1);
        tenantRepository.addTenant(tenant2);
    }

    @After
    public void closeSessions() {
        tenantRepository.close();
    }

    @Test
    public void testStartUp() {
        assertEquals(tenantRepository.getTenant(tenant1).getName(), tenant1);
        assertEquals(tenantRepository.getTenant(tenant2).getName(), tenant2);
    }

    @Test
    public void testListenersAdded() throws IOException, SAXException {
        TenantApplications applicationRepo = tenantRepository.getTenant(tenant1).getApplicationRepo();
        ApplicationId id = ApplicationId.from(tenant1, ApplicationName.defaultName(), InstanceName.defaultName());
        applicationRepo.createApplication(id);
        applicationRepo.createPutTransaction(id, 4).commit();
        applicationRepo.activateApplication(ApplicationSet.fromSingle(new Application(new VespaModel(MockApplicationPackage.createEmpty()),
                                                                                      new ServerCache(),
                                                                                      4L,
                                                                                      false,
                                                                                      new Version(1, 2, 3),
                                                                                      MetricUpdater.createTestUpdater(),
                                                                                      id)),
                                            4);
        assertEquals(1, listener.reloaded.get());
    }

    @Test
    public void testTenantListenersNotified() throws Exception {
        tenantRepository.addTenant(tenant3);
        assertEquals("tenant3 not the last created tenant. Tenants: " + tenantRepository.getAllTenantNames() +
                             ", /config/v2/tenants: " + readZKChildren("/config/v2/tenants"),
                     tenant3, tenantListener.tenantCreatedName);
        tenantRepository.deleteTenant(tenant2);
        assertFalse(tenantRepository.getAllTenantNames().contains(tenant2));
        assertEquals(tenant2, tenantListener.tenantDeletedName);
    }

    @Test
    public void testAddTenant() throws Exception {
        Set<TenantName> allTenants = tenantRepository.getAllTenantNames();
        assertTrue(allTenants.contains(tenant1));
        assertTrue(allTenants.contains(tenant2));
        tenantRepository.addTenant(tenant3);
        assertZooKeeperTenantPathExists(tenant3);
        allTenants = tenantRepository.getAllTenantNames();
        assertTrue(allTenants.contains(tenant1));
        assertTrue(allTenants.contains(tenant2));
        assertTrue(allTenants.contains(tenant3));
    }

    @Test
    public void testDeleteTenant() throws Exception {
        assertZooKeeperTenantPathExists(tenant1);
        tenantRepository.deleteTenant(tenant1);
        assertFalse(tenantRepository.getAllTenantNames().contains(tenant1));

        try {
            tenantRepository.deleteTenant(TenantName.from("non-existing"));
            fail("deletion of non-existing tenant should fail");
        } catch (IllegalArgumentException e) {
            // expected
        }
    }

    @Test(expected = IllegalArgumentException.class)
    public void testDeletingDefaultTenant()  {
        try {
            assertZooKeeperTenantPathExists(TenantName.defaultName());
        } catch (Exception e) {
            fail("default tenant does not exist");
        }
        tenantRepository.deleteTenant(TenantName.defaultName());
    }
    
    @Test
    public void testTenantWatching() throws Exception {
        TenantName newTenant = TenantName.from("newTenant");
        List<TenantName> expectedTenants = Arrays.asList(TenantName.defaultName(), newTenant);
        try {
            tenantRepository.addTenant(newTenant);
            // Poll for the watcher to pick up the tenant from zk, and add it
            int tries=0;
            while(true) {
                if (tries > 5000) fail("Didn't react on watch");
                if (tenantRepository.getAllTenantNames().containsAll(expectedTenants)) {
                    break;
                }
                tries++;
                Thread.sleep(10);
            }
        } finally {
            assertTrue(tenantRepository.getAllTenantNames().containsAll(expectedTenants));
            tenantRepository.close();
        }
    }

    @Test
    public void testFailingBootstrap() throws IOException {
        tenantRepository.close(); // stop using the one setup in Before method

        // Should get exception if config is true
        expectedException.expect(RuntimeException.class);
        expectedException.expectMessage("Could not create all tenants when bootstrapping, failed to create: [default]");
        new FailingDuringBootstrapTenantRepository(createComponentRegistry());
    }

    private List<String> readZKChildren(String path) throws Exception {
        return curator.framework().getChildren().forPath(path);
    }

    private void assertZooKeeperTenantPathExists(TenantName tenantName) throws Exception {
        assertNotNull(globalComponentRegistry.getCurator().framework()
                              .checkExists().forPath(TenantRepository.getTenantPath(tenantName).getAbsolute()));
    }

    private GlobalComponentRegistry createComponentRegistry() throws IOException {
        return new TestComponentRegistry.Builder()
                .curator(new MockCurator())
                .configServerConfig(new ConfigserverConfig(new ConfigserverConfig.Builder()
                                                                   .configDefinitionsDir(temporaryFolder.newFolder("configdefs").getAbsolutePath())
                                                                   .configServerDBDir(temporaryFolder.newFolder("configserverdb").getAbsolutePath())))
                .zone(new Zone(SystemName.cd, Environment.prod, RegionName.from("foo")))
                .build();
    }

    private static class FailingDuringBootstrapTenantRepository extends TenantRepository {

        public FailingDuringBootstrapTenantRepository(GlobalComponentRegistry globalComponentRegistry) {
            super(globalComponentRegistry);
        }

        @Override
        public void bootstrapTenant(TenantName tenantName) {
            throw new RuntimeException("Failed to create: " + tenantName);
        }
    }

}