Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/testhelper" |
| 9 | ) |
| 10 | |
| 11 | const ( |
| 12 | tokenId = "abcabcabcabc" |
| 13 | ) |
| 14 | |
| 15 | var metadata = map[string]string{"gophercloud-test": "containers"} |
| 16 | |
| 17 | func serviceClient() *gophercloud.ServiceClient { |
| 18 | return &gophercloud.ServiceClient{ |
| 19 | Provider: &gophercloud.ProviderClient{TokenID: tokenId}, |
| 20 | Endpoint: testhelper.Endpoint(), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestListContainerInfo(t *testing.T) { |
| 25 | testhelper.SetupHTTP() |
| 26 | defer testhelper.TeardownHTTP() |
| 27 | |
| 28 | testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 29 | testhelper.TestMethod(t, r, "GET") |
| 30 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 31 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 32 | }) |
| 33 | |
| 34 | client := serviceClient() |
| 35 | _, err := List(client, ListOpts{Full: true}) |
| 36 | if err != nil { |
| 37 | t.Fatalf("Unexpected error listing containers info: %v", err) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestListContainerNames(t *testing.T) { |
| 42 | testhelper.SetupHTTP() |
| 43 | defer testhelper.TeardownHTTP() |
| 44 | |
| 45 | testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 46 | testhelper.TestMethod(t, r, "GET") |
| 47 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 48 | testhelper.TestHeader(t, r, "Accept", "text/plain") |
| 49 | }) |
| 50 | |
| 51 | client := serviceClient() |
| 52 | _, err := List(client, ListOpts{}) |
| 53 | if err != nil { |
| 54 | t.Fatalf("Unexpected error listing containers info: %v", err) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestCreateContainer(t *testing.T) { |
| 59 | testhelper.SetupHTTP() |
| 60 | defer testhelper.TeardownHTTP() |
| 61 | |
| 62 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 63 | testhelper.TestMethod(t, r, "PUT") |
| 64 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 65 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 66 | w.WriteHeader(http.StatusNoContent) |
| 67 | }) |
| 68 | |
| 69 | client := serviceClient() |
| 70 | _, err := Create(client, CreateOpts{ |
| 71 | Name: "testContainer", |
| 72 | }) |
| 73 | if err != nil { |
| 74 | t.Fatalf("Unexpected error creating container: %v", err) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestDeleteContainer(t *testing.T) { |
| 79 | testhelper.SetupHTTP() |
| 80 | defer testhelper.TeardownHTTP() |
| 81 | |
| 82 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 83 | testhelper.TestMethod(t, r, "DELETE") |
| 84 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 85 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 86 | w.WriteHeader(http.StatusNoContent) |
| 87 | }) |
| 88 | |
| 89 | client := serviceClient() |
| 90 | err := Delete(client, DeleteOpts{ |
| 91 | Name: "testContainer", |
| 92 | }) |
| 93 | if err != nil { |
| 94 | t.Fatalf("Unexpected error deleting container: %v", err) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestUpateContainer(t *testing.T) { |
| 99 | testhelper.SetupHTTP() |
| 100 | defer testhelper.TeardownHTTP() |
| 101 | |
| 102 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 103 | testhelper.TestMethod(t, r, "POST") |
| 104 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 105 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 106 | w.WriteHeader(http.StatusNoContent) |
| 107 | }) |
| 108 | |
| 109 | client := serviceClient() |
| 110 | err := Update(client, UpdateOpts{ |
| 111 | Name: "testContainer", |
| 112 | }) |
| 113 | if err != nil { |
| 114 | t.Fatalf("Unexpected error updating container metadata: %v", err) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestGetContainer(t *testing.T) { |
| 119 | testhelper.SetupHTTP() |
| 120 | defer testhelper.TeardownHTTP() |
| 121 | |
| 122 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 123 | testhelper.TestMethod(t, r, "HEAD") |
| 124 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 125 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 126 | w.WriteHeader(http.StatusNoContent) |
| 127 | }) |
| 128 | |
| 129 | client := serviceClient() |
| 130 | _, err := Get(client, GetOpts{ |
| 131 | Name: "testContainer", |
| 132 | }) |
| 133 | if err != nil { |
| 134 | t.Fatalf("Unexpected error getting container metadata: %v", err) |
| 135 | } |
| 136 | } |