objectstorage: add WriteObject tests

Test WriteObject using different size values: unspecified, equal to the buffer
size or less than the buffer size.
This commit is contained in:
Simone Gotti 2019-10-25 10:55:01 +02:00
parent 2e520dae55
commit 1eb16886d8
1 changed files with 116 additions and 15 deletions

View File

@ -15,6 +15,7 @@
package objectstorage
import (
"bytes"
"fmt"
"io/ioutil"
"os"
@ -29,6 +30,26 @@ import (
"agola.io/agola/internal/objectstorage/s3"
)
func setupPosix(t *testing.T, dir string) (*posix.PosixStorage, error) {
return posix.New(path.Join(dir, "posix"))
}
func setupPosixFlat(t *testing.T, dir string) (*posixflat.PosixFlatStorage, error) {
return posixflat.New(path.Join(dir, "posixflat"))
}
func setupS3(t *testing.T, dir string) (*s3.S3Storage, error) {
minioEndpoint := os.Getenv("MINIO_ENDPOINT")
minioAccessKey := os.Getenv("MINIO_ACCESSKEY")
minioSecretKey := os.Getenv("MINIO_SECRETKEY")
if minioEndpoint == "" {
t.Logf("missing MINIO_ENDPOINT env, skipping tests with minio storage")
return nil, nil
}
return s3.New(filepath.Base(dir), "", minioEndpoint, minioAccessKey, minioSecretKey, false)
}
func TestList(t *testing.T) {
dir, err := ioutil.TempDir("", "objectstorage")
if err != nil {
@ -36,27 +57,17 @@ func TestList(t *testing.T) {
}
defer os.RemoveAll(dir)
ps, err := posix.New(path.Join(dir, "posix"))
ps, err := setupPosix(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
pfs, err := posixflat.New(path.Join(dir, "posixflat"))
pfs, err := setupPosixFlat(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
var s3s *s3.S3Storage
minioEndpoint := os.Getenv("MINIO_ENDPOINT")
minioAccessKey := os.Getenv("MINIO_ACCESSKEY")
minioSecretKey := os.Getenv("MINIO_SECRETKEY")
if minioEndpoint == "" {
t.Logf("missing MINIO_ENDPOINT env, skipping tests with minio storage")
} else {
var err error
s3s, err = s3.New(filepath.Base(dir), "", minioEndpoint, minioAccessKey, minioSecretKey, false)
s3s, err := setupS3(t, dir)
if err != nil {
t.Fatalf("err: %v", err)
}
t.Fatalf("unexpected err: %v", err)
}
type listop struct {
@ -293,3 +304,93 @@ func TestList(t *testing.T) {
}
}
}
func TestWriteObject(t *testing.T) {
dir, err := ioutil.TempDir("", "objectstorage")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
ps, err := setupPosix(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
pfs, err := setupPosixFlat(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
s3s, err := setupS3(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
newBuf := func(n int64) *bytes.Buffer {
testBytes := make([]byte, n)
for i := int64(0); i < n; i++ {
testBytes[i] = 'a' + byte(i%26)
}
return bytes.NewBuffer(testBytes)
}
for sname, s := range map[string]Storage{"posix": ps, "posixflat": pfs, "minio": s3s} {
t.Run(fmt.Sprintf("test with storage type %s", sname), func(t *testing.T) {
switch s := s.(type) {
case *s3.S3Storage:
if s == nil {
t.SkipNow()
}
}
os := NewObjStorage(s, "/")
n := int64(10000)
// Test write without size. Should write whole buffer.
buf := newBuf(n)
objName := "obj01"
if err := os.WriteObject(objName, buf, -1, false); err != nil {
t.Fatalf("unexpected err: %v", err)
}
oi, err := os.Stat(objName)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if oi.Size != n {
t.Fatalf("expected object size: %d, got %d", n, oi.Size)
}
// Test write with object size equal to buf size.
buf = newBuf(n)
objName = "obj02"
if err := os.WriteObject(objName, buf, n, false); err != nil {
t.Fatalf("unexpected err: %v", err)
}
oi, err = os.Stat(objName)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if oi.Size != n {
t.Fatalf("expected object size: %d, got %d", n, oi.Size)
}
// Test write with object size less than buf size.
buf = newBuf(n)
objName = "obj03"
size := int64(800)
if err := os.WriteObject(objName, buf, int64(size), false); err != nil {
t.Fatalf("unexpected err: %v", err)
}
oi, err = os.Stat(objName)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if oi.Size != size {
t.Fatalf("expected object size: %d, got %d", size, oi.Size)
}
// remaining buf len should be n-size
if int64(buf.Len()) != n-size {
t.Fatalf("expected buf lenght: %d, got %d", n-size, buf.Len())
}
})
}
}