use of com.mesosphere.sdk.storage.Persister in project dcos-commons by mesosphere.
the class CuratorPersisterTest method testDeleteRoot.
// Uses a real ZK instance to ensure that our integration works as expected:
@Test
public void testDeleteRoot() throws Exception {
CuratorTestUtils.clear(testZk);
when(mockServiceSpec.getZookeeperConnection()).thenReturn(testZk.getConnectString());
Persister persister = CuratorPersister.newBuilder(mockServiceSpec).build();
persister.set("lock", DATA_1);
persister.set("a", DATA_2);
persister.set("a/1", DATA_1);
persister.set("a/lock", DATA_2);
persister.set("a/2/a", DATA_1);
persister.set("a/3", DATA_2);
persister.set("a/3/a/1", DATA_1);
persister.set("b", DATA_2);
persister.set("c", DATA_1);
persister.set("d/1/a/1", DATA_2);
persister.recursiveDelete("");
// The root-level "lock" is preserved (it's special):
assertEquals(Collections.singleton("lock"), persister.getChildren(""));
assertArrayEquals(DATA_1, persister.get("lock"));
assertEquals(Collections.singleton("/lock"), PersisterUtils.getAllKeys(persister));
}
use of com.mesosphere.sdk.storage.Persister in project dcos-commons by mesosphere.
the class CuratorPersisterTest method testWriteServiceName.
@Test
public void testWriteServiceName() throws Exception {
CuratorTestUtils.clear(testZk);
String folderedName = "/path/to/myservice";
when(mockServiceSpec.getName()).thenReturn(folderedName);
when(mockServiceSpec.getZookeeperConnection()).thenReturn(testZk.getConnectString());
Persister persister = CuratorPersister.newBuilder(mockServiceSpec).build();
assertEquals(Collections.singleton("servicename"), persister.getChildren(""));
assertArrayEquals(folderedName.getBytes(StandardCharsets.UTF_8), persister.get("servicename"));
}
use of com.mesosphere.sdk.storage.Persister in project dcos-commons by mesosphere.
the class CuratorUtilsTest method testInitServicePathExistingServiceMismatch.
@Test
public void testInitServicePathExistingServiceMismatch() throws Exception {
String originalServiceName = "/folder/path/to/myservice";
Persister persister = new CuratorPersister(originalServiceName, mockClient);
Mockito.when(mockClient.getData()).thenReturn(mockGetDataBuilder);
Mockito.when(mockGetDataBuilder.forPath(Mockito.anyString())).thenReturn("othervalue".getBytes(StandardCharsets.UTF_8));
try {
CuratorUtils.initServiceName(persister, originalServiceName);
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Collision"));
}
Mockito.verify(mockGetDataBuilder).forPath(Mockito.eq("/dcos-service-folder__path__to__myservice/servicename"));
}
use of com.mesosphere.sdk.storage.Persister in project dcos-commons by mesosphere.
the class CuratorUtilsTest method testInitServicePathNewService.
@Test
public void testInitServicePathNewService() throws Exception {
String originalServiceName = "/folder/path/to/myservice";
Persister persister = new CuratorPersister(originalServiceName, mockClient);
Mockito.when(mockClient.getData()).thenReturn(mockGetDataBuilder);
Mockito.when(mockGetDataBuilder.forPath(Mockito.anyString())).thenThrow(new KeeperException.NoNodeException());
Mockito.when(mockClient.create()).thenReturn(mockCreateBuilder);
Mockito.when(mockCreateBuilder.creatingParentsIfNeeded()).thenReturn(mockCreateParentsBuilder);
CuratorUtils.initServiceName(persister, originalServiceName);
Mockito.verify(mockGetDataBuilder).forPath(Mockito.eq("/dcos-service-folder__path__to__myservice/servicename"));
Mockito.verify(mockCreateParentsBuilder).forPath(Mockito.eq("/dcos-service-folder__path__to__myservice/servicename"), Mockito.eq(originalServiceName.getBytes(StandardCharsets.UTF_8)));
}
use of com.mesosphere.sdk.storage.Persister in project dcos-commons by mesosphere.
the class CuratorPersisterTest method testAclBehavior.
// Uses a real ZK instance to ensure that our integration works as expected:
@Test
public void testAclBehavior() throws Exception {
CuratorTestUtils.clear(testZk);
when(mockServiceSpec.getZookeeperConnection()).thenReturn(testZk.getConnectString());
Persister nonAclPersister = CuratorPersister.newBuilder(mockServiceSpec).build();
Persister aclPersister = CuratorPersister.newBuilder(mockServiceSpec).setCredentials("testuser", "testpw").build();
// Store value with ACL.
aclPersister.set(PATH_1, DATA_1);
// Readable with appropriate Auth and ACL.
assertArrayEquals(DATA_1, aclPersister.get(PATH_1));
// Readable with world:anyone permission.
assertArrayEquals(DATA_1, nonAclPersister.get(PATH_1));
// Not overwriteable with world:anyone permission.
try {
nonAclPersister.set(PATH_1, DATA_2);
fail("Should have failed with auth exception");
} catch (PersisterException e) {
assertEquals(Reason.STORAGE_ERROR, e.getReason());
assertTrue(e.getCause() instanceof KeeperException.NoAuthException);
}
// Not overwriteable with incorrect Auth
try {
Persister wrongAclPersister = CuratorPersister.newBuilder(mockServiceSpec).setCredentials("testuser", "otherpw").build();
wrongAclPersister.set(PATH_1, DATA_SUB_1);
fail("Should have failed with auth exception");
} catch (PersisterException e) {
assertEquals(Reason.STORAGE_ERROR, e.getReason());
assertTrue(e.getCause() instanceof KeeperException.NoAuthException);
}
// Delete ACL'ed data so that other tests don't have ACL problems trying to clear it:
aclPersister.recursiveDelete(PATH_PARENT);
}
Aggregations