github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/integration/integration_karma_test.go (about) 1 //go:build integration 2 // +build integration 3 4 // can be executed with 5 // go test -v -tags integration -run TestCLIIntegration ./integration/... 6 7 package main 8 9 import ( 10 "context" 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 t.Skip("Skip failing test for now") 22 23 t.Parallel() 24 ctx := context.Background() 25 26 pwd, err := os.Getwd() 27 assert.NoError(t, err, "Getting current working directory failed.") 28 pwd = filepath.Dir(pwd) 29 30 // using custom createTmpDir function to avoid issues with symlinks on Docker for Mac 31 tempDir, err := createTmpDir(t) 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 os.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-buster", 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 }