github.com/SAP/jenkins-library@v1.362.0/cmd/mavenExecuteIntegration_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"errors"
     8  	"github.com/SAP/jenkins-library/pkg/mock"
     9  	"github.com/stretchr/testify/assert"
    10  	"net/http"
    11  	"path/filepath"
    12  	"testing"
    13  )
    14  
    15  type mavenExecuteIntegrationTestUtilsBundle struct {
    16  	*mock.ExecMockRunner
    17  	*mock.FilesMock
    18  }
    19  
    20  func (m mavenExecuteIntegrationTestUtilsBundle) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
    21  	return errors.New("Test should not download files.")
    22  }
    23  
    24  func TestIntegrationTestModuleDoesNotExist(t *testing.T) {
    25  	t.Parallel()
    26  	utils := newMavenIntegrationTestsUtilsBundle()
    27  	config := mavenExecuteIntegrationOptions{}
    28  
    29  	err := runMavenExecuteIntegration(&config, utils)
    30  
    31  	assert.EqualError(t, err, "maven module 'integration-tests' does not exist in project structure")
    32  }
    33  
    34  func TestHappyPathIntegrationTests(t *testing.T) {
    35  	t.Parallel()
    36  	utils := newMavenIntegrationTestsUtilsBundle()
    37  	utils.FilesMock.AddFile("integration-tests/pom.xml", []byte(`<project> </project>`))
    38  
    39  	config := mavenExecuteIntegrationOptions{
    40  		Retry:     2,
    41  		ForkCount: "1C",
    42  		Goal:      "post-integration-test",
    43  	}
    44  
    45  	err := runMavenExecuteIntegration(&config, utils)
    46  	if err != nil {
    47  		t.Fatalf("Error %s", err)
    48  	}
    49  
    50  	expectedParameters1 := []string{
    51  		"--file",
    52  		filepath.Join(".", "integration-tests", "pom.xml"),
    53  		"-Dsurefire.rerunFailingTestsCount=2",
    54  		"-Dsurefire.forkCount=1C",
    55  		"-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn",
    56  		"--batch-mode",
    57  		"org.jacoco:jacoco-maven-plugin:prepare-agent",
    58  		"post-integration-test",
    59  	}
    60  
    61  	assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: expectedParameters1}, utils.ExecMockRunner.Calls[0])
    62  }
    63  
    64  func TestInvalidForkCountParam(t *testing.T) {
    65  	t.Parallel()
    66  	// init
    67  	utils := newMavenIntegrationTestsUtilsBundle()
    68  	utils.FilesMock.AddFile("integration-tests/pom.xml", []byte(`<project> </project>`))
    69  
    70  	// test
    71  	err := runMavenExecuteIntegration(&mavenExecuteIntegrationOptions{ForkCount: "4.2"}, utils)
    72  
    73  	// assert
    74  	if assert.Error(t, err) {
    75  		assert.Contains(t, err.Error(), "invalid forkCount parameter")
    76  	}
    77  }
    78  
    79  func TestValidateForkCount(t *testing.T) {
    80  	t.Parallel()
    81  
    82  	testCases := []struct {
    83  		name          string
    84  		testValue     string
    85  		expectedError string
    86  	}{
    87  		{
    88  			name:          "valid integer",
    89  			testValue:     "2",
    90  			expectedError: "",
    91  		},
    92  		{
    93  			name:          "zero is valid",
    94  			testValue:     "0",
    95  			expectedError: "",
    96  		},
    97  		{
    98  			name:          "valid floating point",
    99  			testValue:     "2.5C",
   100  			expectedError: "",
   101  		},
   102  		{
   103  			name:          "valid integer with C",
   104  			testValue:     "2C",
   105  			expectedError: "",
   106  		},
   107  		{
   108  			name:          "invalid floating point",
   109  			testValue:     "1.2",
   110  			expectedError: "invalid forkCount parameter",
   111  		},
   112  		{
   113  			name:          "invalid",
   114  			testValue:     "C1",
   115  			expectedError: "invalid forkCount parameter",
   116  		},
   117  		{
   118  			name:          "another invalid",
   119  			testValue:     "1 C",
   120  			expectedError: "invalid forkCount parameter",
   121  		},
   122  		{
   123  			name:          "invalid float",
   124  			testValue:     "1..2C",
   125  			expectedError: "invalid forkCount parameter",
   126  		},
   127  	}
   128  
   129  	for _, testCase := range testCases {
   130  		testCase := testCase
   131  		t.Run(testCase.name, func(t *testing.T) {
   132  			t.Parallel()
   133  			err := validateForkCount(testCase.testValue)
   134  			if testCase.expectedError == "" {
   135  				assert.NoError(t, err)
   136  			} else if assert.Error(t, err) {
   137  				assert.Contains(t, err.Error(), testCase.expectedError)
   138  			}
   139  		})
   140  	}
   141  }
   142  
   143  func newMavenIntegrationTestsUtilsBundle() *mavenExecuteIntegrationTestUtilsBundle {
   144  	utilsBundle := mavenExecuteIntegrationTestUtilsBundle{
   145  		ExecMockRunner: &mock.ExecMockRunner{},
   146  		FilesMock:      &mock.FilesMock{},
   147  	}
   148  	return &utilsBundle
   149  }