func TestNetworkCRUDOperations(t *testing.T) { Setup(t) defer Teardown() // Create a network n, err := networks.Create(Client, networks.CreateOpts{Name: "sample_network", AdminStateUp: networks.Up}).Extract() th.AssertNoErr(t, err) defer networks.Delete(Client, n.ID) th.AssertEquals(t, n.Name, "sample_network") th.AssertEquals(t, n.AdminStateUp, true) networkID := n.ID // List networks pager := networks.List(Client, networks.ListOpts{Limit: 2}) err = pager.EachPage(func(page pagination.Page) (bool, error) { t.Logf("--- Page ---") networkList, err := networks.ExtractNetworks(page) th.AssertNoErr(t, err) for _, n := range networkList { t.Logf("Network: ID [%s] Name [%s] Status [%s] Is shared? [%s]", n.ID, n.Name, n.Status, strconv.FormatBool(n.Shared)) } return true, nil }) th.CheckNoErr(t, err) // Get a network if networkID == "" { t.Fatalf("In order to retrieve a network, the NetworkID must be set") } n, err = networks.Get(Client, networkID).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, n.Status, "ACTIVE") th.AssertDeepEquals(t, n.Subnets, []string{}) th.AssertEquals(t, n.Name, "sample_network") th.AssertEquals(t, n.AdminStateUp, true) th.AssertEquals(t, n.Shared, false) th.AssertEquals(t, n.ID, networkID) // Update network n, err = networks.Update(Client, networkID, networks.UpdateOpts{Name: "new_network_name"}).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, n.Name, "new_network_name") // Delete network res := networks.Delete(Client, networkID) th.AssertNoErr(t, res.Err) }
func waitForNetworkDelete(networkingClient *gophercloud.ServiceClient, networkId string) resource.StateRefreshFunc { return func() (interface{}, string, error) { log.Printf("[DEBUG] Attempting to delete OpenStack Network %s.\n", networkId) n, err := networks.Get(networkingClient, networkId).Extract() if err != nil { errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError) if !ok { return n, "ACTIVE", err } if errCode.Actual == 404 { log.Printf("[DEBUG] Successfully deleted OpenStack Network %s", networkId) return n, "DELETED", nil } } err = networks.Delete(networkingClient, networkId).ExtractErr() if err != nil { errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError) if !ok { return n, "ACTIVE", err } if errCode.Actual == 404 { log.Printf("[DEBUG] Successfully deleted OpenStack Network %s", networkId) return n, "DELETED", nil } } log.Printf("[DEBUG] OpenStack Network %s still active.\n", networkId) return n, "ACTIVE", nil } }
func TestPortCRUD(t *testing.T) { Setup(t) defer Teardown() // Setup network t.Log("Setting up network") networkID, err := createNetwork() th.AssertNoErr(t, err) defer networks.Delete(Client, networkID) // Setup subnet t.Logf("Setting up subnet on network %s", networkID) subnetID, err := createSubnet(networkID) th.AssertNoErr(t, err) defer subnets.Delete(Client, subnetID) // Create port t.Logf("Create port based on subnet %s", subnetID) portID := createPort(t, networkID, subnetID) // List ports t.Logf("Listing all ports") listPorts(t) // Get port if portID == "" { t.Fatalf("In order to retrieve a port, the portID must be set") } p, err := ports.Get(Client, portID).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, p.ID, portID) // Update port updateOpts := ports.UpdateOpts{ Name: "new_port_name", AllowedAddressPairs: []ports.AddressPair{ ports.AddressPair{IPAddress: "192.168.199.201"}, }, } p, err = ports.Update(Client, portID, updateOpts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, p.Name, "new_port_name") updatedPort, err := ports.Get(Client, portID).Extract() th.AssertEquals(t, updatedPort.AllowedAddressPairs[0].IPAddress, "192.168.199.201") // Delete port res := ports.Delete(Client, portID) th.AssertNoErr(t, res.Err) }
func resourceNetworkingNetworkV2Delete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) networkingClient, err := config.networkingV2Client(d.Get("region").(string)) if err != nil { return fmt.Errorf("Error creating OpenStack networking client: %s", err) } err = networks.Delete(networkingClient, d.Id()).ExtractErr() if err != nil { return fmt.Errorf("Error deleting OpenStack Neutron Network: %s", err) } d.SetId("") return nil }
func TestPortBinding(t *testing.T) { base.Setup(t) defer base.Teardown() // Setup network t.Log("Setting up network") networkID, err := createNetwork() th.AssertNoErr(t, err) defer networks.Delete(base.Client, networkID) // Setup subnet t.Logf("Setting up subnet on network %s", networkID) subnetID, err := createSubnet(networkID) th.AssertNoErr(t, err) defer subnets.Delete(base.Client, subnetID) // Create port t.Logf("Create port based on subnet %s", subnetID) hostID := "localhost" portID := createPort(t, networkID, subnetID, hostID) // Get port if portID == "" { t.Fatalf("In order to retrieve a port, the portID must be set") } p, err := portsbinding.Get(base.Client, portID).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, p.ID, portID) th.AssertEquals(t, p.HostID, hostID) // Update port newHostID := "openstack" updateOpts := portsbinding.UpdateOpts{ HostID: newHostID, } p, err = portsbinding.Update(base.Client, portID, updateOpts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, p.HostID, newHostID) // List ports t.Logf("Listing all ports") listPorts(t) // Delete port res := ports.Delete(base.Client, portID) th.AssertNoErr(t, res.Err) }
func TestCRUD(t *testing.T) { Setup(t) defer Teardown() // Setup network t.Log("Setting up network") n, err := networks.Create(Client, networks.CreateOpts{Name: "tmp_network", AdminStateUp: networks.Up}).Extract() th.AssertNoErr(t, err) networkID := n.ID defer networks.Delete(Client, networkID) // Create subnet t.Log("Create subnet") enable := false opts := subnets.CreateOpts{ NetworkID: networkID, CIDR: "192.168.199.0/24", IPVersion: subnets.IPv4, Name: "my_subnet", EnableDHCP: &enable, } s, err := subnets.Create(Client, opts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.NetworkID, networkID) th.AssertEquals(t, s.CIDR, "192.168.199.0/24") th.AssertEquals(t, s.IPVersion, 4) th.AssertEquals(t, s.Name, "my_subnet") th.AssertEquals(t, s.EnableDHCP, false) subnetID := s.ID // Get subnet t.Log("Getting subnet") s, err = subnets.Get(Client, subnetID).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.ID, subnetID) // Update subnet t.Log("Update subnet") s, err = subnets.Update(Client, subnetID, subnets.UpdateOpts{Name: "new_subnet_name"}).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.Name, "new_subnet_name") // Delete subnet t.Log("Delete subnet") res := subnets.Delete(Client, subnetID) th.AssertNoErr(t, res.Err) }
// Delete accepts a unique ID and deletes the network associated with it. func Delete(c *gophercloud.ServiceClient, networkID string) os.DeleteResult { return os.Delete(c, networkID) }
func TestSubnetCRUD(t *testing.T) { Setup(t) defer Teardown() // Setup network t.Log("Setting up network") n, err := networks.Create(Client, networks.CreateOpts{Name: "tmp_network", AdminStateUp: networks.Up}).Extract() th.AssertNoErr(t, err) networkID := n.ID defer networks.Delete(Client, networkID) // Create subnet t.Log("Create subnet") enable := false opts := subnets.CreateOpts{ NetworkID: networkID, CIDR: "192.168.199.0/24", IPVersion: subnets.IPv4, Name: "my_subnet", EnableDHCP: &enable, } s, err := subnets.Create(Client, opts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.NetworkID, networkID) th.AssertEquals(t, s.CIDR, "192.168.199.0/24") th.AssertEquals(t, s.IPVersion, 4) th.AssertEquals(t, s.Name, "my_subnet") th.AssertEquals(t, s.EnableDHCP, false) th.AssertEquals(t, s.GatewayIP, "192.168.199.1") subnetID := s.ID // Get subnet t.Log("Getting subnet") s, err = subnets.Get(Client, subnetID).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.ID, subnetID) // Update subnet t.Log("Update subnet") s, err = subnets.Update(Client, subnetID, subnets.UpdateOpts{Name: "new_subnet_name"}).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.Name, "new_subnet_name") // Delete subnet t.Log("Delete subnet") res := subnets.Delete(Client, subnetID) th.AssertNoErr(t, res.Err) // Create subnet with no gateway t.Log("Create subnet with no gateway") opts = subnets.CreateOpts{ NetworkID: networkID, CIDR: "192.168.199.0/24", IPVersion: subnets.IPv4, Name: "my_subnet", EnableDHCP: &enable, NoGateway: true, } s, err = subnets.Create(Client, opts).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.NetworkID, networkID) th.AssertEquals(t, s.CIDR, "192.168.199.0/24") th.AssertEquals(t, s.IPVersion, 4) th.AssertEquals(t, s.Name, "my_subnet") th.AssertEquals(t, s.EnableDHCP, false) th.AssertEquals(t, s.GatewayIP, "") subnetID = s.ID // Get subnet t.Log("Getting subnet with no gateway") s, err = subnets.Get(Client, subnetID).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.ID, subnetID) // Update subnet t.Log("Update subnet with no gateway") s, err = subnets.Update(Client, subnetID, subnets.UpdateOpts{Name: "new_subnet_name"}).Extract() th.AssertNoErr(t, err) th.AssertEquals(t, s.Name, "new_subnet_name") // Delete subnet t.Log("Delete subnet with no gateway") res = subnets.Delete(Client, subnetID) th.AssertNoErr(t, res.Err) // Create subnet with invalid gateway configuration t.Log("Create subnet with invalid gateway configuration") opts = subnets.CreateOpts{ NetworkID: networkID, CIDR: "192.168.199.0/24", IPVersion: subnets.IPv4, Name: "my_subnet", EnableDHCP: &enable, NoGateway: true, GatewayIP: "192.168.199.1", } _, err = subnets.Create(Client, opts).Extract() if err == nil { t.Fatalf("Expected an error, got none") } }
func TestNeutron(t *testing.T) { g := newGraph(t) authUrl := os.Getenv("OS_AUTH_URL") username := os.Getenv("OS_USERNAME") password := os.Getenv("OS_PASSWORD") tenantName := os.Getenv("OS_TENANT_NAME") regionName := os.Getenv("OS_REGION_NAME") ovsdbPort := os.Getenv("SKYDIVE_OVSDB_REMOTE_PORT") params := map[string]interface{}{ "OsAuthUrl": authUrl, "OsUsername": username, "OsPassword": password, "OsTenantName": tenantName, "OsRegionName": regionName, "OvsdbPort": ovsdbPort, } agent := helper.StartAgentWithConfig(t, confNeutron, params) defer agent.Stop() opts := gophercloud.AuthOptions{ IdentityEndpoint: authUrl, Username: username, Password: password, TenantName: tenantName, } provider, err := openstack.AuthenticatedClient(opts) if err != nil { t.Fatal(err.Error()) } client, err := openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{ Name: "neutron", Region: regionName, Availability: gophercloud.AvailabilityPublic, }) if err != nil { t.Fatalf("Failed to create neutron client: %s", err.Error()) } result := networks.Create(client, networks.CreateOpts{Name: "skydive-test-network"}) if result.Err != nil { t.Fatalf("Failed to create neutron network: %s", result.Err.Error()) } network, err := result.Extract() if err != nil { t.Fatalf("Failed to create neutron network: %s", err.Error()) } defer networks.Delete(client, network.ID) setupCmds := []helper.Cmd{ {fmt.Sprintf("neutron subnet-create --name skydive-test-subnet-%s %s 10.0.0.0/24", network.ID, network.ID), false}, {fmt.Sprintf("neutron-debug probe-create %s", network.ID), false}, } tearDownCmds := []helper.Cmd{ {fmt.Sprintf("neutron subnet-delete skydive-test-subnet-%s", network.ID), false}, } var port *ports.Port testPassed := false onChange := func(ws *websocket.Conn) { g.Lock() defer g.Unlock() if port == nil { portListOpts := ports.ListOpts{} pager := ports.List(client, portListOpts) err = pager.EachPage(func(page pagination.Page) (bool, error) { portList, err := ports.ExtractPorts(page) if err != nil { return false, err } for _, p := range portList { if p.DeviceOwner == "network:probe" && p.NetworkID == network.ID { port = &p return false, nil } } return true, nil }) if port != nil { tearDownCmds = append(tearDownCmds, helper.Cmd{}) copy(tearDownCmds[1:], tearDownCmds[0:]) tearDownCmds[0] = helper.Cmd{fmt.Sprintf("neutron-debug probe-delete %s", port.ID), false} } } if !testPassed && len(g.GetNodes()) >= 1 && len(g.GetEdges()) >= 1 && port != nil { if g.LookupFirstNode(graph.Metadata{"Name": fmt.Sprintf("qdhcp-%s", network.ID), "Type": "netns"}) != nil { if g.LookupFirstNode(graph.Metadata{"Name": fmt.Sprintf("qprobe-%s", port.ID), "Type": "netns"}) != nil { if g.LookupFirstNode(graph.Metadata{"Type": "internal", "Driver": "openvswitch", "Manager": "neutron", "Neutron.NetworkID": network.ID}) != nil { testPassed = true ws.Close() } } } } } testTopology(t, g, setupCmds, onChange) if !testPassed { t.Error("test not executed or failed") } testCleanup(t, g, tearDownCmds, []string{fmt.Sprintf("qdhcp-%s", network.ID), fmt.Sprintf("qprobe-%s", port.ID)}) }
func deleteTopology(t *testing.T, networkID string) { res := networks.Delete(base.Client, networkID) th.AssertNoErr(t, res.Err) t.Logf("deleted network, ID %s", networkID) }
func deleteNetwork(t *testing.T, networkID string) { t.Logf("Deleting network %s", networkID) networks.Delete(base.Client, networkID) }