File: dlo_test.go

package info (click to toggle)
golang-github-ncw-swift 1.0.52-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, experimental, sid
  • size: 412 kB
  • sloc: python: 29; sh: 18; makefile: 4
file content (187 lines) | stat: -rw-r--r-- 6,055 bytes parent folder | download | duplicates (2)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package swift

import (
	"bytes"
	"github.com/ncw/swift/swifttest"
	"net/http"
	"testing"
	"time"
)

var srv *swifttest.SwiftServer
var con *Connection
var err error
var filecontent = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var segmentContainer = "segment_container112"

func initTest(t *testing.T) {
	con, err = initTestConnection(t)
	if err != nil {
		t.Fail()
	}
}

func teardown() {
	if srv != nil {
		srv.Close()
	}
}

func initTestConnection(t *testing.T) (*Connection, error) {
	//Uses /swifttest
	//in-memory implementation to start
	//a swift object store to test against
	srv, err = swifttest.NewSwiftServer("localhost")
	if err != nil {
		return nil, err
	}
	swiftCon := Connection{
		UserName:       "swifttest",
		ApiKey:         "swifttest",
		AuthUrl:        srv.AuthURL,
		Region:         "",
		Tenant:         "",
		ConnectTimeout: time.Second,
		Timeout:        time.Second,
		Transport:      new(http.Transport),
		Domain:         "Default",
		AuthVersion:    1,
	}
	err = swiftCon.Authenticate()
	return &swiftCon, err
}
func TestCases(t *testing.T) {
	initTest(t)
	createContainers([]string{"c1", "c2", segmentContainer}, t)
	createDynamicObject("c1", "o", t)
	moveDynamicObject("c1", "o", "c2", "oo", t)
	deleteDynamicObject("c2", "oo", t)
	createStaticObject("c2", "o2", t)
	moveStaticObject("c2", "o2", "c1", "oo2", t)
	deleteStaticObject("c1", "oo2", t)
	teardown()
}

func createContainers(containers []string, t *testing.T) {
	for i := 0; i < len(containers); i++ {
		err = con.ContainerCreate(containers[i], nil) // Create container
		if err != nil {
			t.Errorf("Fail at create container %s", containers[i])
		}
	}
}

func createDynamicObject(container, object string, t *testing.T) {
	metadata := map[string]string{}
	metadata["Custom-Field"] = "SomeValue"
	ops := LargeObjectOpts{
		Container:        container,                          // Name of container to place object
		ObjectName:       object,                             // Name of object
		CheckHash:        false,                              // If set Check the hash
		ContentType:      "application/octet-stream",         // Content-Type of the object
		Headers:          Metadata(metadata).ObjectHeaders(), // Additional headers to upload the object with
		SegmentContainer: segmentContainer,                   // Name of the container to place segments
		SegmentPrefix:    "sg",                               // Prefix to use for the segments
	}
	bigfile, err := con.DynamicLargeObjectCreate(&ops)
	if err != nil {
		t.Errorf("Fail at dynamic create Large Object")
	}
	bigfile.Write(filecontent)
	bigfile.Close()
	checkObject(container, object, t)
}
func checkObject(container, object string, t *testing.T) {
	info, header, err := con.Object(container, object)
	if err != nil {
		t.Errorf("Fail at get Large Object metadata: %s", err.Error())
	}
	if !header.IsLargeObject() {
		t.Errorf("Fail: object is not a large object")
	}
	if info.Bytes != 10 {
		t.Errorf("Fail: mismatch content lengh")
	}
	if val, ok := header["X-Object-Meta-Custom-Field"]; !ok || val != "SomeValue" {
		t.Errorf("Fail: lost custom metadata header")
	}

	content, err := con.ObjectGetBytes(container, object)
	if err != nil {
		t.Errorf("Fail at read Large Object : %s", err.Error())
	}
	if !bytes.Equal(content, filecontent) {
		t.Errorf("Fail: mismatch content")
	}

}
func checkNotExistObject(container, object string, t *testing.T) {
	_, _, err = con.Object(container, object)
	if err == nil || err.Error() != "Object Not Found" {
		t.Errorf("Fail at checkNotExistObject object: %s", err)
	}
}
func moveDynamicObject(sc, so, dc, do string, t *testing.T) {
	err = con.DynamicLargeObjectMove(sc, so, dc, do)
	if err != nil {
		t.Errorf("Fail at dynamic move Large Object: %s", err.Error())
	}
	checkNotExistObject(sc, so, t)
	checkObject(dc, do, t)
}
func deleteDynamicObject(container, object string, t *testing.T) {
	err = con.DynamicLargeObjectDelete(container, object)
	if err != nil {
		t.Errorf("Fail at delte dynamic Large Object: %s", err.Error())
	}
	checkNotExistObject(container, object, t)
	objs, err := con.ObjectsAll(segmentContainer, nil)
	if err != nil {
		t.Errorf("Fail at check delte dynamic Large Object: %s", err.Error())
	}
	if len(objs) != 0 {
		t.Errorf("Fail at check delte dynamic Large Object: seg not deleted")
	}
}
func createStaticObject(container, object string, t *testing.T) {
	metadata := map[string]string{}
	metadata["Custom-Field"] = "SomeValue"
	ops := LargeObjectOpts{
		Container:        container,                          // Name of container to place object
		ObjectName:       object,                             // Name of object
		CheckHash:        false,                              // If set Check the hash
		ContentType:      "application/octet-stream",         // Content-Type of the object
		Headers:          Metadata(metadata).ObjectHeaders(), // Additional headers to upload the object with
		SegmentContainer: segmentContainer,                   // Name of the container to place segments
		SegmentPrefix:    "sg",                               // Prefix to use for the segments
	}
	bigfile, err := con.StaticLargeObjectCreate(&ops)
	if err != nil {
		t.Errorf("Fail at static create Large Object")
	}
	bigfile.Write(filecontent)
	bigfile.Close()
	checkObject(container, object, t)
}
func moveStaticObject(sc, so, dc, do string, t *testing.T) {
	err = con.StaticLargeObjectMove(sc, so, dc, do)
	if err != nil {
		t.Errorf("Fail at static move Large Object: %s", err.Error())
	}
	checkNotExistObject(sc, so, t)
	checkObject(dc, do, t)
}
func deleteStaticObject(container, object string, t *testing.T) {
	err = con.StaticLargeObjectDelete(container, object)
	if err != nil {
		t.Errorf("Fail at delte dynamic Large Object: %s", err.Error())
	}
	checkNotExistObject(container, object, t)
	objs, err := con.ObjectsAll(segmentContainer, nil)
	if err != nil {
		t.Errorf("Fail at check delte dynamic Large Object: %s", err.Error())
	}
	if len(objs) != 0 {
		t.Errorf("Fail at check delte dynamic Large Object: seg not deleted")
	}
}