124 lines
2.6 KiB
Go
124 lines
2.6 KiB
Go
|
package drivers
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
const testDirectory = "./tmp"
|
||
|
const testKey = "sourcefile"
|
||
|
const testFile = testDirectory + "/" + testKey
|
||
|
const testContent = "test content"
|
||
|
|
||
|
func TestMain(m *testing.M) {
|
||
|
testS3Endpoint := "localhost:9000"
|
||
|
if os.Getenv("TEST_S3_ENDPOINT") != "" {
|
||
|
testS3Endpoint = os.Getenv("TEST_S3_ENDPOINT")
|
||
|
}
|
||
|
|
||
|
testS3Driver = S3Driver{
|
||
|
S3AccessKey: "test",
|
||
|
S3SecretKey: "testtest",
|
||
|
S3Endpoint: testS3Endpoint,
|
||
|
S3SSL: false,
|
||
|
Bucket: "testsnapshots",
|
||
|
}
|
||
|
|
||
|
// Test directory
|
||
|
err := os.MkdirAll(testDirectory, 0755)
|
||
|
if err != nil {
|
||
|
fmt.Printf("Creating directory structure error: %v\n", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
// Test source file
|
||
|
err = ioutil.WriteFile(testFile, []byte(testContent), 0755)
|
||
|
if err != nil {
|
||
|
fmt.Printf("Creating test file error: %v\n", err)
|
||
|
os.Exit(2)
|
||
|
}
|
||
|
|
||
|
exitVal := m.Run()
|
||
|
|
||
|
err = os.RemoveAll(testDirectory)
|
||
|
if err != nil {
|
||
|
fmt.Printf("Removing directory structure error: %v\n", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
os.Exit(exitVal)
|
||
|
}
|
||
|
|
||
|
func TestFSCreate(t *testing.T) {
|
||
|
driver := FSDriver{Path: testDirectory}
|
||
|
|
||
|
err := os.MkdirAll(testDirectory, 0755)
|
||
|
assert.Nil(t, err)
|
||
|
|
||
|
err = driver.Create("testkey.txt", testFile)
|
||
|
assert.Nil(t, err)
|
||
|
|
||
|
body, err := driver.Read("testkey.txt")
|
||
|
assert.Nil(t, err)
|
||
|
|
||
|
assert.Equal(t, []byte(testContent), body)
|
||
|
}
|
||
|
|
||
|
func TestFSWrite(t *testing.T) {
|
||
|
driver := FSDriver{Path: testDirectory}
|
||
|
|
||
|
err := driver.Write("testkey2", []byte("another test content"))
|
||
|
assert.Nil(t, err)
|
||
|
|
||
|
body, err := driver.Read("testkey2")
|
||
|
assert.Nil(t, err)
|
||
|
|
||
|
assert.Equal(t, []byte("another test content"), body)
|
||
|
}
|
||
|
|
||
|
func TestFSGet(t *testing.T) {
|
||
|
driver := FSDriver{Path: testDirectory}
|
||
|
|
||
|
err := driver.Get(testKey, testDirectory+"/testdstfile")
|
||
|
assert.Nil(t, err)
|
||
|
|
||
|
body, err := ioutil.ReadFile(testDirectory + "/testdstfile")
|
||
|
assert.Nil(t, err)
|
||
|
|
||
|
assert.Equal(t, []byte(testContent), body)
|
||
|
}
|
||
|
|
||
|
func TestFSRead(t *testing.T) {
|
||
|
driver := FSDriver{Path: testDirectory}
|
||
|
body, err := driver.Read(testKey)
|
||
|
assert.Nil(t, err)
|
||
|
assert.Equal(t, []byte(testContent), body)
|
||
|
}
|
||
|
|
||
|
func TestFSList(t *testing.T) {
|
||
|
driver := FSDriver{Path: testDirectory}
|
||
|
keys, err := driver.List("")
|
||
|
assert.Nil(t, err)
|
||
|
assert.Contains(t, keys, "testkey.txt")
|
||
|
}
|
||
|
|
||
|
func TestFSDelete(t *testing.T) {
|
||
|
driver := FSDriver{Path: testDirectory}
|
||
|
err := driver.Write("keytodelete", []byte("non-needed content"))
|
||
|
assert.Nil(t, err)
|
||
|
|
||
|
keys, err := driver.List("")
|
||
|
assert.Nil(t, err)
|
||
|
assert.Contains(t, keys, "keytodelete")
|
||
|
|
||
|
driver.Delete("keytodelete")
|
||
|
|
||
|
keys, err = driver.List("")
|
||
|
assert.Nil(t, err)
|
||
|
assert.NotContains(t, keys, "keytodelete")
|
||
|
}
|