Search in sources :

Example 1 with CronNodeResponse

use of org.opensearch.ad.transport.CronNodeResponse in project anomaly-detection by opensearch-project.

the class HourlyCronTests method templateHourlyCron.

@SuppressWarnings("unchecked")
public void templateHourlyCron(HourlyCronTestExecutionMode mode) {
    super.setUpLog4jForJUnit(HourlyCron.class);
    ClusterService clusterService = mock(ClusterService.class);
    ClusterState state = ClusterCreation.state(1);
    when(clusterService.state()).thenReturn(state);
    HashMap<String, String> ignoredAttributes = new HashMap<String, String>();
    ignoredAttributes.put(CommonName.BOX_TYPE_KEY, CommonName.WARM_BOX_TYPE);
    DiscoveryNodeFilterer nodeFilter = new DiscoveryNodeFilterer(clusterService);
    Client client = mock(Client.class);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        assertTrue(String.format("The size of args is %d.  Its content is %s", args.length, Arrays.toString(args)), args.length == 3);
        assertTrue(args[2] instanceof ActionListener);
        ActionListener<CronResponse> listener = (ActionListener<CronResponse>) args[2];
        if (mode == HourlyCronTestExecutionMode.NODE_FAIL) {
            listener.onResponse(new CronResponse(new ClusterName("test"), Collections.singletonList(new CronNodeResponse(state.nodes().getLocalNode())), Collections.singletonList(new FailedNodeException("foo0", "blah", new OpenSearchException("bar")))));
        } else if (mode == HourlyCronTestExecutionMode.ALL_FAIL) {
            listener.onFailure(new OpenSearchException("bar"));
        } else {
            CronNodeResponse nodeResponse = new CronNodeResponse(state.nodes().getLocalNode());
            BytesStreamOutput nodeResponseOut = new BytesStreamOutput();
            nodeResponseOut.setVersion(Version.CURRENT);
            nodeResponse.writeTo(nodeResponseOut);
            StreamInput siNode = nodeResponseOut.bytes().streamInput();
            CronNodeResponse nodeResponseRead = new CronNodeResponse(siNode);
            CronResponse response = new CronResponse(new ClusterName("test"), Collections.singletonList(nodeResponseRead), Collections.EMPTY_LIST);
            BytesStreamOutput out = new BytesStreamOutput();
            out.setVersion(Version.CURRENT);
            response.writeTo(out);
            StreamInput si = out.bytes().streamInput();
            CronResponse responseRead = new CronResponse(si);
            listener.onResponse(responseRead);
        }
        return null;
    }).when(client).execute(eq(CronAction.INSTANCE), any(), any());
    HourlyCron cron = new HourlyCron(client, nodeFilter);
    cron.run();
    Logger LOG = LogManager.getLogger(HourlyCron.class);
    LOG.info(testAppender.messages);
    if (mode == HourlyCronTestExecutionMode.NODE_FAIL) {
        assertTrue(testAppender.containsMessage(HourlyCron.NODE_EXCEPTION_LOG_MSG));
    } else if (mode == HourlyCronTestExecutionMode.ALL_FAIL) {
        assertTrue(testAppender.containsMessage(HourlyCron.EXCEPTION_LOG_MSG));
    } else {
        assertTrue(testAppender.containsMessage(HourlyCron.SUCCEEDS_LOG_MSG));
    }
    super.tearDownLog4jForJUnit();
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) HashMap(java.util.HashMap) CronResponse(org.opensearch.ad.transport.CronResponse) Logger(org.apache.logging.log4j.Logger) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) CronNodeResponse(org.opensearch.ad.transport.CronNodeResponse) StreamInput(org.opensearch.common.io.stream.StreamInput) ClusterName(org.opensearch.cluster.ClusterName) FailedNodeException(org.opensearch.action.FailedNodeException) OpenSearchException(org.opensearch.OpenSearchException) Client(org.opensearch.client.Client)

Aggregations

HashMap (java.util.HashMap)1 Logger (org.apache.logging.log4j.Logger)1 OpenSearchException (org.opensearch.OpenSearchException)1 ActionListener (org.opensearch.action.ActionListener)1 FailedNodeException (org.opensearch.action.FailedNodeException)1 CronNodeResponse (org.opensearch.ad.transport.CronNodeResponse)1 CronResponse (org.opensearch.ad.transport.CronResponse)1 DiscoveryNodeFilterer (org.opensearch.ad.util.DiscoveryNodeFilterer)1 Client (org.opensearch.client.Client)1 ClusterName (org.opensearch.cluster.ClusterName)1 ClusterState (org.opensearch.cluster.ClusterState)1 ClusterService (org.opensearch.cluster.service.ClusterService)1 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)1 StreamInput (org.opensearch.common.io.stream.StreamInput)1