From 99f123ac6cad4d1b7ebbbc6cf10567fd2f498a93 Mon Sep 17 00:00:00 2001 From: Yannay Hammer Date: Fri, 6 Sep 2024 10:56:52 +0300 Subject: [PATCH] better matchers --- internal/resource/default_user_secret_test.go | 37 ++++--------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/internal/resource/default_user_secret_test.go b/internal/resource/default_user_secret_test.go index 26ce6931c..0a97edb1a 100644 --- a/internal/resource/default_user_secret_test.go +++ b/internal/resource/default_user_secret_test.go @@ -57,7 +57,6 @@ var _ = Describe("DefaultUserSecret", func() { var password []byte var host []byte var port []byte - var connection_string []byte var ok bool obj, err := defaultUserSecretBuilder.Build() @@ -104,22 +103,12 @@ var _ = Describe("DefaultUserSecret", func() { }) By("Setting a connection string", func() { - connection_string, ok = secret.Data["connection_string"] - Expect(ok).To(BeTrue(), "Failed to find a key \"connection_string\" in the generated Secret") + Expect(secret.Data).To(HaveKey("username"), "Failed to find a key \"username\" in the generated Secret") + Expect(secret.Data).To(HaveKey("password"), "Failed to find a key \"password\" in the generated Secret") + Expect(secret.Data).To(HaveKey("host"), "Failed to find a key \"host\" in the generated Secret") + Expect(secret.Data).To(HaveKey("port"), "Failed to find a key \"port\" in the generated Secret") - username, ok = secret.Data["username"] - Expect(ok).To(BeTrue(), "Failed to find a key \"username\" in the generated Secret") - - password, ok = secret.Data["password"] - Expect(ok).To(BeTrue(), "Failed to find a key \"password\" in the generated Secret") - - host, ok = secret.Data["host"] - Expect(ok).To(BeTrue(), "Failed to find a key \"host\" in the generated Secret") - - port, ok = secret.Data["port"] - Expect(ok).To(BeTrue(), "Failed to find a key \"port\" in the generated Secret") - - Expect(connection_string).To(BeEquivalentTo(fmt.Sprintf("amqp://%s:%s@%s:%s/", username, password, host, port))) + Expect(secret.Data).To(HaveKeyWithValue("connection_string", []byte(fmt.Sprintf("amqp://%s:%s@%s:%s/", secret.Data["username"], secret.Data["password"], secret.Data["host"], secret.Data["port"])))) }) By("creating a default_user.conf file that contains the correct sysctl config format to be parsed by RabbitMQ", func() { @@ -197,18 +186,11 @@ var _ = Describe("DefaultUserSecret", func() { secret = obj.(*corev1.Secret) By("Setting the AMQPS port in the user secret", func() { - var port []byte - port, ok := secret.Data["port"] - Expect(ok).To(BeTrue(), "Failed to find key \"port\" in the generated Secret") - Expect(port).To(BeEquivalentTo("5671")) + Expect(secret.Data).To(HaveKeyWithValue("port", []byte("5671"))) }) By("setting the connection string to use the AMQPS protocol", func() { - var connection_string []byte - connection_string, ok := secret.Data["connection_string"] - Expect(ok).To(BeTrue(), "Failed to find key \"connection_string\" in the generated Secret") - Expect(connection_string).To(HavePrefix("amqps:")) - Expect(connection_string).To(HaveSuffix(":5671/")) + Expect(secret.Data).To(HaveKeyWithValue("connection_string", MatchRegexp("amqps:.*:5671/"))) }) }) @@ -369,10 +351,7 @@ var _ = Describe("DefaultUserSecret", func() { Expect(ok).To(BeTrue()) Expect(port).To(BeEquivalentTo("5671")) - connection_string, ok := secret.Data["connection_string"] - Expect(ok).To(BeTrue(), "Failed to find key \"connection_string\" in the generated Secret") - Expect(connection_string).To(HavePrefix("amqps:")) - Expect(connection_string).To(HaveSuffix(":5671/")) + Expect(secret.Data).To(HaveKeyWithValue("connection_string", MatchRegexp("amqps:.*:5671/"))) port, ok = secret.Data["mqtt-port"] Expect(ok).To(BeTrue())