github.com/jaylevin/jenkins-library@v1.230.4/integration/integration_cli_test.go (about) 1 //go:build integration 2 // +build integration 3 4 // can be execute with go test -tags=integration ./integration/... 5 6 package main 7 8 import ( 9 "context" 10 "io/ioutil" 11 "os" 12 "path/filepath" 13 "testing" 14 15 "github.com/google/uuid" 16 "github.com/stretchr/testify/assert" 17 "github.com/testcontainers/testcontainers-go" 18 ) 19 20 func TestKarmaIntegration(t *testing.T) { 21 22 t.Parallel() 23 ctx := context.Background() 24 25 pwd, err := os.Getwd() 26 assert.NoError(t, err, "Getting current working directory failed.") 27 pwd = filepath.Dir(pwd) 28 29 // using custom createTmpDir function to avoid issues with symlinks on Docker for Mac 30 tempDir, err := createTmpDir("") 31 defer os.RemoveAll(tempDir) // clean up 32 assert.NoError(t, err, "Error when creating temp dir") 33 34 err = copyDir(filepath.Join(pwd, "integration", "testdata", t.Name()), tempDir) 35 if err != nil { 36 t.Fatal("Failed to copy test project.") 37 } 38 39 //workaround to use test script util it is possible to set workdir for Exec call 40 testScript := `#!/bin/sh 41 cd /test 42 /piperbin/piper karmaExecuteTests 43 ` 44 ioutil.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700) 45 46 networkName := "sidecar-" + uuid.New().String() 47 48 reqNode := testcontainers.ContainerRequest{ 49 Image: "node:lts-stretch", 50 Cmd: []string{"tail", "-f"}, 51 BindMounts: map[string]string{ 52 pwd: "/piperbin", 53 tempDir: "/test", 54 }, 55 Networks: []string{networkName}, 56 NetworkAliases: map[string][]string{networkName: {"karma"}}, 57 } 58 59 reqSel := testcontainers.ContainerRequest{ 60 Image: "selenium/standalone-chrome:3.141.59-20210713", 61 Networks: []string{networkName}, 62 NetworkAliases: map[string][]string{networkName: {"selenium"}}, 63 } 64 65 provider, err := testcontainers.ProviderDocker.GetProvider() 66 assert.NoError(t, err) 67 68 network, err := provider.CreateNetwork(ctx, testcontainers.NetworkRequest{Name: networkName, CheckDuplicate: true}) 69 if err != nil { 70 t.Fatal(err) 71 } 72 defer network.Remove(ctx) 73 74 nodeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ 75 ContainerRequest: reqNode, 76 Started: true, 77 }) 78 if err != nil { 79 t.Fatal(err) 80 } 81 defer nodeContainer.Terminate(ctx) 82 83 selContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ 84 ContainerRequest: reqSel, 85 Started: true, 86 }) 87 if err != nil { 88 t.Fatal(err) 89 } 90 defer selContainer.Terminate(ctx) 91 92 // cannot use piper command directly since it is not possible to set Workdir for Exec call 93 // workaround use shell call in container (see above) 94 //piperOptions := []string{ 95 // "karmaExecuteTests", 96 // "--help", 97 //} 98 //code, err := nodeContainer.Exec(ctx, append([]string{"/data/piper"}, piperOptions...)) 99 100 code, err := nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"}) 101 assert.NoError(t, err) 102 assert.Equal(t, 0, code) 103 }