open-nomad/command/operator_snapshot_save_test.go
2020-05-21 20:04:38 -04:00

52 lines
1.1 KiB
Go

package command
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/hashicorp/nomad/command/agent"
"github.com/hashicorp/nomad/helper/snapshot"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
)
func TestOperatorSnapshotSave_Works(t *testing.T) {
t.Parallel()
tmpDir, err := ioutil.TempDir("", "nomad-tempdir")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
srv, _, url := testServer(t, false, func(c *agent.Config) {
c.DevMode = false
c.DataDir = filepath.Join(tmpDir, "server")
c.AdvertiseAddrs.HTTP = "127.0.0.1"
c.AdvertiseAddrs.RPC = "127.0.0.1"
c.AdvertiseAddrs.Serf = "127.0.0.1"
})
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &OperatorSnapshotSaveCommand{Meta: Meta{Ui: ui}}
dest := filepath.Join(tmpDir, "backup.snap")
code := cmd.Run([]string{
"--address=" + url,
dest,
})
require.Zero(t, code)
require.Contains(t, ui.OutputWriter.String(), "State file written to "+dest)
f, err := os.Open(dest)
require.NoError(t, err)
meta, err := snapshot.Verify(f)
require.NoError(t, err)
require.NotZero(t, meta.Index)
}