blob: 96a7d8cb80f087ca3c04b4967141cc6c488b5dc3 [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 Perritta77ba0d2014-10-17 01:15:29 -050033 defer func() {
Jon Perritt63e7a482014-12-04 09:47:23 -070034 t.Logf("Deleting object o1...")
Jon Perritt7e70c402014-10-24 16:15:09 -050035 res := raxObjects.Delete(c, "gophercloud-test", "o1", nil)
36 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050037 }()
38
39 t.Logf("Objects Info available to the currently issued token:")
40 count := 0
41 err = raxObjects.List(c, "gophercloud-test", &osObjects.ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
42 t.Logf("--- Page %02d ---", count)
43
44 objects, err := raxObjects.ExtractInfo(page)
45 th.AssertNoErr(t, err)
46
47 for i, object := range objects {
48 t.Logf("[%02d] name=[%s]", i, object.Name)
49 t.Logf(" content-type=[%s]", object.ContentType)
50 t.Logf(" bytes=[%d]", object.Bytes)
51 t.Logf(" last-modified=[%s]", object.LastModified)
52 t.Logf(" hash=[%s]", object.Hash)
53 }
54
55 count++
56 return true, nil
57 })
58 th.AssertNoErr(t, err)
59 if count == 0 {
60 t.Errorf("No objects listed for your current token.")
61 }
62 t.Logf("Container Names available to the currently issued token:")
63 count = 0
64 err = raxObjects.List(c, "gophercloud-test", &osObjects.ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
65 t.Logf("--- Page %02d ---", count)
66
67 names, err := raxObjects.ExtractNames(page)
68 th.AssertNoErr(t, err)
69
70 for i, name := range names {
71 t.Logf("[%02d] %s", i, name)
72 }
73
74 count++
75 return true, nil
76 })
77 th.AssertNoErr(t, err)
78 if count == 0 {
79 t.Errorf("No objects listed for your current token.")
80 }
81
Jon Perritt7e70c402014-10-24 16:15:09 -050082 copyres := raxObjects.Copy(c, "gophercloud-test", "o1", &raxObjects.CopyOpts{Destination: "gophercloud-test/o2"})
83 th.AssertNoErr(t, copyres.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050084 defer func() {
Jon Perritt63e7a482014-12-04 09:47:23 -070085 t.Logf("Deleting object o2...")
Jon Perritt7e70c402014-10-24 16:15:09 -050086 res := raxObjects.Delete(c, "gophercloud-test", "o2", nil)
87 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -050088 }()
89
90 o1Content, err := raxObjects.Download(c, "gophercloud-test", "o1", nil).ExtractContent()
91 th.AssertNoErr(t, err)
92 o2Content, err := raxObjects.Download(c, "gophercloud-test", "o2", nil).ExtractContent()
93 th.AssertNoErr(t, err)
94 th.AssertEquals(t, string(o2Content), string(o1Content))
95
Jon Perritt7e70c402014-10-24 16:15:09 -050096 updateres := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": "mountains"}})
97 th.AssertNoErr(t, updateres.Err)
98 t.Logf("Headers from Update Account request: %+v\n", updateres.Header)
Jon Perritta77ba0d2014-10-17 01:15:29 -050099 defer func() {
Jon Perritt7e70c402014-10-24 16:15:09 -0500100 res := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": ""}})
101 th.AssertNoErr(t, res.Err)
Jon Perritta77ba0d2014-10-17 01:15:29 -0500102 metadata, err := raxObjects.Get(c, "gophercloud-test", "o2", nil).ExtractMetadata()
103 th.AssertNoErr(t, err)
104 t.Logf("Metadata from Get Account request (after update reverted): %+v\n", metadata)
Jon Perritt63e7a482014-12-04 09:47:23 -0700105 th.CheckEquals(t, "", metadata["White"])
Jon Perritta77ba0d2014-10-17 01:15:29 -0500106 }()
107
Jon Perritt7e70c402014-10-24 16:15:09 -0500108 getres := raxObjects.Get(c, "gophercloud-test", "o2", nil)
109 th.AssertNoErr(t, getres.Err)
110 t.Logf("Headers from Get Account request (after update): %+v\n", getres.Header)
111 metadata, err := getres.ExtractMetadata()
Jon Perritta77ba0d2014-10-17 01:15:29 -0500112 th.AssertNoErr(t, err)
113 t.Logf("Metadata from Get Account request (after update): %+v\n", metadata)
Jon Perritt63e7a482014-12-04 09:47:23 -0700114 th.CheckEquals(t, "mountains", metadata["White"])
Jon Perritta77ba0d2014-10-17 01:15:29 -0500115}