blob: 462f2847dbca2b49ebd8277b883bd15885e74bc9 [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 Perritt7e70c402014-10-24 16:15:09 -050024 res := raxContainers.Delete(c, "gophercloud-test")
25 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050026 }()
27
28 content := bytes.NewBufferString("Lewis Carroll")
29 options := &osObjects.CreateOpts{ContentType: "text/plain"}
Jon Perritt7e70c402014-10-24 16:15:09 -050030 createres := raxObjects.Create(c, "gophercloud-test", "o1", content, options)
31 th.AssertNoErr(t, createres.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050032 defer func() {
Jon Perritt7e70c402014-10-24 16:15:09 -050033 res := raxObjects.Delete(c, "gophercloud-test", "o1", nil)
34 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050035 }()
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
Jon Perritt7e70c402014-10-24 16:15:09 -050080 copyres := raxObjects.Copy(c, "gophercloud-test", "o1", &raxObjects.CopyOpts{Destination: "gophercloud-test/o2"})
81 th.AssertNoErr(t, copyres.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050082 defer func() {
Jon Perritt7e70c402014-10-24 16:15:09 -050083 res := raxObjects.Delete(c, "gophercloud-test", "o2", nil)
84 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050085 }()
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
Jon Perritt7e70c402014-10-24 16:15:09 -050093 updateres := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": "mountains"}})
94 th.AssertNoErr(t, updateres.Err)
95 t.Logf("Headers from Update Account request: %+v\n", updateres.Header)
Jon Perritta77ba0d2014-10-17 01:15:29 -050096 defer func() {
Jon Perritt7e70c402014-10-24 16:15:09 -050097 res := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": ""}})
98 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050099 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
Jon Perritt7e70c402014-10-24 16:15:09 -0500105 getres := raxObjects.Get(c, "gophercloud-test", "o2", nil)
106 th.AssertNoErr(t, getres.Err)
107 t.Logf("Headers from Get Account request (after update): %+v\n", getres.Header)
108 metadata, err := getres.ExtractMetadata()
Jon Perritta77ba0d2014-10-17 01:15:29 -0500109 th.AssertNoErr(t, err)
110 t.Logf("Metadata from Get Account request (after update): %+v\n", metadata)
111 th.CheckEquals(t, metadata["White"], "mountains")
112}