How to use configureTC method of testcontainers Package

Best Testcontainers-go code snippet using testcontainers.configureTC

docker_test.go

Source:docker_test.go Github

copy

Full Screen

...1140}1141func TestReadTCPropsFile(t *testing.T) {1142 t.Run("HOME is not set", func(t *testing.T) {1143 env.Patch(t, "HOME", "")1144 config := configureTC()1145 assert.Empty(t, config, "TC props file should not exist")1146 })1147 t.Run("HOME is not set - TESTCONTAINERS_ env is set", func(t *testing.T) {1148 env.Patch(t, "HOME", "")1149 env.Patch(t, "TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true")1150 config := configureTC()1151 expected := TestContainersConfig{}1152 expected.RyukPrivileged = true1153 assert.Equal(t, expected, config)1154 })1155 t.Run("HOME does not contain TC props file", func(t *testing.T) {1156 tmpDir := fs.NewDir(t, os.TempDir())1157 env.Patch(t, "HOME", tmpDir.Path())1158 config := configureTC()1159 assert.Empty(t, config, "TC props file should not exist")1160 })1161 t.Run("HOME does not contain TC props file - TESTCONTAINERS_ env is set", func(t *testing.T) {1162 tmpDir := fs.NewDir(t, os.TempDir())1163 env.Patch(t, "HOME", tmpDir.Path())1164 env.Patch(t, "TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true")1165 config := configureTC()1166 expected := TestContainersConfig{}1167 expected.RyukPrivileged = true1168 assert.Equal(t, expected, config)1169 })1170 t.Run("HOME contains TC properties file", func(t *testing.T) {1171 tests := []struct {1172 content string1173 env map[string]string1174 expected TestContainersConfig1175 }{1176 {1177 "docker.host = tcp://127.0.0.1:33293",1178 map[string]string{},1179 TestContainersConfig{1180 Host: "tcp://127.0.0.1:33293",1181 TLSVerify: 0,1182 CertPath: "",1183 },1184 },1185 {1186 "docker.host = tcp://127.0.0.1:33293",1187 map[string]string{},1188 TestContainersConfig{1189 Host: "tcp://127.0.0.1:33293",1190 TLSVerify: 0,1191 CertPath: "",1192 },1193 },1194 {1195 `docker.host = tcp://127.0.0.1:332931196 docker.host = tcp://127.0.0.1:47111197 `,1198 map[string]string{},1199 TestContainersConfig{1200 Host: "tcp://127.0.0.1:4711",1201 TLSVerify: 0,1202 CertPath: "",1203 },1204 },1205 {1206 `docker.host = tcp://127.0.0.1:332931207 docker.host = tcp://127.0.0.1:47111208 docker.host = tcp://127.0.0.1:12341209 docker.tls.verify = 11210 `,1211 map[string]string{},1212 TestContainersConfig{1213 Host: "tcp://127.0.0.1:1234",1214 TLSVerify: 1,1215 CertPath: "",1216 },1217 },1218 {1219 "",1220 map[string]string{},1221 TestContainersConfig{1222 Host: "",1223 TLSVerify: 0,1224 CertPath: "",1225 },1226 },1227 {1228 `foo = bar1229 docker.host = tcp://127.0.0.1:12341230 `,1231 map[string]string{},1232 TestContainersConfig{1233 Host: "tcp://127.0.0.1:1234",1234 TLSVerify: 0,1235 CertPath: "",1236 },1237 },1238 {1239 "docker.host=tcp://127.0.0.1:33293",1240 map[string]string{},1241 TestContainersConfig{1242 Host: "tcp://127.0.0.1:33293",1243 TLSVerify: 0,1244 CertPath: "",1245 },1246 },1247 {1248 `#docker.host=tcp://127.0.0.1:33293`,1249 map[string]string{},1250 TestContainersConfig{1251 Host: "",1252 TLSVerify: 0,1253 CertPath: "",1254 },1255 },1256 {1257 `#docker.host = tcp://127.0.0.1:332931258 docker.host = tcp://127.0.0.1:47111259 docker.host = tcp://127.0.0.1:12341260 docker.cert.path=/tmp/certs`,1261 map[string]string{},1262 TestContainersConfig{1263 Host: "tcp://127.0.0.1:1234",1264 TLSVerify: 0,1265 CertPath: "/tmp/certs",1266 },1267 },1268 {1269 `ryuk.container.privileged=true`,1270 map[string]string{},1271 TestContainersConfig{1272 Host: "",1273 TLSVerify: 0,1274 CertPath: "",1275 RyukPrivileged: true,1276 },1277 },1278 {1279 ``,1280 map[string]string{1281 "TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED": "true",1282 },1283 TestContainersConfig{1284 Host: "",1285 TLSVerify: 0,1286 CertPath: "",1287 RyukPrivileged: true,1288 },1289 },1290 {1291 `ryuk.container.privileged=true`,1292 map[string]string{1293 "TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED": "true",1294 },1295 TestContainersConfig{1296 Host: "",1297 TLSVerify: 0,1298 CertPath: "",1299 RyukPrivileged: true,1300 },1301 },1302 {1303 `ryuk.container.privileged=false`,1304 map[string]string{1305 "TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED": "true",1306 },1307 TestContainersConfig{1308 Host: "",1309 TLSVerify: 0,1310 CertPath: "",1311 RyukPrivileged: true,1312 },1313 },1314 {1315 `ryuk.container.privileged=true`,1316 map[string]string{1317 "TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED": "false",1318 },1319 TestContainersConfig{1320 Host: "",1321 TLSVerify: 0,1322 CertPath: "",1323 RyukPrivileged: false,1324 },1325 },1326 {1327 `ryuk.container.privileged=false`,1328 map[string]string{1329 "TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED": "false",1330 },1331 TestContainersConfig{1332 Host: "",1333 TLSVerify: 0,1334 CertPath: "",1335 RyukPrivileged: false,1336 },1337 },1338 {1339 `ryuk.container.privileged=false1340 docker.tls.verify = ERROR`,1341 map[string]string{1342 "TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED": "true",1343 },1344 TestContainersConfig{1345 Host: "",1346 TLSVerify: 0,1347 CertPath: "",1348 RyukPrivileged: true,1349 },1350 },1351 {1352 `ryuk.container.privileged=false`,1353 map[string]string{1354 "TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED": "foo",1355 },1356 TestContainersConfig{1357 Host: "",1358 TLSVerify: 0,1359 CertPath: "",1360 RyukPrivileged: false,1361 },1362 },1363 }1364 for i, tt := range tests {1365 t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) {1366 tmpDir := fs.NewDir(t, os.TempDir())1367 env.Patch(t, "HOME", tmpDir.Path())1368 for k, v := range tt.env {1369 env.Patch(t, k, v)1370 }1371 if err := ioutil.WriteFile(tmpDir.Join(".testcontainers.properties"), []byte(tt.content), 0o600); err != nil {1372 t.Errorf("Failed to create the file: %v", err)1373 return1374 }1375 config := configureTC()1376 assert.Equal(t, tt.expected, config, "Configuration doesn't not match")1377 })1378 }1379 })1380}1381func ExampleDockerProvider_CreateContainer() {1382 ctx := context.Background()1383 req := ContainerRequest{1384 Image: "docker.io/nginx:alpine",1385 ExposedPorts: []string{"80/tcp"},1386 WaitingFor: wait.ForHTTP("/"),1387 }1388 nginxC, _ := GenericContainer(ctx, GenericContainerRequest{1389 ContainerRequest: req,...

Full Screen

Full Screen

docker.go

Source:docker.go Github

copy

Full Screen

...614 opts.defaultBridgeNetworkName = bridgeNetworkName615 })616}617func NewDockerClient() (cli *client.Client, host string, tcConfig TestContainersConfig, err error) {618 tcConfig = configureTC()619 host = tcConfig.Host620 opts := []client.Opt{client.FromEnv}621 if host != "" {622 opts = append(opts, client.WithHost(host))623 // for further informacion, read https://docs.docker.com/engine/security/protect-access/624 if tcConfig.TLSVerify == 1 {625 cacertPath := filepath.Join(tcConfig.CertPath, "ca.pem")626 certPath := filepath.Join(tcConfig.CertPath, "cert.pem")627 keyPath := filepath.Join(tcConfig.CertPath, "key.pem")628 opts = append(opts, client.WithTLSClientConfig(cacertPath, certPath, keyPath))629 }630 } else if dockerHostEnv := os.Getenv("DOCKER_HOST"); dockerHostEnv != "" {631 host = dockerHostEnv632 } else {633 host = "unix:///var/run/docker.sock"634 }635 opts = append(opts, client.WithHTTPHeaders(636 map[string]string{637 "x-tc-sid": sessionID().String(),638 }),639 )640 cli, err = client.NewClientWithOpts(opts...)641 if err != nil {642 return nil, "", TestContainersConfig{}, err643 }644 cli.NegotiateAPIVersion(context.Background())645 return cli, host, tcConfig, nil646}647// NewDockerProvider creates a Docker provider with the EnvClient648func NewDockerProvider(provOpts ...DockerProviderOption) (*DockerProvider, error) {649 o := &DockerProviderOptions{650 GenericProviderOptions: &GenericProviderOptions{651 Logger: Logger,652 },653 }654 for idx := range provOpts {655 provOpts[idx].ApplyDockerTo(o)656 }657 c, host, tcConfig, err := NewDockerClient()658 if err != nil {659 return nil, err660 }661 _, err = c.Ping(context.TODO())662 if err != nil {663 // fallback to environment664 c, err = client.NewClientWithOpts(client.FromEnv)665 if err != nil {666 return nil, err667 }668 }669 c.NegotiateAPIVersion(context.Background())670 p := &DockerProvider{671 DockerProviderOptions: o,672 host: host,673 client: c,674 config: tcConfig,675 }676 // log docker server info only once677 logOnce.Do(func() {678 logDockerServerInfo(context.Background(), p.client, p.Logger)679 })680 return p, nil681}682func logDockerServerInfo(ctx context.Context, client client.APIClient, logger Logging) {683 infoMessage := `%v - Connected to docker: 684 Server Version: %v685 API Version: %v686 Operating System: %v687 Total Memory: %v MB688`689 info, err := client.Info(ctx)690 if err != nil {691 logger.Printf("failed getting information about docker server: %s", err)692 return693 }694 logger.Printf(infoMessage, packagePath,695 info.ServerVersion, client.ClientVersion(),696 info.OperatingSystem, info.MemTotal/1024/1024)697}698// configureTC reads from testcontainers properties file, if it exists699// it is possible that certain values get overridden when set as environment variables700func configureTC() TestContainersConfig {701 config := TestContainersConfig{}702 applyEnvironmentConfiguration := func(config TestContainersConfig) TestContainersConfig {703 ryukPrivilegedEnv := os.Getenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED")704 if ryukPrivilegedEnv != "" {705 config.RyukPrivileged = ryukPrivilegedEnv == "true"706 }707 return config708 }709 home, err := os.UserHomeDir()710 if err != nil {711 return applyEnvironmentConfiguration(config)712 }713 tcProp := filepath.Join(home, ".testcontainers.properties")714 // init from a file...

Full Screen

Full Screen

configureTC

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"80/tcp"},6 WaitingFor: wait.ForListeningPort("80/tcp"),7 }8 provider, err := testcontainers.NewDockerProvider()9 if err != nil {10 log.Fatal(err)11 }12 container, err := provider.CreateContainer(ctx, req)13 if err != nil {14 log.Fatal(err)15 }16 if err := container.Start(ctx); err != nil {17 log.Fatal(err)18 }19 defer container.Terminate(ctx)20 ip, err := container.Host(ctx)21 if err != nil {22 log.Fatal(err)23 }24 port, err := container.MappedPort(ctx, "80")25 if err != nil {26 log.Fatal(err)27 }28 log.Printf("Container ip: %s, port: %s", ip, port.Port())29 time.Sleep(10 * time.Second)30}

