diff --git a/checker/mysql/mysql_test.go b/checker/mysql/mysql_test.go new file mode 100644 index 0000000..e5f3a3f --- /dev/null +++ b/checker/mysql/mysql_test.go @@ -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)) +} diff --git a/checker/postgresql/postgresql.go b/checker/postgresql/postgresql.go index f1d0e8f..da1d2dd 100644 --- a/checker/postgresql/postgresql.go +++ b/checker/postgresql/postgresql.go @@ -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, @@ -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) diff --git a/checker/postgresql/postgresql_test.go b/checker/postgresql/postgresql_test.go new file mode 100644 index 0000000..2eab815 --- /dev/null +++ b/checker/postgresql/postgresql_test.go @@ -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)) +} diff --git a/checker/redis/redis_test.go b/checker/redis/redis_test.go index 41f69d0..554eca7 100644 --- a/checker/redis/redis_test.go +++ b/checker/redis/redis_test.go @@ -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 ( diff --git a/go.mod b/go.mod index b5130ce..5302e41 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 17aec48..35d2945 100644 --- a/go.sum +++ b/go.sum @@ -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=