From ef30b3a0fc100f58e382a0c09b0d7aa9a900e815 Mon Sep 17 00:00:00 2001 From: Michael Calmer Date: Thu, 15 Aug 2024 16:56:13 +0200 Subject: [PATCH] fixup! adapt unit tests for synchronous call of ChannelsChangedEventMessageAction --- .../xmlrpc/system/test/SystemHandlerTest.java | 16 ++++++++ .../system/test/SystemManagerTest.java | 6 +-- .../test/JobReturnEventMessageActionTest.java | 37 +++++++------------ .../test/SaltServerActionServiceTest.java | 10 ----- 4 files changed, 33 insertions(+), 36 deletions(-) diff --git a/java/code/src/com/redhat/rhn/frontend/xmlrpc/system/test/SystemHandlerTest.java b/java/code/src/com/redhat/rhn/frontend/xmlrpc/system/test/SystemHandlerTest.java index a1c974f539ba..1d8936b0b06c 100644 --- a/java/code/src/com/redhat/rhn/frontend/xmlrpc/system/test/SystemHandlerTest.java +++ b/java/code/src/com/redhat/rhn/frontend/xmlrpc/system/test/SystemHandlerTest.java @@ -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 finishedActions = new ArrayList<>(); Server server = MinionServerFactoryTest.createTestMinionServer(admin); @@ -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 finishedActions = new ArrayList<>(); Server server = MinionServerFactoryTest.createTestMinionServer(admin); @@ -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 finishedActions = new ArrayList<>(); MinionServer server = MinionServerFactoryTest.createTestMinionServer(admin); @@ -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()); @@ -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()); @@ -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 finishedActions = new ArrayList<>(); MinionServer server = MinionServerFactoryTest.createTestMinionServer(admin); @@ -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()); @@ -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()); diff --git a/java/code/src/com/redhat/rhn/manager/system/test/SystemManagerTest.java b/java/code/src/com/redhat/rhn/manager/system/test/SystemManagerTest.java index d3c3c3fd0929..3a2bb9c9390a 100644 --- a/java/code/src/com/redhat/rhn/manager/system/test/SystemManagerTest.java +++ b/java/code/src/com/redhat/rhn/manager/system/test/SystemManagerTest.java @@ -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()); @@ -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()); @@ -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()); diff --git a/java/code/src/com/suse/manager/reactor/messaging/test/JobReturnEventMessageActionTest.java b/java/code/src/com/suse/manager/reactor/messaging/test/JobReturnEventMessageActionTest.java index 26b7d722ede6..4007f9b50278 100644 --- a/java/code/src/com/suse/manager/reactor/messaging/test/JobReturnEventMessageActionTest.java +++ b/java/code/src/com/suse/manager/reactor/messaging/test/JobReturnEventMessageActionTest.java @@ -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; @@ -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"); @@ -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 @@ -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()); @@ -2167,11 +2149,19 @@ public void testSubscribeChannelsActionNullTokens() throws Exception { ServerAction sa = ActionFactoryTest.createServerAction(minion, action); action.addServerAction(sa); - saltServerActionService.setCommitTransaction(false); Map, List> 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 @@ -2183,8 +2173,9 @@ public void testSubscribeChannelsActionNullTokens() throws Exception { JobReturnEventMessageAction messageAction = new JobReturnEventMessageAction(saltServerActionService, saltUtils); messageAction.execute(message); + Optional 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) { diff --git a/java/code/src/com/suse/manager/webui/services/test/SaltServerActionServiceTest.java b/java/code/src/com/suse/manager/webui/services/test/SaltServerActionServiceTest.java index c9c51ac6a3d8..65e52317fa68 100644 --- a/java/code/src/com/suse/manager/webui/services/test/SaltServerActionServiceTest.java +++ b/java/code/src/com/suse/manager/webui/services/test/SaltServerActionServiceTest.java @@ -688,15 +688,6 @@ public Map 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); @@ -721,7 +712,6 @@ public void refreshPillar(MinionList minionList) { ActionFactory.addServerToAction(minion1, action); Map, List> calls = saltServerActionService.callsForAction(action); - //ccema.execute(new ChannelsChangedEventMessage(minion1.getId(), user.getId())); HibernateFactory.getSession().flush(); HibernateFactory.getSession().clear();