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