blob: 585dea7696a459cd906b8431f0759cd83d20d14c [file] [log] [blame]
Jon Perritta77ba0d2014-10-17 01:15:29 -05001// +build acceptance rackspace objectstorage v1
2
Jon Perritt8abd2c22014-10-16 20:28:51 -05003package v1
Jon Perritta77ba0d2014-10-17 01:15:29 -05004
5import (
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
16func TestObjects(t *testing.T) {
17 c, err := createClient(t, false)
18 th.AssertNoErr(t, err)
19
Jon Perritt7e70c402014-10-24 16:15:09 -050020 res := raxContainers.Create(c, "gophercloud-test", nil)
21 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050022
23 defer func() {
Jon Perritt63e7a482014-12-04 09:47:23 -070024 t.Logf("Deleting container...")
Jon Perritt7e70c402014-10-24 16:15:09 -050025 res := raxContainers.Delete(c, "gophercloud-test")
26 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050027 }()
28
29 content := bytes.NewBufferString("Lewis Carroll")
30 options := &osObjects.CreateOpts{ContentType: "text/plain"}
Jon Perritt7e70c402014-10-24 16:15:09 -050031 createres := raxObjects.Create(c, "gophercloud-test", "o1", content, options)
32 th.AssertNoErr(t, createres.Err)
Jon Perritt90957602015-02-01 17:03:06 -070033
Jon Perritta77ba0d2014-10-17 01:15:29 -050034 defer func() {
Jon Perritt63e7a482014-12-04 09:47:23 -070035 t.Logf("Deleting object o1...")
Jon Perritt7e70c402014-10-24 16:15:09 -050036 res := raxObjects.Delete(c, "gophercloud-test", "o1", nil)
37 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050038 }()
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 Perritt7e70c402014-10-24 16:15:09 -050083 copyres := raxObjects.Copy(c, "gophercloud-test", "o1", &raxObjects.CopyOpts{Destination: "gophercloud-test/o2"})
84 th.AssertNoErr(t, copyres.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050085 defer func() {
Jon Perritt63e7a482014-12-04 09:47:23 -070086 t.Logf("Deleting object o2...")
Jon Perritt7e70c402014-10-24 16:15:09 -050087 res := raxObjects.Delete(c, "gophercloud-test", "o2", nil)
88 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050089 }()
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 Perritt7e70c402014-10-24 16:15:09 -050097 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 Perritta77ba0d2014-10-17 01:15:29 -0500100 defer func() {
Jon Perritt7e70c402014-10-24 16:15:09 -0500101 res := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": ""}})
102 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -0500103 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 Perritt63e7a482014-12-04 09:47:23 -0700106 th.CheckEquals(t, "", metadata["White"])
Jon Perritta77ba0d2014-10-17 01:15:29 -0500107 }()
108
Jon Perritt7e70c402014-10-24 16:15:09 -0500109 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 Perritta77ba0d2014-10-17 01:15:29 -0500113 th.AssertNoErr(t, err)
114 t.Logf("Metadata from Get Account request (after update): %+v\n", metadata)
Jon Perritt63e7a482014-12-04 09:47:23 -0700115 th.CheckEquals(t, "mountains", metadata["White"])
Jon Perritt90957602015-02-01 17:03:06 -0700116
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 Perritta77ba0d2014-10-17 01:15:29 -0500124}