Skip to content

Commit

Permalink
tests for pause, resume, createSnapshot
Browse files Browse the repository at this point in the history
Signed-off-by: Plamen Petrov <plamb0brt@gmail.com>
  • Loading branch information
plamenmpetrov committed Nov 9, 2020
1 parent 5e1606f commit b86c07b
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ GID = $(shell id -g)

# The below files are needed and can be downloaded from the internet
testdata_objects = testdata/vmlinux testdata/root-drive.img testdata/firecracker testdata/jailer
firecracker_version = v0.22.0
firecracker_version = v0.23.0

# --location is needed to follow redirects on github.com
curl = curl --location
Expand Down
169 changes: 169 additions & 0 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,3 +1476,172 @@ func TestSignalForwarding(t *testing.T) {

assert.ElementsMatch(t, forwardedSignals, receivedSignals)
}

func TestPauseResume(t *testing.T) {
fctesting.RequiresRoot(t)

cases := []struct {
name string
state func(m *Machine, ctx context.Context)
}{
{
name: "PauseVM",
state: func(m *Machine, ctx context.Context) {
err := m.PauseVM(ctx)
require.NoError(t, err)
},
},
{
name: "ResumeVM",
state: func(m *Machine, ctx context.Context) {
err := m.ResumeVM(ctx)
require.NoError(t, err)
},
},
{
name: "Consecutive PauseVM",
state: func(m *Machine, ctx context.Context) {
err := m.PauseVM(ctx)
require.NoError(t, err)

err = m.PauseVM(ctx)
require.NoError(t, err)
},
},
{
name: "Consecutive ResumeVM",
state: func(m *Machine, ctx context.Context) {
err := m.ResumeVM(ctx)
require.NoError(t, err)

err = m.ResumeVM(ctx)
require.NoError(t, err)
},
},
{
name: "ResumeVM PauseVM",
state: func(m *Machine, ctx context.Context) {
err := m.ResumeVM(ctx)
require.NoError(t, err)

err = m.PauseVM(ctx)
require.NoError(t, err)
},
},
{
name: "PauseVM ResumeVM",
state: func(m *Machine, ctx context.Context) {
err := m.PauseVM(ctx)
require.NoError(t, err)

err = m.ResumeVM(ctx)
require.NoError(t, err)
},
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ctx := context.Background()

socketPath := filepath.Join(testDataPath, fsSafeTestName.Replace(t.Name()))
defer os.Remove(socketPath)

// Tee logs for validation:
var logBuffer bytes.Buffer
machineLogger := logrus.New()
machineLogger.Out = io.MultiWriter(os.Stderr, &logBuffer)

cfg := createValidConfig(t, socketPath)
m, err := NewMachine(ctx, cfg, func(m *Machine) {
// Rewriting m.cmd partially wouldn't work since Cmd has
// some unexported members
args := m.cmd.Args[1:]
m.cmd = exec.Command(getFirecrackerBinaryPath(), args...)
}, WithLogger(logrus.NewEntry(machineLogger)))
require.NoError(t, err)

err = m.PauseVM(ctx)
require.Error(t, err, "PauseVM must fail before Start is called")

err = m.ResumeVM(ctx)
require.Error(t, err, "ResumeVM must fail before Start is called")

err = m.Start(ctx)
require.NoError(t, err)

c.state(m, ctx)

err = m.StopVMM()
require.NoError(t, err)

err = m.PauseVM(ctx)
require.Error(t, err, "PauseVM must fail after StopVMM is called")

err = m.ResumeVM(ctx)
require.Error(t, err, "ResumeVM must fail after StopVMM is called")
})
}
}

func TestCreateSnapshot(t *testing.T) {
fctesting.RequiresRoot(t)

cases := []struct {
name string
createSnapshot func(m *Machine, ctx context.Context, memPath, snapPath string)
}{
{
name: "CreateSnapshot",
createSnapshot: func(m *Machine, ctx context.Context, memPath, snapPath string) {
err := m.PauseVM(ctx)
require.NoError(t, err)

err = m.CreateSnapshot(ctx, memPath, snapPath)
require.NoError(t, err)
},
},
{
name: "CreateSnapshot before pause",
createSnapshot: func(m *Machine, ctx context.Context, memPath, snapPath string) {
err := m.CreateSnapshot(ctx, memPath, snapPath)
require.Error(t, err)
},
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ctx := context.Background()

socketPath := filepath.Join(testDataPath, fsSafeTestName.Replace(t.Name()))
snapPath := socketPath + "SnapFile"
memPath := socketPath + "MemFile"
defer os.Remove(socketPath)
defer os.Remove(snapPath)
defer os.Remove(memPath)

// Tee logs for validation:
var logBuffer bytes.Buffer
machineLogger := logrus.New()
machineLogger.Out = io.MultiWriter(os.Stderr, &logBuffer)

cfg := createValidConfig(t, socketPath)
m, err := NewMachine(ctx, cfg, func(m *Machine) {
// Rewriting m.cmd partially wouldn't work since Cmd has
// some unexported members
args := m.cmd.Args[1:]
m.cmd = exec.Command(getFirecrackerBinaryPath(), args...)
}, WithLogger(logrus.NewEntry(machineLogger)))
require.NoError(t, err)

err = m.Start(ctx)
require.NoError(t, err)

c.createSnapshot(m, ctx, memPath, snapPath)

err = m.StopVMM()
require.NoError(t, err)
})
}
}

0 comments on commit b86c07b

Please sign in to comment.