2022-02-23 10:30:19 +02:00
//go:build integration
2021-06-01 13:15:10 +02:00
// +build integration
2022-02-23 10:30:19 +02:00
2022-09-19 12:47:13 +02:00
// can be executed with
// go test -v -tags integration -run TestGaugeIntegration ./integration/...
2021-06-01 13:15:10 +02:00
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
)
const (
installCommand string = "npm install -g @getgauge/cli --prefix=~/.npm-global --unsafe-perm" //option --unsafe-perm need to install gauge in docker container. See this issue: https://github.com/getgauge/gauge/issues/1470
)
func runTest ( t * testing . T , languageRunner string ) {
ctx := context . Background ( )
pwd , err := os . Getwd ( )
assert . NoError ( t , err , "Getting current working directory failed." )
pwd = filepath . Dir ( pwd )
// using custom createTmpDir function to avoid issues with symlinks on Docker for Mac
2022-07-12 15:19:12 +02:00
tempDir , err := createTmpDir ( t )
2021-06-01 13:15:10 +02:00
assert . NoError ( t , err , "Error when creating temp dir" )
err = copyDir ( filepath . Join ( pwd , "integration" , "testdata" , "TestGaugeIntegration" , "gauge-" + languageRunner ) , tempDir )
if err != nil {
t . Fatal ( "Failed to copy test project." )
}
2022-07-12 15:19:12 +02:00
//workaround to use test script until it is possible to set workdir for Exec call
2021-06-01 13:15:10 +02:00
testScript := fmt . Sprintf ( ` # ! / bin / sh
cd / test
/ piperbin / piper gaugeExecuteTests -- installCommand = "%v" -- languageRunner = % v -- runCommand = "run" > test - log . txt 2 > & 1
` , installCommand , languageRunner )
2023-07-11 15:47:57 +02:00
2023-08-16 12:57:04 +02:00
os . WriteFile ( filepath . Join ( tempDir , "runPiper.sh" ) , [ ] byte ( testScript ) , 0700 )
2021-06-01 13:15:10 +02:00
reqNode := testcontainers . ContainerRequest {
Image : "getgauge/gocd-jdk-mvn-node" ,
Cmd : [ ] string { "tail" , "-f" } ,
BindMounts : map [ string ] string {
pwd : "/piperbin" ,
tempDir : "/test" ,
} ,
}
2023-07-11 15:47:57 +02:00
if languageRunner == "js" {
reqNode . Image = "node:lts-buster"
}
2021-06-01 13:15:10 +02:00
nodeContainer , err := testcontainers . GenericContainer ( ctx , testcontainers . GenericContainerRequest {
ContainerRequest : reqNode ,
Started : true ,
} )
code , err := nodeContainer . Exec ( ctx , [ ] string { "sh" , "/test/runPiper.sh" } )
assert . NoError ( t , err )
assert . Equal ( t , 0 , code )
2022-07-12 15:19:12 +02:00
t . Cleanup ( func ( ) {
// Remove files that are created by the container. t.TempDir() will
// fail to remove them since it does not have the root permission
_ , err := nodeContainer . Exec ( ctx , [ ] string { "sh" , "-c" , "find /test -name . -o -prune -exec rm -rf -- {} +" } )
assert . NoError ( t , err )
assert . NoError ( t , nodeContainer . Terminate ( ctx ) )
} )
2023-08-16 12:57:04 +02:00
content , err := os . ReadFile ( filepath . Join ( tempDir , "/test-log.txt" ) )
2021-06-01 13:15:10 +02:00
if err != nil {
t . Fatal ( "Could not read test-log.txt." , err )
}
output := string ( content )
assert . Contains ( t , output , "info gaugeExecuteTests - Scenarios: 2 executed 2 passed 0 failed 0 skipped" )
assert . Contains ( t , output , "info gaugeExecuteTests - SUCCESS" )
}
2022-09-19 12:47:13 +02:00
func TestGaugeIntegrationJava ( t * testing . T ) {
2021-06-01 13:15:10 +02:00
t . Parallel ( )
runTest ( t , "java" )
}
2022-09-19 12:47:13 +02:00
func TestGaugeIntegrationJS ( t * testing . T ) {
2021-06-01 13:15:10 +02:00
t . Parallel ( )
runTest ( t , "js" )
}