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