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:eyJhcHBfbmFtZSI6ImFwcF8wMTAyIiwidHMiOjE2MzQ1MTAwMzUsImxhYmVscyI6WyJ1c2VyaWQ6MSJdfQ==", snapshot.KeyName()) snapshot2, err := DecodeKeyName("app_0102:1634510035:eyJhcHBfbmFtZSI6ImFwcF8wMTAyIiwidHMiOjE2MzQ1MTAwMzUsImxhYmVscyI6WyJ1c2VyaWQ6MSJdfQ==") 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) } func TestGetSnapshot(t *testing.T) { snapshot, err := snapshotProcessor.GetSnapshot("app_0102:1634510035:eyJhcHBfbmFtZSI6ImFwcF8wMTAyIiwidHMiOjE2MzQ1MTAwMzUsImxhYmVscyI6WyJ1c2VyaWQ6MSJdfQ==") assert.Nil(t, err) assert.Equal(t, "app_0102", snapshot.AppName) } 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 snapshotName, err := snapshotProcessor.CreateSnapshot(appName, []string{"app:test2", "almost_no_content"}) assert.Nil(t, err) assert.Equal(t, strings.HasPrefix(snapshotName, appName+":"), true) snapshots, err := snapshotProcessor.ListAppsSnapshotsByLabel("app:test2") assert.Nil(t, err) assert.True(t, len(snapshots) > 0) assert.Equal(t, appName, snapshots[0].AppName) }