1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/cnbutils/env_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

61 lines
1.6 KiB
Go

//go:build unit
// +build unit
package cnbutils_test
import (
"fmt"
"testing"
"github.com/SAP/jenkins-library/pkg/cnbutils"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
func TestCreateEnvFiles(t *testing.T) {
t.Run("successfully writes environment files", func(t *testing.T) {
mockUtils := &cnbutils.MockUtils{
FilesMock: &mock.FilesMock{},
}
envVars := map[string]interface{}{
"FOO": "BAR",
"BAR": "BAZ",
"COMPLEX": "{\"foo\": \"bar=3\"}",
}
err := cnbutils.CreateEnvFiles(mockUtils, "/tmp/platform", envVars)
assert.NoError(t, err)
assert.True(t, mockUtils.HasWrittenFile("/tmp/platform/env/FOO"))
assert.True(t, mockUtils.HasWrittenFile("/tmp/platform/env/BAR"))
assert.True(t, mockUtils.HasWrittenFile("/tmp/platform/env/COMPLEX"))
result1, err := mockUtils.FileRead("/tmp/platform/env/FOO")
assert.NoError(t, err)
assert.Equal(t, "BAR", string(result1))
result2, err := mockUtils.FileRead("/tmp/platform/env/BAR")
assert.NoError(t, err)
assert.Equal(t, "BAZ", string(result2))
result3, err := mockUtils.FileRead("/tmp/platform/env/COMPLEX")
assert.NoError(t, err)
assert.Equal(t, "{\"foo\": \"bar=3\"}", string(result3))
})
t.Run("raises an error if unable to write to a file", func(t *testing.T) {
mockUtils := &cnbutils.MockUtils{
FilesMock: &mock.FilesMock{
FileWriteErrors: map[string]error{
"/tmp/platform/env/FOO": fmt.Errorf("unable to create dir"),
},
},
}
err := cnbutils.CreateEnvFiles(mockUtils, "/tmp/platform", map[string]interface{}{"FOO": "BAR"})
assert.Error(t, err)
assert.Equal(t, err.Error(), "unable to create dir")
})
}