Skip to content

Commit

Permalink
delete federation v0,v1 queues after tests
Browse files Browse the repository at this point in the history
  • Loading branch information
battermann committed Dec 13, 2024
1 parent 20e3d64 commit 2ea2bb2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
8 changes: 8 additions & 0 deletions charts/integration/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ data:
host: rabbitmq
adminPort: 15671
rabbitmq-v0:
host: rabbitmq.wire-federation-v0.svc.cluster.local
adminPort: 15671
rabbitmq-v1:
host: rabbitmq.wire-federation-v1.svc.cluster.local
adminPort: 15671
backendTwo:
brig:
Expand Down
8 changes: 8 additions & 0 deletions charts/integration/templates/integration-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ spec:
secretKeyRef:
name: brig
key: rabbitmqPassword
- name: RABBITMQ_USERNAME_V0
value: "dummy-rabbit-mq-username-v0" # todo
- name: RABBITMQ_PASSWORD_V0
value: "dummy-rabbit-mq-password-v0" # todo
- name: RABBITMQ_USERNAME_V1
value: "dummy-rabbit-mq-username-v1" # todo
- name: RABBITMQ_PASSWORD_V1
value: "dummy-rabbit-mq-password-v1" # todo
{{- if hasKey .Values.secrets "redisUsername" }}
- name: REDIS_USERNAME
valueFrom:
Expand Down
2 changes: 2 additions & 0 deletions integration/test/Testlib/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ mkGlobalEnv cfgFile = do
gServicesCwdBase = devEnvProjectRoot <&> (</> "services"),
gBackendResourcePool = resourcePool,
gRabbitMQConfig = intConfig.rabbitmq,
gRabbitMQConfigV0 = intConfig.rabbitmqV0,
gRabbitMQConfigV1 = intConfig.rabbitmqV1,
gTempDir = tempDir,
gTimeOutSeconds = timeOutSeconds
}
Expand Down
51 changes: 48 additions & 3 deletions integration/test/Testlib/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import Testlib.XML
import Text.Printf
import UnliftIO.Async
import Prelude
import Network.AMQP.Extended
import Network.RabbitMqAdmin
import qualified Data.Text as T
import Data.Text (Text)
import Data.Maybe (fromMaybe)

runTest :: GlobalEnv -> App a -> IO (Either String a)
runTest ge action = lowerCodensity $ do
Expand Down Expand Up @@ -135,12 +140,52 @@ runTests tests mXMLOutput cfg = do
wait displayThread
printReport report
mapM_ (saveXMLReport report) mXMLOutput
deleteFederationQueues
deleteFederationV0AndV1Queues genv
when (any (\testCase -> testCase.result /= TestSuccess) report.cases) $
exitFailure

deleteFederationQueues :: IO ()
deleteFederationQueues = pure ()
deleteFederationV0AndV1Queues :: GlobalEnv -> IO ()
deleteFederationV0AndV1Queues env = do
putStrLn "Attempting to delete federation V0 and V1 queues..."
mNs <- fmap T.pack <$> lookupEnv "NAMESPACE"
case mNs of
Nothing -> putStrLn "No NAMESPACE env var set, skipping deletion of federation V0,V1 queues"
Just ns -> do
(mV0User, mV0Pass) <- readV0CredsFromEnv
fromMaybe (putStrLn "No or incomplete credentials for fed V0 RabbitMQ") $ deleteFederationQueues ns <$> env.gRabbitMQConfigV0 <*> mV0User <*> mV0Pass
(mV1User, mV1Pass) <- readV1CredsFromEnv
fromMaybe (putStrLn "No or incomplete credentials for fed V1 RabbitMQ") $ deleteFederationQueues ns <$> env.gRabbitMQConfigV1 <*> mV1User <*> mV1Pass
where
readV0CredsFromEnv :: IO (Maybe Text, Maybe Text)
readV0CredsFromEnv =
(,)
<$> (fmap T.pack <$> lookupEnv "RABBITMQ_USERNAME_V0")
<*> (fmap T.pack <$> lookupEnv "RABBITMQ_PASSWORD_V0")

readV1CredsFromEnv :: IO (Maybe Text, Maybe Text)
readV1CredsFromEnv =
(,)
<$> (fmap T.pack <$> lookupEnv "RABBITMQ_USERNAME_V1")
<*> (fmap T.pack <$> lookupEnv "RABBITMQ_PASSWORD_V1")