Full Screen

Full Screen

configureTC

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"80/tcp"},6 WaitingFor: wait.ForListeningPort("80/tcp"),7 }8 alpine, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 panic(err)12 }13 defer alpine.Terminate(ctx)14 port, err := alpine.MappedPort(ctx, "80")15 if err != nil {16 panic(err)17 }18 fmt.Println(port.Int())19}

Full Screen

Full Screen

configureTC

Using AI Code Generation

copy

Full Screen

1func main() {2 ctx := context.Background()3 req := testcontainers.ContainerRequest{4 Cmd: []string{"sh", "-c", "while true; do echo hello world; sleep 1; done"},5 NetworkPorts: map[string]string{"8080/tcp": "8080"},6 }7 alpine, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{8 })9 if err != nil {10 log.Fatal(err)11 }12 defer alpine.Terminate(ctx)13 ip, err := alpine.Host(ctx)14 if err != nil {15 log.Fatal(err)16 }17 port, err := alpine.MappedPort(ctx, "8080")18 if err != nil {19 log.Fatal(err)20 }21 fmt.Println("alpine is listening on ", net.JoinHostPort(ip, port.Port()))22}23func main() {24 ctx := context.Background()25 req := testcontainers.ContainerRequest{26 Cmd: []string{"sh", "-c", "while true; do echo hello world; sleep 1; done"},27 NetworkPorts: map[string]string{"8080/tcp": "8080"},28 }29 alpine, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{30 })31 if err != nil {32 log.Fatal(err)33 }34 defer alpine.Terminate(ctx)35 ip, err := alpine.Host(ctx)36 if err != nil {37 log.Fatal(err)38 }39 port, err := alpine.MappedPort(ctx, "8080")40 if err != nil {41 log.Fatal(err)42 }43 fmt.Println("alpine is listening on ", net.JoinHostPort(ip, port.Port()))44}45func main() {46 ctx := context.Background()47 req := testcontainers.ContainerRequest{48 Cmd: []string{"sh", "-

Full Screen

Full Screen

configureTC

Using AI Code Generation

copy

Full Screen

1import (2func TestTestContainer(t *testing.T) {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"5432/tcp"},6 WaitingFor: wait.ForListeningPort("5432/tcp"),7 }8 c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 fmt.Println("Error occured while creating container: ", err)12 }13 defer c.Terminate(ctx)14 ip, err := c.Host(ctx)15 if err != nil {16 fmt.Println("Error occured while getting container ip: ", err)17 }18 port, err := c.MappedPort(ctx, "5432/tcp")19 if err != nil {20 fmt.Println("Error occured while getting container port: ", err)21 }22 fmt.Println("Host IP: ", ip)23 fmt.Println("Host Port: ", port.Int())24}

Full Screen

Full Screen

configureTC

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"80/tcp"},6 WaitingFor: wait.ForListeningPort("80/tcp"),7 }8 alpine, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 panic(err)12 }13 defer alpine.Terminate(ctx)14 port, err := alpine.MappedPort(ctx, "80")15 if err != nil {16 panic(err)17 }18 ip, err := alpine.Host(ctx)19 if err != nil {20 panic(err)21 }22 fmt.Printf("Container IP: %s23 fmt.Printf("Container Port: %s24", port.Port())25 time.Sleep(10 * time.Second)26}

Full Screen

Full Screen

configureTC

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"4444/tcp"},6 PortBindings: map[string]string{"4444/tcp": "4444"},7 WaitingFor: wait.ForListeningPort("4444/tcp"),8 }9 chromeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{10 })11 if err != nil {12 panic(err)13 }14 ip, err := chromeContainer.Host(ctx)15 if err != nil {16 panic(err)17 }18 port, err := chromeContainer.MappedPort(ctx, "4444")19 if err != nil {20 panic(err)21 }22 fmt.Println(ip, port.Int())23 chromeContainer.Terminate(ctx)24}25import (26func main() {27 ctx := context.Background()28 req := testcontainers.ContainerRequest{29 ExposedPorts: []string{"4444/tcp"},30 PortBindings: map[string]string{"4444/tcp": "4444"},31 WaitingFor: wait.ForListeningPort("4444/tcp"),32 }33 chromeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{

Full Screen

Full Screen

configureTC

Using AI Code Generation

copy

Full Screen

1import (2func configureTC(container testcontainers.Container) testcontainers.Container {3 container = container.WithEnv("TEST_ENV", "TEST_ENV_VALUE")4 container = container.WithEnv("TEST_ENV_2", "TEST_ENV_2_VALUE")5}6func main() {7 ctx := context.Background()8 req := testcontainers.ContainerRequest{9 ExposedPorts: []string{"80/tcp"},10 WaitingFor: wait.ForLog("Hello from Docker!"),11 }12 req = configureTC(req)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-go automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful