-
Notifications
You must be signed in to change notification settings - Fork 458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enable dhcp as optional parameter (typelist) #385
Changes from 4 commits
6c4b831
482dc22
a91d424
bf1c495
10fc80a
1ad52dd
ced540d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,45 +9,6 @@ import ( | |
"github.com/libvirt/libvirt-go" | ||
) | ||
|
||
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] | ||
|
@@ -81,6 +42,48 @@ func networkExists(n string, network *libvirt.Network) resource.TestCheckFunc { | |
} | ||
} | ||
|
||
func testAccCheckLibvirtNetworkDhcpStatus(name string, network *libvirt.Network, DhcpStatus string) resource.TestCheckFunc { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Speaking of this... In the comments, "dhcp" should be "DHCP" 😃 |
||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No libvirt network ID is set") | ||
} | ||
|
||
virConn := testAccProvider.Meta().(*Client).libvirt | ||
|
||
network, err := virConn.LookupNetworkByUUIDString(rs.Primary.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
networkDef, err := newDefNetworkfromLibvirt(network) | ||
if err != nil { | ||
return fmt.Errorf("Error reading libvirt network XML description: %s", err) | ||
} | ||
if DhcpStatus == "disabled" { | ||
for _, ips := range networkDef.IPs { | ||
// &libvirtxml.NetworkDHCP{..} should be nil when dhcp is disabled | ||
if ips.DHCP != nil { | ||
fmt.Printf("%#v", ips.DHCP) | ||
return fmt.Errorf("the network should have DHCP disabled") | ||
} | ||
} | ||
} | ||
if DhcpStatus == "enabled" { | ||
for _, ips := range networkDef.IPs { | ||
if ips.DHCP == nil { | ||
return fmt.Errorf("the network should have DHCP enabled") | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckLibvirtNetworkDestroy(s *terraform.State) error { | ||
virtConn := testAccProvider.Meta().(*Client).libvirt | ||
|
||
|
@@ -130,3 +133,96 @@ func TestAccLibvirtNetwork_Import(t *testing.T) { | |
}, | ||
}) | ||
} | ||
|
||
func TestAccLibvirtNetwork_EnableDhcpAndDisableItAfter(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To whom are you speaking to? 😸 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i just reviewed my code myself 🚀 🤖 was a sidenote 🤣 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't get angry with your reviewer 😋 . Enjoy your week-end too 🎆 . |
||
var network1 libvirt.Network | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckLibvirtNetworkDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
// dhcp is enabled true by default. | ||
Config: fmt.Sprintf(` | ||
resource "libvirt_network" "test_net" { | ||
name = "networktest" | ||
mode = "nat" | ||
domain = "k8s.local" | ||
addresses = ["10.17.3.0/24"] | ||
dhcp { | ||
enabled = true | ||
} | ||
}`), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("libvirt_network.test_net", "dhcp.0.enabled", "false"), | ||
testAccCheckLibvirtNetworkDhcpStatus("libvirt_network.test_net", &network1, "enabled"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccLibvirtNetwork_dhcpDisabled(t *testing.T) { | ||
var network1 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"] | ||
dhcp { | ||
enabled = false | ||
} | ||
}`), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("libvirt_network.test_net", "dhcp.0.enabled", "false"), | ||
testAccCheckLibvirtNetworkDhcpStatus("libvirt_network.test_net", &network1, "disabled"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
func TestAccLibvirtNetwork_Autostart(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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you are not retrieving a network ID, but looking a network by its ID. I would suggest: