Search in sources :

Example 1 with CreateBuilder

use of org.apache.curator.framework.api.CreateBuilder in project incubator-atlas by apache.

the class SetupStepsTest method shouldCreateSetupInProgressNode.

@Test
public void shouldCreateSetupInProgressNode() throws Exception {
    Set<SetupStep> steps = new LinkedHashSet<>();
    SetupStep setupStep1 = mock(SetupStep.class);
    steps.add(setupStep1);
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    when(configuration.getString(HAConfiguration.HA_ZOOKEEPER_ACL)).thenReturn("digest:user:pwd");
    List<ACL> aclList = Arrays.asList(new ACL(ZooDefs.Perms.ALL, new Id("digest", "user:pwd")));
    setupServerIdSelectionMocks();
    CreateBuilder createBuilder = setupSetupInProgressPathMocks(aclList).getLeft();
    InterProcessMutex lock = mock(InterProcessMutex.class);
    when(curatorFactory.lockInstance(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(lock);
    SetupSteps setupSteps = new SetupSteps(steps, curatorFactory, configuration);
    setupSteps.runSetup();
    verify(createBuilder).withACL(aclList);
    verify(createBuilder).forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT + SetupSteps.SETUP_IN_PROGRESS_NODE, "id2".getBytes(Charsets.UTF_8));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ACL(org.apache.zookeeper.data.ACL) SetupStep(org.apache.atlas.setup.SetupStep) Id(org.apache.zookeeper.data.Id) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) Test(org.testng.annotations.Test)

Example 2 with CreateBuilder

use of org.apache.curator.framework.api.CreateBuilder in project incubator-atlas by apache.

the class ActiveInstanceStateTest method testSharedPathIsCreatedIfNotExists.

@Test
public void testSharedPathIsCreatedIfNotExists() throws Exception {
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX + "id1")).thenReturn(HOST_PORT);
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    when(curatorFactory.clientInstance()).thenReturn(curatorFramework);
    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(curatorFramework.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(getPath())).thenReturn(null);
    CreateBuilder createBuilder = mock(CreateBuilder.class);
    when(curatorFramework.create()).thenReturn(createBuilder);
    when(createBuilder.withMode(CreateMode.EPHEMERAL)).thenReturn(createBuilder);
    when(createBuilder.withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)).thenReturn(createBuilder);
    SetDataBuilder setDataBuilder = mock(SetDataBuilder.class);
    when(curatorFramework.setData()).thenReturn(setDataBuilder);
    ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
    activeInstanceState.update("id1");
    verify(createBuilder).forPath(getPath());
}
Also used : SetDataBuilder(org.apache.curator.framework.api.SetDataBuilder) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ExistsBuilder(org.apache.curator.framework.api.ExistsBuilder) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with CreateBuilder

use of org.apache.curator.framework.api.CreateBuilder in project incubator-atlas by apache.

the class ActiveInstanceStateTest method testSharedPathIsCreatedWithRightACLIfNotExists.

@Test
public void testSharedPathIsCreatedWithRightACLIfNotExists() throws Exception {
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX + "id1")).thenReturn(HOST_PORT);
    when(configuration.getString(HAConfiguration.HA_ZOOKEEPER_ACL)).thenReturn("sasl:myclient@EXAMPLE.COM");
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    when(curatorFactory.clientInstance()).thenReturn(curatorFramework);
    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(curatorFramework.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(getPath())).thenReturn(null);
    CreateBuilder createBuilder = mock(CreateBuilder.class);
    when(curatorFramework.create()).thenReturn(createBuilder);
    when(createBuilder.withMode(CreateMode.EPHEMERAL)).thenReturn(createBuilder);
    ACL expectedAcl = new ACL(ZooDefs.Perms.ALL, new Id("sasl", "myclient@EXAMPLE.COM"));
    when(createBuilder.withACL(Arrays.asList(new ACL[] { expectedAcl }))).thenReturn(createBuilder);
    SetDataBuilder setDataBuilder = mock(SetDataBuilder.class);
    when(curatorFramework.setData()).thenReturn(setDataBuilder);
    ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
    activeInstanceState.update("id1");
    verify(createBuilder).forPath(getPath());
}
Also used : SetDataBuilder(org.apache.curator.framework.api.SetDataBuilder) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ExistsBuilder(org.apache.curator.framework.api.ExistsBuilder) ACL(org.apache.zookeeper.data.ACL) Id(org.apache.zookeeper.data.Id) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 4 with CreateBuilder

use of org.apache.curator.framework.api.CreateBuilder in project heron by twitter.

the class CuratorStateManagerTest method testCreateNode.

/**
   * test createNode method
   * @throws Exception
   */
