예제 #1
0
func TestAccounts(t *testing.T) {
	c, err := createClient(t, false)
	th.AssertNoErr(t, err)

	updateHeaders, err := raxAccounts.Update(c, raxAccounts.UpdateOpts{Metadata: map[string]string{"white": "mountains"}}).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Update Account Response Headers: %+v\n", updateHeaders)
	defer func() {
		updateres := raxAccounts.Update(c, raxAccounts.UpdateOpts{Metadata: map[string]string{"white": ""}})
		th.AssertNoErr(t, updateres.Err)
		metadata, err := raxAccounts.Get(c).ExtractMetadata()
		th.AssertNoErr(t, err)
		t.Logf("Metadata from Get Account request (after update reverted): %+v\n", metadata)
		th.CheckEquals(t, metadata["White"], "")
	}()

	getResp := raxAccounts.Get(c)
	th.AssertNoErr(t, getResp.Err)

	getHeaders, _ := getResp.Extract()
	t.Logf("Get Account Response Headers: %+v\n", getHeaders)

	metadata, _ := getResp.ExtractMetadata()
	t.Logf("Metadata from Get Account request (after update): %+v\n", metadata)

	th.CheckEquals(t, metadata["White"], "mountains")
}
예제 #2
0
func TestGetExtension(t *testing.T) {
	service := authenticatedClient(t)

	ext, err := extensions2.Get(service, "OS-KSCRUD").Extract()
	th.AssertNoErr(t, err)

	th.CheckEquals(t, "OpenStack Keystone User CRUD", ext.Name)
	th.CheckEquals(t, "http://docs.openstack.org/identity/api/ext/OS-KSCRUD/v1.0", ext.Namespace)
	th.CheckEquals(t, "OS-KSCRUD", ext.Alias)
	th.CheckEquals(t, "OpenStack extensions to Keystone v2.0 API enabling User Operations.", ext.Description)
}
예제 #3
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)
	c, err := res.Extract()
	th.CheckNoErr(t, err)
	th.CheckEquals(t, "bar", res.Header["X-Container-Meta-Foo"][0])
	th.CheckEquals(t, "1234567", c.TransID)
}
예제 #4
0
func TestListURL(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	c := client.ServiceClient()

	th.CheckEquals(t, c.Endpoint+"os-keypairs", listURL(c))
}
예제 #5
0
func TestList(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	th.Mux.HandleFunc("/os-networksv2", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "GET")
		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

		w.Header().Add("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)

		fmt.Fprintf(w, `
{
    "networks": [
        {
            "label": "test-network-1",
            "cidr": "192.168.100.0/24",
            "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
        },
        {
            "label": "test-network-2",
            "cidr": "192.30.250.00/18",
            "id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324"
        }
    ]
}
      `)
	})

	client := fake.ServiceClient()
	count := 0

	err := List(client).EachPage(func(page pagination.Page) (bool, error) {
		count++
		actual, err := ExtractNetworks(page)
		if err != nil {
			t.Errorf("Failed to extract networks: %v", err)
			return false, err
		}

		expected := []Network{
			Network{
				Label: "test-network-1",
				CIDR:  "192.168.100.0/24",
				ID:    "d32019d3-bc6e-4319-9c1d-6722fc136a22",
			},
			Network{
				Label: "test-network-2",
				CIDR:  "192.30.250.00/18",
				ID:    "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
			},
		}

		th.CheckDeepEquals(t, expected, actual)

		return true, nil
	})
	th.AssertNoErr(t, err)
	th.CheckEquals(t, 1, count)
}
예제 #6
0
func TestAuthenticatedClientV2(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	th.Mux.HandleFunc("/v2.0/tokens", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, `
      {
        "access": {
          "token": {
            "id": "01234567890",
            "expires": "2014-10-01T10:00:00.000000Z"
          },
          "serviceCatalog": []
        }
      }
    `)
	})

	options := gophercloud.AuthOptions{
		Username:         "******",
		APIKey:           "09876543210",
		IdentityEndpoint: th.Endpoint() + "v2.0/",
	}
	client, err := AuthenticatedClient(options)
	th.AssertNoErr(t, err)
	th.CheckEquals(t, "01234567890", client.TokenID)
}
func (s *CinderV2Suite) TestGetVolumes() {
	Convey("Given Cinder volumes are requested", s.T(), func() {

		Convey("When authentication is required", func() {
			provider, err := openstackintel.Authenticate(th.Endpoint(), "me", "secret", "tenant")
			th.AssertNoErr(s.T(), err)
			th.CheckEquals(s.T(), s.Token, provider.TokenID)

			Convey("and GetVolumes called", func() {
				dispatch := ServiceV2{}
				volumes, err := dispatch.GetVolumes(provider)

				Convey("Then proper limits values are returned", func() {
					So(len(volumes), ShouldEqual, 2)
					So(volumes[s.Tenant1ID].Bytes, ShouldEqual, s.Vol1Size*1024*1024*1024)
					So(volumes[s.Tenant2ID].Bytes, ShouldEqual, s.Vol2Size*1024*1024*1024)
					So(volumes[s.Tenant1ID].Count, ShouldEqual, 1)
					So(volumes[s.Tenant2ID].Count, ShouldEqual, 1)
				})

				Convey("and no error reported", func() {
					So(err, ShouldBeNil)
				})
			})
		})
	})
}
func TestV3EndpointNoService(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "GET")
		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

		w.Header().Add("Content-Type", "application/json")
		fmt.Fprintf(w, `
      {
        "links": {
          "next": null,
          "previous": null
        },
        "services": []
      }
    `)
	})

	_, err := V3EndpointURL(fake.ServiceClient(), gophercloud.EndpointOpts{
		Type:         "nope",
		Name:         "same",
		Region:       "same",
		Availability: gophercloud.AvailabilityPublic,
	})
	th.CheckEquals(t, gophercloud.ErrServiceNotFound, err)
}
예제 #9
0
func TestV2EndpointNone(t *testing.T) {
	_, err := V2EndpointURL(&catalog2, gophercloud.EndpointOpts{
		Type:         "nope",
		Availability: gophercloud.AvailabilityPublic,
	})
	th.CheckEquals(t, gophercloud.ErrEndpointNotFound, err)
}
예제 #10
0
func TestListImageDetails(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	th.Mux.HandleFunc("/images/detail", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "GET")
		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)

		w.Header().Add("Content-Type", "application/json")
		r.ParseForm()
		marker := r.Form.Get("marker")
		switch marker {
		case "":
			fmt.Fprintf(w, ListOutput)
		case "e19a734c-c7e6-443a-830c-242209c4d65d":
			fmt.Fprintf(w, `{ "images": [] }`)
		default:
			t.Fatalf("Unexpected marker: [%s]", marker)
		}
	})

	count := 0
	err := ListDetail(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
		count++
		actual, err := ExtractImages(page)
		th.AssertNoErr(t, err)
		th.CheckDeepEquals(t, ExpectedImageSlice, actual)

		return true, nil
	})
	th.AssertNoErr(t, err)
	th.CheckEquals(t, 1, count)
}
예제 #11
0
func TestCreateURL(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	c := client.ServiceClient()

	th.CheckEquals(t, c.Endpoint+"os-floating-ips", createURL(c))
}
예제 #12
0
func TestGetURL(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	c := client.ServiceClient()

	th.CheckEquals(t, c.Endpoint+"os-quota-sets/wat", getURL(c, "wat"))
}
예제 #13
0
func TestDeleteURL(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	c := client.ServiceClient()

	th.CheckEquals(t, c.Endpoint+"os-keypairs/wat", deleteURL(c, "wat"))
}
예제 #14
0
func TestKeystoneLogin(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	const ID = "0123456789"

	th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Add("X-Subject-Token", ID)
		type AuthRequest struct {
			Auth struct {
				Identity struct {
					Password struct {
						User struct {
							Domain   struct{ Name string }
							Name     string
							Password string
						}
					}
				}
			}
		}
		var x AuthRequest
		body, _ := ioutil.ReadAll(r.Body)
		json.Unmarshal(body, &x)
		domainName := x.Auth.Identity.Password.User.Domain.Name
		userName := x.Auth.Identity.Password.User.Name
		password := x.Auth.Identity.Password.User.Password
		if domainName == "default" && userName == "testuser" && password == "testpw" {
			w.WriteHeader(http.StatusCreated)
			fmt.Fprintf(w, `{ "token": { "expires_at": "2020-02-02T18:30:59.000000Z" } }`)
		} else {
			w.WriteHeader(http.StatusUnauthorized)
		}
	})

	keystoneAuth := New("keystone_auth", th.Endpoint(), http.DefaultTransport, "default", &TestUserIdentityMapper{})
	_, ok, err := keystoneAuth.AuthenticatePassword("testuser", "testpw")
	th.AssertNoErr(t, err)
	th.CheckEquals(t, ok, true)
	_, ok, err = keystoneAuth.AuthenticatePassword("testuser", "badpw")
	th.AssertNoErr(t, err)
	th.CheckEquals(t, ok, false)
	_, ok, err = keystoneAuth.AuthenticatePassword("testuser", "")
	th.AssertNoErr(t, err)
	th.CheckEquals(t, ok, false)
}
예제 #15
0
func TestGetURL(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	c := client.ServiceClient()
	id := "1"

	th.CheckEquals(t, c.Endpoint+"os-tenant-networks/"+id, getURL(c, id))
}
예제 #16
0
func TestDisassociateURL(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	c := client.ServiceClient()
	serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"

	th.CheckEquals(t, c.Endpoint+"servers/"+serverId+"/action", disassociateURL(c, serverId))
}
예제 #17
0
func TestDeleteURL(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	c := client.ServiceClient()
	id := "1"

	th.CheckEquals(t, c.Endpoint+"os-floating-ips/"+id, deleteURL(c, id))
}
예제 #18
0
func TestListURL(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	c := client.ServiceClient()
	serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"

	th.CheckEquals(t, c.Endpoint+"servers/"+serverId+"/os-volume_attachments", listURL(c, serverId))
}
예제 #19
0
func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	HandleTokenPost(t, "")

	actualErr := Create(client.ServiceClient(), AuthOptions{options}).Err
	th.CheckEquals(t, expectedErr, actualErr)
}
예제 #20
0
func TestExtractList(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	servers.HandleServerListSuccessfully(t)

	pages := 0
	err := servers.List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
		pages++

		config, err := ExtractDiskConfig(page, 0)
		th.AssertNoErr(t, err)
		th.CheckEquals(t, Manual, *config)

		return true, nil
	})
	th.AssertNoErr(t, err)
	th.CheckEquals(t, pages, 1)
}
예제 #21
0
func TestDownloadObject(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	os.HandleDownloadObjectSuccessfully(t)

	content, err := Download(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractContent()
	th.AssertNoErr(t, err)
	th.CheckEquals(t, string(content), "Successful download with Gophercloud")
}
func TestUserAgent(t *testing.T) {
	p := &ProviderClient{}

	p.UserAgent.Prepend("custom-user-agent/v2.4")
	expected := "custom-user-agent/v2.4 gophercloud/v1.0"
	actual := p.UserAgent.Join()
	th.CheckEquals(t, expected, actual)

	p.UserAgent.Prepend("another-custom-user-agent/v0.3", "a-third-ua/v5.9")
	expected = "another-custom-user-agent/v0.3 a-third-ua/v5.9 custom-user-agent/v2.4 gophercloud/v1.0"
	actual = p.UserAgent.Join()
	th.CheckEquals(t, expected, actual)

	p.UserAgent = UserAgent{}
	expected = "gophercloud/v1.0"
	actual = p.UserAgent.Join()
	th.CheckEquals(t, expected, actual)
}
예제 #23
0
func TestExtractGet(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	servers.HandleServerGetSuccessfully(t)

	config, err := ExtractGet(servers.Get(client.ServiceClient(), "1234asdf"))
	th.AssertNoErr(t, err)
	th.CheckEquals(t, Manual, *config)
}
예제 #24
0
func TestUserAgent(t *testing.T) {
	p := &ProviderClient{}

	p.UserAgent.Prepend("custom-user-agent/2.4.0")
	expected := "custom-user-agent/2.4.0 gophercloud/1.0.0"
	actual := p.UserAgent.Join()
	th.CheckEquals(t, expected, actual)

	p.UserAgent.Prepend("another-custom-user-agent/0.3.0", "a-third-ua/5.9.0")
	expected = "another-custom-user-agent/0.3.0 a-third-ua/5.9.0 custom-user-agent/2.4.0 gophercloud/1.0.0"
	actual = p.UserAgent.Join()
	th.CheckEquals(t, expected, actual)

	p.UserAgent = UserAgent{}
	expected = "gophercloud/1.0.0"
	actual = p.UserAgent.Join()
	th.CheckEquals(t, expected, actual)
}
예제 #25
0
func TestDeleteURL(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	c := client.ServiceClient()
	serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
	aId := "a26887c6-c47b-4654-abb5-dfadf7d3f804"

	th.CheckEquals(t, c.Endpoint+"servers/"+serverId+"/os-volume_attachments/"+aId, deleteURL(c, serverId, aId))
}
예제 #26
0
func TestV3EndpointBadAvailability(t *testing.T) {
	_, err := V3EndpointURL(&catalog3, gophercloud.EndpointOpts{
		Type:         "same",
		Name:         "same",
		Region:       "same",
		Availability: "wat",
	})
	th.CheckEquals(t, "Unexpected availability in endpoint query: wat", err.Error())
}
예제 #27
0
func TestEnableCDNContainer(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "PUT")
		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
		th.TestHeader(t, r, "Accept", "application/json")

		w.Header().Add("X-Ttl", "259200")
		w.Header().Add("X-Cdn-Enabled", "True")
		w.WriteHeader(http.StatusNoContent)
	})

	options := &EnableOpts{CDNEnabled: true, TTL: 259200}
	actual := Enable(fake.ServiceClient(), "testContainer", options)
	th.AssertNoErr(t, actual.Err)
	th.CheckEquals(t, actual.Header["X-Ttl"][0], "259200")
	th.CheckEquals(t, actual.Header["X-Cdn-Enabled"][0], "True")
}
예제 #28
0
func TestNormalizePathURL(t *testing.T) {
	baseDir, _ := os.Getwd()

	rawPath := "template.yaml"
	basePath, _ := filepath.Abs(".")
	result, _ := NormalizePathURL(basePath, rawPath)
	expected := strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "template.yaml"}, "/")
	th.CheckEquals(t, expected, result)

	rawPath = "http://www.google.com"
	basePath, _ = filepath.Abs(".")
	result, _ = NormalizePathURL(basePath, rawPath)
	expected = "http://www.google.com"
	th.CheckEquals(t, expected, result)

	rawPath = "very/nested/file.yaml"
	basePath, _ = filepath.Abs(".")
	result, _ = NormalizePathURL(basePath, rawPath)
	expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "very/nested/file.yaml"}, "/")
	th.CheckEquals(t, expected, result)

	rawPath = "very/nested/file.yaml"
	basePath = "http://www.google.com"
	result, _ = NormalizePathURL(basePath, rawPath)
	expected = "http://www.google.com/very/nested/file.yaml"
	th.CheckEquals(t, expected, result)

	rawPath = "very/nested/file.yaml/"
	basePath = "http://www.google.com/"
	result, _ = NormalizePathURL(basePath, rawPath)
	expected = "http://www.google.com/very/nested/file.yaml"
	th.CheckEquals(t, expected, result)

	rawPath = "very/nested/file.yaml"
	basePath = "http://www.google.com/even/more"
	result, _ = NormalizePathURL(basePath, rawPath)
	expected = "http://www.google.com/even/more/very/nested/file.yaml"
	th.CheckEquals(t, expected, result)

	rawPath = "very/nested/file.yaml"
	basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/")
	result, _ = NormalizePathURL(basePath, rawPath)
	expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/")
	th.CheckEquals(t, expected, result)

	rawPath = "very/nested/file.yaml/"
	basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/")
	result, _ = NormalizePathURL(basePath, rawPath)
	expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/")
	th.CheckEquals(t, expected, result)

}
예제 #29
0
func TestUpdate(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	os.MockUpdateResponse(t)

	options := &UpdateOpts{Name: "vol-002"}
	v, err := Update(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
	th.AssertNoErr(t, err)
	th.CheckEquals(t, "vol-002", v.Name)
}
예제 #30
0
func TestDownloadExtraction(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	HandleDownloadObjectSuccessfully(t)

	response := Download(fake.ServiceClient(), "testContainer", "testObject", nil)

	// Check []byte extraction
	bytes, err := response.ExtractContent()
	th.AssertNoErr(t, err)
	th.CheckEquals(t, "Successful download with Gophercloud", string(bytes))
}