-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from imperiuse/fix_testcontainers
fix. testscontainers
- Loading branch information
Showing
9 changed files
with
327 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package environment | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/imperiuse/golib/testcontainer" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
const ( | ||
NetworkName = "test_id_provisioning_network" | ||
|
||
AppName = "app" | ||
|
||
KafkaImage = "confluentinc/cp-kafka:7.2.0" | ||
KafkaBrokerPort = "9092" | ||
KafkaClientPort = "9101" | ||
KafkaContainerName = "broker" | ||
|
||
ZookeeperImage = "confluentinc/cp-zookeeper:7.2.0" | ||
ZooKeeperPort = "2181" | ||
ZooKeeperContainerName = "zookeeper" | ||
ZooTickTime = "2000" | ||
|
||
PostgresImage = "postgres:14" | ||
PostgresPort = "5432" | ||
PostgresContainerName = "db" | ||
PostgresUsername = "admin" | ||
PostgresPassword = "password" | ||
PostgresDB = "id-provisioning" | ||
) | ||
|
||
var ( | ||
zooCfg = testcontainer.ZookeeperConfig{ | ||
BaseContainerConfig: testcontainer.BaseContainerConfig{ | ||
Image: ZookeeperImage, | ||
Port: ZooKeeperPort, | ||
Name: ZooKeeperContainerName, | ||
ExposedPorts: []string{fmt.Sprintf("0.0.0.0:%[1]s:%[1]s", ZooKeeperPort)}, | ||
Envs: map[string]string{ | ||
"ZOOKEEPER_SERVER_ID": "1", | ||
"ZOOKEEPER_SERVERS": "zoo1:2888:3888", | ||
"ZOOKEEPER_CLIENT_PORT": ZooKeeperPort, | ||
"ZOOKEEPER_TICK_TIME": ZooTickTime, | ||
}, | ||
WaitingForStrategy: wait.ForLog("binding to port 0.0.0.0/0.0.0.0:2181"), | ||
}, | ||
} | ||
|
||
kafkaCfg = testcontainer.KafkaConfig{ | ||
BaseContainerConfig: testcontainer.BaseContainerConfig{ | ||
Image: KafkaImage, | ||
Name: KafkaContainerName, | ||
Port: KafkaClientPort, | ||
ExposedPorts: []string{ | ||
fmt.Sprintf("0.0.0.0:%[1]s:%[1]s", KafkaClientPort), | ||
fmt.Sprintf("0.0.0.0:%[1]s:%[1]s", KafkaBrokerPort), | ||
}, | ||
Envs: map[string]string{ | ||
"KAFKA_BROKER_ID": "1", | ||
"KAFKA_ZOOKEEPER_CONNECT": ZooKeeperContainerName + ":" + ZooKeeperPort, | ||
"KAFKA_LISTENER_SECURITY_PROTOCOL_MAP": "PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT", | ||
"KAFKA_ADVERTISED_HOST_NAME": KafkaContainerName, | ||
"KAFKA_ADVERTISED_LISTENERS": "PLAINTEXT://" + KafkaContainerName + ":29092,PLAINTEXT_HOST://localhost:" + | ||
KafkaBrokerPort, | ||
"KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR": "1", | ||
"KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS": "0", | ||
"KAFKA_CONFLUENT_LICENSE_TOPIC_REPLICATION_FACTOR": "1", | ||
"KAFKA_CONFLUENT_BALANCER_TOPIC_REPLICATION_FACTOR": "1", | ||
"KAFKA_TRANSACTION_STATE_LOG_MIN_ISR": "1", | ||
"KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR": "1", | ||
"KAFKA_AUTO_CREATE_TOPICS.ENABLE": "true", | ||
}, | ||
WaitingForStrategy: wait.ForLog("[KafkaServer id=1] started"), | ||
}, | ||
ClientPort: KafkaClientPort, | ||
BrokerPort: KafkaBrokerPort, | ||
} | ||
|
||
postgresCfg = testcontainer.PostgresConfig{BaseContainerConfig: testcontainer.BaseContainerConfig{ | ||
Name: PostgresContainerName, | ||
Image: PostgresImage, | ||
Port: PostgresPort, | ||
ExposedPorts: []string{fmt.Sprintf("0.0.0.0:%[1]s:%[1]s", PostgresPort)}, | ||
Envs: map[string]string{ | ||
"POSTGRES_USER": PostgresUsername, | ||
"POSTGRES_PASSWORD": PostgresPassword, | ||
"POSTGRES_DB": PostgresDB, | ||
}, | ||
WaitingForStrategy: wait.ForLog("database system is ready to accept connections"), | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package environment | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/imperiuse/golib/testcontainer" | ||
"github.com/stretchr/testify/require" | ||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
type ContainersEnvironment struct { | ||
// First - native go-testcontainers way for creating docker containers. | ||
dockerNetwork *testcontainers.DockerNetwork | ||
kafkaContainer *testcontainer.KafkaCluster | ||
postgresContainer testcontainers.Container | ||
|
||
// Second - docker-compose way + go-testcontainers for create docker container env. | ||
compose testcontainers.DockerCompose | ||
} | ||
|
||
// StartPureDockerEnvironment - create and start docker containers env with first way. | ||
func (c *ContainersEnvironment) StartPureDockerEnvironment(t *testing.T, ctx context.Context) { | ||
t.Log("> From SetupEnvironment") | ||
|
||
t.Log("Create docker network") | ||
dn, err := testcontainer.NewDockerNetwork(ctx, NetworkName) | ||
require.Nil(t, err, "error must be nil for NewDockerNetwork") | ||
require.NotNil(t, dn, "docker network must be not nil") | ||
c.dockerNetwork = dn.(*testcontainers.DockerNetwork) | ||
|
||
t.Log("Create service deps") | ||
c.kafkaContainer, err = testcontainer.NewKafkaCluster(ctx, kafkaCfg, zooCfg, c.dockerNetwork) | ||
require.Nil(t, err, "error must be nil, when create NewKafkaCluster") | ||
require.NotNil(t, c.kafkaContainer, "kafka cluster must be not nil") | ||
|
||
c.postgresContainer, err = testcontainer.NewPostgresContainer(ctx, postgresCfg, c.dockerNetwork) | ||
require.Nil(t, err, "error must be nil, when create NewPostgresContainer") | ||
require.NotNil(t, c.postgresContainer, "postgres container must be not nil") | ||
|
||
t.Log("Start deps services containers") | ||
|
||
require.Nil(t, c.kafkaContainer.Start(ctx), "kafka cluster must start without errors") | ||
|
||
require.Nil(t, c.postgresContainer.Start(ctx), "postgres must start without errors") | ||
|
||
const magicTime = time.Second * 3 | ||
time.Sleep(magicTime) // time sleep development // todo think how to remove this | ||
} | ||
|
||
// FinishedPureDockerEnvironment - finished containers (env) which we created by first way. | ||
func (c *ContainersEnvironment) FinishedPureDockerEnvironment(t *testing.T, ctx context.Context) { | ||
require.Nil(t, testcontainer.TerminateIfNotNil(ctx, c.kafkaContainer), "must not get an error while terminate kafka cluster") | ||
require.Nil(t, testcontainer.TerminateIfNotNil(ctx, c.postgresContainer), "must not get an error while terminate postgres cluster") | ||
require.Nil(t, c.dockerNetwork.Remove(ctx), "must not get an error while remove docker network") | ||
} | ||
|
||
// StartDockerComposeEnvironment - create and start docker containers env with second way. | ||
func (c *ContainersEnvironment) StartDockerComposeEnvironment( | ||
t *testing.T, | ||
composeFilePaths []string, | ||
identifier string, | ||
) { | ||
c.compose = testcontainers.NewLocalDockerCompose(composeFilePaths, identifier). | ||
WaitForService(PostgresContainerName, wait.ForLog("database system is ready to accept connections")). | ||
WaitForService(ZooKeeperContainerName, wait.ForLog("binding to port 0.0.0.0/0.0.0.0:"+ZooKeeperPort)). | ||
WaitForService(KafkaContainerName, wait.ForLog("[KafkaServer id=1] started")) | ||
|
||
if len(composeFilePaths) > 1 { // this is little tricky hack here. :) | ||
// if we have one docker-compose file for app container, that add wait strategy. | ||
c.compose = c.compose.WaitForService(AppName, wait.ForLog("App starting successfully! Ready for hard work!")) | ||
} | ||
|
||
require.Nil(t, c.compose.WithCommand([]string{"up", "--force-recreate", "-d"}).Invoke().Error) | ||
} | ||
|
||
// FinishedDockerComposeEnvironment - finished containers (env) which we created by second way. | ||
func (c *ContainersEnvironment) FinishedDockerComposeEnvironment(t *testing.T) { | ||
require.Nil(t, c.compose.Down().Error, "docker compose must down without errors") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.