blob: 1cfd2ef2d933b6e5ed1f6291551d40afcb1b3f4c [file] [log] [blame]
package devices
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
)
var (
testDeviceConfigs = []DeviceConfig{
DeviceConfig{
Type: "Astro",
Network: &NetworkProperties{
Nodename: "astro-test-device",
IPv4Addr: "255.255.255.255",
Mac: "00:00:00:00:00:00",
},
Power: &PowerClient{
Type: "PDU",
Host: "255.255.255.255",
Username: "username",
Password: "password",
PDUIp: "255.255.255.255",
PDUPort: "1",
},
},
DeviceConfig{
Type: "Intel NUC Kit NUC7i5DNHE",
Network: &NetworkProperties{
Nodename: "nuc-test-device",
IPv4Addr: "255.255.255.255",
Mac: "00:00:00:00:00:00",
},
Power: &PowerClient{
Type: "AMT",
Host: "255.255.255.255",
Username: "username",
Password: "password",
},
},
DeviceConfig{
Type: "Khadas Vim2 Max",
Network: &NetworkProperties{
Nodename: "vim-test-device",
IPv4Addr: "255.255.255.255",
Mac: "00:00:00:00:00:00",
},
Power: &PowerClient{
Type: "WOL",
Host: "255.255.255.255",
},
},
}
)
// deviceSanityCheck identifies basic differences between a Device and its source DeviceConfig.
// This function does not check every property; it is meant to be a sanity check.
func deviceSanityCheck(t *testing.T, actual Device, expected DeviceConfig) {
if actual.Nodename() != expected.Network.Nodename {
t.Errorf("nodenames not equal; expected: %s, actual: %s", expected.Network.Nodename, actual.Nodename())
}
if actual.Mac() != expected.Network.Mac {
t.Errorf("macs not equal; expected: %s, actual: %s", expected.Network.Mac, actual.Mac())
}
if actual.HasSerial() {
t.Errorf("test device should not have serial")
}
}
func TestLoadDeviceConfigs(t *testing.T) {
// Test that loading a nonexistent config fails.
if _, err := LoadDeviceConfigs("./test_data/nonexistent.json"); err == nil {
t.Error("loading nonexistent config did not fail")
}
// Test that loading misformatted json files fails.
if _, err := LoadDeviceConfigs("./test_data/invalid.json"); err == nil {
t.Error("loading invalid json did not produce error")
}
// Test that loading correctly formatted config files succeeds
configs, err := LoadDeviceConfigs("./test_data/config.json")
if err != nil {
t.Fatalf("loading device configs failed: %v", err)
}
if diff := cmp.Diff(configs, testDeviceConfigs); diff != "" {
t.Errorf("parsed configs do not match expected: (-want +got):\n%s", diff)
}
}
func TestCreateDevices(t *testing.T) {
ctx := context.Background()
devices, err := CreateDevices(ctx, testDeviceConfigs, []string{"test"})
if err != nil {
t.Fatalf("creating devices from configs failed: %v", err)
}
if len(devices) != 3 {
t.Fatalf("expected three devices to be created, got %d", len(devices))
}
// Check the types of each device.
if _, ok := devices[0].(*ArmCDCether); !ok {
t.Errorf("device one is not an arm cdcether device: %v", devices[0])
}
if _, ok := devices[1].(*Nuc); !ok {
t.Errorf("device one is not a nuc: %v", devices[1])
}
if _, ok := devices[2].(*Vim); !ok {
t.Errorf("device one is not a vim: %v", devices[2])
}
// Perform a quick sanity check on the devices.
for i, device := range devices {
deviceSanityCheck(t, device, testDeviceConfigs[i])
}
}