Ejemplo n.º 1
0
func TestWaitForStatus(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	th.Mux.HandleFunc("/volumes/1234", func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(3 * time.Second)
		w.Header().Add("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, `
		{
			"volume": {
				"display_name": "vol-001",
				"id": "1234",
				"status":"available"
			}
		}`)
	})

	err := WaitForStatus(client.ServiceClient(), "1234", "available", 0)
	if err == nil {
		t.Errorf("Expected error: 'Time Out in WaitFor'")
	}

	err = WaitForStatus(client.ServiceClient(), "1234", "available", 6)
	th.CheckNoErr(t, err)
}
Ejemplo n.º 2
0
func TestWaitForStatus(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	th.Mux.HandleFunc("/servers/4321", func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(2 * time.Second)
		w.Header().Add("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, `
		{
			"server": {
				"name": "the-server",
				"id": "4321",
				"status": "ACTIVE"
			}
		}`)
	})

	err := WaitForStatus(client.ServiceClient(), "4321", "ACTIVE", 0)
	if err == nil {
		t.Errorf("Expected error: 'Time Out in WaitFor'")
	}

	err = WaitForStatus(client.ServiceClient(), "4321", "ACTIVE", 3)
	th.CheckNoErr(t, err)
}
Ejemplo n.º 3
0
func TestBuildHeaders(t *testing.T) {
	testStruct := struct {
		Accept string `h:"Accept"`
		Num    int    `h:"Number,required"`
		Style  bool   `h:"Style"`
	}{
		Accept: "application/json",
		Num:    4,
		Style:  true,
	}
	expected := map[string]string{"Accept": "application/json", "Number": "4", "Style": "true"}
	actual, err := BuildHeaders(&testStruct)
	th.CheckNoErr(t, err)
	th.CheckDeepEquals(t, expected, actual)

	testStruct.Num = 0
	_, err = BuildHeaders(&testStruct)
	if err == nil {
		t.Errorf("Expected error: 'Required header not set'")
	}

	_, err = BuildHeaders(map[string]interface{}{"Number": 4})
	if err == nil {
		t.Errorf("Expected error: 'Options type is not a struct'")
	}
}
Ejemplo n.º 4
0
func TestGetHomeDocument(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	os.HandleGetSuccessfully(t)

	actual, err := Get(fake.ServiceClient()).Extract()
	th.CheckNoErr(t, err)

	expected := os.HomeDocument{
		"rel/cdn": map[string]interface{}{
			"href-template": "services{?marker,limit}",
			"href-vars": map[string]interface{}{
				"marker": "param/marker",
				"limit":  "param/limit",
			},
			"hints": map[string]interface{}{
				"allow": []string{"GET"},
				"formats": map[string]interface{}{
					"application/json": map[string]interface{}{},
				},
			},
		},
	}
	th.CheckDeepEquals(t, expected, *actual)
}
Ejemplo n.º 5
0
func TestProjectCRUDOperations(t *testing.T) {
	serviceClient := createAuthenticatedClient(t)
	if serviceClient == nil {
		return
	}

	// Create project
	opts := projects.ProjectOpts{
		Enabled:     true,
		Name:        "Test project",
		Description: "This is test project",
	}
	project, err := projects.Create(serviceClient, opts).Extract()
	th.AssertNoErr(t, err)
	defer projects.Delete(serviceClient, project.ID)
	th.AssertEquals(t, project.Enabled, true)
	th.AssertEquals(t, project.Name, "Test project")
	th.AssertEquals(t, project.Description, "This is test project")

	// List projects
	pager := projects.List(serviceClient, projects.ListOpts{})
	err = pager.EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page ---")

		projectList, err := projects.ExtractProjects(page)
		th.AssertNoErr(t, err)

		for _, p := range projectList {
			t.Logf("Project: ID [%s] Name [%s] Is enabled? [%s]",
				p.ID, p.Name, p.Enabled)
		}

		return true, nil
	})
	th.CheckNoErr(t, err)
	projectID := project.ID

	// Get a project
	if projectID == "" {
		t.Fatalf("In order to retrieve a project, the ProjectID must be set")
	}
	project, err = projects.Get(serviceClient, projectID).Extract()
	th.AssertNoErr(t, err)
	th.AssertEquals(t, project.ID, projectID)
	th.AssertEquals(t, project.DomainID, "")
	th.AssertEquals(t, project.ParentID, "")
	th.AssertEquals(t, project.Enabled, true)
	th.AssertEquals(t, project.Name, "Test project")
	th.AssertEquals(t, project.Description, "This is test project")

	// Update project
	project, err = projects.Update(serviceClient, projectID, projects.ProjectOpts{Name: "New test project name"}).Extract()
	th.AssertNoErr(t, err)
	th.AssertEquals(t, project.Name, "New test project name")

	// Delete project
	res := projects.Delete(serviceClient, projectID)
	th.AssertNoErr(t, res.Err)
}
Ejemplo n.º 6
0
func TestDeleteContainers(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	os.HandleDeleteContainerSuccessfully(t)

	res := Delete(fake.ServiceClient(), "testContainer")
	th.CheckNoErr(t, res.Err)
}
Ejemplo n.º 7
0
func TestPing(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	os.HandlePingSuccessfully(t)

	err := Ping(fake.ServiceClient()).ExtractErr()
	th.CheckNoErr(t, err)
}
Ejemplo n.º 8
0
func TestGetContainers(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	os.HandleGetContainerSuccessfully(t)

	_, err := Get(fake.ServiceClient(), "testContainer").ExtractMetadata()
	th.CheckNoErr(t, err)
}
Ejemplo n.º 9
0
func TestGetAccounts(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	os.HandleGetAccountSuccessfully(t)

	options := &UpdateOpts{Metadata: map[string]string{"gophercloud-test": "accounts"}}
	res := Update(fake.ServiceClient(), options)
	th.CheckNoErr(t, res.Err)
}
Ejemplo n.º 10
0
func TestUpdateContainers(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	os.HandleUpdateContainerSuccessfully(t)

	options := &os.UpdateOpts{Metadata: map[string]string{"foo": "bar"}}
	res := Update(fake.ServiceClient(), "testContainer", options)
	th.CheckNoErr(t, res.Err)
}
Ejemplo n.º 11
0
func TestCreateContainer(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	HandleCreateContainerSuccessfully(t)

	options := CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}}
	res := Create(fake.ServiceClient(), "testContainer", options)
	th.CheckNoErr(t, res.Err)
	th.CheckEquals(t, "bar", res.Header["X-Container-Meta-Foo"][0])
}
Ejemplo n.º 12
0
func TestUpdateAccounts(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	os.HandleUpdateAccountSuccessfully(t)

	expected := map[string]string{"Foo": "bar"}
	actual, err := Get(fake.ServiceClient()).ExtractMetadata()
	th.CheckNoErr(t, err)
	th.CheckDeepEquals(t, expected, actual)
}
Ejemplo n.º 13
0
func TestUpdateCDNContainer(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	os.HandleUpdateContainerSuccessfully(t)

	options := &UpdateOpts{TTL: 3600}
	res := Update(fake.ServiceClient(), "testContainer", options)
	th.CheckNoErr(t, res.Err)

}
Ejemplo n.º 14
0
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)
}
Ejemplo n.º 15
0
func TestEnumerateSinglePaged(t *testing.T) {
	callCount := 0
	pager := setupSinglePaged()
	defer testhelper.TeardownHTTP()

	err := pager.EachPage(func(page Page) (bool, error) {
		callCount++

		expected := []int{1, 2, 3}
		actual, err := ExtractSingleInts(page)
		testhelper.AssertNoErr(t, err)
		testhelper.CheckDeepEquals(t, expected, actual)
		return true, nil
	})
	testhelper.CheckNoErr(t, err)
	testhelper.CheckEquals(t, 1, callCount)
}
Ejemplo n.º 16
0
func TestListAPIVersions(t *testing.T) {
	Setup(t)
	defer Teardown()

	pager := apiversions.ListVersions(Client)
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page ---")

		versions, err := apiversions.ExtractAPIVersions(page)
		th.AssertNoErr(t, err)

		for _, v := range versions {
			t.Logf("API Version: ID [%s] Status [%s]", v.ID, v.Status)
		}

		return true, nil
	})
	th.CheckNoErr(t, err)
}
Ejemplo n.º 17
0
func TestListAPIResources(t *testing.T) {
	Setup(t)
	defer Teardown()

	pager := apiversions.ListVersionResources(Client, "v2.0")
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page ---")

		vrs, err := apiversions.ExtractVersionResources(page)
		th.AssertNoErr(t, err)

		for _, vr := range vrs {
			t.Logf("Network: Name [%s] Collection [%s]", vr.Name, vr.Collection)
		}

		return true, nil
	})
	th.CheckNoErr(t, err)
}
Ejemplo n.º 18
0
func TestListExts(t *testing.T) {
	Setup(t)
	defer Teardown()

	pager := extensions.List(Client)
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page ---")

		exts, err := extensions.ExtractExtensions(page)
		th.AssertNoErr(t, err)

		for _, ext := range exts {
			t.Logf("Extension: Name [%s] Description [%s]", ext.Name, ext.Description)
		}

		return true, nil
	})
	th.CheckNoErr(t, err)
}
Ejemplo n.º 19
0
func TestSubnetList(t *testing.T) {
	Setup(t)
	defer Teardown()

	pager := subnets.List(Client, subnets.ListOpts{Limit: 2})
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page ---")

		subnetList, err := subnets.ExtractSubnets(page)
		th.AssertNoErr(t, err)

		for _, s := range subnetList {
			t.Logf("Subnet: ID [%s] Name [%s] IP Version [%d] CIDR [%s] GatewayIP [%s]",
				s.ID, s.Name, s.IPVersion, s.CIDR, s.GatewayIP)
		}

		return true, nil
	})
	th.CheckNoErr(t, err)
}
Ejemplo n.º 20
0
func TestNetworks(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	// Create a network
	n, err := networks.Create(client, networks.CreateOpts{Label: "sample_network", CIDR: "172.20.0.0/24"}).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created network: %+v\n", n)
	defer networks.Delete(client, n.ID)
	th.AssertEquals(t, n.Label, "sample_network")
	th.AssertEquals(t, n.CIDR, "172.20.0.0/24")
	networkID := n.ID

	// List networks
	pager := networks.List(client)
	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] Label [%s] CIDR [%s]",
				n.ID, n.Label, n.CIDR)
		}

		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()
	t.Logf("Retrieved Network: %+v\n", n)
	th.AssertNoErr(t, err)
	th.AssertEquals(t, n.CIDR, "172.20.0.0/24")
	th.AssertEquals(t, n.Label, "sample_network")
	th.AssertEquals(t, n.ID, networkID)
}
Ejemplo n.º 21
0
func TestVirtualInterfaces(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	// Create a server
	server := createServer(t, client, "")
	t.Logf("Created Server: %v\n", server)
	defer deleteServer(t, client, server)
	serverID := server.ID

	// Create a network
	n, err := networks.Create(client, networks.CreateOpts{Label: "sample_network", CIDR: "172.20.0.0/24"}).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created Network: %v\n", n)
	defer networks.Delete(client, n.ID)
	networkID := n.ID

	// Create a virtual interface
	vi, err := virtualinterfaces.Create(client, serverID, networkID).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created virtual interface: %+v\n", vi)
	defer virtualinterfaces.Delete(client, serverID, vi.ID)

	// List virtual interfaces
	pager := virtualinterfaces.List(client, serverID)
	err = pager.EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page ---")

		virtualinterfacesList, err := virtualinterfaces.ExtractVirtualInterfaces(page)
		th.AssertNoErr(t, err)

		for _, vi := range virtualinterfacesList {
			t.Logf("Virtual Interface: ID [%s] MAC Address [%s] IP Addresses [%v]",
				vi.ID, vi.MACAddress, vi.IPAddresses)
		}

		return true, nil
	})
	th.CheckNoErr(t, err)
}
Ejemplo n.º 22
0
func listPorts(t *testing.T) {
	count := 0
	pager := ports.List(base.Client, ports.ListOpts{})
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		count++
		t.Logf("--- Page ---")

		portList, err := portsbinding.ExtractPorts(page)
		th.AssertNoErr(t, err)

		for _, p := range portList {
			t.Logf("Port: ID [%s] Name [%s] HostID [%s] VNICType [%s] VIFType [%s]",
				p.ID, p.Name, p.HostID, p.VNICType, p.VIFType)
		}

		return true, nil
	})

	th.CheckNoErr(t, err)

	if count == 0 {
		t.Logf("No pages were iterated over when listing ports")
	}
}
Ejemplo n.º 23
0
func listPorts(t *testing.T) {
	count := 0
	pager := ports.List(Client, ports.ListOpts{})
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		count++
		t.Logf("--- Page ---")

		portList, err := ports.ExtractPorts(page)
		th.AssertNoErr(t, err)

		for _, p := range portList {
			t.Logf("Port: ID [%s] Name [%s] Status [%d] MAC addr [%s] Fixed IPs [%#v] Security groups [%#v]",
				p.ID, p.Name, p.Status, p.MACAddress, p.FixedIPs, p.SecurityGroups)
		}

		return true, nil
	})

	th.CheckNoErr(t, err)

	if count == 0 {
		t.Logf("No pages were iterated over when listing ports")
	}
}
Ejemplo n.º 24
0
func TestWaitFor(t *testing.T) {
	err := WaitFor(5, func() (bool, error) {
		return true, nil
	})
	th.CheckNoErr(t, err)
}