1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/pkg/abaputils/descriptor_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

152 lines
4.8 KiB
Go

//go:build unit
// +build unit
package abaputils
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func readFileMock(FileName string) ([]byte, error) {
return []byte(FileName), nil
}
func TestAddonDescriptorNew(t *testing.T) {
var addonDescriptor AddonDescriptor
err := addonDescriptor.initFromYmlFile(TestAddonDescriptorYAML, readFileMock)
assert.NoError(t, err)
err = CheckAddonDescriptorForRepositories(addonDescriptor)
assert.NoError(t, err)
t.Run("Import addon.yml", func(t *testing.T) {
assert.Equal(t, "/DMO/myAddonProduct", addonDescriptor.AddonProduct)
assert.Equal(t, "/DMO/REPO_A", addonDescriptor.Repositories[0].Name)
assert.Equal(t, "JEK8S273S", addonDescriptor.Repositories[1].CommitID)
assert.Equal(t, "FR", addonDescriptor.Repositories[1].Languages[2])
assert.Equal(t, `ISO-DEENFR`, addonDescriptor.Repositories[1].GetAakAasLanguageVector())
assert.Equal(t, true, addonDescriptor.Repositories[1].UseClassicCTS)
})
t.Run("getRepositoriesInBuildScope", func(t *testing.T) {
assert.Equal(t, 2, len(addonDescriptor.Repositories))
addonDescriptor.Repositories[1].InBuildScope = true
repos := addonDescriptor.GetRepositoriesInBuildScope()
assert.Equal(t, 1, len(repos))
assert.Equal(t, "/DMO/REPO_B", repos[0].Name)
})
}
var TestAddonDescriptorYAML = `---
addonProduct: /DMO/myAddonProduct
addonVersion: 3.1.4
repositories:
- name: /DMO/REPO_A
tag: v-1.0.1
commitID: 89fLKS273S
branch: release-v.1.0.1
version: 1.0.1
languages:
- DE
- EN
- name: /DMO/REPO_B
tag: rel-2.1.1
commitID: JEK8S273S
branch: release-v.2.1.1
version: 2.1.1
languages:
- DE
- EN
- FR
useClassicCTS: true`
func TestReadAddonDescriptor(t *testing.T) {
t.Run("Test: success case", func(t *testing.T) {
dir := t.TempDir()
oldCWD, _ := os.Getwd()
_ = os.Chdir(dir)
// clean up tmp dir
defer func() {
_ = os.Chdir(oldCWD)
}()
file, _ := os.Create("filename.yaml")
file.Write([]byte(TestAddonDescriptorYAML))
addonDescriptor, err := ReadAddonDescriptor("filename.yaml")
assert.NoError(t, err)
assert.Equal(t, `/DMO/myAddonProduct`, addonDescriptor.AddonProduct)
assert.Equal(t, `3.1.4`, addonDescriptor.AddonVersionYAML)
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`, addonDescriptor.Repositories[0].Tag)
assert.Equal(t, `rel-2.1.1`, addonDescriptor.Repositories[1].Tag)
assert.Equal(t, `release-v.1.0.1`, addonDescriptor.Repositories[0].Branch)
assert.Equal(t, `release-v.2.1.1`, addonDescriptor.Repositories[1].Branch)
assert.Equal(t, `1.0.1`, addonDescriptor.Repositories[0].VersionYAML)
assert.Equal(t, `2.1.1`, addonDescriptor.Repositories[1].VersionYAML)
assert.Equal(t, ``, addonDescriptor.Repositories[0].SpLevel)
assert.Equal(t, ``, addonDescriptor.Repositories[1].SpLevel)
err = CheckAddonDescriptorForRepositories(addonDescriptor)
assert.NoError(t, err)
})
t.Run("Test: file does not exist", func(t *testing.T) {
expectedErrorMessage := "AddonDescriptor doesn't contain any repositories"
addonDescriptor, err := ReadAddonDescriptor("filename.yaml")
assert.EqualError(t, err, fmt.Sprintf("Could not find %v", "filename.yaml"))
assert.Equal(t, AddonDescriptor{}, addonDescriptor)
err = CheckAddonDescriptorForRepositories(addonDescriptor)
assert.EqualError(t, err, expectedErrorMessage)
})
t.Run("Test: empty config - failure case", func(t *testing.T) {
expectedErrorMessage := "AddonDescriptor doesn't contain any repositories"
addonDescriptor, err := ReadAddonDescriptor("")
assert.EqualError(t, err, fmt.Sprintf("Could not find %v", ""))
assert.Equal(t, AddonDescriptor{}, addonDescriptor)
err = CheckAddonDescriptorForRepositories(addonDescriptor)
assert.EqualError(t, err, expectedErrorMessage)
})
t.Run("Read empty addon descriptor from wrong config - failure case", func(t *testing.T) {
expectedErrorMessage := "AddonDescriptor doesn't contain any repositories"
expectedRepositoryList := AddonDescriptor{Repositories: []Repository{{}, {}}}
dir := t.TempDir()
oldCWD, _ := os.Getwd()
_ = os.Chdir(dir)
// clean up tmp dir
defer func() {
_ = os.Chdir(oldCWD)
}()
manifestFileString := `
repositories:
- repo: 'testRepo'
- repo: 'testRepo2'`
err := ioutil.WriteFile("repositories.yml", []byte(manifestFileString), 0644)
assert.NoError(t, err)
addonDescriptor, err := ReadAddonDescriptor("repositories.yml")
assert.Equal(t, expectedRepositoryList, addonDescriptor)
assert.NoError(t, err)
err = CheckAddonDescriptorForRepositories(addonDescriptor)
assert.EqualError(t, err, expectedErrorMessage)
})
}