added simple test for transformer.go
This commit is contained in:
@@ -2,6 +2,7 @@ package node
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@@ -347,3 +348,19 @@ func NewInfo() (nodeInfo NodeInfo) {
|
||||
nodeInfo.NetDevs = make(map[string]*NetDevEntry)
|
||||
return nodeInfo
|
||||
}
|
||||
|
||||
/*
|
||||
Get a entry by its name
|
||||
*/
|
||||
func GetByName(node interface{}, name string) (string, error) {
|
||||
valEntry := reflect.ValueOf(node)
|
||||
entryField := valEntry.Elem().FieldByName(name)
|
||||
if entryField == (reflect.Value{}) {
|
||||
return "", fmt.Errorf("couldn't find field with name: %s", name)
|
||||
}
|
||||
if entryField.Type() != reflect.TypeOf(Entry{}) {
|
||||
return "", fmt.Errorf("field %s is not of type node.Entry", name)
|
||||
}
|
||||
myEntry := entryField.Interface().(Entry)
|
||||
return myEntry.Get(), nil
|
||||
}
|
||||
|
||||
37
internal/pkg/node/transformer_test.go
Normal file
37
internal/pkg/node/transformer_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_nodeYaml_SetFrom(t *testing.T) {
|
||||
c, _ := NewTestNode()
|
||||
singleNodeConf := c.Nodes["test_node"]
|
||||
singleNodeInfo := NewInfo()
|
||||
singleNodeInfo.SetFrom(singleNodeConf)
|
||||
tests := []struct {
|
||||
name string
|
||||
arg string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{"Right comment", "Comment", "Node Comment", false},
|
||||
{"FieldName", "comment", "NodeComment", true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := GetByName(&singleNodeInfo, tt.arg)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetByName(%s,%s) error = %v, wantErr %v",
|
||||
reflect.TypeOf(singleNodeConf), tt.arg, err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if (got != tt.want) != tt.wantErr {
|
||||
t.Errorf("GetByName(%s,%s) got = %v, want = %v",
|
||||
reflect.TypeOf(singleNodeConf), tt.arg, got, tt.want)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ nodeprofiles:
|
||||
comment: This profile is automatically included for each node
|
||||
nodes:
|
||||
test_node:
|
||||
comment: Node Comment
|
||||
profiles:
|
||||
- default
|
||||
network devices:
|
||||
|
||||
Reference in New Issue
Block a user