Search in sources :

Example 1 with CustomTypeSafeMatcher

use of org.hamcrest.CustomTypeSafeMatcher in project helios by spotify.

the class AuthenticatingHttpConnectorTest method matchesAnyEndpoint.

private CustomTypeSafeMatcher<URI> matchesAnyEndpoint(final String path) {
    return new CustomTypeSafeMatcher<URI>("A URI matching one of the endpoints in " + endpoints) {

        @Override
        protected boolean matchesSafely(final URI item) {
            for (final Endpoint endpoint : endpoints) {
                final InetAddress ip = endpoint.getIp();
                final URI uri = endpoint.getUri();
                if (item.getScheme().equals(uri.getScheme()) && item.getHost().equals(ip.getHostAddress()) && item.getPath().equals(path)) {
                    return true;
                }
            }
            return false;
        }
    };
}
Also used : CustomTypeSafeMatcher(org.hamcrest.CustomTypeSafeMatcher) URI(java.net.URI) InetAddress(java.net.InetAddress)

Example 2 with CustomTypeSafeMatcher

use of org.hamcrest.CustomTypeSafeMatcher in project helios by spotify.

the class HealthCheckTest method execCompatibleDockerVersion.

private static Matcher<Version> execCompatibleDockerVersion() {
    return new CustomTypeSafeMatcher<Version>("apiVersion >= 1.18") {

        @Override
        protected boolean matchesSafely(final Version version) {
            try {
                final Iterator<String> versionParts = Splitter.on('.').split(version.apiVersion()).iterator();
                final int apiVersionMajor = Integer.parseInt(versionParts.next());
                final int apiVersionMinor = Integer.parseInt(versionParts.next());
                return apiVersionMajor == 1 && apiVersionMinor >= 18;
            } catch (Exception e) {
                return false;
            }
        }
    };
}
Also used : CustomTypeSafeMatcher(org.hamcrest.CustomTypeSafeMatcher) Version(com.spotify.docker.client.messages.Version) ServiceEndpoint(com.spotify.helios.common.descriptors.ServiceEndpoint) Endpoint(com.spotify.helios.serviceregistration.ServiceRegistration.Endpoint) IOException(java.io.IOException)

Example 3 with CustomTypeSafeMatcher

use of org.hamcrest.CustomTypeSafeMatcher in project beam by apache.

the class DisplayDataMatchers method includesDisplayDataFor.

/**
   * Create a matcher that matches if the examined {@link DisplayData} contains all display data
   * registered from the specified subcomponent and namespace.
   */
public static Matcher<DisplayData> includesDisplayDataFor(final String path, final HasDisplayData subComponent) {
    return new CustomTypeSafeMatcher<DisplayData>("includes subcomponent") {

        @Override
        protected boolean matchesSafely(DisplayData displayData) {
            DisplayData subComponentData = subComponentData(path);
            if (subComponentData.items().size() == 0) {
                throw new UnsupportedOperationException("subComponent contains no display data; " + "cannot verify whether it is included");
            }
            DisplayDataComparison comparison = checkSubset(displayData, subComponentData, path);
            return comparison.missingItems.isEmpty();
        }

        @Override
        protected void describeMismatchSafely(DisplayData displayData, Description mismatchDescription) {
            DisplayData subComponentDisplayData = subComponentData(path);
            DisplayDataComparison comparison = checkSubset(displayData, subComponentDisplayData, path);
            mismatchDescription.appendText("did not include:\n").appendValue(comparison.missingItems).appendText("\nNon-matching items:\n").appendValue(comparison.unmatchedItems);
        }

        private DisplayData subComponentData(final String path) {
            return DisplayData.from(new HasDisplayData() {

                @Override
                public void populateDisplayData(DisplayData.Builder builder) {
                    builder.include(path, subComponent);
                }
            });
        }

        private DisplayDataComparison checkSubset(DisplayData displayData, DisplayData included, String path) {
            DisplayDataComparison comparison = new DisplayDataComparison(displayData.items());
            for (Item item : included.items()) {
                Item matchedItem = displayData.asMap().get(DisplayData.Identifier.of(DisplayData.Path.absolute(path), item.getNamespace(), item.getKey()));
                if (matchedItem != null) {
                    comparison.matched(matchedItem);
                } else {
                    comparison.missing(item);
                }
            }
            return comparison;
        }

        class DisplayDataComparison {

            Collection<Item> missingItems;

            Collection<Item> unmatchedItems;

            DisplayDataComparison(Collection<Item> superset) {
                missingItems = Sets.newHashSet();
                unmatchedItems = Sets.newHashSet(superset);
            }

            void matched(Item supersetItem) {
                unmatchedItems.remove(supersetItem);
            }

            void missing(Item subsetItem) {
                missingItems.add(subsetItem);
            }
        }
    };
}
Also used : Item(org.apache.beam.sdk.transforms.display.DisplayData.Item) Description(org.hamcrest.Description) CustomTypeSafeMatcher(org.hamcrest.CustomTypeSafeMatcher)

