github.com/jaylevin/jenkins-library@v1.230.4/pkg/maven/settings_test.go (about) 1 package maven 2 3 import ( 4 "encoding/xml" 5 "fmt" 6 "net/http" 7 "os" 8 "strings" 9 "testing" 10 11 piperhttp "github.com/SAP/jenkins-library/pkg/http" 12 "github.com/SAP/jenkins-library/pkg/mock" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestSettings(t *testing.T) { 17 18 defer func() { 19 getenv = os.Getenv 20 }() 21 22 getenv = func(name string) string { 23 if name == "M2_HOME" { 24 return "/usr/share/maven" 25 } else if name == "HOME" { 26 return "/home/me" 27 } 28 return "" 29 } 30 31 t.Run("Settings file source location not provided", func(t *testing.T) { 32 33 utilsMock := newSettingsDownloadTestUtilsBundle() 34 35 err := downloadAndCopySettingsFile("", "foo", utilsMock) 36 37 assert.EqualError(t, err, "Settings file source location not provided") 38 }) 39 40 t.Run("Settings file destination location not provided", func(t *testing.T) { 41 42 utilsMock := newSettingsDownloadTestUtilsBundle() 43 44 err := downloadAndCopySettingsFile("/opt/sap/maven/global-settings.xml", "", utilsMock) 45 46 assert.EqualError(t, err, "Settings file destination location not provided") 47 }) 48 49 t.Run("Retrieve settings files", func(t *testing.T) { 50 51 utilsMock := newSettingsDownloadTestUtilsBundle() 52 53 utilsMock.AddFile("/opt/sap/maven/global-settings.xml", []byte("")) 54 utilsMock.AddFile("/opt/sap/maven/project-settings.xml", []byte("")) 55 56 err := DownloadAndCopySettingsFiles("/opt/sap/maven/global-settings.xml", "/opt/sap/maven/project-settings.xml", utilsMock) 57 58 if assert.NoError(t, err) { 59 assert.True(t, utilsMock.HasCopiedFile("/opt/sap/maven/global-settings.xml", "/usr/share/maven/conf/settings.xml")) 60 assert.True(t, utilsMock.HasCopiedFile("/opt/sap/maven/project-settings.xml", "/home/me/.m2/settings.xml")) 61 } 62 63 assert.Empty(t, utilsMock.downloadedFiles) 64 }) 65 66 t.Run("Retrieve settings file via http", func(t *testing.T) { 67 68 utilsMock := newSettingsDownloadTestUtilsBundle() 69 70 err := downloadAndCopySettingsFile("https://example.org/maven/global-settings.xml", "/usr/share/maven/conf/settings.xml", utilsMock) 71 72 if assert.NoError(t, err) { 73 assert.Equal(t, "/usr/share/maven/conf/settings.xml", utilsMock.downloadedFiles["https://example.org/maven/global-settings.xml"]) 74 } 75 }) 76 77 t.Run("Retrieve settings file via http - received error from downloader", func(t *testing.T) { 78 79 utilsMock := newSettingsDownloadTestUtilsBundle() 80 utilsMock.expectedError = fmt.Errorf("Download failed") 81 82 err := downloadAndCopySettingsFile("https://example.org/maven/global-settings.xml", "/usr/share/maven/conf/settings.xml", utilsMock) 83 84 if assert.Error(t, err) { 85 assert.Contains(t, err.Error(), "failed to download maven settings from URL") 86 } 87 }) 88 89 t.Run("Retrieve project settings file - file not found", func(t *testing.T) { 90 91 utilsMock := newSettingsDownloadTestUtilsBundle() 92 93 err := downloadAndCopySettingsFile("/opt/sap/maven/project-settings.xml", "/home/me/.m2/settings.xml", utilsMock) 94 95 if assert.Error(t, err) { 96 assert.Contains(t, err.Error(), "cannot copy '/opt/sap/maven/project-settings.xml': file does not exist") 97 } 98 }) 99 100 t.Run("create new Project settings file", func(t *testing.T) { 101 102 utilsMock := newSettingsDownloadTestUtilsBundle() 103 104 projectSettingsFilePath, err := CreateNewProjectSettingsXML("dummyRepoId", "dummyRepoUser", "dummyRepoPassword", utilsMock) 105 if assert.NoError(t, err) { 106 projectSettingsContent, _ := utilsMock.FileRead(projectSettingsFilePath) 107 var projectSettings Settings 108 109 err = xml.Unmarshal(projectSettingsContent, &projectSettings) 110 111 if assert.NoError(t, err) { 112 assert.Equal(t, projectSettings.Servers.ServerType[0].ID, "dummyRepoId") 113 assert.Equal(t, projectSettings.Servers.ServerType[0].Username, "dummyRepoUser") 114 assert.Equal(t, projectSettings.Servers.ServerType[0].ID, "dummyRepoId") 115 } 116 117 } 118 }) 119 120 t.Run("update server tag in existing settings file", func(t *testing.T) { 121 122 utilsMock := newSettingsDownloadTestUtilsBundle() 123 var projectSettings Settings 124 projectSettings.Xsi = "http://www.w3.org/2001/XMLSchema-instance" 125 projectSettings.SchemaLocation = "http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd" 126 projectSettings.Servers.ServerType = []Server{ 127 { 128 ID: "dummyRepoId1", 129 Username: "dummyRepoUser1", 130 Password: "dummyRepoId1", 131 }, 132 } 133 settingsXml, err := xml.MarshalIndent(projectSettings, "", " ") 134 settingsXmlString := string(settingsXml) 135 Replacer := strings.NewReplacer("
", "", "	", "") 136 settingsXmlString = Replacer.Replace(settingsXmlString) 137 138 xmlstring := []byte(xml.Header + settingsXmlString) 139 140 utilsMock.FileWrite(".pipeline/mavenProjectSettings", xmlstring, 0777) 141 142 projectSettingsFilePath, err := UpdateProjectSettingsXML(".pipeline/mavenProjectSettings", "dummyRepoId2", "dummyRepoUser2", "dummyRepoPassword2", utilsMock) 143 if assert.NoError(t, err) { 144 projectSettingsContent, _ := utilsMock.FileRead(projectSettingsFilePath) 145 var projectSettings Settings 146 147 err = xml.Unmarshal(projectSettingsContent, &projectSettings) 148 149 if assert.NoError(t, err) { 150 assert.Equal(t, projectSettings.Servers.ServerType[1].ID, "dummyRepoId2") 151 assert.Equal(t, projectSettings.Servers.ServerType[1].Username, "dummyRepoUser2") 152 assert.Equal(t, projectSettings.Servers.ServerType[1].ID, "dummyRepoId2") 153 } 154 155 } 156 }) 157 158 t.Run("update active profile tag in existing settings file", func(t *testing.T) { 159 160 utilsMock := newSettingsDownloadTestUtilsBundle() 161 var projectSettings Settings 162 projectSettings.Xsi = "http://www.w3.org/2001/XMLSchema-instance" 163 projectSettings.SchemaLocation = "http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd" 164 projectSettings.ActiveProfiles.ActiveProfile = []string{"dummyProfile"} 165 settingsXml, err := xml.MarshalIndent(projectSettings, "", " ") 166 settingsXmlString := string(settingsXml) 167 Replacer := strings.NewReplacer("
", "", "	", "") 168 settingsXmlString = Replacer.Replace(settingsXmlString) 169 170 xmlstring := []byte(xml.Header + settingsXmlString) 171 172 destination, _ := getGlobalSettingsFileDest() 173 174 utilsMock.FileWrite("/usr/share/maven/conf/settings.xml", xmlstring, 0777) 175 176 err = UpdateActiveProfileInSettingsXML([]string{"newProfile"}, utilsMock) 177 178 if assert.NoError(t, err) { 179 projectSettingsContent, _ := utilsMock.FileRead(destination) 180 var projectSettings Settings 181 182 err = xml.Unmarshal(projectSettingsContent, &projectSettings) 183 184 if assert.NoError(t, err) { 185 assert.Equal(t, projectSettings.ActiveProfiles.ActiveProfile[0], "newProfile") 186 } 187 188 } 189 }) 190 } 191 192 func newSettingsDownloadTestUtilsBundle() *settingsDownloadTestUtils { 193 utilsBundle := settingsDownloadTestUtils{ 194 FilesMock: &mock.FilesMock{}, 195 } 196 return &utilsBundle 197 } 198 199 type settingsDownloadTestUtils struct { 200 *mock.FilesMock 201 expectedError error 202 downloadedFiles map[string]string // src, dest 203 } 204 205 func (c *settingsDownloadTestUtils) SetOptions(options piperhttp.ClientOptions) { 206 } 207 208 func (c *settingsDownloadTestUtils) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error { 209 210 if c.expectedError != nil { 211 return c.expectedError 212 } 213 214 if c.downloadedFiles == nil { 215 c.downloadedFiles = make(map[string]string) 216 } 217 c.downloadedFiles[url] = filename 218 return nil 219 }