Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 1 | // +build acceptance rackspace objectstorage v1 |
| 2 | |
Jon Perritt | 8abd2c2 | 2014-10-16 20:28:51 -0500 | [diff] [blame] | 3 | package v1 |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "testing" |
| 8 | |
| 9 | osObjects "github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects" |
| 10 | "github.com/rackspace/gophercloud/pagination" |
| 11 | raxContainers "github.com/rackspace/gophercloud/rackspace/objectstorage/v1/containers" |
| 12 | raxObjects "github.com/rackspace/gophercloud/rackspace/objectstorage/v1/objects" |
| 13 | th "github.com/rackspace/gophercloud/testhelper" |
| 14 | ) |
| 15 | |
| 16 | func TestObjects(t *testing.T) { |
| 17 | c, err := createClient(t, false) |
| 18 | th.AssertNoErr(t, err) |
| 19 | |
Jon Perritt | 7e70c40 | 2014-10-24 16:15:09 -0500 | [diff] [blame] | 20 | res := raxContainers.Create(c, "gophercloud-test", nil) |
| 21 | th.AssertNoErr(t, res.Err) |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 22 | |
| 23 | defer func() { |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 24 | t.Logf("Deleting container...") |
Jon Perritt | 7e70c40 | 2014-10-24 16:15:09 -0500 | [diff] [blame] | 25 | res := raxContainers.Delete(c, "gophercloud-test") |
| 26 | th.AssertNoErr(t, res.Err) |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 27 | }() |
| 28 | |
| 29 | content := bytes.NewBufferString("Lewis Carroll") |
| 30 | options := &osObjects.CreateOpts{ContentType: "text/plain"} |
Jon Perritt | 7e70c40 | 2014-10-24 16:15:09 -0500 | [diff] [blame] | 31 | createres := raxObjects.Create(c, "gophercloud-test", "o1", content, options) |
| 32 | th.AssertNoErr(t, createres.Err) |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 33 | |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 34 | defer func() { |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 35 | t.Logf("Deleting object o1...") |
Jon Perritt | 7e70c40 | 2014-10-24 16:15:09 -0500 | [diff] [blame] | 36 | res := raxObjects.Delete(c, "gophercloud-test", "o1", nil) |
| 37 | th.AssertNoErr(t, res.Err) |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 38 | }() |
| 39 | |
| 40 | t.Logf("Objects Info available to the currently issued token:") |
| 41 | count := 0 |
| 42 | err = raxObjects.List(c, "gophercloud-test", &osObjects.ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) { |
| 43 | t.Logf("--- Page %02d ---", count) |
| 44 | |
| 45 | objects, err := raxObjects.ExtractInfo(page) |
| 46 | th.AssertNoErr(t, err) |
| 47 | |
| 48 | for i, object := range objects { |
| 49 | t.Logf("[%02d] name=[%s]", i, object.Name) |
| 50 | t.Logf(" content-type=[%s]", object.ContentType) |
| 51 | t.Logf(" bytes=[%d]", object.Bytes) |
| 52 | t.Logf(" last-modified=[%s]", object.LastModified) |
| 53 | t.Logf(" hash=[%s]", object.Hash) |
| 54 | } |
| 55 | |
| 56 | count++ |
| 57 | return true, nil |
| 58 | }) |
| 59 | th.AssertNoErr(t, err) |
| 60 | if count == 0 { |
| 61 | t.Errorf("No objects listed for your current token.") |
| 62 | } |
| 63 | t.Logf("Container Names available to the currently issued token:") |
| 64 | count = 0 |
| 65 | err = raxObjects.List(c, "gophercloud-test", &osObjects.ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) { |
| 66 | t.Logf("--- Page %02d ---", count) |
| 67 | |
| 68 | names, err := raxObjects.ExtractNames(page) |
| 69 | th.AssertNoErr(t, err) |
| 70 | |
| 71 | for i, name := range names { |
| 72 | t.Logf("[%02d] %s", i, name) |
| 73 | } |
| 74 | |
| 75 | count++ |
| 76 | return true, nil |
| 77 | }) |
| 78 | th.AssertNoErr(t, err) |
| 79 | if count == 0 { |
| 80 | t.Errorf("No objects listed for your current token.") |
| 81 | } |
| 82 | |
Jon Perritt | 7e70c40 | 2014-10-24 16:15:09 -0500 | [diff] [blame] | 83 | copyres := raxObjects.Copy(c, "gophercloud-test", "o1", &raxObjects.CopyOpts{Destination: "gophercloud-test/o2"}) |
| 84 | th.AssertNoErr(t, copyres.Err) |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 85 | defer func() { |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 86 | t.Logf("Deleting object o2...") |
Jon Perritt | 7e70c40 | 2014-10-24 16:15:09 -0500 | [diff] [blame] | 87 | res := raxObjects.Delete(c, "gophercloud-test", "o2", nil) |
| 88 | th.AssertNoErr(t, res.Err) |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 89 | }() |
| 90 | |
| 91 | o1Content, err := raxObjects.Download(c, "gophercloud-test", "o1", nil).ExtractContent() |
| 92 | th.AssertNoErr(t, err) |
| 93 | o2Content, err := raxObjects.Download(c, "gophercloud-test", "o2", nil).ExtractContent() |
| 94 | th.AssertNoErr(t, err) |
| 95 | th.AssertEquals(t, string(o2Content), string(o1Content)) |
| 96 | |
Jon Perritt | 7e70c40 | 2014-10-24 16:15:09 -0500 | [diff] [blame] | 97 | updateres := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": "mountains"}}) |
| 98 | th.AssertNoErr(t, updateres.Err) |
| 99 | t.Logf("Headers from Update Account request: %+v\n", updateres.Header) |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 100 | defer func() { |
Jon Perritt | 7e70c40 | 2014-10-24 16:15:09 -0500 | [diff] [blame] | 101 | res := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": ""}}) |
| 102 | th.AssertNoErr(t, res.Err) |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 103 | metadata, err := raxObjects.Get(c, "gophercloud-test", "o2", nil).ExtractMetadata() |
| 104 | th.AssertNoErr(t, err) |
| 105 | t.Logf("Metadata from Get Account request (after update reverted): %+v\n", metadata) |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 106 | th.CheckEquals(t, "", metadata["White"]) |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 107 | }() |
| 108 | |
Jon Perritt | 7e70c40 | 2014-10-24 16:15:09 -0500 | [diff] [blame] | 109 | getres := raxObjects.Get(c, "gophercloud-test", "o2", nil) |
| 110 | th.AssertNoErr(t, getres.Err) |
| 111 | t.Logf("Headers from Get Account request (after update): %+v\n", getres.Header) |
| 112 | metadata, err := getres.ExtractMetadata() |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 113 | th.AssertNoErr(t, err) |
| 114 | t.Logf("Metadata from Get Account request (after update): %+v\n", metadata) |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 115 | th.CheckEquals(t, "mountains", metadata["White"]) |
Jon Perritt | 9095760 | 2015-02-01 17:03:06 -0700 | [diff] [blame] | 116 | |
| 117 | createTempURLOpts := osObjects.CreateTempURLOpts{ |
| 118 | Method: osObjects.GET, |
| 119 | TTL: 600, |
| 120 | } |
| 121 | tempURL, err := raxObjects.CreateTempURL(c, "gophercloud-test", "o1", createTempURLOpts) |
| 122 | th.AssertNoErr(t, err) |
| 123 | t.Logf("TempURL for object (%s): %s", "o1", tempURL) |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 124 | } |