Search in sources :

Example 1 with Persister

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));
}
Also used : Persister(com.mesosphere.sdk.storage.Persister)

Example 2 with 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"));
}
Also used : Persister(com.mesosphere.sdk.storage.Persister)

Example 3 with Persister

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"));
}
Also used : MemPersister(com.mesosphere.sdk.storage.MemPersister) Persister(com.mesosphere.sdk.storage.Persister) Test(org.junit.Test)

Example 4 with Persister

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)));
}
Also used : MemPersister(com.mesosphere.sdk.storage.MemPersister) Persister(com.mesosphere.sdk.storage.Persister) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 5 with Persister

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);
}
Also used : PersisterException(com.mesosphere.sdk.storage.PersisterException) Persister(com.mesosphere.sdk.storage.Persister)

Aggregations

Persister (com.mesosphere.sdk.storage.Persister)21 MemPersister (com.mesosphere.sdk.storage.MemPersister)17 StateStore (com.mesosphere.sdk.state.StateStore)10 Test (org.junit.Test)8 FrameworkStore (com.mesosphere.sdk.state.FrameworkStore)6 Plan (com.mesosphere.sdk.scheduler.plan.Plan)4 UUID (java.util.UUID)4 Before (org.junit.Before)4 OfferOutcomeTracker (com.mesosphere.sdk.offer.history.OfferOutcomeTracker)3 OfferAccepter (com.mesosphere.sdk.offer.OfferAccepter)2 OfferEvaluator (com.mesosphere.sdk.offer.evaluate.OfferEvaluator)2 DefaultPlan (com.mesosphere.sdk.scheduler.plan.DefaultPlan)2 Phase (com.mesosphere.sdk.scheduler.plan.Phase)2 RawServiceSpec (com.mesosphere.sdk.specification.yaml.RawServiceSpec)2 PersisterException (com.mesosphere.sdk.storage.PersisterException)2 File (java.io.File)2 ApiServer (com.mesosphere.sdk.framework.ApiServer)1 FrameworkRunner (com.mesosphere.sdk.framework.FrameworkRunner)1 HealthResource (com.mesosphere.sdk.http.endpoints.HealthResource)1 PlansResource (com.mesosphere.sdk.http.endpoints.PlansResource)1