added simple test for transformer.go

This commit is contained in:
Christian Goll
2022-11-29 14:30:41 +01:00
parent 53be931f87
commit 79ed2a33ff
3 changed files with 55 additions and 0 deletions

View File

@@ -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
}

View 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
}
})
}
}

View File

@@ -13,6 +13,7 @@ nodeprofiles:
comment: This profile is automatically included for each node
nodes:
test_node:
comment: Node Comment
profiles:
- default
network devices: