Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit b4bf00f

Browse files
James FisherSamuel Ortiz
authored andcommitted
start testing putting image data
1 parent 8644708 commit b4bf00f

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

openstack/imageservice/v2/fixtures.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package v2
44

55
import (
66
"fmt"
7+
"io/ioutil"
78
"net/http"
89
"testing"
910

@@ -138,3 +139,20 @@ func HandleImageUpdateSuccessfully(t *testing.T) {
138139
}`)
139140
})
140141
}
142+
143+
func HandlePutImageDataSuccessfully(t *testing.T) {
144+
th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/file", func(w http.ResponseWriter, r *http.Request) {
145+
th.TestMethod(t, r, "PUT")
146+
th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
147+
148+
b, err := ioutil.ReadAll(r.Body)
149+
if err != nil {
150+
t.Errorf("Unable to read request body: %v", err)
151+
}
152+
153+
th.AssertByteArrayEquals(t, []byte{5,3,7,24}, b)
154+
155+
w.WriteHeader(http.StatusNoContent)
156+
})
157+
}
158+

openstack/imageservice/v2/requests_test.go

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package v2
22

3-
// TODO
4-
// compare with openstack/compute/v2/servers/requests_test.go
5-
63
import (
4+
"io"
75
"testing"
86

97
th "github.com/rackspace/gophercloud/testhelper"
@@ -156,3 +154,63 @@ func TestUpdateImage(t *testing.T) {
156154

157155
th.AssertDeepEquals(t, &expectedImage, actualImage)
158156
}
157+
158+
func TestPutImageData(t *testing.T) {
159+
th.SetupHTTP()
160+
defer th.TeardownHTTP()
161+
162+
HandlePutImageDataSuccessfully(t)
163+
164+
PutImageData(
165+
fakeclient.ServiceClient(),
166+
"da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
167+
readSeekerOfBytes([]byte{5,3,7,24}))
168+
169+
// TODO
170+
}
171+
172+
func readSeekerOfBytes(bs []byte) io.ReadSeeker {
173+
return &RS{bs: bs}
174+
}
175+
176+
// implements io.ReadSeeker
177+
type RS struct {
178+
bs []byte
179+
offset int
180+
}
181+
182+
func (rs *RS) Read(p []byte) (int, error) {
183+
leftToRead := len(rs.bs) - rs.offset
184+
185+
if 0 < leftToRead {
186+
bytesToWrite := min(leftToRead, len(p))
187+
for i := 0; i < bytesToWrite; i++ {
188+
p[i] = rs.bs[rs.offset]
189+
rs.offset++
190+
}
191+
return bytesToWrite, nil
192+
} else {
193+
return 0, io.EOF
194+
}
195+
}
196+
197+
func min(a int, b int) int {
198+
if a < b {
199+
return a
200+
} else {
201+
return b
202+
}
203+
}
204+
205+
func (rs *RS) Seek(offset int64, whence int) (int64, error) {
206+
var offsetInt int = int(offset)
207+
if whence == 0 {
208+
rs.offset = offsetInt
209+
} else if whence == 1 {
210+
rs.offset = rs.offset + offsetInt
211+
} else if whence == 2 {
212+
rs.offset = len(rs.bs) - offsetInt
213+
}
214+
215+
return int64(rs.offset), nil
216+
}

testhelper/convenience.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package testhelper
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"path/filepath"
@@ -271,6 +272,22 @@ func CheckDeepEquals(t *testing.T, expected, actual interface{}) {
271272
})
272273
}
273274

275+
func isByteArrayEquals(t *testing.T, expectedBytes []byte, actualBytes []byte) bool {
276+
return bytes.Equal(expectedBytes, actualBytes)
277+
}
278+
279+
func AssertByteArrayEquals(t *testing.T, expectedBytes []byte, actualBytes []byte) {
280+
if !isByteArrayEquals(t, expectedBytes, actualBytes) {
281+
logFatal(t, "The bytes differed.")
282+
}
283+
}
284+
285+
func CheckByteArrayEquals(t *testing.T, expectedBytes []byte, actualBytes []byte) {
286+
if !isByteArrayEquals(t, expectedBytes, actualBytes) {
287+
logError(t, "The bytes differed.")
288+
}
289+
}
290+
274291
// isJSONEquals is a utility function that implements JSON comparison for AssertJSONEquals and
275292
// CheckJSONEquals.
276293
func isJSONEquals(t *testing.T, expectedJSON string, actual interface{}) bool {

0 commit comments

Comments
 (0)