Skip to content

Commit

Permalink
fixup! adapt unit tests for synchronous call of ChannelsChangedEventM…
Browse files Browse the repository at this point in the history
…essageAction
  • Loading branch information
mcalmer committed Aug 15, 2024
1 parent f86c42d commit ef30b3a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ public void testSetChildChannelsDeprecated() throws Exception {
ActionChainManager.setTaskomaticApi(mockedHandler.getTaskomaticApi());
SaltServerActionService sa = GlobalInstanceHolder.SALT_SERVER_ACTION_SERVICE;
sa.setCommitTransaction(false);
sa.setSaltApi(saltApi);
List<Long> finishedActions = new ArrayList<>();

Server server = MinionServerFactoryTest.createTestMinionServer(admin);
Expand Down Expand Up @@ -470,6 +471,7 @@ public void testSetChildChannels() throws Exception {
ActionChainManager.setTaskomaticApi(mockedHandler.getTaskomaticApi());
SaltServerActionService sa = GlobalInstanceHolder.SALT_SERVER_ACTION_SERVICE;
sa.setCommitTransaction(false);
sa.setSaltApi(saltApi);
List<Long> finishedActions = new ArrayList<>();

Server server = MinionServerFactoryTest.createTestMinionServer(admin);
Expand Down Expand Up @@ -595,6 +597,7 @@ public void testSetBaseChannelDeprecated() throws Exception {
ActionChainManager.setTaskomaticApi(mockedHandler.getTaskomaticApi());
SaltServerActionService sa = GlobalInstanceHolder.SALT_SERVER_ACTION_SERVICE;
sa.setCommitTransaction(false);
sa.setSaltApi(saltApi);
List<Long> finishedActions = new ArrayList<>();

MinionServer server = MinionServerFactoryTest.createTestMinionServer(admin);
Expand All @@ -619,6 +622,9 @@ public void testSetBaseChannelDeprecated() throws Exception {
sa.execute(first.get(), false, false, Optional.empty());
finishedActions.add(first.get().getId());

HibernateFactory.getSession().flush();
HibernateFactory.getSession().clear();

server = reload(server);
assertEquals(1, result);
assertNotNull(server.getBaseChannel());
Expand All @@ -633,6 +639,9 @@ public void testSetBaseChannelDeprecated() throws Exception {
assertTrue(first.isPresent(), "No Subscribe Channels Action created");
sa.execute(first.get(), false, false, Optional.empty());

HibernateFactory.getSession().flush();
HibernateFactory.getSession().clear();

server = TestUtils.reload(server);
assertEquals(1, result);
assertNotNull(server.getBaseChannel());
Expand Down Expand Up @@ -671,6 +680,7 @@ public void testSetBaseChannel() throws Exception {
ActionChainManager.setTaskomaticApi(mockedHandler.getTaskomaticApi());
SaltServerActionService sa = GlobalInstanceHolder.SALT_SERVER_ACTION_SERVICE;
sa.setCommitTransaction(false);
sa.setSaltApi(saltApi);
List<Long> finishedActions = new ArrayList<>();

MinionServer server = MinionServerFactoryTest.createTestMinionServer(admin);
Expand All @@ -695,6 +705,9 @@ public void testSetBaseChannel() throws Exception {
sa.execute(first.get(), false, false, Optional.empty());
finishedActions.add(first.get().getId());

HibernateFactory.getSession().flush();
HibernateFactory.getSession().clear();

server = reload(server);
assertEquals(1, result);
assertNotNull(server.getBaseChannel());
Expand All @@ -710,6 +723,9 @@ public void testSetBaseChannel() throws Exception {
sa.execute(first.get(), false, false, Optional.empty());
finishedActions.add(first.get().getId());

HibernateFactory.getSession().flush();
HibernateFactory.getSession().clear();

server = TestUtils.reload(server);
assertEquals(1, result);
assertNotNull(server.getBaseChannel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ public void testUpdateServerChannels() throws Exception {

HibernateFactory.getSession().flush();

SystemManager.updateServerChannels(user, server, of(base2), Arrays.asList(ch21, ch22));
systemManager.updateServerChannels(user, server, of(base2), Arrays.asList(ch21, ch22));

assertEquals(base2.getId(), server.getBaseChannel().getId());
assertEquals(2, server.getChildChannels().size());
Expand Down Expand Up @@ -1382,7 +1382,7 @@ public void testUpdateServerChannelsNoChildren() throws Exception {

HibernateFactory.getSession().flush();

SystemManager.updateServerChannels(user, server, of(base2), Collections.emptyList());
systemManager.updateServerChannels(user, server, of(base2), Collections.emptyList());

assertEquals(base2.getId(), server.getBaseChannel().getId());
assertEquals(0, server.getChildChannels().size());
Expand Down Expand Up @@ -1410,7 +1410,7 @@ public void testUpdateServerChannelsNoBase() throws Exception {

HibernateFactory.getSession().flush();

SystemManager.updateServerChannels(user, server, empty(), Arrays.asList(ch21, ch22));
systemManager.updateServerChannels(user, server, empty(), Arrays.asList(ch21, ch22));

assertNull(server.getBaseChannel());
assertEquals(0, server.getChildChannels().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.redhat.rhn.domain.action.script.ScriptRunAction;
import com.redhat.rhn.domain.action.server.ServerAction;
import com.redhat.rhn.domain.action.test.ActionFactoryTest;
import com.redhat.rhn.domain.channel.AccessTokenFactory;
import com.redhat.rhn.domain.channel.Channel;
import com.redhat.rhn.domain.channel.test.ChannelFactoryTest;
import com.redhat.rhn.domain.formula.FormulaFactory;
Expand Down Expand Up @@ -2054,20 +2055,13 @@ public void testSubscribeChannelsActionSuccess() throws Exception {
allowing(taskomaticMock).scheduleSubscribeChannels(
with(any(User.class)), with(any(SubscribeChannelsAction.class)));
} });
saltServerActionService.setCommitTransaction(false);

SaltService saltService = new SaltService() {
@Override
public void refreshPillar(MinionList minionList) {
}
};
//ChannelsChangedEventMessageAction ccema = new ChannelsChangedEventMessageAction(saltService);

/* remove comment when testing with real message queue
saltServerActionService.setCommitTransaction(true);
MessageQueue.registerAction(ccema, ChannelsChangedEventMessage.class);
MessageQueue.startMessaging();
*/
saltServerActionService.setSaltApi(saltService);

MinionServer minion = MinionServerFactoryTest.createTestMinionServer(user);
minion.setMinionId("dev-minsles12sp2.test.local");
Expand Down Expand Up @@ -2096,13 +2090,6 @@ public void refreshPillar(MinionList minionList) {
action.addServerAction(sa);

saltServerActionService.callsForAction(action);


// callsForAction would fire ChannelChangedEventMessage asynchronous to generate tokens.
// We emulate this here only
// comment this block when testing with real message queue
//assertFalse(minion.hasValidTokensForAllChannels(), "Unexpected minion has tokens for all channels");
//ccema.execute(new ChannelsChangedEventMessage(minion.getId(), user.getId()));
assertTrue(minion.hasValidTokensForAllChannels(), "Unexpected minion miss tokens for some channels");

// Setup an event message from file contents
Expand All @@ -2114,11 +2101,6 @@ public void refreshPillar(MinionList minionList) {
JobReturnEventMessageAction messageAction = new JobReturnEventMessageAction(saltServerActionService, saltUtils);
messageAction.execute(message);

/* remove comment when testing with real message queue
MessageQueue.stopMessaging();
MessageQueue.deRegisterAction(ccema, ChannelsChangedEventMessage.class);
*/

assertEquals(ActionFactory.STATUS_COMPLETED, sa.getStatus());
assertEquals(0L, (long)sa.getResultCode());
assertEquals(baseChannel.get().getId(), minion.getBaseChannel().getId());
Expand Down Expand Up @@ -2167,11 +2149,19 @@ public void testSubscribeChannelsActionNullTokens() throws Exception {
ServerAction sa = ActionFactoryTest.createServerAction(minion, action);
action.addServerAction(sa);

saltServerActionService.setCommitTransaction(false);
Map<LocalCall<?>, List<MinionSummary>> calls = saltServerActionService.callsForAction(action);

// artifically expire tokens
action.getDetails().getAccessTokens().forEach(t -> t.setMinion(null));
/*
action.getDetails().getAccessTokens().forEach(t -> {
t.setMinion(null);
AccessTokenFactory.save(t);
});
*/
minion.getAccessTokens().forEach(t -> {
t.setMinion(null);
AccessTokenFactory.save(t);
});
HibernateFactory.getSession().flush();

// Setup an event message from file contents
Expand All @@ -2183,8 +2173,9 @@ public void testSubscribeChannelsActionNullTokens() throws Exception {
JobReturnEventMessageAction messageAction = new JobReturnEventMessageAction(saltServerActionService, saltUtils);
messageAction.execute(message);

Optional<MinionServer> optMinion = MinionServerFactory.lookupById(minion.getId());
// check that tokens are really gone
assertEquals(0, minion.getAccessTokens().size());
assertEquals(0, optMinion.map(m -> m.getAccessTokens().size()).orElse(-1));
}

private void assertTokenChannel(MinionServer minion, Channel channel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,15 +688,6 @@ public Map<MinionSummary, Integer> createActionChainSLSFiles(ActionChain actionC

@Test
public void testSubscribeChannels() throws Exception {
saltServerActionService.setCommitTransaction(false);

SaltService saltService = new SaltService() {
@Override
public void refreshPillar(MinionList minionList) {
}
};
//ChannelsChangedEventMessageAction ccema = new ChannelsChangedEventMessageAction(saltService);

Channel base = ChannelFactoryTest.createBaseChannel(user);
Channel ch1 = ChannelFactoryTest.createTestChannel(user.getOrg());
ch1.setParentChannel(base);
Expand All @@ -721,7 +712,6 @@ public void refreshPillar(MinionList minionList) {
ActionFactory.addServerToAction(minion1, action);

Map<LocalCall<?>, List<MinionSummary>> calls = saltServerActionService.callsForAction(action);
//ccema.execute(new ChannelsChangedEventMessage(minion1.getId(), user.getId()));

HibernateFactory.getSession().flush();
HibernateFactory.getSession().clear();
Expand Down

0 comments on commit ef30b3a

Please sign in to comment.