Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: wait4x/wait4x
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.14.0
Choose a base ref
...
head repository: wait4x/wait4x
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.14.1
Choose a head ref
Loading
3 changes: 2 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: 2
updates:
- package-ecosystem: "gomod"
open-pull-requests-limit: 10
directory: "/"
schedule:
interval: "daily"
@@ -17,4 +18,4 @@ updates:
- "atkrad"
labels:
- "actions"
- "dependencies"
- "dependencies"
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -99,7 +99,7 @@ jobs:
type=edge,branch=${{ github.event.repository.default_branch }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
uses: docker/setup-buildx-action@v3
with:
config-inline: |
[worker.oci]
@@ -140,15 +140,15 @@ jobs:
${{ steps.meta.outputs.bake-file }}
- name: Docker Hub Description
uses: peter-evans/dockerhub-description@v3
uses: peter-evans/dockerhub-description@v4
if: ${{ github.event_name == 'push' && github.ref_name == github.event.repository.default_branch }}
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
short-description: ${{ github.event.repository.description }}

- name: GitHub Release
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2
if: ${{ startsWith(github.ref, 'refs/tags/') }}
with:
draft: true
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ RUN --mount=from=binary,target=/build \
&& tar -czvf "/out/wait4x-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}.tar.gz" * \
# Change dir to "/out" to prevent adding "/out" in the sha256sum command output.
&& cd /out \
&& sha256sum -z "wait4x-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}.tar.gz" > "wait4x-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}.tar.gz.sha256sum"
&& sha256sum "wait4x-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}.tar.gz" > "wait4x-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}.tar.gz.sha256sum"

FROM scratch AS artifact
COPY --from=releaser /out /
12 changes: 12 additions & 0 deletions checker/mongodb/mongodb.go
Original file line number Diff line number Diff line change
@@ -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 (
@@ -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
@@ -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
}

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))
}
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
@@ -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)
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))
}
Loading