Skip to content

Commit

Permalink
Merge pull request #238 from atkrad/add-mongodb-checker-unittests
Browse files Browse the repository at this point in the history
Add unit tests for MongoDB checker
  • Loading branch information
atkrad authored Feb 3, 2024
2 parents e9965e2 + 86d4403 commit 2f0b8c6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
12 changes: 12 additions & 0 deletions checker/mongodb/mongodb.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 mongodb provides MongoDB checker.
package mongodb

import (
Expand All @@ -20,10 +21,14 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
"regexp"
"strings"
"wait4x.dev/v2/checker"
)

var hidePasswordRegexp = regexp.MustCompile(`^(mongodb://[^/:]+):[^:@]+@`)

// MongoDB represents MongoDB checker
type MongoDB struct {
dsn string
Expand Down Expand Up @@ -65,6 +70,13 @@ func (m *MongoDB) Check(ctx context.Context) (err error) {
// Ping the primary
err = c.Ping(ctx, readpref.Primary())
if err != nil {
if checker.IsConnectionRefused(err) || errors.Is(err, topology.ErrServerSelectionTimeout) {
return checker.NewExpectedError(
"failed to establish a connection to the MongoDB server", err,
"dsn", hidePasswordRegexp.ReplaceAllString(m.dsn, `$1:***@`),
)
}

return err
}

Expand Down
84 changes: 84 additions & 0 deletions checker/mongodb/mongodb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 mongodb provides a MongoDB checker.
package mongodb

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

// MongoDBSuite is a test suite for MongoDB checker
type MongoDBSuite struct {
suite.Suite
container *mongodb.MongoDBContainer
}

// SetupSuite starts a MongoDB container
func (s *MongoDBSuite) SetupSuite() {
var err error
s.container, err = mongodb.RunContainer(context.Background())
s.Require().NoError(err)
}

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

// TestIdentity tests the identity of the MongoDB checker
func (s *MongoDBSuite) TestIdentity() {
chk := New("mongodb://127.0.0.1:27017")
identity, err := chk.Identity()

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

// TestInvalidIdentity tests the invalid identity of the MongoDB checker
func (s *MongoDBSuite) TestInvalidIdentity() {
chk := New("xxx://127.0.0.1:3306")
_, err := chk.Identity()

s.Assert().ErrorContains(err, "can't retrieve the checker identity")
}

// TestValidConnection tests the invalid connection of the MongoDB server
func (s *MongoDBSuite) TestInvalidConnection() {
var expectedError *checker.ExpectedError
chk := New("mongodb://127.0.0.1:8080")

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

// TestValidConnection tests the valid connection of the MongoDB server
func (s *MongoDBSuite) TestValidConnection() {
ctx := context.Background()

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

chk := New(endpoint)
s.Assert().Nil(chk.Check(ctx))
}

// TestMongoDB runs the MongoDB test suite
func TestMongoDB(t *testing.T) {
suite.Run(t, new(MongoDBSuite))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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/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/redis v0.27.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/testcontainers/testcontainers-go v0.27.0 h1:IeIrJN4twonTDuMuBNQdKZ+K97yd7VrmNGu+lDpYcDk=
github.com/testcontainers/testcontainers-go v0.27.0/go.mod h1:+HgYZcd17GshBUZv9b+jKFJ198heWPQq3KQIp2+N+7U=
github.com/testcontainers/testcontainers-go/modules/mongodb v0.27.0 h1:vVTdWZtnT8RmzILmEoKryIzTpity+sZw6A8YtWJ4Wvc=
github.com/testcontainers/testcontainers-go/modules/mongodb v0.27.0/go.mod h1:ksr0hw60k3XNHxcGzKyfZAW8aG6Xa88sp1T97UJHEkI=
github.com/testcontainers/testcontainers-go/modules/mysql v0.27.0 h1:6p/o/bAZPcFiBWTd71umQmj/i4L6ipVK3B2ZJBqn5HM=
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=
Expand Down

0 comments on commit 2f0b8c6

Please sign in to comment.