Files
warewulf/internal/pkg/node/util_test.go
MatthewHink d8cd6049ac Initial cut of wwapi.
We need an API for Warewulf in order to automate around it. The initial version of the API will look a lot like wwctl. wwctl will call the API so that we do not need to maintain separate code paths. In this preview wwctl calls the API via direct function call, but we can change that to a gprc or REST call to a Warewulf server in the future.

This commit contains:
wwapid WareWulf API Daemon. A grpc server with mTLS auth.
wwapic WareWulf API Client. A grpc client with mTLS auth. It's just a sample that reads the version from the server.
wwapird WareWulf API Rest Daemon. A http REST reverse proxy to wwapid.
There are also sample insecure and secure curls to test with.

Implemented Functionality:
wwctl node add
wwctl node delete
wwctl node list
wwctl node set
wwctl node status
wwctl container build
wwctl container delete
wwctl container import
wwctl container list
wwctl container show

Some notes on the files:

    Logic that was in wwctl has moved to warewulf/internal/pkg/api. wwctl just calls the API. wwctl functionality is unchanged.
    Everything under the /google directory is copied google proto code to stand up wwapird. It's a bit strange to copy in the code, but that is currently how it's done.
    Everything in warewulf/internal/pkg/api/routes/wwapiv1 is generated code.

There are some loose ends here, such as no service installers. I just ran off the command line for development.
The Makefile could use improvement. I'm building by make clean setup proto all build wwapid wwapic wwapird ; echo $?
I copied the configs from warewulf/etc to /usr/local/etc/warewulf manually.
2022-06-24 08:39:30 -07:00

127 lines
3.3 KiB
Go

package node
import (
"testing"
"gopkg.in/yaml.v2"
)
func NewTestNode() (NodeYaml, error) {
var data = `
nodeprofiles:
default:
comment: This profile is automatically included for each node
nodes:
test_node:
profiles:
- default
network devices:
net0:
default: true
hwaddr: 00:00:00:00:12:34
ipaddr: 1.2.3.4
device: eth0
net1:
default: false
hwaddr: ab:cd:ef:00:12:34
ipaddr: 1.2.3.4
device: eth1
net2:
default: false
hwaddr: aB:Cd:eF:12:34:56
ipaddr: 1.2.3.4
device: eth2
test_node_IPv6:
profiles:
- default
network devices:
net1:
default: false
ipaddr: fd1a:2b3c:4d5e:06f0:1234:5678:90ab:cdef
`
var ret NodeYaml
err := yaml.Unmarshal([]byte(data), &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func Test_nodeYaml_FindByHwaddr(t *testing.T) {
c, _ := NewTestNode()
//type fields struct {
// NodeProfiles map[string]*NodeConf
// Nodes map[string]*NodeConf
//}
type args struct {
hwa string
}
tests := []struct {
name string
//fields fields
config NodeYaml
args args
want string
wantErr bool
}{
{"emptyString", c, args{hwa: ""}, "", true},
{"noIpString", c, args{hwa: "this is not a MAC"}, "", true},
{"intString", c, args{hwa: "4294967296"}, "", true},
{"invalidMAC", c, args{hwa: "xx:00:00:00:12:34"}, "", true},
{"validMACNotFound", c, args{hwa: "aa:FF:ee:65:43:21"}, "", true},
{"validMAC", c, args{hwa: "ab:cd:ef:00:12:34"}, "test_node", false},
{"validMAC2", c, args{hwa: "aB:Cd:eF:00:12:34"}, "test_node", false},
{"validMAC3", c, args{hwa: "Ab:cD:Ef:12:34:56"}, "test_node", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := tt.config
got, err := config.FindByHwaddr(tt.args.hwa)
if (err != nil) != tt.wantErr {
t.Errorf("FindByHwaddr() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !(got.Id.Get() == tt.want) {
t.Errorf("FindByHwaddr() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_nodeYaml_FindByIpaddr(t *testing.T) {
c, _ := NewTestNode()
type args struct {
ipaddr string
}
tests := []struct {
name string
config NodeYaml
args args
want string
wantErr bool
}{
{"emptyString", c, args{ipaddr: ""}, "", true},
{"noIpString", c, args{ipaddr: "this is not an IP"}, "", true},
{"intString", c, args{ipaddr: "4294967296"}, "", true},
{"invalidIPv4", c, args{ipaddr: "1.2.3.256"}, "", true},
{"invalidIPv6", c, args{ipaddr: "xd1a:2b3c:4d5e:06f0:1234:5678:90ab:cdef"}, "", true},
{"validIPv4NotFound", c, args{ipaddr: "1.1.1.1"}, "", true},
{"validIPv6NotFound", c, args{ipaddr: "fd1a:2b3c:4d5e:06f0:1234:5678:90ab:fedc"}, "", true},
{"validIPv4", c, args{ipaddr: "1.2.3.4"}, "test_node", false},
{"validIPv6", c, args{ipaddr: "fd1a:2b3c:4d5e:06f0:1234:5678:90ab:cdef"}, "test_node_IPv6", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := tt.config
got, err := config.FindByIpaddr(tt.args.ipaddr)
if (err != nil) != tt.wantErr {
t.Errorf("FindByIpaddr() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !(got.Id.Get() == tt.want) {
t.Errorf("FindByHwaddr() got = %v, want %v", got, tt.want)
}
})
}
}