1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
package v1
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects"
)
// CompareFiles will compare two files
func CompareFiles(t *testing.T, file1, file2 string) (bool, error) {
f1, err := os.Open(file1)
if err != nil {
return false, fmt.Errorf("unable to open %s: %s", file1, err)
}
defer f1.Close()
f2, err := os.Open(file2)
if err != nil {
return false, fmt.Errorf("unable to open %s: %s", file2, err)
}
defer f2.Close()
contents1, err := ioutil.ReadAll(f1)
if err != nil {
return false, fmt.Errorf("unable to read %s: %s", file1, err)
}
contents2, err := ioutil.ReadAll(f2)
if err != nil {
return false, fmt.Errorf("unable to read %s: %s", file2, err)
}
equal := bytes.Equal(contents1, contents2)
return equal, nil
}
// CreateContainer will create a container with a random name.
func CreateContainer(t *testing.T, client *gophercloud.ServiceClient) (string, error) {
cName := tools.RandomString("test-container-", 8)
res := containers.Create(client, cName, nil)
t.Logf("creating container: %s", cName)
return cName, res.Err
}
// CreateRandomFile will create a file with random content.
func CreateRandomFile(t *testing.T, parentDir string) (string, error) {
tmpfile, err := CreateTempFile(t, parentDir)
if err != nil {
return "", fmt.Errorf("unable to create random file: %s", err)
}
content := tools.RandomString("", 256)
tmpfile.Write([]byte(content))
tmpfile.Close()
return tmpfile.Name(), nil
}
// CreateTempDir will create and return a temp directory.
func CreateTempDir(t *testing.T, parentDir string) (string, error) {
dirName, err := ioutil.TempDir(parentDir, "test-dir-")
if err != nil {
return "", err
}
t.Logf("creating tempdir: %s", dirName)
return dirName, nil
}
// CreateTempFile will create and return a temp file.
func CreateTempFile(t *testing.T, dir string) (*os.File, error) {
fileName := tools.RandomString("test-file-", 8)
t.Logf("creating tempfile: %s", fileName)
return ioutil.TempFile(dir, fileName)
}
// DeleteContainer will delete a container. A fatal error will occur if the
// container failed to be deleted. This works best when used as a deferred
// function.
func DeleteContainer(t *testing.T, client *gophercloud.ServiceClient, cName string) {
t.Logf("deleting container %s", cName)
allPages, err := objects.List(client, cName, nil).AllPages()
if err != nil {
t.Fatalf("unable to list container %s: %s", cName, err)
}
allObjects, err := objects.ExtractNames(allPages)
if err != nil {
t.Fatalf("unable to extract container %s: %s", cName, err)
}
for _, oName := range allObjects {
res := objects.Delete(client, cName, oName, nil)
if res.Err != nil {
t.Fatalf("unable to delete object: %s/%s: %s", cName, oName, oName)
}
}
res := containers.Delete(client, cName)
if res.Err != nil {
t.Fatalf("unable to delete container %s: %s", cName, res.Err)
}
}
// DeleteObject will delete an object. A fatal error will occur if the object
// failed to be deleted. This works best when used as a deferred function.
func DeleteObject(t *testing.T, client *gophercloud.ServiceClient, cName, oName string) {
t.Logf("deleting object %s/%s", cName, oName)
res := objects.Delete(client, cName, oName, nil)
if res.Err != nil {
t.Fatalf("unable to delete object %s/%s: %s", cName, oName, res.Err)
}
}
// DeleteTempFile will delete a temporary file. A fatal error will occur if the
// file could not be deleted. This works best when used as a deferred function.
func DeleteTempFile(t *testing.T, fileName string) {
t.Logf("deleting tempfile %s", fileName)
if err := os.Remove(fileName); err != nil {
t.Fatalf("unable to delete tempfile %s: %s", fileName, err)
}
}
// DeleteTempDir will delete a temporary directory. A fatal error will occur if
// the directory could not be deleted. This works best when used as a deferred
// function.
func DeleteTempDir(t *testing.T, dirName string) {
t.Logf("deleting tempdir %s", dirName)
if err := os.RemoveAll(dirName); err != nil {
t.Fatalf("unable to delete tempdir %s: %s", dirName, err)
}
}
// GetObject is an alias to objects.GetObject so we don't have to import
// gophercloud/gophercloud into objects_test.go and make things confusing.
func GetObject(client *gophercloud.ServiceClient, cName, oName string) (*objects.GetHeader, error) {
return objects.Get(client, cName, oName, nil).Extract()
}
|