github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/integration/integration_gauge_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  // can be executed with
     5  // go test -v -tags integration -run TestGaugeIntegration ./integration/...
     6  
     7  package main
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"os"
    13  	"path/filepath"
    14  	"testing"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/testcontainers/testcontainers-go"
    18  )
    19  
    20  const (
    21  	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
    22  )
    23  
    24  func runTest(t *testing.T, languageRunner string) {
    25  	ctx := context.Background()
    26  
    27  	pwd, err := os.Getwd()
    28  	assert.NoError(t, err, "Getting current working directory failed.")
    29  	pwd = filepath.Dir(pwd)
    30  
    31  	// using custom createTmpDir function to avoid issues with symlinks on Docker for Mac
    32  	tempDir, err := createTmpDir(t)
    33  	assert.NoError(t, err, "Error when creating temp dir")
    34  
    35  	err = copyDir(filepath.Join(pwd, "integration", "testdata", "TestGaugeIntegration", "gauge-"+languageRunner), tempDir)
    36  	if err != nil {
    37  		t.Fatal("Failed to copy test project.")
    38  	}
    39  
    40  	//workaround to use test script until it is possible to set workdir for Exec call
    41  	testScript := fmt.Sprintf(`#!/bin/sh
    42  cd /test
    43  /piperbin/piper gaugeExecuteTests --installCommand="%v" --languageRunner=%v --runCommand="run" >test-log.txt 2>&1
    44  `, installCommand, languageRunner)
    45  
    46  	os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
    47  
    48  	reqNode := testcontainers.ContainerRequest{
    49  		Image: "getgauge/gocd-jdk-mvn-node",
    50  		Cmd:   []string{"tail", "-f"},
    51  		BindMounts: map[string]string{
    52  			pwd:     "/piperbin",
    53  			tempDir: "/test",
    54  		},
    55  	}
    56  
    57  	if languageRunner == "js" {
    58  		reqNode.Image = "node:lts-buster"
    59  	}
    60  
    61  	nodeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
    62  		ContainerRequest: reqNode,
    63  		Started:          true,
    64  	})
    65  
    66  	code, err := nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
    67  	assert.NoError(t, err)
    68  	assert.Equal(t, 0, code)
    69  
    70  	t.Cleanup(func() {
    71  		// Remove files that are created by the container. t.TempDir() will
    72  		// fail to remove them since it does not have the root permission
    73  		_, err := nodeContainer.Exec(ctx, []string{"sh", "-c", "find /test -name . -o -prune -exec rm -rf -- {} +"})
    74  		assert.NoError(t, err)
    75  
    76  		assert.NoError(t, nodeContainer.Terminate(ctx))
    77  	})
    78  
    79  	content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt"))
    80  	if err != nil {
    81  		t.Fatal("Could not read test-log.txt.", err)
    82  	}
    83  	output := string(content)
    84  	assert.Contains(t, output, "info  gaugeExecuteTests - Scenarios:	2 executed	2 passed	0 failed	0 skipped")
    85  	assert.Contains(t, output, "info  gaugeExecuteTests - SUCCESS")
    86  }
    87  
    88  func TestGaugeIntegrationJava(t *testing.T) {
    89  	t.Parallel()
    90  	runTest(t, "java")
    91  }
    92  
    93  func TestGaugeIntegrationJS(t *testing.T) {
    94  	t.Parallel()
    95  	runTest(t, "js")
    96  }