Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for MySQL and PostgreSQL checkers #237

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions checker/mysql/mysql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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 mysql

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

// MySQLSuite is a test suite for MySQL checker
type MySQLSuite struct {
suite.Suite
container *mysql.MySQLContainer
}

// SetupSuite starts a MySQL container
func (s *MySQLSuite) SetupSuite() {
var err error
s.container, err = mysql.RunContainer(context.Background())
s.Require().NoError(err)
}

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

// TestIdentity tests the identity of the MySQL checker
func (s *MySQLSuite) TestIdentity() {
chk := New("user:password@tcp(localhost:3306)/dbname?tls=skip-verify")
identity, err := chk.Identity()

s.Require().NoError(err)
s.Assert().Equal("localhost:3306", identity)
}

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

s.Assert().ErrorContains(err, "default addr for network 'xxx:/' unknown")
}

// TestValidConnection tests the valid connection of the MySQL server
func (s *MySQLSuite) TestInvalidConnection() {
var expectedError *checker.ExpectedError
chk := New("user:password@tcp(localhost:8080)/dbname?tls=skip-verify")

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

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

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

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

// TestMySQL runs the MySQL test suite
func TestMySQL(t *testing.T) {
suite.Run(t, new(MySQLSuite))
}
8 changes: 4 additions & 4 deletions checker/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (

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

// PostgreSQL represents PostgreSQL checker
// PostgreSQL is a checker for PostgreSQL
type PostgreSQL struct {
dsn string
}

// New creates the PostgreSQL checker
// New creates a new PostgreSQL checker
func New(dsn string) checker.Checker {
p := &PostgreSQL{
dsn: dsn,
Expand All @@ -41,8 +41,8 @@ func New(dsn string) checker.Checker {
return p
}

// Identity returns the identity of the checker
func (p PostgreSQL) Identity() (string, error) {
// Identity returns the PostgreSQL checker identity
func (p *PostgreSQL) Identity() (string, error) {
u, err := url.Parse(p.dsn)
if err != nil {
return "", fmt.Errorf("can't retrieve the checker identity: %w", err)
Expand Down
83 changes: 83 additions & 0 deletions checker/postgresql/postgresql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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 postgresql

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

// PostgreSQLSuite is a test suite for PostgreSQL checker
type PostgreSQLSuite struct {
suite.Suite
container *postgres.PostgresContainer
}

// SetupSuite starts a PostgreSQL container
func (s *PostgreSQLSuite) SetupSuite() {
var err error
s.container, err = postgres.RunContainer(context.Background())
s.Require().NoError(err)
}

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

// TestIdentity tests the identity of the PostgreSQL checker
func (s *PostgreSQLSuite) TestIdentity() {
chk := New("postgres://bob:secret@1.2.3.4:5432/mydb?sslmode=verify-full")
identity, err := chk.Identity()

s.Require().NoError(err)
s.Assert().Equal("1.2.3.4:5432", identity)
}

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

s.Assert().ErrorContains(err, "first path segment in URL cannot contain colon")
}

// TestValidConnection tests the valid connection of the PostgreSQL server
func (s *PostgreSQLSuite) TestInvalidConnection() {
var expectedError *checker.ExpectedError
chk := New("postgres://bob:secret@1.2.3.4:5432/mydb?sslmode=verify-full")

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

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

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

chk := New(endpoint + "sslmode=disable")
s.Assert().Nil(chk.Check(ctx))
}

// TestPostgreSQL runs the PostgreSQL test suite
func TestPostgreSQL(t *testing.T) {
suite.Run(t, new(PostgreSQLSuite))
}
14 changes: 14 additions & 0 deletions checker/redis/redis_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2023 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 redis

import (
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ 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/mysql v0.27.0
github.com/testcontainers/testcontainers-go/modules/postgres 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
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ 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/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=
github.com/testcontainers/testcontainers-go/modules/postgres v0.27.0/go.mod h1:VFrFKUUgET2hNXStdtaC7uOIJWviFUrixhKeaVw/4F4=
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
Loading