Skip to content

Commit

Permalink
fix: 馃悰 fix in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Sep 20, 2023
1 parent 4c0b50c commit c34621e
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 108 deletions.
3 changes: 3 additions & 0 deletions internal/pkg/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/lib/pq v1.10.9
github.com/mcuadros/go-defaults v1.2.0
github.com/mehdihadeli/go-mediatr v1.1.10
github.com/michaelklishin/rabbit-hole v1.5.0
github.com/mitchellh/mapstructure v1.5.0
github.com/nolleh/caption_json_formatter v0.2.2
github.com/orlangure/gnomock v0.30.0
Expand Down Expand Up @@ -138,6 +139,7 @@ require (
github.com/moby/term v0.5.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opencontainers/runc v1.1.9 // indirect
Expand All @@ -158,6 +160,7 @@ require (
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/streadway/amqp v1.1.0 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
Expand Down
13 changes: 13 additions & 0 deletions internal/pkg/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw=
Expand Down Expand Up @@ -449,6 +450,8 @@ github.com/mcuadros/go-defaults v1.2.0 h1:FODb8WSf0uGaY8elWJAkoLL0Ri6AlZ1bFlenk5
github.com/mcuadros/go-defaults v1.2.0/go.mod h1:WEZtHEVIGYVDqkKSWBdWKUVdRyKlMfulPaGDWIVeCWY=
github.com/mehdihadeli/go-mediatr v1.1.10 h1:NAzg4065c90lgYeb+Vzbd2WKH0tUFpxzL0mpx6hkU/A=
github.com/mehdihadeli/go-mediatr v1.1.10/go.mod h1:lwgZl7qVL/RKomObBblhG3uEte/r4nJDV95Vd+nGrMw=
github.com/michaelklishin/rabbit-hole v1.5.0 h1:Bex27BiFDsijCM9D0ezSHqyy0kehpYHuNKaPqq/a4RM=
github.com/michaelklishin/rabbit-hole v1.5.0/go.mod h1:vvI1uOitYZi0O5HEGXhaWC1XT80Gy+HvFheJ+5Krlhk=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk=
Expand All @@ -468,9 +471,15 @@ github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nolleh/caption_json_formatter v0.2.2 h1:EKsOr/fCllNQF2ZoajfbSDlV73BNV1bDu1aTTSRrlN0=
github.com/nolleh/caption_json_formatter v0.2.2/go.mod h1:5FYofZA8NAej/eFxa12FvyQKosU1LfyKizZPlY0JojU=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
Expand Down Expand Up @@ -572,6 +581,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.16.0 h1:rGGH0XDZhdUOryiDWjmIvUSWpbNqisK8Wk0Vyefw8hc=
github.com/spf13/viper v1.16.0/go.mod h1:yg78JgCJcbrQOvV9YLXgkLaZqUidkY9K+Dd1FofRzQg=
github.com/streadway/amqp v1.1.0 h1:py12iX8XSyI7aN/3dUT8DFIDJazNJsVJdxNVEpnQTZM=
github.com/streadway/amqp v1.1.0/go.mod h1:WYSrTEYHOXHd0nwFeUXAe2G2hRnQT+deZJJf88uS9Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
Expand Down Expand Up @@ -854,6 +865,7 @@ golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -1108,6 +1120,7 @@ gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
9 changes: 9 additions & 0 deletions internal/pkg/test/containers/contracts/rabbitmq_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package contracts

import (
"context"
"fmt"
"testing"

"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/core/serializer"
Expand All @@ -23,6 +24,14 @@ type RabbitMQContainerOptions struct {
Tag string
}

func (h *RabbitMQContainerOptions) AmqpEndPoint() string {
return fmt.Sprintf("amqp://%s:%s@%s:%d", h.UserName, h.Password, h.Host, h.HostPort)
}

func (h *RabbitMQContainerOptions) HttpEndPoint() string {
return fmt.Sprintf("http://%s:%d", h.Host, h.HttpPort)
}

type RabbitMQContainer interface {
Start(ctx context.Context,
t *testing.T,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"emperror.dev/errors"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
rabbithole "github.com/michaelklishin/rabbit-hole"
"github.com/rabbitmq/amqp091-go"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
Expand Down Expand Up @@ -79,7 +80,10 @@ func (g *rabbitmqTestContainers) CreatingContainerOptions(
})

// get a free random host port for rabbitmq `Tcp Port`
hostPort, err := dbContainer.MappedPort(ctx, nat.Port(g.defaultOptions.Ports[0]))
hostPort, err := dbContainer.MappedPort(
ctx,
nat.Port(g.defaultOptions.Ports[0]),
)
if err != nil {
return nil, err
}
Expand All @@ -88,7 +92,10 @@ func (g *rabbitmqTestContainers) CreatingContainerOptions(

// https://github.com/michaelklishin/rabbit-hole/issues/74
// get a free random host port for rabbitmq UI `Http Port`
uiHttpPort, err := dbContainer.MappedPort(ctx, nat.Port(g.defaultOptions.Ports[1]))
uiHttpPort, err := dbContainer.MappedPort(
ctx,
nat.Port(g.defaultOptions.Ports[1]),
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -136,7 +143,9 @@ func (g *rabbitmqTestContainers) Start(
rabbitHostOptions.AmqpEndPoint(),
)

rabbitmqConfig := &config.RabbitmqOptions{RabbitmqHostOptions: rabbitHostOptions}
rabbitmqConfig := &config.RabbitmqOptions{
RabbitmqHostOptions: rabbitHostOptions,
}
conn, err := types.NewRabbitMQConnection(rabbitmqConfig)
if err != nil {
return nil, err
Expand Down Expand Up @@ -189,9 +198,15 @@ func (g *rabbitmqTestContainers) getRunOptions(
}
}
containerReq := testcontainers.ContainerRequest{
Image: fmt.Sprintf("%s:%s", g.defaultOptions.ImageName, g.defaultOptions.Tag),
Image: fmt.Sprintf(
"%s:%s",
g.defaultOptions.ImageName,
g.defaultOptions.Tag,
),
ExposedPorts: g.defaultOptions.Ports,
WaitingFor: wait.ForListeningPort(nat.Port(g.defaultOptions.Ports[0])),
WaitingFor: wait.ForListeningPort(
nat.Port(g.defaultOptions.Ports[0]),
),
HostConfigModifier: func(hostConfig *container.HostConfig) {
hostConfig.AutoRemove = true
},
Expand All @@ -205,23 +220,53 @@ func (g *rabbitmqTestContainers) getRunOptions(
return containerReq
}

func IsConnectable(logger logger.Logger, options *contracts.RabbitMQContainerOptions) bool {
conn, err := amqp091.Dial(
fmt.Sprintf("amqp://%s:%s@%s:%d", options.UserName, options.Password, options.Host, options.HostPort),
)
func IsConnectable(
logger logger.Logger,
options *contracts.RabbitMQContainerOptions,
) bool {
conn, err := amqp091.Dial(options.AmqpEndPoint())
if err != nil {
logError(logger, options.UserName, options.Password, options.Host, options.HostPort)
logError(
logger,
options.UserName,
options.Password,
options.Host,
options.HostPort,
)

return false
}

defer conn.Close()

if err != nil || (conn != nil && conn.IsClosed()) {
logError(logger, options.UserName, options.Password, options.Host, options.HostPort)
logError(
logger,
options.UserName,
options.Password,
options.Host,
options.HostPort,
)

return false
}

// https://github.com/michaelklishin/rabbit-hole
rmqc, err := rabbithole.NewClient(
options.HttpEndPoint(),
options.UserName,
options.Password,
)
_, err = rmqc.ListExchanges()
if err != nil {
logger.Errorf(
"Error in creating rabbitmq connection with http host: %s",
options.HttpEndPoint(),
)

return false
}

logger.Infof(
"Opened rabbitmq connection on host: amqp://%s:%s@%s:%d",
options.UserName,
Expand All @@ -233,9 +278,19 @@ func IsConnectable(logger logger.Logger, options *contracts.RabbitMQContainerOpt
return true
}

func logError(logger logger.Logger, userName string, password string, host string, hostPort int) {
func logError(
logger logger.Logger,
userName string,
password string,
host string,
hostPort int,
) {
// we should not use `t.Error` or `t.Errorf` for logging errors because it will `fail` our test at the end and, we just should use logs without error like log.Error (not log.Fatal)
logger.Errorf(
"Error in creating rabbitmq connection with host: amqp://%s:%s@%s:%d", userName, password, host, hostPort,
"Error in creating rabbitmq connection with amqp host: amqp://%s:%s@%s:%d",
userName,
password,
host,
hostPort,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package integration

import (
"context"
"fmt"
"testing"
"time"

Expand All @@ -21,7 +20,6 @@ import (
"github.com/brianvoe/gofakeit/v6"
rabbithole "github.com/michaelklishin/rabbit-hole"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.opentelemetry.io/otel/trace"
Expand All @@ -43,16 +41,19 @@ type IntegrationTestSharedFixture struct {
Tracer trace.Tracer
}

func NewIntegrationTestSharedFixture(t *testing.T) *IntegrationTestSharedFixture {
func NewIntegrationTestSharedFixture(
t *testing.T,
) *IntegrationTestSharedFixture {
result := test.NewTestApp().Run(t)

// https://github.com/michaelklishin/rabbit-hole
rmqc, err := rabbithole.NewClient(
fmt.Sprintf(result.RabbitmqOptions.RabbitmqHostOptions.HttpEndPoint()),
result.RabbitmqOptions.RabbitmqHostOptions.HttpEndPoint(),
result.RabbitmqOptions.RabbitmqHostOptions.UserName,
result.RabbitmqOptions.RabbitmqHostOptions.Password)

require.NoError(t, err)
if err != nil {
result.Logger.Error(errors.WrapIf(err, "error in creating rabbithole client"))
}

shared := &IntegrationTestSharedFixture{
Log: result.Logger,
Expand All @@ -78,8 +79,9 @@ func (i *IntegrationTestSharedFixture) InitializeTest() {
// seed data in each test
res, err := seedData(i.mongoClient, i.MongoOptions.Database)
if err != nil {
i.Log.Fatal(err)
i.Log.Error(errors.WrapIf(err, "error in seeding mongodb data"))
}

i.Items = res
}

Expand All @@ -88,15 +90,18 @@ func (i *IntegrationTestSharedFixture) DisposeTest() {

// cleanup test containers with their hooks
if err := i.cleanupRabbitmqData(); err != nil {
i.Log.Fatal(err)
i.Log.Error(errors.WrapIf(err, "error in cleanup rabbitmq data"))
}

if err := i.cleanupMongoData(); err != nil {
i.Log.Fatal(err)
i.Log.Error(errors.WrapIf(err, "error in cleanup mongodb data"))
}
}

func seedData(db *mongo.Client, databaseName string) ([]*models.Product, error) {
func seedData(
db *mongo.Client,
databaseName string,
) ([]*models.Product, error) {
ctx := context.Background()

products := []*models.Product{
Expand All @@ -119,13 +124,18 @@ func seedData(db *mongo.Client, databaseName string) ([]*models.Product, error)
}

//// https://go.dev/doc/faq#convert_slice_of_interface
data := make([]interface{}, len(products))
productsData := make([]interface{}, len(products))

for i, v := range products {
data[i] = v
productsData[i] = v
}

collection := db.Database(databaseName).Collection("products")
_, err := collection.InsertMany(context.Background(), data, &options.InsertManyOptions{})
_, err := collection.InsertMany(
context.Background(),
productsData,
&options.InsertManyOptions{},
)
if err != nil {
return nil, errors.WrapIf(err, "error in seed database")
}
Expand All @@ -136,13 +146,16 @@ func seedData(db *mongo.Client, databaseName string) ([]*models.Product, error)
collection,
nil,
)

return result.Items, nil
}

func (i *IntegrationTestSharedFixture) cleanupRabbitmqData() error {
// https://github.com/michaelklishin/rabbit-hole
// Get all queues
queues, err := i.RabbitmqCleaner.ListQueuesIn(i.rabbitmqOptions.RabbitmqHostOptions.VirtualHost)
queues, err := i.RabbitmqCleaner.ListQueuesIn(
i.rabbitmqOptions.RabbitmqHostOptions.VirtualHost,
)
if err != nil {
return err
}
Expand All @@ -153,6 +166,7 @@ func (i *IntegrationTestSharedFixture) cleanupRabbitmqData() error {
i.rabbitmqOptions.RabbitmqHostOptions.VirtualHost,
queue.Name,
)

return err
}

Expand All @@ -161,11 +175,20 @@ func (i *IntegrationTestSharedFixture) cleanupRabbitmqData() error {

func (i *IntegrationTestSharedFixture) cleanupMongoData() error {
collections := []string{"products"}
err := cleanupCollections(i.mongoClient, collections, i.MongoOptions.Database)
err := cleanupCollections(
i.mongoClient,
collections,
i.MongoOptions.Database,
)

return err
}

func cleanupCollections(db *mongo.Client, collections []string, databaseName string) error {
func cleanupCollections(
db *mongo.Client,
collections []string,
databaseName string,
) error {
database := db.Database(databaseName)
ctx := context.Background()

Expand Down
Loading

0 comments on commit c34621e

Please sign in to comment.