blob: 749f6d36b0c7a1a0876b11c97274fee82ebadf8b [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
20 _, err = raxContainers.Create(c, "gophercloud-test", nil).ExtractHeaders()
21 th.AssertNoErr(t, err)
22
23 defer func() {
24 _, err := raxContainers.Delete(c, "gophercloud-test").ExtractHeaders()
25 th.AssertNoErr(t, err)
26 }()
27
28 content := bytes.NewBufferString("Lewis Carroll")
29 options := &osObjects.CreateOpts{ContentType: "text/plain"}
30 _, err = raxObjects.Create(c, "gophercloud-test", "o1", content, options).ExtractHeaders()
31 th.AssertNoErr(t, err)
32 defer func() {
33 _, err := raxObjects.Delete(c, "gophercloud-test", "o1", nil).ExtractHeaders()
34 th.AssertNoErr(t, err)
35 }()
36
37 t.Logf("Objects Info available to the currently issued token:")
38 count := 0
39 err = raxObjects.List(c, "gophercloud-test", &osObjects.ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
40 t.Logf("--- Page %02d ---", count)
41
42 objects, err := raxObjects.ExtractInfo(page)
43 th.AssertNoErr(t, err)
44
45 for i, object := range objects {
46 t.Logf("[%02d] name=[%s]", i, object.Name)
47 t.Logf(" content-type=[%s]", object.ContentType)
48 t.Logf(" bytes=[%d]", object.Bytes)
49 t.Logf(" last-modified=[%s]", object.LastModified)
50 t.Logf(" hash=[%s]", object.Hash)
51 }
52
53 count++
54 return true, nil
55 })
56 th.AssertNoErr(t, err)
57 if count == 0 {
58 t.Errorf("No objects listed for your current token.")
59 }
60 t.Logf("Container Names available to the currently issued token:")
61 count = 0
62 err = raxObjects.List(c, "gophercloud-test", &osObjects.ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
63 t.Logf("--- Page %02d ---", count)
64
65 names, err := raxObjects.ExtractNames(page)
66 th.AssertNoErr(t, err)
67
68 for i, name := range names {
69 t.Logf("[%02d] %s", i, name)
70 }
71
72 count++
73 return true, nil
74 })
75 th.AssertNoErr(t, err)
76 if count == 0 {
77 t.Errorf("No objects listed for your current token.")
78 }
79
80 _, err = raxObjects.Copy(c, "gophercloud-test", "o1", &raxObjects.CopyOpts{Destination: "gophercloud-test/o2"}).ExtractHeaders()
81 th.AssertNoErr(t, err)
82 defer func() {
83 _, err := raxObjects.Delete(c, "gophercloud-test", "o2", nil).ExtractHeaders()
84 th.AssertNoErr(t, err)
85 }()
86
87 o1Content, err := raxObjects.Download(c, "gophercloud-test", "o1", nil).ExtractContent()
88 th.AssertNoErr(t, err)
89 o2Content, err := raxObjects.Download(c, "gophercloud-test", "o2", nil).ExtractContent()
90 th.AssertNoErr(t, err)
91 th.AssertEquals(t, string(o2Content), string(o1Content))
92
93 headers, err := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": "mountains"}}).ExtractHeaders()
94 th.AssertNoErr(t, err)
95 t.Logf("Headers from Update Account request: %+v\n", headers)
96 defer func() {
97 _, err := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": ""}}).ExtractHeaders()
98 th.AssertNoErr(t, err)
99 metadata, err := raxObjects.Get(c, "gophercloud-test", "o2", nil).ExtractMetadata()
100 th.AssertNoErr(t, err)
101 t.Logf("Metadata from Get Account request (after update reverted): %+v\n", metadata)
102 th.CheckEquals(t, metadata["White"], "")
103 }()
104
105 getResult := raxObjects.Get(c, "gophercloud-test", "o2", nil)
106 headers, err = getResult.ExtractHeaders()
107 th.AssertNoErr(t, err)
108 t.Logf("Headers from Get Account request (after update): %+v\n", headers)
109 metadata, err := getResult.ExtractMetadata()
110 th.AssertNoErr(t, err)
111 t.Logf("Metadata from Get Account request (after update): %+v\n", metadata)
112 th.CheckEquals(t, metadata["White"], "mountains")
113}