diff --git a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/subtasks/AnsibleConfigureServers.java b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/subtasks/AnsibleConfigureServers.java index 401e81841cab..99b6463fab03 100644 --- a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/subtasks/AnsibleConfigureServers.java +++ b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/subtasks/AnsibleConfigureServers.java @@ -116,7 +116,7 @@ && taskParams().isMasterInShellMode // Reset may be set only if node is not a master. // Once isMaster is set, it can be tied to a cluster. resetMasterState = - isChangeMasterConfigDone(universe, node, true, node.cloudInfo.private_ip); + isChangeMasterConfigDone(universe, node, false, node.cloudInfo.private_ip); } } log.debug( diff --git a/managed/src/test/java/com/yugabyte/yw/commissioner/tasks/subtasks/AnsibleConfigureServerTest.java b/managed/src/test/java/com/yugabyte/yw/commissioner/tasks/subtasks/AnsibleConfigureServerTest.java new file mode 100644 index 000000000000..46b5cf0c99c7 --- /dev/null +++ b/managed/src/test/java/com/yugabyte/yw/commissioner/tasks/subtasks/AnsibleConfigureServerTest.java @@ -0,0 +1,130 @@ +// Copyright (c) YugaByte, Inc. + +package com.yugabyte.yw.commissioner.tasks.subtasks; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.yugabyte.yw.commissioner.AbstractTaskBase; +import com.yugabyte.yw.commissioner.Common; +import com.yugabyte.yw.commissioner.tasks.UniverseTaskBase.ServerType; +import com.yugabyte.yw.common.ApiUtils; +import com.yugabyte.yw.common.FakeDBApplication; +import com.yugabyte.yw.common.ModelFactory; +import com.yugabyte.yw.common.NodeManager; +import com.yugabyte.yw.common.ShellResponse; +import com.yugabyte.yw.models.AccessKey; +import com.yugabyte.yw.models.AvailabilityZone; +import com.yugabyte.yw.models.Customer; +import com.yugabyte.yw.models.Provider; +import com.yugabyte.yw.models.Region; +import com.yugabyte.yw.models.Universe; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import junitparams.JUnitParamsRunner; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.yb.client.ListMastersResponse; +import org.yb.client.YBClient; +import org.yb.util.ServerInfo; + +@RunWith(JUnitParamsRunner.class) +public class AnsibleConfigureServerTest extends FakeDBApplication { + private Customer defaultCustomer; + private AvailabilityZone az; + private Provider provider; + private Universe universe; + private ListMastersResponse mockMastersResponse; + private YBClient mockYBClient; + + @Before + public void setup() { + defaultCustomer = ModelFactory.testCustomer(); + setupUniverse(Common.CloudType.onprem); + mockMastersResponse = mock(ListMastersResponse.class); + mockYBClient = mock(YBClient.class); + when(mockService.getClient(any(), any())).thenReturn(mockYBClient); + List servers = new ArrayList<>(); + // IP for host-n1. + servers.add(new ServerInfo(UUID.randomUUID().toString(), "10.0.0.1", 9070, false, "NONE")); + try { + when(mockNodeManager.nodeCommand(any(), any())).thenReturn(ShellResponse.create(0, "")); + when(mockYBClient.listMasters()).thenReturn(mockMastersResponse); + } catch (Exception e) { + fail(); + } + when(mockMastersResponse.getMasters()).thenReturn(servers); + } + + private void setupUniverse(Common.CloudType cloudType) { + AccessKey.KeyInfo keyInfo = new AccessKey.KeyInfo(); + provider = ModelFactory.newProvider(defaultCustomer, cloudType); + Region r = Region.create(provider, "r-1", "r-1", "yb-image"); + AccessKey.create(provider.uuid, "demo-key", keyInfo); + az = AvailabilityZone.createOrThrow(r, "az-1", "az-1", "subnet-1"); + universe = + ModelFactory.createUniverse( + cloudType.name() + "-universe", defaultCustomer.getCustomerId(), cloudType); + // Save the updates to the universe. + Universe.saveDetails(universe.getUniverseUUID(), ApiUtils.mockUniverseUpdater()); + } + + @Test + public void testNoResetMasterStateInMaster() { + AnsibleConfigureServers.Params params = new AnsibleConfigureServers.Params(); + params.azUuid = az.uuid; + params.universeUUID = universe.getUniverseUUID(); + params.nodeName = "host-n1"; + params.resetMasterState = true; + params.isMasterInShellMode = true; + params.setProperty("processType", ServerType.MASTER.name().toLowerCase()); + AnsibleConfigureServers ansibleConfigServer = + AbstractTaskBase.createTask(AnsibleConfigureServers.class); + ansibleConfigServer.initialize(params); + ansibleConfigServer.run(); + verify(mockNodeManager, times(1)).nodeCommand(NodeManager.NodeCommandType.Configure, params); + assertFalse(params.resetMasterState); + } + + @Test + public void testResetMasterState() { + AnsibleConfigureServers.Params params = new AnsibleConfigureServers.Params(); + params.azUuid = az.uuid; + params.universeUUID = universe.getUniverseUUID(); + params.nodeName = "host-n2"; + params.resetMasterState = true; + params.isMasterInShellMode = true; + params.setProperty("processType", ServerType.MASTER.name().toLowerCase()); + AnsibleConfigureServers ansibleConfigServer = + AbstractTaskBase.createTask(AnsibleConfigureServers.class); + ansibleConfigServer.initialize(params); + ansibleConfigServer.run(); + verify(mockNodeManager, times(1)).nodeCommand(NodeManager.NodeCommandType.Configure, params); + assertTrue(params.resetMasterState); + } + + @Test + public void testNoResetMasterStateNonShellMode() { + AnsibleConfigureServers.Params params = new AnsibleConfigureServers.Params(); + params.azUuid = az.uuid; + params.universeUUID = universe.getUniverseUUID(); + params.nodeName = "host-n2"; + params.resetMasterState = true; + params.isMasterInShellMode = false; + params.setProperty("processType", ServerType.MASTER.name().toLowerCase()); + AnsibleConfigureServers ansibleConfigServer = + AbstractTaskBase.createTask(AnsibleConfigureServers.class); + ansibleConfigServer.initialize(params); + ansibleConfigServer.run(); + verify(mockNodeManager, times(1)).nodeCommand(NodeManager.NodeCommandType.Configure, params); + assertFalse(params.resetMasterState); + } +}