@Test
public void testCreateNode() throws Exception {
    CuratorStateManager spyStateManager = spy(new CuratorStateManager());
    CuratorFramework mockClient = mock(CuratorFramework.class);
    CreateBuilder mockCreateBuilder = mock(CreateBuilder.class);
    // Mockito doesn't support mock type-parametrized class, thus suppress the warning
    @SuppressWarnings("rawtypes") ACLBackgroundPathAndBytesable mockPath = spy(ACLBackgroundPathAndBytesable.class);
    final byte[] data = new byte[10];
    doReturn(mockClient).when(spyStateManager).getCuratorClient();
    doReturn(true).when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
    doReturn(mockCreateBuilder).when(mockClient).create();
    doReturn(mockPath).when(mockCreateBuilder).withMode(any(CreateMode.class));
    spyStateManager.initialize(config);
    // Verify the node is created successfully
    ListenableFuture<Boolean> result = spyStateManager.createNode(PATH, data, false);
    verify(mockCreateBuilder).withMode(any(CreateMode.class));
    verify(mockPath).forPath(PATH, data);
    assertTrue(result.get());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CreateMode(org.apache.zookeeper.CreateMode) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ACLBackgroundPathAndBytesable(org.apache.curator.framework.api.ACLBackgroundPathAndBytesable) TimeUnit(java.util.concurrent.TimeUnit) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Test(org.junit.Test)

Example 5 with CreateBuilder

use of org.apache.curator.framework.api.CreateBuilder in project flink by apache.

the class ZooKeeperLeaderElectionTest method testExceptionForwarding.

/**
	 *  Test that errors in the {@link LeaderElectionService} are correctly forwarded to the
	 *  {@link LeaderContender}.
	 */
@Test
public void testExceptionForwarding() throws Exception {
    Configuration configuration = new Configuration();
    configuration.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
    configuration.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
    ZooKeeperLeaderElectionService leaderElectionService = null;
    ZooKeeperLeaderRetrievalService leaderRetrievalService = null;
    TestingListener listener = new TestingListener();
    TestingContender testingContender;
    CuratorFramework client;
    final CreateBuilder mockCreateBuilder = mock(CreateBuilder.class);
    final ProtectACLCreateModePathAndBytesable<String> mockCreateParentsIfNeeded = mock(ProtectACLCreateModePathAndBytesable.class);
    final Exception testException = new Exception("Test exception");
    try {
        client = spy(ZooKeeperUtils.startCuratorFramework(configuration));
        Answer<CreateBuilder> answer = new Answer<CreateBuilder>() {

            private int counter = 0;

            @Override
            public CreateBuilder answer(InvocationOnMock invocation) throws Throwable {
                counter++;
                // at first we have to create the leader latch, there it mustn't fail yet
                if (counter < 2) {
                    return (CreateBuilder) invocation.callRealMethod();
                } else {
                    return mockCreateBuilder;
                }
            }
        };
        doAnswer(answer).when(client).create();
        when(mockCreateBuilder.creatingParentsIfNeeded()).thenReturn(mockCreateParentsIfNeeded);
        when(mockCreateParentsIfNeeded.withMode(Matchers.any(CreateMode.class))).thenReturn(mockCreateParentsIfNeeded);
        when(mockCreateParentsIfNeeded.forPath(Matchers.any(String.class), Matchers.any(byte[].class))).thenThrow(testException);
        leaderElectionService = new ZooKeeperLeaderElectionService(client, "/latch", "/leader");
        leaderRetrievalService = ZooKeeperUtils.createLeaderRetrievalService(configuration);
        testingContender = new TestingContender(TEST_URL, leaderElectionService);
        leaderElectionService.start(testingContender);
        leaderRetrievalService.start(listener);
        testingContender.waitForError(timeout.toMillis());
        assertNotNull(testingContender.getError());
        assertEquals(testException, testingContender.getError().getCause());
    } finally {
        if (leaderElectionService != null) {
            leaderElectionService.stop();
        }
        if (leaderRetrievalService != null) {
            leaderRetrievalService.stop();
        }
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) Answer(org.mockito.stubbing.Answer) CuratorFramework(org.apache.curator.framework.CuratorFramework) CreateMode(org.apache.zookeeper.CreateMode) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ZooKeeperLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalService) Test(org.junit.Test)

Aggregations

CreateBuilder (org.apache.curator.framework.api.CreateBuilder)8 ExistsBuilder (org.apache.curator.framework.api.ExistsBuilder)3 Test (org.testng.annotations.Test)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 SetDataBuilder (org.apache.curator.framework.api.SetDataBuilder)2 CreateMode (org.apache.zookeeper.CreateMode)2 KeeperException (org.apache.zookeeper.KeeperException)2 ACL (org.apache.zookeeper.data.ACL)2 Id (org.apache.zookeeper.data.Id)2 Test (org.junit.Test)2 BeforeTest (org.testng.annotations.BeforeTest)2 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 SetupStep (org.apache.atlas.setup.SetupStep)1 ACLBackgroundPathAndBytesable (org.apache.curator.framework.api.ACLBackgroundPathAndBytesable)1 DeleteBuilder (org.apache.curator.framework.api.DeleteBuilder)1 ProtectACLCreateModePathAndBytesable (org.apache.curator.framework.api.ProtectACLCreateModePathAndBytesable)1 InterProcessMutex (org.apache.curator.framework.recipes.locks.InterProcessMutex)1