mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-16 11:09:33 +02:00
ee7279c8fc
Add helper for finding uninitialized config
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package validation
|
|
|
|
import (
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
)
|
|
|
|
type Connection struct {
|
|
Endpoint string
|
|
User string
|
|
Password string
|
|
}
|
|
|
|
type Dummy struct {
|
|
Connection Connection
|
|
Prop1 string
|
|
Prop2 string
|
|
Prop3 string
|
|
Bool1 bool
|
|
Int1 int64
|
|
List []string
|
|
}
|
|
|
|
func TestWeProvideNotAStruct(t *testing.T) {
|
|
_, err := FindEmptyStringsInConfigStruct("Hello World")
|
|
assert.EqualError(t, err, "'Hello World' (string) is not a struct")
|
|
}
|
|
|
|
func TestUnsupportedType(t *testing.T) {
|
|
|
|
type DummyWithUnsupportedType struct {
|
|
Dummy
|
|
NotExpected float32
|
|
}
|
|
|
|
_, err := FindEmptyStringsInConfigStruct(DummyWithUnsupportedType{})
|
|
assert.EqualError(t, err, "unexpected type 'float32' of field: 'NotExpected', value: '0'")
|
|
|
|
}
|
|
|
|
func TestFindEmptyStringsInConfig(t *testing.T) {
|
|
myStruct := Dummy{
|
|
Connection: Connection{
|
|
Endpoint: "<set>",
|
|
User: "",
|
|
Password: "<set>",
|
|
},
|
|
Prop1: "<set>",
|
|
Prop2: "", // this is empty
|
|
// Prop3: "this is missing intentionally"
|
|
Bool1: false,
|
|
Int1: 42,
|
|
List: []string{"1", "2"},
|
|
}
|
|
emptyStrings, err := FindEmptyStringsInConfigStruct(myStruct)
|
|
if assert.NoError(t, err) {
|
|
assert.Len(t, emptyStrings, 3)
|
|
assert.Subset(t, emptyStrings, []string{
|
|
"Connection.User", // empty value, nested
|
|
"Prop2", // empty value
|
|
"Prop3", // missing value
|
|
})
|
|
}
|
|
}
|