Skip to content

Commit

Permalink
Add unit tests for RabbitMQ checker
Browse files Browse the repository at this point in the history
  • Loading branch information
atkrad committed Feb 3, 2024
1 parent 2f0b8c6 commit b062e6b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
14 changes: 8 additions & 6 deletions checker/rabbitmq/rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package rabbitmq provides RabbitMQ checker.
package rabbitmq

import (
Expand All @@ -35,9 +36,9 @@ const (
DefaultHeartbeat = 10 * time.Second
// DefaultConnectionTimeout is the default connection timeout duration
DefaultConnectionTimeout = 3 * time.Second
// DefaultLocale is the default connection locale
// DefaultLocale is the default locale
DefaultLocale = "en_US"
// DefaultInsecureSkipTLSVerify is the default insecure skip tls verify
// DefaultInsecureSkipTLSVerify is the default value for whether to skip tls verify
DefaultInsecureSkipTLSVerify = false
)

Expand All @@ -56,30 +57,30 @@ func New(dsn string, opts ...Option) checker.Checker {
insecureSkipTLSVerify: DefaultInsecureSkipTLSVerify,
}

// apply the list of options to RabbitMQ
// Apply options to RabbitMQ checker
for _, opt := range opts {
opt(t)
}

return t
}

// WithTimeout configures a timeout for maximum amount of time a dial will wait for a connection to complete
// WithTimeout configures a timeout for establishing new connections
func WithTimeout(timeout time.Duration) Option {
return func(r *RabbitMQ) {
r.timeout = timeout
}
}

// WithInsecureSkipTLSVerify controls whether a client verifies the server's certificate chain and hostname
// WithInsecureSkipTLSVerify configures whether to skip tls verify
func WithInsecureSkipTLSVerify(insecureSkipTLSVerify bool) Option {
return func(r *RabbitMQ) {
r.insecureSkipTLSVerify = insecureSkipTLSVerify
}
}

// Identity returns the identity of the checker
func (r RabbitMQ) Identity() (string, error) {
func (r *RabbitMQ) Identity() (string, error) {
u, err := amqp.ParseURI(r.dsn)
if err != nil {
return "", fmt.Errorf("can't retrieve the checker identity: %w", err)
Expand Down Expand Up @@ -134,6 +135,7 @@ func (r *RabbitMQ) Check(ctx context.Context) (err error) {
}
}(conn)

// Open a channel to check the connection.
_, err = conn.Channel()
if err != nil {
return err
Expand Down
86 changes: 86 additions & 0 deletions checker/rabbitmq/rabbitmq_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2024 The Wait4X Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package rabbitmq provides RabbitMQ checker.
package rabbitmq

import (
"context"
"github.com/stretchr/testify/suite"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/rabbitmq"
"testing"
"time"
"wait4x.dev/v2/checker"
)

// RabbitMQSuite is a test suite for RabbitMQ checker
type RabbitMQSuite struct {
suite.Suite
container *rabbitmq.RabbitMQContainer
}

// SetupSuite starts a RabbitMQ container
func (s *RabbitMQSuite) SetupSuite() {
var err error
s.container, err = rabbitmq.RunContainer(context.Background(), testcontainers.WithImage("rabbitmq:3.12.12-management-alpine"))
s.Require().NoError(err)
}

// TearDownSuite stops the RabbitMQ container
func (s *RabbitMQSuite) TearDownSuite() {
err := s.container.Terminate(context.Background())
s.Require().NoError(err)
}

// TestIdentity tests the identity of the RabbitMQ checker
func (s *RabbitMQSuite) TestIdentity() {
chk := New("amqp://guest:[email protected]:5672/vhost")
identity, err := chk.Identity()

s.Require().NoError(err)
s.Assert().Equal("127.0.0.1:5672", identity)
}

// TestInvalidIdentity tests the invalid identity of the RabbitMQ checker
func (s *RabbitMQSuite) TestInvalidIdentity() {
chk := New("127.0.0.1:5672")
_, err := chk.Identity()

s.Assert().ErrorContains(err, `can't retrieve the checker identity: parse "127.0.0.1:5672"`)
}

// TestValidConnection tests the valid connection of the RabbitMQ server
func (s *RabbitMQSuite) TestInvalidConnection() {
var expectedError *checker.ExpectedError
chk := New("amqp://user:[email protected]:5672/vhost")

s.Assert().ErrorAs(chk.Check(context.Background()), &expectedError)
}

// TestValidAddress tests the valid address of the RabbitMQ server
func (s *RabbitMQSuite) TestValidConnection() {
ctx := context.Background()

endpoint, err := s.container.AmqpURL(ctx)
s.Require().NoError(err)

chk := New(endpoint, WithTimeout(5*time.Second), WithInsecureSkipTLSVerify(true))
s.Assert().Nil(chk.Check(ctx))
}

// TestRabbitMQ runs the RabbitMQ test suite
func TestRabbitMQ(t *testing.T) {
suite.Run(t, new(RabbitMQSuite))
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ require (
github.com/rs/zerolog v1.31.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.27.0
github.com/testcontainers/testcontainers-go/modules/mongodb v0.27.0
github.com/testcontainers/testcontainers-go/modules/mysql v0.27.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.27.0
github.com/testcontainers/testcontainers-go/modules/rabbitmq v0.27.0
github.com/testcontainers/testcontainers-go/modules/redis v0.27.0
github.com/tidwall/gjson v1.17.0
github.com/tonglil/buflogr v1.1.1
Expand Down Expand Up @@ -72,7 +74,6 @@ require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/testcontainers/testcontainers-go v0.27.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ github.com/testcontainers/testcontainers-go/modules/mysql v0.27.0 h1:6p/o/bAZPcF
github.com/testcontainers/testcontainers-go/modules/mysql v0.27.0/go.mod h1:zhVYEruMWC10K9sNwpUqpY3/vUmnyfhSWFs80ySA4mY=
github.com/testcontainers/testcontainers-go/modules/postgres v0.27.0 h1:gbA/HYjBIwOwhE/t4p3kIprfI0qsxCk+YVW7P9XFOus=
github.com/testcontainers/testcontainers-go/modules/postgres v0.27.0/go.mod h1:VFrFKUUgET2hNXStdtaC7uOIJWviFUrixhKeaVw/4F4=
github.com/testcontainers/testcontainers-go/modules/rabbitmq v0.27.0 h1:PGcjFP0x4Da00ApAHEZz6DHWS6ymww+eO52IpP9oO80=
github.com/testcontainers/testcontainers-go/modules/rabbitmq v0.27.0/go.mod h1:4yBccxbVj58w9LC95QMXRHETmJNCwjiUU+cL1XRElv4=
github.com/testcontainers/testcontainers-go/modules/redis v0.27.0 h1:DAs9D0BmBPuvEnsY8fWLwQVx5ZGdDNJTj+iK+p5Dj98=
github.com/testcontainers/testcontainers-go/modules/redis v0.27.0/go.mod h1:xkfSzACp1p97x84TpLR8ZDzSimFv8ZP/pIrX8Nhf2gg=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
Expand Down

0 comments on commit b062e6b

Please sign in to comment.