Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
| 3 | package v1 |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/rackspace/gophercloud/acceptance/tools" |
Jon Perritt | a9caabd | 2014-09-30 13:02:34 -0500 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/openstack/objectstorage/v1/containers" |
| 12 | "github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects" |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/pagination" |
| 14 | ) |
| 15 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 16 | // numObjects is the number of objects to create for testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 17 | var numObjects = 2 |
| 18 | |
| 19 | func TestObjects(t *testing.T) { |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 20 | // Create a provider client for executing the HTTP request. |
| 21 | // See common.go for more information. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 22 | client, err := newClient() |
| 23 | if err != nil { |
| 24 | t.Error(err) |
| 25 | return |
| 26 | } |
| 27 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 28 | // Make a slice of length numObjects to hold the random object names. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 29 | oNames := make([]string, numObjects) |
| 30 | for i := 0; i < len(oNames); i++ { |
| 31 | oNames[i] = tools.RandomString("test-object-", 8) |
| 32 | } |
| 33 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 34 | // Create a container to hold the test objects. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 35 | cName := tools.RandomString("test-container-", 8) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame^] | 36 | _, err = containers.Create(client, cName, nil).ExtractHeaders() |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 37 | if err != nil { |
| 38 | t.Error(err) |
| 39 | return |
| 40 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 41 | // Defer deletion of the container until after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 42 | defer func() { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame^] | 43 | _, err = containers.Delete(client, cName).ExtractHeaders() |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 44 | if err != nil { |
| 45 | t.Error(err) |
| 46 | return |
| 47 | } |
| 48 | }() |
| 49 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 50 | // Create a slice of buffers to hold the test object content. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 51 | oContents := make([]*bytes.Buffer, numObjects) |
| 52 | for i := 0; i < numObjects; i++ { |
| 53 | oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10))) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame^] | 54 | _, err = objects.Create(client, cName, oNames[i], oContents[i], nil).ExtractHeaders() |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 55 | if err != nil { |
| 56 | t.Error(err) |
| 57 | return |
| 58 | } |
| 59 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 60 | // Delete the objects after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 61 | defer func() { |
| 62 | for i := 0; i < numObjects; i++ { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame^] | 63 | _, err = objects.Delete(client, cName, oNames[i], nil).ExtractHeaders() |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 64 | } |
| 65 | }() |
| 66 | |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 67 | ons := make([]string, 0, len(oNames)) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 68 | err = objects.List(client, cName, &objects.ListOpts{Full: false, Prefix: "test-object-"}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 69 | names, err := objects.ExtractNames(page) |
| 70 | if err != nil { |
| 71 | return false, err |
| 72 | } |
| 73 | ons = append(ons, names...) |
| 74 | |
| 75 | return true, nil |
| 76 | }) |
| 77 | if err != nil { |
| 78 | t.Error(err) |
| 79 | return |
| 80 | } |
| 81 | if len(ons) != len(oNames) { |
| 82 | t.Errorf("Expected %d names and got %d", len(oNames), len(ons)) |
| 83 | return |
| 84 | } |
| 85 | |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 86 | ois := make([]objects.Object, 0, len(oNames)) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 87 | err = objects.List(client, cName, &objects.ListOpts{Full: true, Prefix: "test-object-"}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 88 | info, err := objects.ExtractInfo(page) |
| 89 | if err != nil { |
| 90 | return false, nil |
| 91 | } |
| 92 | |
| 93 | ois = append(ois, info...) |
| 94 | |
| 95 | return true, nil |
| 96 | }) |
| 97 | if err != nil { |
| 98 | t.Error(err) |
| 99 | return |
| 100 | } |
| 101 | if len(ois) != len(oNames) { |
| 102 | t.Errorf("Expected %d containers and got %d", len(oNames), len(ois)) |
| 103 | return |
| 104 | } |
| 105 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 106 | // Copy the contents of one object to another. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame^] | 107 | _, err = objects.Copy(client, cName, oNames[0], &objects.CopyOpts{Destination: cName + "/" + oNames[1]}).ExtractHeaders() |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 108 | if err != nil { |
| 109 | t.Error(err) |
| 110 | return |
| 111 | } |
| 112 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 113 | // Download one of the objects that was created above. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 114 | o1Content, err := objects.Download(client, cName, oNames[0], nil).ExtractContent() |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 115 | if err != nil { |
| 116 | t.Error(err) |
| 117 | return |
| 118 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 119 | // Download the another object that was create above. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 120 | o2Content, err := objects.Download(client, cName, oNames[1], nil).ExtractContent() |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 121 | if err != nil { |
| 122 | t.Error(err) |
| 123 | return |
| 124 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 125 | // Compare the two object's contents to test that the copy worked. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 126 | if string(o2Content) != string(o1Content) { |
| 127 | t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content)) |
| 128 | return |
| 129 | } |
| 130 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 131 | // Update an object's metadata. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame^] | 132 | _, err = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: metadata}).ExtractHeaders() |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 133 | if err != nil { |
| 134 | t.Error(err) |
| 135 | return |
| 136 | } |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 137 | // Delete the object's metadata after testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 138 | defer func() { |
| 139 | tempMap := make(map[string]string) |
| 140 | for k := range metadata { |
| 141 | tempMap[k] = "" |
| 142 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame^] | 143 | _, err = objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap}).ExtractHeaders() |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 144 | if err != nil { |
| 145 | t.Error(err) |
| 146 | return |
| 147 | } |
| 148 | }() |
| 149 | |
Jon Perritt | b1eea51 | 2014-09-15 23:27:33 -0500 | [diff] [blame] | 150 | // Retrieve an object's metadata. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 151 | om, err := objects.Get(client, cName, oNames[0], nil).ExtractMetadata() |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 152 | if err != nil { |
| 153 | t.Error(err) |
| 154 | return |
| 155 | } |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 156 | for k := range metadata { |
| 157 | if om[k] != metadata[strings.Title(k)] { |
| 158 | t.Errorf("Expected custom metadata with key: %s", k) |
| 159 | return |
| 160 | } |
| 161 | } |
| 162 | } |