summaryrefslogtreecommitdiff
path: root/libvirt/resource_libvirt_domain_test.go
diff options
context:
space:
mode:
authorEamonn O'Toole <eamonn.otoole@hpe.com>2017-03-03 14:30:50 +0000
committerAlvaro <alvaro.saurin@gmail.com>2017-12-14 12:52:41 +0100
commit7b2adfc1f28d76399d63aaf50ff914268efbb87a (patch)
treed7bbeace25610659938b777ae6c0bf3d2c1ba03b /libvirt/resource_libvirt_domain_test.go
parent8aa37bc03ce12e487d8a4d1ba9df56355da235ea (diff)
downloadterraform-provider-libvirt-7b2adfc1f28d76399d63aaf50ff914268efbb87a.tar
terraform-provider-libvirt-7b2adfc1f28d76399d63aaf50ff914268efbb87a.tar.gz
Add new method to get information on domain interfaces
We've found that, when executing destroy/create cycles on a domain where the virtual network(s) to which it is attached are persisted across these cycles then we get network failures on recreation of the domain since the old entries for the domain are still present in the virtual network(s). This issue should (hopefully) be fixed by commit a20d2a92668b2d5e077267c50074bf853ef371d. We've also found that, on occasion, we don't get complete information on a domain's interfaces from ListAllInterfaceAddresses. This patch implements a new method getDomainInterfacesFromNetworks which is called from domainGetIfacesInfo after the qemu agent method has been used if the qemu agent method doesn't return any information. This new method builds the interface information from information collected from the networks themselves. Change-Id: I5271ee191db93b2b1a0f14dc226d0b945f77a901
Diffstat (limited to 'libvirt/resource_libvirt_domain_test.go')
-rw-r--r--libvirt/resource_libvirt_domain_test.go127
1 files changed, 127 insertions, 0 deletions
diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go
index 43ce2478..ac05c494 100644
--- a/libvirt/resource_libvirt_domain_test.go
+++ b/libvirt/resource_libvirt_domain_test.go
@@ -335,6 +335,65 @@ func TestAccLibvirtDomain_NetworkInterface(t *testing.T) {
})
}
+func TestAccLibvirtDomain_CheckDHCPEntries(t *testing.T) {
+ var domain libvirt.Domain
+ var network libvirt.Network
+
+ var configWithDomain = fmt.Sprintf(`
+ resource "libvirt_network" "acceptance-test-network" {
+ name = "acceptance-test-network"
+ mode = "nat"
+ domain = "acceptance-test-network-local"
+ addresses = ["192.0.0.0/24"]
+ }
+
+ resource "libvirt_domain" "acceptance-test-domain" {
+ name = "terraform-test"
+ network_interface {
+ network_id = "${libvirt_network.acceptance-test-network.id}"
+ hostname = "terraform-test"
+ addresses = ["192.0.0.2"]
+ }
+ }`)
+
+ var configWithoutDomain = fmt.Sprintf(`
+ resource "libvirt_network" "acceptance-test-network" {
+ name = "acceptance-test-network"
+ mode = "nat"
+ domain = "acceptance-test-network-local"
+ addresses = ["192.0.0.0/24"]
+ }`)
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: testAccCheckLibvirtDomainDestroy,
+ Steps: []resource.TestStep{
+ resource.TestStep{
+ Config: configWithDomain,
+ Check: resource.ComposeTestCheckFunc(
+ testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain),
+ testAccCheckLibvirtNetworkExists("libvirt_network.acceptance-test-network", &network),
+ ),
+ },
+ resource.TestStep{
+ Config: configWithoutDomain,
+ Check: resource.ComposeTestCheckFunc(
+ testAccCheckLibvirtDestroyLeavesIPs("libvirt_network.acceptance-test-network",
+ "192.0.0.2", &network),
+ ),
+ },
+ resource.TestStep{
+ Config: configWithDomain,
+ ExpectNonEmptyPlan: true,
+ Check: resource.ComposeTestCheckFunc(
+ testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain),
+ ),
+ },
+ },
+ })
+}
+
func TestAccLibvirtDomain_Graphics(t *testing.T) {
var domain libvirt.Domain
@@ -717,6 +776,38 @@ func testAccCheckLibvirtURLDisk(u *url.URL, domain *libvirt.Domain) resource.Tes
}
}
+func testAccCheckLibvirtDestroyLeavesIPs(n string, ip string, network *libvirt.Network) resource.TestCheckFunc {
+ return func(s *terraform.State) error {
+ rs, ok := s.RootModule().Resources[n]
+ if !ok {
+ return fmt.Errorf("Not found: %s", n)
+ }
+
+ if rs.Primary.ID == "" {
+ return fmt.Errorf("No libvirt network ID is set")
+ }
+
+ virConn := testAccProvider.Meta().(*Client).libvirt
+
+ retrieveNetwork, err := virConn.LookupNetworkByUUIDString(rs.Primary.ID)
+
+ if err != nil {
+ return err
+ }
+
+ networkDef, err := newDefNetworkfromLibvirt(retrieveNetwork)
+
+ for _, ips := range networkDef.IPs {
+ for _, dhcpHost := range ips.DHCP.Hosts {
+ if dhcpHost.IP == ip {
+ return nil
+ }
+ }
+ }
+ return fmt.Errorf("Hostname with ip '%s' does not have a dhcp entry in network", ip)
+ }
+}
+
func testAccCheckLibvirtDomainKernelInitrdCmdline(domain *libvirt.Domain, kernel *libvirt.StorageVol, initrd *libvirt.StorageVol) resource.TestCheckFunc {
return func(s *terraform.State) error {
xmlDesc, err := domain.GetXMLDesc(0)
@@ -923,3 +1014,39 @@ func TestAccLibvirtDomain_ArchType(t *testing.T) {
},
})
}
+
+func testAccCheckLibvirtNetworkExists(n string, network *libvirt.Network) resource.TestCheckFunc {
+ return func(s *terraform.State) error {
+ rs, ok := s.RootModule().Resources[n]
+ if !ok {
+ return fmt.Errorf("Not found: %s", n)
+ }
+
+ if rs.Primary.ID == "" {
+ return fmt.Errorf("No libvirt network ID is set")
+ }
+
+ virConn := testAccProvider.Meta().(*Client).libvirt
+
+ retrieveNetwork, err := virConn.LookupNetworkByUUIDString(rs.Primary.ID)
+
+ if err != nil {
+ return err
+ }
+
+ log.Printf("The ID is %s", rs.Primary.ID)
+
+ realID, err := retrieveNetwork.GetUUIDString()
+ if err != nil {
+ return err
+ }
+
+ if realID != rs.Primary.ID {
+ return fmt.Errorf("Libvirt network not found")
+ }
+
+ network = retrieveNetwork
+
+ return nil
+ }
+}