37baabe134
* EE-384: add endpoint to set auto backup (#224) * EE-383: add endpoint to fetch backup settings (#231) * add get backup settings handler * add api docs desc * EE-382: restore from s3 (#233) * EE-381: add GET backup status handler (#234) * EE-385: Add S3 backup execute handler (#237) * add s3 backup execute handler * refactories inside `./api/backup/backup_scheduler.go` and `./api/backup/backup_scheduler.go` * fix tests * EE-375: added backup to S3 form * EE-376: added restore from S3 form * EE-377: Update Home screen to display last backup run status * update backup service with back end endpoints. * restart admin monitor during s3 restores * use go 1.13 * go 1.13 compatibility * EE-375: added cron-validator lib * EE-375: using enum to compare form types * EE-375: validate cron rule field * try fix windows build * EE-375 EE-376 backup and restore forms validation changes * fix(autobackup): update autobackup settings validation rules (#260) * fix(autobackup): automate backup to s3 fe update (#261) * EE-292: fixed typo in property. * EE-292: updated auto backup front end validation. * EE-292: updated lib to validate cron rule in front end * fix dependencies * bumped libcompose version Co-authored-by: Hui <arris_li@hotmail.com> Co-authored-by: Felix Han <felix.han@portainer.io> Co-authored-by: fhanportainer <79428273+fhanportainer@users.noreply.github.com>
122 lines
3.6 KiB
Go
122 lines
3.6 KiB
Go
package backup
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/portainer/portainer/api/adminmonitor"
|
|
"github.com/portainer/portainer/api/crypto"
|
|
"github.com/portainer/portainer/api/http/offlinegate"
|
|
i "github.com/portainer/portainer/api/internal/testhelpers"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func listFiles(dir string) []string {
|
|
items := make([]string, 0)
|
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
if path == dir {
|
|
return nil
|
|
}
|
|
items = append(items, path)
|
|
return nil
|
|
})
|
|
|
|
return items
|
|
}
|
|
|
|
func contains(t *testing.T, list []string, path string) {
|
|
assert.Contains(t, list, path)
|
|
copyContent, _ := ioutil.ReadFile(path)
|
|
assert.Equal(t, "content\n", string(copyContent))
|
|
}
|
|
|
|
func Test_backupHandlerWithoutPassword_shouldCreateATarballArchive(t *testing.T) {
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"password":""}`))
|
|
w := httptest.NewRecorder()
|
|
|
|
gate := offlinegate.NewOfflineGate()
|
|
adminMonitor := adminmonitor.New(time.Hour, nil, context.Background())
|
|
|
|
handlerErr := NewHandler(nil, i.NewDatastore(), gate, "./test_assets/handler_test", nil, func() {}, adminMonitor).backup(w, r)
|
|
assert.Nil(t, handlerErr, "Handler should not fail")
|
|
|
|
response := w.Result()
|
|
body, _ := io.ReadAll(response.Body)
|
|
|
|
tmpdir, _ := ioutil.TempDir("", "backup")
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
archivePath := filepath.Join(tmpdir, "archive.tar.gz")
|
|
err := ioutil.WriteFile(archivePath, body, 0600)
|
|
if err != nil {
|
|
t.Fatal("Failed to save downloaded .tar.gz archive: ", err)
|
|
}
|
|
cmd := exec.Command("tar", "-xzf", archivePath, "-C", tmpdir)
|
|
err = cmd.Run()
|
|
if err != nil {
|
|
t.Fatal("Failed to extract archive: ", err)
|
|
}
|
|
|
|
createdFiles := listFiles(tmpdir)
|
|
|
|
contains(t, createdFiles, path.Join(tmpdir, "portainer.key"))
|
|
contains(t, createdFiles, path.Join(tmpdir, "portainer.pub"))
|
|
contains(t, createdFiles, path.Join(tmpdir, "tls", "file1"))
|
|
contains(t, createdFiles, path.Join(tmpdir, "tls", "file2"))
|
|
assert.NotContains(t, createdFiles, path.Join(tmpdir, "extra_file"))
|
|
assert.NotContains(t, createdFiles, path.Join(tmpdir, "extra_folder", "file1"))
|
|
}
|
|
|
|
func Test_backupHandlerWithPassword_shouldCreateEncryptedATarballArchive(t *testing.T) {
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"password":"secret"}`))
|
|
w := httptest.NewRecorder()
|
|
|
|
gate := offlinegate.NewOfflineGate()
|
|
adminMonitor := adminmonitor.New(time.Hour, nil, nil)
|
|
|
|
handlerErr := NewHandler(nil, i.NewDatastore(), gate, "./test_assets/handler_test", nil, func() {}, adminMonitor).backup(w, r)
|
|
assert.Nil(t, handlerErr, "Handler should not fail")
|
|
|
|
response := w.Result()
|
|
body, _ := io.ReadAll(response.Body)
|
|
|
|
tmpdir, _ := ioutil.TempDir("", "backup")
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
dr, err := crypto.AesDecrypt(bytes.NewReader(body), []byte("secret"))
|
|
if err != nil {
|
|
t.Fatal("Failed to decrypt archive")
|
|
}
|
|
|
|
archivePath := filepath.Join(tmpdir, "archive.tag.gz")
|
|
archive, _ := os.Create(archivePath)
|
|
defer archive.Close()
|
|
io.Copy(archive, dr)
|
|
|
|
cmd := exec.Command("tar", "-xzf", archivePath, "-C", tmpdir)
|
|
err = cmd.Run()
|
|
if err != nil {
|
|
t.Fatal("Failed to extract archive: ", err)
|
|
}
|
|
|
|
createdFiles := listFiles(tmpdir)
|
|
|
|
contains(t, createdFiles, path.Join(tmpdir, "portainer.key"))
|
|
contains(t, createdFiles, path.Join(tmpdir, "portainer.pub"))
|
|
contains(t, createdFiles, path.Join(tmpdir, "tls", "file1"))
|
|
contains(t, createdFiles, path.Join(tmpdir, "tls", "file2"))
|
|
assert.NotContains(t, createdFiles, path.Join(tmpdir, "extra_file"))
|
|
assert.NotContains(t, createdFiles, path.Join(tmpdir, "extra_folder", "file1"))
|
|
}
|