2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
2020-01-15 13:16:25 +02:00
|
|
|
package piperenv
|
|
|
|
|
|
|
|
import (
|
2023-08-16 12:57:04 +02:00
|
|
|
"os"
|
2020-10-05 15:33:28 +02:00
|
|
|
"path/filepath"
|
2020-01-15 13:16:25 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-05 15:33:28 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-15 13:16:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetResourceParameter(t *testing.T) {
|
2020-10-05 15:33:28 +02:00
|
|
|
type args struct {
|
|
|
|
path string
|
|
|
|
resourceName string
|
|
|
|
paramName string
|
|
|
|
value interface{}
|
2020-01-15 13:16:25 +02:00
|
|
|
}
|
2020-10-05 15:33:28 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want string
|
|
|
|
args args
|
|
|
|
}{
|
|
|
|
{name: "string", want: "testVal", args: args{path: "", resourceName: "testRes", paramName: "testParamString", value: "testVal"}},
|
|
|
|
{name: "boolean", want: "true", args: args{path: "", resourceName: "testRes", paramName: "testParamBool", value: true}},
|
|
|
|
{name: "integer", want: "1", args: args{path: "", resourceName: "testRes", paramName: "testParamInt", value: 1}},
|
|
|
|
{name: "float", want: "0.123", args: args{path: "", resourceName: "testRes", paramName: "testParamFloat", value: 0.123}},
|
|
|
|
{name: "string list", want: "[\"test\",\"abc\"]", args: args{path: "", resourceName: "testRes", paramName: "testParamStringList", value: []string{"test", "abc"}}},
|
|
|
|
{name: "boolean list", want: "[true,false]", args: args{path: "", resourceName: "testRes", paramName: "testParamBoolList", value: []bool{true, false}}},
|
|
|
|
{name: "integer list", want: "[1,2]", args: args{path: "", resourceName: "testRes", paramName: "testParamIntList", value: []int{1, 2}}},
|
|
|
|
}
|
|
|
|
for _, testCase := range tests {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
// init
|
2022-07-12 15:19:12 +02:00
|
|
|
dir := t.TempDir()
|
2020-10-05 15:33:28 +02:00
|
|
|
targetFile := filepath.Join(dir, testCase.args.resourceName, testCase.args.paramName)
|
|
|
|
// test
|
|
|
|
err := SetResourceParameter(dir, testCase.args.resourceName, testCase.args.paramName, testCase.args.value)
|
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
var v []byte
|
|
|
|
// var err error
|
|
|
|
switch testCase.args.value.(type) {
|
|
|
|
case string:
|
|
|
|
default:
|
|
|
|
targetFile += ".json"
|
|
|
|
}
|
|
|
|
assert.FileExists(t, targetFile)
|
2023-08-16 12:57:04 +02:00
|
|
|
v, err = os.ReadFile(targetFile)
|
2020-10-05 15:33:28 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, testCase.want, string(v))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-01-15 13:16:25 +02:00
|
|
|
|
2020-10-05 15:33:28 +02:00
|
|
|
func TestGetResourceParameter(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
path string
|
|
|
|
resourceName string
|
|
|
|
paramName string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{name: "default", args: args{path: "", resourceName: "", paramName: ""}, want: ""},
|
|
|
|
}
|
|
|
|
for _, testCase := range tests {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
// test
|
|
|
|
result := GetResourceParameter(testCase.args.path, testCase.args.resourceName, testCase.args.paramName)
|
|
|
|
// assert
|
|
|
|
assert.Equal(t, testCase.want, result)
|
|
|
|
})
|
|
|
|
}
|
2020-01-15 13:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetParameter(t *testing.T) {
|
2022-07-12 15:19:12 +02:00
|
|
|
dir := t.TempDir()
|
2020-01-15 13:16:25 +02:00
|
|
|
|
2022-07-12 15:19:12 +02:00
|
|
|
err := SetParameter(dir, "testParam", "testVal")
|
2020-01-15 13:16:25 +02:00
|
|
|
|
2020-09-24 07:41:06 +02:00
|
|
|
assert.NoError(t, err, "Error occurred but none expected")
|
2020-01-15 13:16:25 +02:00
|
|
|
assert.Equal(t, "testVal", GetParameter(dir, "testParam"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadFromDisk(t *testing.T) {
|
2022-07-12 15:19:12 +02:00
|
|
|
dir := t.TempDir()
|
2020-01-15 13:16:25 +02:00
|
|
|
|
|
|
|
assert.Equal(t, "", GetParameter(dir, "testParamNotExistingYet"))
|
|
|
|
}
|