node-api/apps/snapshots_test.go
Adam Štrauch bc50cb1105
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Support for snapshots
* full implementation of snapshots
* tests of the snapshot backend
* Drone CI pipeline
* New snapshots handlers
* Filesystem driver
* S3 driver
2021-10-26 18:57:52 +02:00

156 lines
3.8 KiB
Go

package apps
import (
"fmt"
"os"
"os/exec"
"path"
"strings"
"testing"
"github.com/rosti-cz/node-api/apps/drivers"
"github.com/stretchr/testify/assert"
)
var snapshotProcessor *SnapshotProcessor
var initialPwd string
var testS3Endpoint string
func TestMain(m *testing.M) {
testS3Endpoint = "localhost:9000"
if os.Getenv("TEST_S3_ENDPOINT") != "" {
testS3Endpoint = os.Getenv("TEST_S3_ENDPOINT")
}
pwd, err := os.Getwd()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
toRemovePath := path.Join(pwd, "..", "tmp")
initialPwd = path.Join(pwd, "..")
// Removing temporary test files from previous runs
err = os.RemoveAll(toRemovePath)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
minioHost := os.Getenv("MINIO_HOST")
if minioHost == "" {
minioHost = "localhost:9000"
}
snapshotProcessor = &SnapshotProcessor{
AppsPath: path.Join(initialPwd, "tmp/apps"),
TmpSnapshotPath: path.Join(initialPwd, "tmp/snapshots"),
Driver: drivers.S3Driver{
S3AccessKey: "test",
S3SecretKey: "testtest",
S3Endpoint: testS3Endpoint,
S3SSL: false,
Bucket: "testsnapshots",
},
}
err = os.MkdirAll(path.Join(initialPwd, "tmp/apps"), 0755)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
err = os.MkdirAll(path.Join(initialPwd, "tmp/snapshots"), 0755)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
code := m.Run()
os.Exit(code)
}
func TestSnapshot(t *testing.T) {
snapshot := Snapshot{
AppName: "app_0102",
TimeStamp: 1634510035,
Labels: []string{"userid:1"},
}
assert.Equal(t, "app_0102:1634510035:eyJBcHBOYW1lIjoiYXBwXzAxMDIiLCJUaW1lU3RhbXAiOjE2MzQ1MTAwMzUsIkxhYmVscyI6WyJ1c2VyaWQ6MSJdfQ==", snapshot.KeyName())
snapshot2, err := DecodeKeyName("app_0102:1634510035:eyJBcHBOYW1lIjoiYXBwXzAxMDIiLCJUaW1lU3RhbXAiOjE2MzQ1MTAwMzUsIkxhYmVscyI6WyJ1c2VyaWQ6MSJdfQ==")
assert.Nil(t, err)
assert.Equal(t, "app_0102", snapshot2.AppName)
assert.Equal(t, int64(1634510035), snapshot2.TimeStamp)
assert.Equal(t, "userid:1", snapshot2.Labels[0])
}
func TestCreateRestoreListSnapshot(t *testing.T) {
appName := "app_0101"
// Create an app structure
err := os.MkdirAll(path.Join(snapshotProcessor.AppsPath, appName), 0755)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
_, err = exec.Command("bash", "-c", "echo content > "+path.Join(snapshotProcessor.AppsPath, appName)+"/a_file.txt").CombinedOutput()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
// Create an snapshot
snapshotName, err := snapshotProcessor.CreateSnapshot(appName, []string{"app:test", "almost_no_content"})
assert.Nil(t, err)
assert.Equal(t, strings.HasPrefix(snapshotName, appName+":"), true)
// Remove app
err = os.RemoveAll(path.Join(snapshotProcessor.AppsPath, appName))
assert.Nil(t, err)
// Check if the file doesn't exist
_, err = os.Stat(path.Join(snapshotProcessor.AppsPath, appName, "a_file.txt"))
assert.Equal(t, os.IsNotExist(err), true)
// list snapshots
snapshots, err := snapshotProcessor.ListAppSnapshots(appName)
assert.Nil(t, err)
assert.True(t, len(snapshots) >= 1, true, "not snapshots found")
var found bool
for _, snapshot := range snapshots {
if snapshot.AppName == appName {
found = true
}
}
assert.True(t, found, "not the right snapshot found")
// List snapshots for list of apps
snapshots, err = snapshotProcessor.ListAppsSnapshots([]string{appName})
assert.Nil(t, err)
assert.True(t, len(snapshots) >= 1, true, "not snapshots found")
found = false
for _, snapshot := range snapshots {
if snapshot.AppName == appName {
found = true
}
}
assert.True(t, found, "not the right snapshot found")
// Restore snapshot
err = snapshotProcessor.RestoreSnapshot(snapshotName, appName)
assert.Nil(t, err)
_, err = os.Stat(path.Join(snapshotProcessor.AppsPath, appName, "a_file.txt"))
assert.Equal(t, os.IsNotExist(err), false)
}