deleteFederationQueues :: Text -> RabbitMQConfig -> Text -> Text -> IO ()
deleteFederationQueues ns rc username password = do
let vHost = T.pack "\\"
let opts =
RabbitMqAdminOpts
{ host = rc.host,
port = 0,
adminPort = fromIntegral rc.adminPort,
vHost = vHost,
tls = Just $ RabbitMqTlsOpts Nothing True
}
client <- mkRabbitMqAdminClientEnvWithCreds opts username password
queues <- listQueuesByVHost client vHost Nothing Nothing
for_ queues $ \queue -> do
when (ns `T.isInfixOf` queue.name) $ do
putStrLn $ "Deleting queue " <> T.unpack queue.name
void $ deleteQueue client vHost queue.name

doListTests :: [(String, String, String, x)] -> IO ()
doListTests tests = for_ tests $ \(qname, _desc, _full, _) -> do
Expand Down
6 changes: 6 additions & 0 deletions integration/test/Testlib/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ data GlobalEnv = GlobalEnv
gServicesCwdBase :: Maybe FilePath,
gBackendResourcePool :: ResourcePool BackendResource,
gRabbitMQConfig :: RabbitMQConfig,
gRabbitMQConfigV0 :: Maybe RabbitMQConfig,
gRabbitMQConfigV1 :: Maybe RabbitMQConfig,
gTempDir :: FilePath,
gTimeOutSeconds :: Int
}
Expand All @@ -127,6 +129,8 @@ data IntegrationConfig = IntegrationConfig
integrationTestHostName :: String,
dynamicBackends :: Map String DynamicBackendConfig,
rabbitmq :: RabbitMQConfig,
rabbitmqV0 :: Maybe RabbitMQConfig,
rabbitmqV1 :: Maybe RabbitMQConfig,
cassandra :: CassandraConfig
}
deriving (Show, Generic)
Expand All @@ -142,6 +146,8 @@ instance FromJSON IntegrationConfig where
<*> o .: fromString "integrationTestHostName"
<*> o .: fromString "dynamicBackends"
<*> o .: fromString "rabbitmq"
<*> o .:? fromString "rabbitmq-v0"
<*> o .:? fromString "rabbitmq-v1"
<*> o .: fromString "cassandra"

data ServiceMap = ServiceMap
Expand Down
9 changes: 6 additions & 3 deletions libs/extended/src/Network/AMQP/Extended.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Network.AMQP.Extended
withConnection,
openConnectionWithRetries,
mkRabbitMqAdminClientEnv,
mkRabbitMqAdminClientEnvWithCreds,
mkRabbitMqChannelMVar,
demoteOpts,
RabbitMqTlsOpts (..),
Expand Down Expand Up @@ -91,9 +92,8 @@ instance FromJSON RabbitMqAdminOpts where
<*> parseTlsJson v
<*> v .: "adminPort"

mkRabbitMqAdminClientEnv :: RabbitMqAdminOpts -> IO (AdminAPI (AsClientT IO))
mkRabbitMqAdminClientEnv opts = do
(username, password) <- readCredsFromEnv
mkRabbitMqAdminClientEnvWithCreds ::RabbitMqAdminOpts -> Text -> Text -> IO (AdminAPI (AsClientT IO))
mkRabbitMqAdminClientEnvWithCreds opts username password = do
mTlsSettings <- traverse (mkTLSSettings opts.host) opts.tls
let (protocol, managerSettings) = case mTlsSettings of
Nothing -> (Servant.Http, HTTP.defaultManagerSettings)
Expand All @@ -107,6 +107,9 @@ mkRabbitMqAdminClientEnv opts = do
(either throwM pure <=< flip runClientM clientEnv)
(toServant $ adminClient basicAuthData)

mkRabbitMqAdminClientEnv :: RabbitMqAdminOpts -> IO (AdminAPI (AsClientT IO))
mkRabbitMqAdminClientEnv opts = readCredsFromEnv >>= uncurry (mkRabbitMqAdminClientEnvWithCreds opts)

-- | When admin opts are needed use `AmqpEndpoint Identity`, otherwise use
-- `AmqpEndpoint NoAdmin`.
data AmqpEndpoint = AmqpEndpoint
Expand Down

0 comments on commit 2ea2bb2

Please sign in to comment.