Example 4 with CustomTypeSafeMatcher

use of org.hamcrest.CustomTypeSafeMatcher in project elasticsearch by elastic.

the class PipelineExecutionServiceTests method testExecuteBulkPipelineDoesNotExist.

public void testExecuteBulkPipelineDoesNotExist() {
    CompoundProcessor processor = mock(CompoundProcessor.class);
    when(store.get("_id")).thenReturn(new Pipeline("_id", "_description", version, processor));
    BulkRequest bulkRequest = new BulkRequest();
    IndexRequest indexRequest1 = new IndexRequest("_index", "_type", "_id").source(Collections.emptyMap()).setPipeline("_id");
    bulkRequest.add(indexRequest1);
    IndexRequest indexRequest2 = new IndexRequest("_index", "_type", "_id").source(Collections.emptyMap()).setPipeline("does_not_exist");
    bulkRequest.add(indexRequest2);
    @SuppressWarnings("unchecked") BiConsumer<IndexRequest, Exception> failureHandler = mock(BiConsumer.class);
    @SuppressWarnings("unchecked") Consumer<Exception> completionHandler = mock(Consumer.class);
    executionService.executeBulkRequest(bulkRequest.requests(), failureHandler, completionHandler);
    verify(failureHandler, times(1)).accept(argThat(new CustomTypeSafeMatcher<IndexRequest>("failure handler was not called with the expected arguments") {

        @Override
        protected boolean matchesSafely(IndexRequest item) {
            return item == indexRequest2;
        }
    }), argThat(new CustomTypeSafeMatcher<IllegalArgumentException>("failure handler was not called with the expected arguments") {

        @Override
        protected boolean matchesSafely(IllegalArgumentException iae) {
            return "pipeline with id [does_not_exist] does not exist".equals(iae.getMessage());
        }
    }));
    verify(completionHandler, times(1)).accept(null);
}
Also used : CustomTypeSafeMatcher(org.hamcrest.CustomTypeSafeMatcher) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 5 with CustomTypeSafeMatcher

use of org.hamcrest.CustomTypeSafeMatcher in project helios by spotify.

the class ZooKeeperClusterIdTest method testAgent.

@Test
public void testAgent() throws Exception {
    startDefaultMaster("--zk-cluster-id=" + zkClusterId);
    startDefaultAgent(testHost(), "--zk-cluster-id=" + zkClusterId);
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    // Create job and deploy it
    final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, IDLE_COMMAND);
    deployJob(jobId, testHost());
    final TaskStatus runningStatus = awaitTaskState(jobId, testHost(), RUNNING);
    final String containerId = runningStatus.getContainerId();
    // Delete the config node which contains the cluster ID and all the job definitions
    zk().curatorWithSuperAuth().delete().deletingChildrenIfNeeded().forPath("/config");
    // Sleep for a second so agent has a chance to react to deletion
    Thread.sleep(1000);
    // Make sure the agent didn't stop the job
    try (final DockerClient docker = getNewDockerClient()) {
        final List<Container> containers = docker.listContainers();
        final CustomTypeSafeMatcher<Container> containerIdMatcher = new CustomTypeSafeMatcher<Container>("Container with id " + containerId) {

            @Override
            protected boolean matchesSafely(Container container) {
                return container.id().equals(containerId);
            }
        };
        assertContainersMatch(containers, containerIdMatcher);
    }
}
Also used : Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) CustomTypeSafeMatcher(org.hamcrest.CustomTypeSafeMatcher) Matchers.containsString(org.hamcrest.Matchers.containsString) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Aggregations

CustomTypeSafeMatcher (org.hamcrest.CustomTypeSafeMatcher)6 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 DockerClient (com.spotify.docker.client.DockerClient)1 Container (com.spotify.docker.client.messages.Container)1 Version (com.spotify.docker.client.messages.Version)1 JobId (com.spotify.helios.common.descriptors.JobId)1 ServiceEndpoint (com.spotify.helios.common.descriptors.ServiceEndpoint)1 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)1 Endpoint (com.spotify.helios.serviceregistration.ServiceRegistration.Endpoint)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 URI (java.net.URI)1 Pattern (java.util.regex.Pattern)1 Builder (org.apache.beam.sdk.transforms.display.DisplayData.Builder)1 Item (org.apache.beam.sdk.transforms.display.DisplayData.Item)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)1 IndexRequest (org.elasticsearch.action.index.IndexRequest)1 Description (org.hamcrest.Description)1