2020-02-25 15:17:00 +02:00
|
|
|
package maven
|
|
|
|
|
|
|
|
import (
|
2021-06-01 09:24:36 +02:00
|
|
|
"encoding/xml"
|
2020-02-25 15:17:00 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-06-01 09:24:36 +02:00
|
|
|
"strings"
|
2020-02-25 15:17:00 +02:00
|
|
|
"testing"
|
2021-06-01 09:24:36 +02:00
|
|
|
|
|
|
|
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-02-25 15:17:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSettings(t *testing.T) {
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
getenv = os.Getenv
|
|
|
|
}()
|
|
|
|
|
|
|
|
getenv = func(name string) string {
|
|
|
|
if name == "M2_HOME" {
|
|
|
|
return "/usr/share/maven"
|
|
|
|
} else if name == "HOME" {
|
|
|
|
return "/home/me"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-07-30 10:35:46 +02:00
|
|
|
t.Run("Settings file source location not provided", func(t *testing.T) {
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
utilsMock := newSettingsDownloadTestUtilsBundle()
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
err := downloadAndCopySettingsFile("", "foo", utilsMock)
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-07-30 10:35:46 +02:00
|
|
|
assert.EqualError(t, err, "Settings file source location not provided")
|
2020-02-25 15:17:00 +02:00
|
|
|
})
|
|
|
|
|
2020-07-30 10:35:46 +02:00
|
|
|
t.Run("Settings file destination location not provided", func(t *testing.T) {
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
utilsMock := newSettingsDownloadTestUtilsBundle()
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
err := downloadAndCopySettingsFile("/opt/sap/maven/global-settings.xml", "", utilsMock)
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-07-30 10:35:46 +02:00
|
|
|
assert.EqualError(t, err, "Settings file destination location not provided")
|
2020-02-25 15:17:00 +02:00
|
|
|
})
|
|
|
|
|
2020-07-30 10:35:46 +02:00
|
|
|
t.Run("Retrieve settings files", func(t *testing.T) {
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
utilsMock := newSettingsDownloadTestUtilsBundle()
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
utilsMock.AddFile("/opt/sap/maven/global-settings.xml", []byte(""))
|
|
|
|
utilsMock.AddFile("/opt/sap/maven/project-settings.xml", []byte(""))
|
|
|
|
|
|
|
|
err := DownloadAndCopySettingsFiles("/opt/sap/maven/global-settings.xml", "/opt/sap/maven/project-settings.xml", utilsMock)
|
2020-02-25 15:17:00 +02:00
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
2020-11-10 18:14:55 +02:00
|
|
|
assert.True(t, utilsMock.HasCopiedFile("/opt/sap/maven/global-settings.xml", "/usr/share/maven/conf/settings.xml"))
|
|
|
|
assert.True(t, utilsMock.HasCopiedFile("/opt/sap/maven/project-settings.xml", "/home/me/.m2/settings.xml"))
|
2020-02-25 15:17:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
assert.Empty(t, utilsMock.downloadedFiles)
|
2020-02-25 15:17:00 +02:00
|
|
|
})
|
|
|
|
|
2020-07-30 10:35:46 +02:00
|
|
|
t.Run("Retrieve settings file via http", func(t *testing.T) {
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
utilsMock := newSettingsDownloadTestUtilsBundle()
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
err := downloadAndCopySettingsFile("https://example.org/maven/global-settings.xml", "/usr/share/maven/conf/settings.xml", utilsMock)
|
2020-02-25 15:17:00 +02:00
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
2020-11-10 18:14:55 +02:00
|
|
|
assert.Equal(t, "/usr/share/maven/conf/settings.xml", utilsMock.downloadedFiles["https://example.org/maven/global-settings.xml"])
|
2020-02-25 15:17:00 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Retrieve settings file via http - received error from downloader", func(t *testing.T) {
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
utilsMock := newSettingsDownloadTestUtilsBundle()
|
|
|
|
utilsMock.expectedError = fmt.Errorf("Download failed")
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
err := downloadAndCopySettingsFile("https://example.org/maven/global-settings.xml", "/usr/share/maven/conf/settings.xml", utilsMock)
|
2020-02-25 15:17:00 +02:00
|
|
|
|
|
|
|
if assert.Error(t, err) {
|
2020-07-30 10:35:46 +02:00
|
|
|
assert.Contains(t, err.Error(), "failed to download maven settings from URL")
|
2020-02-25 15:17:00 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Retrieve project settings file - file not found", func(t *testing.T) {
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
utilsMock := newSettingsDownloadTestUtilsBundle()
|
2020-02-25 15:17:00 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
err := downloadAndCopySettingsFile("/opt/sap/maven/project-settings.xml", "/home/me/.m2/settings.xml", utilsMock)
|
2020-02-25 15:17:00 +02:00
|
|
|
|
|
|
|
if assert.Error(t, err) {
|
2020-11-10 18:14:55 +02:00
|
|
|
assert.Contains(t, err.Error(), "cannot copy '/opt/sap/maven/project-settings.xml': file does not exist")
|
2020-02-25 15:17:00 +02:00
|
|
|
}
|
|
|
|
})
|
2021-06-01 09:24:36 +02:00
|
|
|
|
|
|
|
t.Run("create new Project settings file", func(t *testing.T) {
|
|
|
|
|
|
|
|
utilsMock := newSettingsDownloadTestUtilsBundle()
|
|
|
|
|
|
|
|
projectSettingsFilePath, err := CreateNewProjectSettingsXML("dummyRepoId", "dummyRepoUser", "dummyRepoPassword", utilsMock)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
projectSettingsContent, _ := utilsMock.FileRead(projectSettingsFilePath)
|
|
|
|
var projectSettings Settings
|
|
|
|
|
|
|
|
err = xml.Unmarshal(projectSettingsContent, &projectSettings)
|
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, projectSettings.Servers.ServerType[0].ID, "dummyRepoId")
|
|
|
|
assert.Equal(t, projectSettings.Servers.ServerType[0].Username, "dummyRepoUser")
|
|
|
|
assert.Equal(t, projectSettings.Servers.ServerType[0].ID, "dummyRepoId")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update server tag in existing settings file", func(t *testing.T) {
|
|
|
|
|
|
|
|
utilsMock := newSettingsDownloadTestUtilsBundle()
|
|
|
|
var projectSettings Settings
|
|
|
|
projectSettings.Xsi = "http://www.w3.org/2001/XMLSchema-instance"
|
|
|
|
projectSettings.SchemaLocation = "http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"
|
|
|
|
projectSettings.Servers.ServerType = []Server{
|
|
|
|
{
|
|
|
|
ID: "dummyRepoId1",
|
|
|
|
Username: "dummyRepoUser1",
|
|
|
|
Password: "dummyRepoId1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
settingsXml, err := xml.MarshalIndent(projectSettings, "", " ")
|
|
|
|
settingsXmlString := string(settingsXml)
|
|
|
|
Replacer := strings.NewReplacer("
", "", "	", "")
|
|
|
|
settingsXmlString = Replacer.Replace(settingsXmlString)
|
|
|
|
|
|
|
|
xmlstring := []byte(xml.Header + settingsXmlString)
|
|
|
|
|
|
|
|
utilsMock.FileWrite(".pipeline/mavenProjectSettings", xmlstring, 0777)
|
|
|
|
|
|
|
|
projectSettingsFilePath, err := UpdateProjectSettingsXML(".pipeline/mavenProjectSettings", "dummyRepoId2", "dummyRepoUser2", "dummyRepoPassword2", utilsMock)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
projectSettingsContent, _ := utilsMock.FileRead(projectSettingsFilePath)
|
|
|
|
var projectSettings Settings
|
|
|
|
|
|
|
|
err = xml.Unmarshal(projectSettingsContent, &projectSettings)
|
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, projectSettings.Servers.ServerType[1].ID, "dummyRepoId2")
|
|
|
|
assert.Equal(t, projectSettings.Servers.ServerType[1].Username, "dummyRepoUser2")
|
|
|
|
assert.Equal(t, projectSettings.Servers.ServerType[1].ID, "dummyRepoId2")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
2020-02-25 15:17:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func newSettingsDownloadTestUtilsBundle() *settingsDownloadTestUtils {
|
|
|
|
utilsBundle := settingsDownloadTestUtils{
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
}
|
|
|
|
return &utilsBundle
|
|
|
|
}
|
|
|
|
|
|
|
|
type settingsDownloadTestUtils struct {
|
|
|
|
*mock.FilesMock
|
2020-02-25 15:17:00 +02:00
|
|
|
expectedError error
|
|
|
|
downloadedFiles map[string]string // src, dest
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func (c *settingsDownloadTestUtils) SetOptions(options piperhttp.ClientOptions) {
|
2020-02-25 15:17:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func (c *settingsDownloadTestUtils) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
|
2020-02-25 15:17:00 +02:00
|
|
|
|
|
|
|
if c.expectedError != nil {
|
|
|
|
return c.expectedError
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.downloadedFiles == nil {
|
|
|
|
c.downloadedFiles = make(map[string]string)
|
|
|
|
}
|
|
|
|
c.downloadedFiles[url] = filename
|
|
|
|
return nil
|
|
|
|
}
|