From 2a8bc45bc4605d8a77a00d7ac17e1e4a25fa9907 Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Tue, 26 May 2020 22:56:02 -0500 Subject: [PATCH] Added unit-tests for the netmap parsing components in the vmware builder. --- builder/vmware/common/driver_parser_test.go | 125 +++++++++++++++++- .../common/testdata/netmap-example.conf | 11 ++ 2 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 builder/vmware/common/testdata/netmap-example.conf diff --git a/builder/vmware/common/driver_parser_test.go b/builder/vmware/common/driver_parser_test.go index 66511390a..042b1b0a7 100644 --- a/builder/vmware/common/driver_parser_test.go +++ b/builder/vmware/common/driver_parser_test.go @@ -143,15 +143,15 @@ subnet 127.0.0.0 netmask 255.255.255.252 { } result := tokenizeDhcpConfigFromString(test_1) - t.Logf("testing for: %#v", expected) - t.Logf("checking out: %#v", result) + t.Logf("testing for: %v", expected) + t.Logf("checking out: %v", result) if len(result) != len(expected) { t.Fatalf("length of token lists do not match (%d != %d)", len(result), len(expected)) } for index := range expected { if string(expected[index]) != string(result[index]) { - t.Errorf("unexpected token at index %d: %s != %s", index, expected[index], result[index]) + t.Errorf("unexpected token at index %d: %v != %v", index, expected[index], result[index]) } } } @@ -390,3 +390,122 @@ parameters : map[default-lease-time:1800 max-lease-time:7200] } } } + +func TestParserTokenizeNetworkMap(t *testing.T) { + + test_1 := "group.attribute = \"string\"" + expected := []string{ + "group.attribute", "=", "\"string\"", + } + result := tokenizeDhcpConfigFromString(test_1) + if len(result) != len(expected) { + t.Fatalf("length of token lists do not match (%d != %d)", len(result), len(expected)) + } + + for index := range expected { + if string(expected[index]) != string(result[index]) { + t.Errorf("unexpected token at index %d: %v != %v", index, expected[index], result[index]) + } + } + + test_2 := "attribute == \"" + expected = []string{ + "attribute", "==", "\"", + } + result = tokenizeDhcpConfigFromString(test_2) + if len(result) != len(expected) { + t.Fatalf("length of token lists do not match (%d != %d)", len(result), len(expected)) + } + + test_3 := "attribute ....... ======\nnew lines should make no difference" + expected = []string{ + "attribute", ".......", "======", "new", "lines", "should", "make", "no", "difference", + } + result = tokenizeDhcpConfigFromString(test_3) + if len(result) != len(expected) { + t.Fatalf("length of token lists do not match (%d != %d)", len(result), len(expected)) + } + + test_4 := "\t\t\t\t thishadwhitespacebeforebeingparsed\t \t \t \t\n\n" + expected = []string{ + "thishadwhitespacebeforebeingparsed", + } + result = tokenizeDhcpConfigFromString(test_4) + if len(result) != len(expected) { + t.Fatalf("length of token lists do not match (%d != %d)", len(result), len(expected)) + } +} + +func TestParserReadNetworkMap(t *testing.T) { + f, err := os.Open(filepath.Join("testdata", "netmap-example.conf")) + if err != nil { + t.Fatalf("Unable to open netmap.conf sample: %s", err) + } + defer f.Close() + + netmap, err := ReadNetworkMap(f) + if err != nil { + t.Fatalf("Unable to read netmap.conf samplpe: %s", err) + } + + expected_keys := []string{"device", "name"} + for _, item := range netmap { + for _, name := range expected_keys { + _, ok := item[name] + if !ok { + t.Errorf("unable to find expected key %v in map: %v", name, item) + } + } + } + + expected_vmnet0 := [][]string{ + []string{"device", "vmnet0"}, + []string{"name", "Bridged"}, + } + for _, item := range netmap { + if item["device"] != "vmnet0" { + continue + } + for _, expectpair := range expected_vmnet0 { + name := expectpair[0] + value := expectpair[1] + if item[name] != value { + t.Errorf("expected value %v for attribute %v, got %v", value, name, item[name]) + } + } + } + + expected_vmnet1 := [][]string{ + []string{"device", "vmnet1"}, + []string{"name", "HostOnly"}, + } + for _, item := range netmap { + if item["device"] != "vmnet1" { + continue + } + for _, expectpair := range expected_vmnet1 { + name := expectpair[0] + value := expectpair[1] + if item[name] != value { + t.Errorf("expected value %v for attribute %v, got %v", value, name, item[name]) + } + } + } + + expected_vmnet8 := [][]string{ + []string{"device", "vmnet8"}, + []string{"name", "NAT"}, + } + for _, item := range netmap { + if item["device"] != "vmnet8" { + continue + } + for _, expectpair := range expected_vmnet8 { + name := expectpair[0] + value := expectpair[1] + if item[name] != value { + t.Errorf("expected value %v for attribute %v, got %v", value, name, item[name]) + } + } + } +} diff --git a/builder/vmware/common/testdata/netmap-example.conf b/builder/vmware/common/testdata/netmap-example.conf new file mode 100644 index 000000000..d697419de --- /dev/null +++ b/builder/vmware/common/testdata/netmap-example.conf @@ -0,0 +1,11 @@ +network8.device = "vmnet8" +network0.name = "Bridged" +network2.device = "vmnet57005" +network3.name = "butneeds" +network0.device = "vmnet0" +network1.name = "HostOnly" +network8.name = "NAT" +network1.device = "vmnet1" +network3.notuseful = "blah" +network3.device = "nameanddevice" +network2.name = "bleep bloop"