|
16 | 16 | import static org.mockito.Mockito.when; |
17 | 17 | import static org.opensearch.ml.common.CommonValue.ML_MODEL_INDEX; |
18 | 18 | import static org.opensearch.ml.utils.TestHelper.ML_ROLE; |
| 19 | +import static org.opensearch.ml.utils.TestHelper.setupTestClusterState; |
19 | 20 |
|
20 | 21 | import java.io.IOException; |
21 | 22 | import java.time.Instant; |
|
26 | 27 | import java.util.List; |
27 | 28 | import java.util.Map; |
28 | 29 | import java.util.Set; |
| 30 | +import java.util.concurrent.atomic.AtomicInteger; |
29 | 31 |
|
30 | 32 | import org.apache.lucene.search.TotalHits; |
31 | 33 | import org.junit.Before; |
|
41 | 43 | import org.opensearch.action.update.UpdateRequest; |
42 | 44 | import org.opensearch.client.Client; |
43 | 45 | import org.opensearch.cluster.ClusterName; |
| 46 | +import org.opensearch.cluster.ClusterState; |
| 47 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 48 | +import org.opensearch.cluster.metadata.Metadata; |
44 | 49 | import org.opensearch.cluster.node.DiscoveryNode; |
| 50 | +import org.opensearch.cluster.node.DiscoveryNodeRole; |
| 51 | +import org.opensearch.cluster.node.DiscoveryNodes; |
45 | 52 | import org.opensearch.cluster.service.ClusterService; |
46 | 53 | import org.opensearch.common.bytes.BytesReference; |
| 54 | +import org.opensearch.common.collect.ImmutableOpenMap; |
| 55 | +import org.opensearch.common.transport.TransportAddress; |
47 | 56 | import org.opensearch.common.xcontent.XContentBuilder; |
48 | 57 | import org.opensearch.ml.common.MLModel; |
49 | 58 | import org.opensearch.ml.common.model.MLModelState; |
@@ -77,12 +86,45 @@ public class MLSyncUpCronTests extends OpenSearchTestCase { |
77 | 86 | private final String mlNode1Id = "mlNode1"; |
78 | 87 | private final String mlNode2Id = "mlNode2"; |
79 | 88 |
|
| 89 | + private ClusterState testState; |
| 90 | + |
80 | 91 | @Before |
81 | 92 | public void setup() throws IOException { |
82 | 93 | MockitoAnnotations.openMocks(this); |
83 | 94 | mlNode1 = new DiscoveryNode(mlNode1Id, buildNewFakeTransportAddress(), emptyMap(), ImmutableSet.of(ML_ROLE), Version.CURRENT); |
84 | 95 | mlNode2 = new DiscoveryNode(mlNode2Id, buildNewFakeTransportAddress(), emptyMap(), ImmutableSet.of(ML_ROLE), Version.CURRENT); |
85 | 96 | syncUpCron = new MLSyncUpCron(client, clusterService, nodeHelper, null); |
| 97 | + |
| 98 | + testState = setupTestClusterState(); |
| 99 | + when(clusterService.state()).thenReturn(testState); |
| 100 | + } |
| 101 | + |
| 102 | + public void testRun_NoMLModelIndex() { |
| 103 | + Metadata metadata = new Metadata.Builder().indices(ImmutableOpenMap.<String, IndexMetadata>builder().build()).build(); |
| 104 | + DiscoveryNode node = new DiscoveryNode( |
| 105 | + "node", |
| 106 | + new TransportAddress(TransportAddress.META_ADDRESS, new AtomicInteger().incrementAndGet()), |
| 107 | + new HashMap<>(), |
| 108 | + ImmutableSet.of(DiscoveryNodeRole.DATA_ROLE), |
| 109 | + Version.CURRENT |
| 110 | + ); |
| 111 | + ClusterState state = new ClusterState( |
| 112 | + new ClusterName("test cluster"), |
| 113 | + 123l, |
| 114 | + "111111", |
| 115 | + metadata, |
| 116 | + null, |
| 117 | + DiscoveryNodes.builder().add(node).build(), |
| 118 | + null, |
| 119 | + null, |
| 120 | + 0, |
| 121 | + false |
| 122 | + ); |
| 123 | + ; |
| 124 | + when(clusterService.state()).thenReturn(state); |
| 125 | + |
| 126 | + syncUpCron.run(); |
| 127 | + verify(client, never()).execute(eq(MLSyncUpAction.INSTANCE), any(), any()); |
86 | 128 | } |
87 | 129 |
|
88 | 130 | public void testRun() { |
|
0 commit comments