2022-01-11 11:01:15 +02:00
|
|
|
package cnbutils_test
|
2021-09-29 18:21:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2022-01-11 11:01:15 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/cnbutils"
|
2021-09-29 18:21:13 +02:00
|
|
|
"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) {
|
2022-01-11 11:01:15 +02:00
|
|
|
mockUtils := &cnbutils.MockUtils{
|
2021-09-29 18:21:13 +02:00
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:32:32 +02:00
|
|
|
envVars := map[string]interface{}{
|
|
|
|
"FOO": "BAR",
|
|
|
|
"BAR": "BAZ",
|
|
|
|
"COMPLEX": "{\"foo\": \"bar=3\"}",
|
|
|
|
}
|
|
|
|
|
2022-01-11 11:01:15 +02:00
|
|
|
err := cnbutils.CreateEnvFiles(mockUtils, "/tmp/platform", envVars)
|
2021-09-29 18:21:13 +02:00
|
|
|
|
|
|
|
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) {
|
2022-01-11 11:01:15 +02:00
|
|
|
mockUtils := &cnbutils.MockUtils{
|
2021-09-29 18:21:13 +02:00
|
|
|
FilesMock: &mock.FilesMock{
|
|
|
|
FileWriteErrors: map[string]error{
|
|
|
|
"/tmp/platform/env/FOO": fmt.Errorf("unable to create dir"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-11 11:01:15 +02:00
|
|
|
err := cnbutils.CreateEnvFiles(mockUtils, "/tmp/platform", map[string]interface{}{"FOO": "BAR"})
|
2021-09-29 18:21:13 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err.Error(), "unable to create dir")
|
|
|
|
})
|
|
|
|
}
|