mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
Add utils function for addon descriptor (#1919)
* adding ReadAddonDescriptor * Update abaputils_test.go
This commit is contained in:
parent
c6ffb23fdf
commit
0b47748386
@ -3,9 +3,11 @@ package abaputils
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -15,6 +17,7 @@ import (
|
||||
"github.com/SAP/jenkins-library/pkg/command"
|
||||
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
||||
"github.com/SAP/jenkins-library/pkg/log"
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@ -167,6 +170,39 @@ func ConvertTime(logTimeStamp string) time.Time {
|
||||
return t
|
||||
}
|
||||
|
||||
// ReadAddonDescriptor parses AddonDescriptor YAML file
|
||||
func ReadAddonDescriptor(FileName string) (AddonDescriptor, error) {
|
||||
|
||||
var addonDescriptor AddonDescriptor
|
||||
var addonYAMLFile []byte
|
||||
filelocation, err := filepath.Glob(FileName)
|
||||
|
||||
if err != nil || len(filelocation) != 1 {
|
||||
return addonDescriptor, errors.New(fmt.Sprintf("Could not find %v.", FileName))
|
||||
}
|
||||
filename, err := filepath.Abs(filelocation[0])
|
||||
if err != nil {
|
||||
return addonDescriptor, errors.New(fmt.Sprintf("Could not get path of %v.", FileName))
|
||||
}
|
||||
addonYAMLFile, err = ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return addonDescriptor, errors.New(fmt.Sprintf("Could not read %v.", FileName))
|
||||
}
|
||||
|
||||
var jsonBytes []byte
|
||||
jsonBytes, err = yaml.YAMLToJSON(addonYAMLFile)
|
||||
if err != nil {
|
||||
return addonDescriptor, errors.New(fmt.Sprintf("Could not parse %v.", FileName))
|
||||
}
|
||||
|
||||
err = json.Unmarshal(jsonBytes, &addonDescriptor)
|
||||
if err != nil {
|
||||
return addonDescriptor, errors.New(fmt.Sprintf("Could not unmarshal %v.", FileName))
|
||||
}
|
||||
|
||||
return addonDescriptor, nil
|
||||
}
|
||||
|
||||
/*******************************
|
||||
* Structs for specific steps *
|
||||
*******************************/
|
||||
@ -259,6 +295,34 @@ type AbapBinding struct {
|
||||
Env string `json:"env"`
|
||||
}
|
||||
|
||||
// AddonDescriptor contains fields about the addonProduct
|
||||
type AddonDescriptor struct {
|
||||
AddonProduct string `json:"addonProduct"`
|
||||
AddonVersion string `json:"addonVersion"`
|
||||
AddonUniqueID string `json:"addonUniqueID"`
|
||||
CustomerID interface{} `json:"customerID"`
|
||||
AddonSpsLevel string
|
||||
AddonPatchLevel string
|
||||
TargetVectorID string
|
||||
Repositories []Repositories `json:"repositories"`
|
||||
}
|
||||
|
||||
// Repositories contains fields for the repository/component version
|
||||
type Repositories struct {
|
||||
Name string `json:"name"`
|
||||
Tag string `json:"tag"`
|
||||
Branch string `json:"branch"`
|
||||
Version string `json:"version"`
|
||||
VersionOtherFormat string
|
||||
SpsLevel string
|
||||
PackageName string
|
||||
PatchLevel string
|
||||
PredecessorCommitID string
|
||||
Status string
|
||||
Namespace string
|
||||
SarXMLFilePath string
|
||||
}
|
||||
|
||||
/********************************
|
||||
* Testing with a client mock *
|
||||
********************************/
|
||||
|
@ -1,6 +1,9 @@
|
||||
package abaputils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/command"
|
||||
@ -242,3 +245,61 @@ func TestTimeConverter(t *testing.T) {
|
||||
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
|
||||
})
|
||||
}
|
||||
|
||||
func TestReadAddonDescriptor(t *testing.T) {
|
||||
t.Run("Test: success case", func(t *testing.T) {
|
||||
|
||||
dir, err := ioutil.TempDir("", "test read addon descriptor")
|
||||
if err != nil {
|
||||
t.Fatal("Failed to create temporary directory")
|
||||
}
|
||||
oldCWD, _ := os.Getwd()
|
||||
_ = os.Chdir(dir)
|
||||
// clean up tmp dir
|
||||
defer func() {
|
||||
_ = os.Chdir(oldCWD)
|
||||
_ = os.RemoveAll(dir)
|
||||
}()
|
||||
|
||||
body := `---
|
||||
addonProduct: /DMO/myAddonProduct
|
||||
addonVersion: 3.1.4
|
||||
addonUniqueId: myAddonId
|
||||
customerID: 1234
|
||||
repositories:
|
||||
- name: /DMO/REPO_A
|
||||
tag: v-1.0.1-build-0001
|
||||
branch: branchA
|
||||
version: 1.0.1
|
||||
- name: /DMO/REPO_B
|
||||
tag: rel-2.1.1-build-0001
|
||||
branch: branchB
|
||||
version: 2.1.1
|
||||
`
|
||||
file, _ := os.Create("filename.yaml")
|
||||
file.Write([]byte(body))
|
||||
|
||||
addonDescriptor, err := ReadAddonDescriptor("filename.yaml")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, `/DMO/myAddonProduct`, addonDescriptor.AddonProduct)
|
||||
assert.Equal(t, `3.1.4`, addonDescriptor.AddonVersion)
|
||||
assert.Equal(t, `myAddonId`, addonDescriptor.AddonUniqueID)
|
||||
assert.Equal(t, float64(1234), addonDescriptor.CustomerID)
|
||||
assert.Equal(t, ``, addonDescriptor.AddonSpsLevel)
|
||||
assert.Equal(t, `/DMO/REPO_A`, addonDescriptor.Repositories[0].Name)
|
||||
assert.Equal(t, `/DMO/REPO_B`, addonDescriptor.Repositories[1].Name)
|
||||
assert.Equal(t, `v-1.0.1-build-0001`, addonDescriptor.Repositories[0].Tag)
|
||||
assert.Equal(t, `rel-2.1.1-build-0001`, addonDescriptor.Repositories[1].Tag)
|
||||
assert.Equal(t, `branchA`, addonDescriptor.Repositories[0].Branch)
|
||||
assert.Equal(t, `branchB`, addonDescriptor.Repositories[1].Branch)
|
||||
assert.Equal(t, `1.0.1`, addonDescriptor.Repositories[0].Version)
|
||||
assert.Equal(t, `2.1.1`, addonDescriptor.Repositories[1].Version)
|
||||
assert.Equal(t, ``, addonDescriptor.Repositories[0].SpsLevel)
|
||||
assert.Equal(t, ``, addonDescriptor.Repositories[1].SpsLevel)
|
||||
})
|
||||
|
||||
t.Run("Test: file does not exist", func(t *testing.T) {
|
||||
_, err := ReadAddonDescriptor("filename.yaml")
|
||||
assert.EqualError(t, err, fmt.Sprintf("Could not find %v.", "filename.yaml"))
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user