|
| 1 | +// +build acceptance restclient |
| 2 | + |
| 3 | +package restclient |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + acc_clients "github.com/gophercloud/gophercloud/acceptance/clients" |
| 9 | + acc_tools "github.com/gophercloud/gophercloud/acceptance/tools" |
| 10 | + |
| 11 | + th "github.com/gophercloud/gophercloud/testhelper" |
| 12 | + cc "github.com/gophercloud/utils/openstack/clientconfig" |
| 13 | + "github.com/gophercloud/utils/openstack/restclient" |
| 14 | +) |
| 15 | + |
| 16 | +func TestRESTClient(t *testing.T) { |
| 17 | + acc_clients.RequireAdmin(t) |
| 18 | + |
| 19 | + // This will be populated by environment variables. |
| 20 | + clientOpts := &cc.ClientOpts{} |
| 21 | + |
| 22 | + computeClient, err := cc.NewServiceClient("compute", clientOpts) |
| 23 | + th.AssertNoErr(t, err) |
| 24 | + |
| 25 | + // Test creating a flavor |
| 26 | + flavorName := acc_tools.RandomString("TESTACC-", 8) |
| 27 | + flavorID := acc_tools.RandomString("TESTACC-", 8) |
| 28 | + flavorOpts := map[string]interface{}{ |
| 29 | + "name": flavorName, |
| 30 | + "ram": 512, |
| 31 | + "vcpus": 1, |
| 32 | + "disk": 5, |
| 33 | + "id": flavorID, |
| 34 | + } |
| 35 | + |
| 36 | + postOpts := &restclient.PostOpts{ |
| 37 | + Params: map[string]interface{}{"flavor": flavorOpts}, |
| 38 | + } |
| 39 | + |
| 40 | + postURL := computeClient.ServiceURL("flavors") |
| 41 | + postRes := restclient.Post(computeClient, postURL, postOpts) |
| 42 | + th.AssertNoErr(t, postRes.Err) |
| 43 | + flavorResult, err := postRes.Extract() |
| 44 | + th.AssertNoErr(t, postRes.Err) |
| 45 | + acc_tools.PrintResource(t, flavorResult) |
| 46 | + |
| 47 | + // Test deleting a flavor |
| 48 | + defer func() { |
| 49 | + deleteURL := computeClient.ServiceURL("flavors", flavorID) |
| 50 | + deleteRes := restclient.Delete(computeClient, deleteURL, nil) |
| 51 | + th.AssertNoErr(t, deleteRes.Err) |
| 52 | + err = deleteRes.ExtractErr() |
| 53 | + th.AssertNoErr(t, err) |
| 54 | + }() |
| 55 | + |
| 56 | + // Test retrieving a flavor |
| 57 | + getURL := computeClient.ServiceURL("flavors", flavorID) |
| 58 | + getRes := restclient.Get(computeClient, getURL, nil) |
| 59 | + th.AssertNoErr(t, getRes.Err) |
| 60 | + |
| 61 | + flavorResult, err = getRes.Extract() |
| 62 | + th.AssertNoErr(t, err) |
| 63 | + |
| 64 | + flavor := flavorResult["flavor"].(map[string]interface{}) |
| 65 | + |
| 66 | + acc_tools.PrintResource(t, flavor) |
| 67 | + |
| 68 | + th.AssertEquals(t, flavor["disk"], float64(5)) |
| 69 | + th.AssertEquals(t, flavor["id"], flavorID) |
| 70 | + th.AssertEquals(t, flavor["name"], flavorName) |
| 71 | + th.AssertEquals(t, flavor["ram"], float64(512)) |
| 72 | + th.AssertEquals(t, flavor["swap"], "") |
| 73 | + th.AssertEquals(t, flavor["vcpus"], float64(1)) |
| 74 | + |
| 75 | + // Test listing flavors |
| 76 | + getOpts := &restclient.GetOpts{ |
| 77 | + Query: map[string]interface{}{ |
| 78 | + "limit": 2, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + getURL = computeClient.ServiceURL("flavors") |
| 83 | + getRes = restclient.Get(computeClient, getURL, getOpts) |
| 84 | + th.AssertNoErr(t, getRes.Err) |
| 85 | + flavorResult, err = getRes.Extract() |
| 86 | + th.AssertNoErr(t, err) |
| 87 | + |
| 88 | + flavors := flavorResult["flavors"].([]interface{}) |
| 89 | + acc_tools.PrintResource(t, flavors) |
| 90 | + th.AssertEquals(t, len(flavors), 2) |
| 91 | +} |
0 commit comments