diff options
author | Flavio Castelli <fcastelli@suse.com> | 2017-03-02 23:06:24 +0100 |
---|---|---|
committer | Alvaro <alvaro.saurin@gmail.com> | 2017-03-03 09:50:04 +0100 |
commit | 01d6e3972e69e1158fba260b97f47921b3d7a4c4 (patch) | |
tree | 1df6c1d88c741801eb8295e1ec02bc8c151f1145 /libvirt | |
parent | dea5495901aa84488b7b446a6708aeccf00c680a (diff) | |
download | terraform-provider-libvirt-01d6e3972e69e1158fba260b97f47921b3d7a4c4.tar terraform-provider-libvirt-01d6e3972e69e1158fba260b97f47921b3d7a4c4.tar.gz |
Improve code coverage
Improve code coverage results by writing some unit tests.
These are really the low hanging fruits, more should come later.
Signed-off-by: Flavio Castelli <fcastelli@suse.com>
Diffstat (limited to 'libvirt')
-rw-r--r-- | libvirt/disk_def_test.go | 41 | ||||
-rw-r--r-- | libvirt/domain_def_test.go | 27 | ||||
-rw-r--r-- | libvirt/network_def_test.go | 12 | ||||
-rw-r--r-- | libvirt/resource_libvirt_domain_test.go | 11 | ||||
-rw-r--r-- | libvirt/utils_libvirt_test.go | 32 | ||||
-rw-r--r-- | libvirt/utils_net_test.go | 19 | ||||
-rw-r--r-- | libvirt/volume_def_test.go | 27 |
7 files changed, 168 insertions, 1 deletions
diff --git a/libvirt/disk_def_test.go b/libvirt/disk_def_test.go new file mode 100644 index 00000000..c27c9b64 --- /dev/null +++ b/libvirt/disk_def_test.go @@ -0,0 +1,41 @@ +package libvirt + +import ( + "bytes" + "encoding/xml" + "testing" + + "github.com/davecgh/go-spew/spew" +) + +func init() { + spew.Config.Indent = "\t" +} + +func TestDefaultDiskMarshall(t *testing.T) { + b := newDefDisk() + prettyB := spew.Sdump(b) + t.Logf("Parsed default disk:\n%s", prettyB) + + buf := new(bytes.Buffer) + enc := xml.NewEncoder(buf) + enc.Indent(" ", " ") + if err := enc.Encode(b); err != nil { + t.Fatalf("could not marshall this:\n%s", spew.Sdump(b)) + } + t.Logf("Marshalled default disk:\n%s", buf.String()) +} + +func TestDefaultCDROMMarshall(t *testing.T) { + b := newCDROM() + prettyB := spew.Sdump(b) + t.Logf("Parsed default cdrom:\n%s", prettyB) + + buf := new(bytes.Buffer) + enc := xml.NewEncoder(buf) + enc.Indent(" ", " ") + if err := enc.Encode(b); err != nil { + t.Fatalf("could not marshall this:\n%s", spew.Sdump(b)) + } + t.Logf("Marshalled default cdrom:\n%s", buf.String()) +} diff --git a/libvirt/domain_def_test.go b/libvirt/domain_def_test.go new file mode 100644 index 00000000..9d0eee75 --- /dev/null +++ b/libvirt/domain_def_test.go @@ -0,0 +1,27 @@ +package libvirt + +import ( + "bytes" + "encoding/xml" + "testing" + + "github.com/davecgh/go-spew/spew" +) + +func init() { + spew.Config.Indent = "\t" +} + +func TestDefaultDomainMarshall(t *testing.T) { + b := newDomainDef() + prettyB := spew.Sdump(b) + t.Logf("Parsed default domain:\n%s", prettyB) + + buf := new(bytes.Buffer) + enc := xml.NewEncoder(buf) + enc.Indent(" ", " ") + if err := enc.Encode(b); err != nil { + t.Fatalf("could not marshall this:\n%s", spew.Sdump(b)) + } + t.Logf("Marshalled default domain:\n%s", buf.String()) +} diff --git a/libvirt/network_def_test.go b/libvirt/network_def_test.go index cd2048c7..d84c3ce8 100644 --- a/libvirt/network_def_test.go +++ b/libvirt/network_def_test.go @@ -91,3 +91,15 @@ func TestNetworkDefUnmarshall(t *testing.T) { t.Logf("Marshalled:\n%s", bs) } } + +func TestBrokenNetworkDefUnmarshall(t *testing.T) { + // Try unmarshalling some broken xml + text := ` + <network> + ` + + _, err := newDefNetworkFromXML(text) + if err == nil { + t.Error("Unmarshal was supposed to fail") + } +} diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go index 6749283c..749f5e17 100644 --- a/libvirt/resource_libvirt_domain_test.go +++ b/libvirt/resource_libvirt_domain_test.go @@ -236,7 +236,7 @@ func TestAccLibvirtDomain_IgnitionObject(t *testing.T) { CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: config, + Config: config, ExpectNonEmptyPlan: true, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -337,3 +337,12 @@ func testAccCheckIgnitionFileNameExists(domain *libvirt.VirDomain) resource.Test return nil } } + +func TestHash(t *testing.T) { + actual := hash("this is a test") + expected := "2e99758548972a8e8822ad47fa1017ff72f06f3ff6a016851f45c398732bc50c" + + if actual != expected { + t.Errorf("Expected %s, got %s", expected, actual) + } +} diff --git a/libvirt/utils_libvirt_test.go b/libvirt/utils_libvirt_test.go new file mode 100644 index 00000000..d73e81f7 --- /dev/null +++ b/libvirt/utils_libvirt_test.go @@ -0,0 +1,32 @@ +package libvirt + +import ( + "encoding/xml" + "testing" +) + +func TestGetHostXMLDesc(t *testing.T) { + ip := "127.0.0.1" + mac := "XX:YY:ZZ" + name := "localhost" + + data := getHostXMLDesc(ip, mac, name) + + dd := defNetworkIpDhcpHost{} + err := xml.Unmarshal([]byte(data), &dd) + if err != nil { + t.Errorf("error %v", err) + } + + if dd.Ip != ip { + t.Errorf("expected ip %s, got %s", ip, dd.Ip) + } + + if dd.Mac != mac { + t.Errorf("expected mac %s, got %s", mac, dd.Mac) + } + + if dd.Name != name { + t.Errorf("expected name %s, got %s", name, dd.Name) + } +} diff --git a/libvirt/utils_net_test.go b/libvirt/utils_net_test.go new file mode 100644 index 00000000..036b3616 --- /dev/null +++ b/libvirt/utils_net_test.go @@ -0,0 +1,19 @@ +package libvirt + +import ( + "net" + "testing" +) + +func TestRandomMACAddress(t *testing.T) { + mac, err := RandomMACAddress() + if err != nil { + t.Errorf("unexpected error: %v", err) + } + + _, err = net.ParseMAC(mac) + + if err != nil { + t.Errorf("Invalid MAC address generated: %s - %v", mac, err) + } +} diff --git a/libvirt/volume_def_test.go b/libvirt/volume_def_test.go new file mode 100644 index 00000000..6936154c --- /dev/null +++ b/libvirt/volume_def_test.go @@ -0,0 +1,27 @@ +package libvirt + +import ( + "bytes" + "encoding/xml" + "testing" + + "github.com/davecgh/go-spew/spew" +) + +func init() { + spew.Config.Indent = "\t" +} + +func TestDefaultVolumeMarshall(t *testing.T) { + b := newDefVolume() + prettyB := spew.Sdump(b) + t.Logf("Parsed default volume:\n%s", prettyB) + + buf := new(bytes.Buffer) + enc := xml.NewEncoder(buf) + enc.Indent(" ", " ") + if err := enc.Encode(b); err != nil { + t.Fatalf("could not marshall this:\n%s", spew.Sdump(b)) + } + t.Logf("Marshalled default volume:\n%s", buf.String()) +} |