2021-10-02 18:00:35 +00:00
|
|
|
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"),
|
2021-11-01 00:05:59 +00:00
|
|
|
IndexLabel: "testlabel",
|
2021-10-02 18:00:35 +00:00
|
|
|
|
|
|
|
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{
|
2021-11-01 00:05:59 +00:00
|
|
|
UUID: "ABCDEF",
|
2021-10-02 18:00:35 +00:00
|
|
|
AppName: "app_0102",
|
|
|
|
TimeStamp: 1634510035,
|
|
|
|
Labels: []string{"userid:1"},
|
|
|
|
}
|
|
|
|
|
2021-11-01 00:05:59 +00:00
|
|
|
assert.Equal(t, "app_0102:ABCDEF:1:1634510035", snapshot.KeyName("userid"))
|
2021-10-02 18:00:35 +00:00
|
|
|
|
2021-11-01 00:05:59 +00:00
|
|
|
err := snapshotProcessor.saveMetadata(snapshot)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
snapshot2, err := snapshotProcessor.metadataForSnapshotKey("app_0102:ABCDEF:1:1634510035")
|
2021-10-02 18:00:35 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
2021-10-28 22:57:06 +00:00
|
|
|
|
2021-10-31 11:01:24 +00:00
|
|
|
func TestGetSnapshot(t *testing.T) {
|
2021-11-01 00:05:59 +00:00
|
|
|
snapshot, err := snapshotProcessor.GetSnapshot("app_0102:ABCDEF:1:1634510035")
|
2021-10-31 11:01:24 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "app_0102", snapshot.AppName)
|
|
|
|
}
|
|
|
|
|
2021-10-28 22:57:06 +00:00
|
|
|
func TestListAppsSnapshotsByLabel(t *testing.T) {
|
|
|
|
appName := "app_0102"
|
|
|
|
|
|
|
|
// 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
|
2021-11-01 00:05:59 +00:00
|
|
|
snapshotName, err := snapshotProcessor.CreateSnapshot(appName, []string{"app:test2", "almost_no_content", "testlabel:abcde"})
|
2021-10-28 22:57:06 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, strings.HasPrefix(snapshotName, appName+":"), true)
|
|
|
|
|
2021-11-01 00:05:59 +00:00
|
|
|
snapshots, err := snapshotProcessor.ListAppsSnapshotsByLabel("abcde")
|
2021-10-28 22:57:06 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, len(snapshots) > 0)
|
|
|
|
|
|
|
|
assert.Equal(t, appName, snapshots[0].AppName)
|
|
|
|
}
|