diff options
author | Dario Maiocchi <dmaiocchi@suse.com> | 2017-11-23 19:51:56 +0100 |
---|---|---|
committer | Alvaro <alvaro.saurin@gmail.com> | 2017-11-27 09:57:46 +0100 |
commit | f6f1310689089c4f669229a3ac93e75fd5f573c1 (patch) | |
tree | f8c51d04da8deaf8d498219b2a14c0e0a6e038d3 | |
parent | 538f29d84684083cabfacde7f901ebc4f7c6e37a (diff) | |
download | terraform-provider-libvirt-f6f1310689089c4f669229a3ac93e75fd5f573c1.tar terraform-provider-libvirt-f6f1310689089c4f669229a3ac93e75fd5f573c1.tar.gz |
add test for autostart option
-rw-r--r-- | libvirt/resource_libvirt_network.go | 4 | ||||
-rw-r--r-- | libvirt/resource_libvirt_network_test.go | 101 |
2 files changed, 102 insertions, 3 deletions
diff --git a/libvirt/resource_libvirt_network.go b/libvirt/resource_libvirt_network.go index 78be64a9..a108d648 100644 --- a/libvirt/resource_libvirt_network.go +++ b/libvirt/resource_libvirt_network.go @@ -161,8 +161,6 @@ func resourceLibvirtNetworkUpdate(d *schema.ResourceData, meta interface{}) erro d.SetPartial("running") } - d.Partial(false) - if d.HasChange("autostart") { err = network.SetAutostart(d.Get("autostart").(bool)) if err != nil { @@ -170,7 +168,7 @@ func resourceLibvirtNetworkUpdate(d *schema.ResourceData, meta interface{}) erro } d.SetPartial("autostart") } - + d.Partial(false) return nil } diff --git a/libvirt/resource_libvirt_network_test.go b/libvirt/resource_libvirt_network_test.go new file mode 100644 index 00000000..ae450012 --- /dev/null +++ b/libvirt/resource_libvirt_network_test.go @@ -0,0 +1,101 @@ +package libvirt + +import ( + "fmt" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/libvirt/libvirt-go" + "testing" +) + +func TestNetworkAutostart(t *testing.T) { + var network libvirt.Network + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLibvirtNetworkDestroy, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(` + resource "libvirt_network" "test_net" { + name = "networktest" + mode = "nat" + domain = "k8s.local" + addresses = ["10.17.3.0/24"] + autostart = true + }`), + Check: resource.ComposeTestCheckFunc( + networkExists("libvirt_network.test_net", &network), + resource.TestCheckResourceAttr("libvirt_network.test_net", "autostart", "true"), + ), + }, + { + Config: fmt.Sprintf(` + resource "libvirt_network" "test_net" { + name = "networktest" + mode = "nat" + domain = "k8s.local" + addresses = ["10.17.3.0/24"] + autostart = false + }`), + Check: resource.ComposeTestCheckFunc( + networkExists("libvirt_network.test_net", &network), + resource.TestCheckResourceAttr("libvirt_network.test_net", "autostart", "false"), + ), + }, + }, + }) +} + +func networkExists(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 + + networkRetrived, err := virConn.LookupNetworkByUUIDString(rs.Primary.ID) + if err != nil { + return err + } + + realID, err := networkRetrived.GetUUIDString() + if err != nil { + return err + } + + if realID != rs.Primary.ID { + return fmt.Errorf("Libvirt network not found") + } + + *network = *networkRetrived + + return nil + } +} + +func testAccCheckLibvirtNetworkDestroy(s *terraform.State) error { + virtConn := testAccProvider.Meta().(*Client).libvirt + + for _, rs := range s.RootModule().Resources { + if rs.Type != "libvirt_network" { + continue + } + + // Try to find the server + _, err := virtConn.LookupNetworkByUUIDString(rs.Primary.ID) + if err == nil { + return fmt.Errorf( + "Error waiting for network (%s) to be destroyed: %s", + rs.Primary.ID, err) + } + } + + return nil +} |