github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/integration/integration_npm_test.go (about) 1 //go:build integration 2 // +build integration 3 4 // can be executed with 5 // go test -v -tags integration -run TestNPMIntegration ./integration/... 6 7 package main 8 9 import ( 10 "context" 11 "os" 12 "path/filepath" 13 "testing" 14 15 "github.com/stretchr/testify/assert" 16 "github.com/testcontainers/testcontainers-go" 17 ) 18 19 func TestNPMIntegrationRunScriptsWithOptions(t *testing.T) { 20 t.Parallel() 21 ctx := context.Background() 22 23 pwd, err := os.Getwd() 24 assert.NoError(t, err, "Getting current working directory failed.") 25 pwd = filepath.Dir(pwd) 26 27 // using custom createTmpDir function to avoid issues with symlinks on Docker for Mac 28 tempDir, err := createTmpDir(t) 29 assert.NoError(t, err, "Error when creating temp dir") 30 31 err = copyDir(filepath.Join(pwd, "integration", "testdata", "TestNpmIntegration", "runScriptsWithOptions"), tempDir) 32 if err != nil { 33 t.Fatal("Failed to copy test project.") 34 } 35 36 //workaround to use test script util it is possible to set workdir for Exec call 37 testScript := `#!/bin/sh 38 cd /test 39 /piperbin/piper npmExecuteScripts --runScripts=start --scriptOptions=--tag,tag1 >test-log.txt 2>&1 40 ` 41 os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700) 42 43 reqNode := testcontainers.ContainerRequest{ 44 Image: "node:12-slim", 45 Cmd: []string{"tail", "-f"}, 46 BindMounts: map[string]string{ 47 pwd: "/piperbin", 48 tempDir: "/test", 49 }, 50 } 51 52 nodeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ 53 ContainerRequest: reqNode, 54 Started: true, 55 }) 56 57 code, err := nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"}) 58 assert.NoError(t, err) 59 assert.Equal(t, 0, code) 60 61 content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt")) 62 if err != nil { 63 t.Fatal("Could not read test-log.txt.", err) 64 } 65 output := string(content) 66 assert.Contains(t, output, "info npmExecuteScripts - running command: npm run start -- --tag tag1") 67 assert.Contains(t, output, "info npmExecuteScripts - [ '--tag', 'tag1' ]") 68 } 69 70 func TestNPMIntegrationRegistrySetInFlags(t *testing.T) { 71 t.Parallel() 72 ctx := context.Background() 73 74 pwd, err := os.Getwd() 75 assert.NoError(t, err, "Getting current working directory failed.") 76 pwd = filepath.Dir(pwd) 77 78 // using custom createTmpDir function to avoid issues with symlinks on Docker for Mac 79 tempDir, err := createTmpDir(t) 80 assert.NoError(t, err, "Error when creating temp dir") 81 82 err = copyDir(filepath.Join(pwd, "integration", "testdata", "TestNpmIntegration", "registrySetInFlags"), tempDir) 83 if err != nil { 84 t.Fatal("Failed to copy test project.") 85 } 86 87 //workaround to use test script util it is possible to set workdir for Exec call 88 testScript := `#!/bin/sh 89 cd /test 90 /piperbin/piper npmExecuteScripts --install --runScripts=ci-build,ci-backend-unit-test --defaultNpmRegistry=https://foo.bar >test-log.txt 2>&1 91 ` 92 os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700) 93 94 reqNode := testcontainers.ContainerRequest{ 95 Image: "node:12-slim", 96 Cmd: []string{"tail", "-f"}, 97 BindMounts: map[string]string{ 98 pwd: "/piperbin", 99 tempDir: "/test", 100 }, 101 } 102 103 nodeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ 104 ContainerRequest: reqNode, 105 Started: true, 106 }) 107 108 code, err := nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"}) 109 assert.NoError(t, err) 110 assert.Equal(t, 0, code) 111 112 content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt")) 113 if err != nil { 114 t.Fatal("Could not read test-log.txt.", err) 115 } 116 output := string(content) 117 assert.Contains(t, output, "info npmExecuteScripts - https://foo.bar") 118 } 119 120 func TestNPMIntegrationRegistrySetInNpmrc(t *testing.T) { 121 t.Parallel() 122 ctx := context.Background() 123 124 pwd, err := os.Getwd() 125 assert.NoError(t, err, "Getting current working directory failed.") 126 pwd = filepath.Dir(pwd) 127 128 // using custom createTmpDir function to avoid issues with symlinks on Docker for Mac 129 tempDir, err := createTmpDir(t) 130 assert.NoError(t, err, "Error when creating temp dir") 131 132 err = copyDir(filepath.Join(pwd, "integration", "testdata", "TestNpmIntegration", "registrySetInNpmrc"), tempDir) 133 if err != nil { 134 t.Fatal("Failed to copy test project.") 135 } 136 137 //workaround to use test script util it is possible to set workdir for Exec call 138 testScript := `#!/bin/sh 139 cd /test 140 /piperbin/piper npmExecuteScripts --install --runScripts=ci-build,ci-backend-unit-test >test-log.txt 2>&1 141 ` 142 os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700) 143 144 reqNode := testcontainers.ContainerRequest{ 145 Image: "node:12-slim", 146 Cmd: []string{"tail", "-f"}, 147 BindMounts: map[string]string{ 148 pwd: "/piperbin", 149 tempDir: "/test", 150 }, 151 } 152 153 nodeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ 154 ContainerRequest: reqNode, 155 Started: true, 156 }) 157 158 code, err := nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"}) 159 assert.NoError(t, err) 160 assert.Equal(t, 0, code) 161 162 content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt")) 163 if err != nil { 164 t.Fatal("Could not read test-log.txt.", err) 165 } 166 output := string(content) 167 assert.Contains(t, output, "info npmExecuteScripts - https://example.com") 168 } 169 170 func TestNPMIntegrationRegistryWithTwoModules(t *testing.T) { 171 t.Parallel() 172 ctx := context.Background() 173 174 pwd, err := os.Getwd() 175 assert.NoError(t, err, "Getting current working directory failed.") 176 pwd = filepath.Dir(pwd) 177 178 // using custom createTmpDir function to avoid issues with symlinks on Docker for Mac 179 tempDir, err := createTmpDir(t) 180 assert.NoError(t, err, "Error when creating temp dir") 181 182 err = copyDir(filepath.Join(pwd, "integration", "testdata", "TestNpmIntegration", "registryWithTwoModules"), tempDir) 183 if err != nil { 184 t.Fatal("Failed to copy test project.") 185 } 186 187 //workaround to use test script util it is possible to set workdir for Exec call 188 testScript := `#!/bin/sh 189 cd /test 190 /piperbin/piper npmExecuteScripts --install --runScripts=ci-build,ci-backend-unit-test --defaultNpmRegistry=https://foo.bar >test-log.txt 2>&1 191 ` 192 os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700) 193 194 reqNode := testcontainers.ContainerRequest{ 195 Image: "node:12-slim", 196 Cmd: []string{"tail", "-f"}, 197 BindMounts: map[string]string{ 198 pwd: "/piperbin", 199 tempDir: "/test", 200 }, 201 } 202 203 nodeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ 204 ContainerRequest: reqNode, 205 Started: true, 206 }) 207 208 code, err := nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"}) 209 assert.NoError(t, err) 210 assert.Equal(t, 0, code) 211 212 content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt")) 213 if err != nil { 214 t.Fatal("Could not read test-log.txt.", err) 215 } 216 output := string(content) 217 assert.Contains(t, output, "info npmExecuteScripts - https://example.com") 218 assert.Contains(t, output, "info npmExecuteScripts - https://foo.bar") 219 }