Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Commit

Permalink
tests/sandbox: add functional test for mounts
Browse files Browse the repository at this point in the history
  • Loading branch information
s-urbaniak committed Jan 16, 2017
1 parent be03488 commit b87c707
Showing 1 changed file with 94 additions and 31 deletions.
125 changes: 94 additions & 31 deletions tests/rkt_app_sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -43,17 +44,17 @@ func TestAppSandboxAddStartRemove(t *testing.T) {
aciHello := patchTestACI("rkt-inspect-hello.aci", "--name="+imageName, "--exec=/inspect --print-msg="+msg)
defer os.Remove(aciHello)

testCmd{ctx.ExecCmd("fetch", "--insecure-options=image", aciHello)}.CombinedOutput(t)
testCmd{ctx.ExecCmd("app", "add", "--debug", podUUID, imageName, "--name="+appName)}.CombinedOutput(t)
testCmd{ctx.ExecCmd("app", "start", "--debug", podUUID, "--app="+appName)}.CombinedOutput(t)
combinedOutput(t, ctx.ExecCmd("fetch", "--insecure-options=image", aciHello))
combinedOutput(t, ctx.ExecCmd("app", "add", "--debug", podUUID, imageName, "--name="+appName))
combinedOutput(t, ctx.ExecCmd("app", "start", "--debug", podUUID, "--app="+appName))

if err := expectTimeoutWithOutput(child, msg, actionTimeout); err != nil {
t.Fatalf("Expected %q but not found: %v", msg, err)
}

testCmd{ctx.ExecCmd("app", "rm", "--debug", podUUID, "--app="+appName)}.CombinedOutput(t)
combinedOutput(t, ctx.ExecCmd("app", "rm", "--debug", podUUID, "--app="+appName))

out := testCmd{ctx.ExecCmd("app", "list", "--no-legend", podUUID)}.CombinedOutput(t)
out := combinedOutput(t, ctx.ExecCmd("app", "list", "--no-legend", podUUID))
if out != "\n" {
t.Errorf("unexpected output %q", out)
return
Expand All @@ -65,7 +66,7 @@ func TestAppSandboxAddStartRemove(t *testing.T) {
// one that exits successfully, one that exits with an error, and one that keeps running.
func TestAppSandboxMultipleApps(t *testing.T) {
testSandbox(t, func(ctx *testutils.RktRunCtx, child *gexpect.ExpectSubprocess, podUUID string) {
actionTimeout := 60 * time.Second
actionTimeout := 30 * time.Second

type app struct {
name, image, exec, aci string
Expand Down Expand Up @@ -97,9 +98,9 @@ func TestAppSandboxMultipleApps(t *testing.T) {
aci := patchTestACI(app.aci, "--name="+app.image, "--exec="+app.exec)
defer os.Remove(aci)

testCmd{ctx.ExecCmd("fetch", "--insecure-options=image", aci)}.CombinedOutput(t)
testCmd{ctx.ExecCmd("app", "add", "--debug", podUUID, app.image, "--name="+app.name)}.CombinedOutput(t)
testCmd{ctx.ExecCmd("app", "start", "--debug", podUUID, "--app="+app.name)}.CombinedOutput(t)
combinedOutput(t, ctx.ExecCmd("fetch", "--insecure-options=image", aci))
combinedOutput(t, ctx.ExecCmd("app", "add", "--debug", podUUID, app.image, "--name="+app.name))
combinedOutput(t, ctx.ExecCmd("app", "start", "--debug", podUUID, "--app="+app.name))
}

// check for app output messages
Expand All @@ -121,7 +122,7 @@ func TestAppSandboxMultipleApps(t *testing.T) {

// assert `rkt app list` for the apps
if err := r.Retry(func() error {
got := testCmd{ctx.ExecCmd("app", "list", "--no-legend", podUUID)}.CombinedOutput(t)
got := combinedOutput(t, ctx.ExecCmd("app", "list", "--no-legend", podUUID))

if strings.Contains(got, "winner\texited") &&
strings.Contains(got, "loser\texited") &&
Expand Down Expand Up @@ -160,7 +161,7 @@ func TestAppSandboxMultipleApps(t *testing.T) {
},
} {
if err := r.Retry(func() error {
got := testCmd{ctx.ExecCmd("app", "status", podUUID, "--app="+app.name)}.CombinedOutput(t)
got := combinedOutput(t, ctx.ExecCmd("app", "status", podUUID, "--app="+app.name))
ok := true

if app.checkExitCode {
Expand All @@ -182,12 +183,12 @@ func TestAppSandboxMultipleApps(t *testing.T) {

// remove all apps
for _, app := range apps {
testCmd{ctx.ExecCmd("app", "rm", "--debug", podUUID, "--app="+app.name)}.CombinedOutput(t)
combinedOutput(t, ctx.ExecCmd("app", "rm", "--debug", podUUID, "--app="+app.name))
}

// assert empty `rkt app list`, no need for retrying,
// as after removal no leftovers are expected to be present
got := testCmd{ctx.ExecCmd("app", "list", "--no-legend", podUUID)}.CombinedOutput(t)
got := combinedOutput(t, ctx.ExecCmd("app", "list", "--no-legend", podUUID))
if got != "\n" {
t.Errorf("unexpected result, got %q", got)
return
Expand Down Expand Up @@ -222,9 +223,9 @@ func TestAppSandboxRestart(t *testing.T) {
aci := patchTestACI(app.aci, "--name="+app.image, "--exec="+app.exec)
defer os.Remove(aci)

testCmd{ctx.ExecCmd("fetch", "--insecure-options=image", aci)}.CombinedOutput(t)
testCmd{ctx.ExecCmd("app", "add", "--debug", podUUID, app.image, "--name="+app.name)}.CombinedOutput(t)
testCmd{ctx.ExecCmd("app", "start", "--debug", podUUID, "--app="+app.name)}.CombinedOutput(t)
combinedOutput(t, ctx.ExecCmd("fetch", "--insecure-options=image", aci))
combinedOutput(t, ctx.ExecCmd("app", "add", "--debug", podUUID, app.image, "--name="+app.name))
combinedOutput(t, ctx.ExecCmd("app", "start", "--debug", podUUID, "--app="+app.name))
}

// total retry timeout: 10s
Expand All @@ -235,7 +236,7 @@ func TestAppSandboxRestart(t *testing.T) {

// assert `rkt app list` for the apps
if err := r.Retry(func() error {
got := testCmd{ctx.ExecCmd("app", "list", "--no-legend", podUUID)}.CombinedOutput(t)
got := combinedOutput(t, ctx.ExecCmd("app", "list", "--no-legend", podUUID))

if strings.Contains(got, "app1\trunning") &&
strings.Contains(got, "app2\trunning") {
Expand All @@ -250,7 +251,7 @@ func TestAppSandboxRestart(t *testing.T) {

assertStatus := func(name, status string) error {
return r.Retry(func() error {
got := testCmd{ctx.ExecCmd("app", "status", podUUID, "--app="+name)}.CombinedOutput(t)
got := combinedOutput(t, ctx.ExecCmd("app", "status", podUUID, "--app="+name))

if !strings.Contains(got, status) {
return fmt.Errorf("unexpected result, got %q", got)
Expand All @@ -269,7 +270,7 @@ func TestAppSandboxRestart(t *testing.T) {
}

// stop app1
testCmd{ctx.ExecCmd("app", "stop", podUUID, "--app=app1")}.CombinedOutput(t)
combinedOutput(t, ctx.ExecCmd("app", "stop", podUUID, "--app=app1"))

// assert `rkt app status` for the apps
for _, app := range []struct {
Expand All @@ -293,7 +294,7 @@ func TestAppSandboxRestart(t *testing.T) {

// assert `rkt app list` for the apps
if err := r.Retry(func() error {
got := testCmd{ctx.ExecCmd("app", "list", "--no-legend", podUUID)}.CombinedOutput(t)
got := combinedOutput(t, ctx.ExecCmd("app", "list", "--no-legend", podUUID))

if strings.Contains(got, "app1\texited") &&
strings.Contains(got, "app2\trunning") {
Expand All @@ -307,7 +308,7 @@ func TestAppSandboxRestart(t *testing.T) {
}

// start app1
testCmd{ctx.ExecCmd("app", "start", podUUID, "--app=app1")}.CombinedOutput(t)
combinedOutput(t, ctx.ExecCmd("app", "start", podUUID, "--app=app1"))

// assert `rkt app status` for the apps
for _, app := range []struct {
Expand All @@ -331,7 +332,7 @@ func TestAppSandboxRestart(t *testing.T) {

// assert `rkt app list` for the apps
if err := r.Retry(func() error {
got := testCmd{ctx.ExecCmd("app", "list", "--no-legend", podUUID)}.CombinedOutput(t)
got := combinedOutput(t, ctx.ExecCmd("app", "list", "--no-legend", podUUID))

if strings.Contains(got, "app1\trunning") &&
strings.Contains(got, "app2\trunning") {
Expand All @@ -346,6 +347,72 @@ func TestAppSandboxRestart(t *testing.T) {
})
}

func TestAppSandboxMount(t *testing.T) {
mntSrcDir := mustTempDir("rkt-mount-test-")
defer os.RemoveAll(mntSrcDir)

mntSrcFile := filepath.Join(mntSrcDir, "test")
if err := ioutil.WriteFile(mntSrcFile, []byte("content"), 0666); err != nil {
t.Fatalf("Cannot write file: %v", err)
}

testSandbox(t, func(ctx *testutils.RktRunCtx, child *gexpect.ExpectSubprocess, podUUID string) {
aci := patchTestACI(
"rkt-inspect-mounter.aci",
"--name=coreos.com/rkt-inspect/mounter",
"--exec=/inspect -read-file",
)
defer os.Remove(aci)
combinedOutput(t, ctx.ExecCmd("fetch", "--insecure-options=image", aci))

for _, tt := range []struct {
mntTarget string
expectedFile string
}{
{
mntTarget: "/dir2",
expectedFile: "/dir2/test",
},
{
mntTarget: "/dir1/link_rel_dir2",
expectedFile: "/dir2/test",
},
{
mntTarget: "/dir1/link_abs_dir2",
expectedFile: "/dir2/test",
},
{
mntTarget: "/dir1/link_abs_root/notexists",
expectedFile: "/notexists/test",
},
{
mntTarget: "/../../../../../../../../notexists",
expectedFile: "/notexists/test",
},
{
mntTarget: "../../../../../../../../notexists",
expectedFile: "/notexists/test",
},
} {
combinedOutput(t, ctx.ExecCmd(
"app", "add", "--debug", podUUID,
"coreos.com/rkt-inspect/mounter",
"--name=mounter",
"--environment=FILE="+tt.expectedFile,
"--mnt-volume=name=test,kind=host,source="+mntSrcDir+",target="+tt.mntTarget,
))

combinedOutput(t, ctx.ExecCmd("app", "start", "--debug", podUUID, "--app=mounter"))

if err := expectTimeoutWithOutput(child, "content", 10*time.Second); err != nil {
t.Fatalf("Expected \"content\" but not found: %v", err)
}

combinedOutput(t, ctx.ExecCmd("app", "rm", "--debug", podUUID, "--app=mounter"))
}
})
}

func testSandbox(t *testing.T, testFunc func(*testutils.RktRunCtx, *gexpect.ExpectSubprocess, string)) {
if err := os.Setenv("RKT_EXPERIMENT_APP", "true"); err != nil {
panic(err)
Expand All @@ -371,13 +438,13 @@ func testSandbox(t *testing.T, testFunc func(*testutils.RktRunCtx, *gexpect.Expe
testFunc(ctx, child, podUUID)

// assert that the pod is still running
got := testCmd{ctx.ExecCmd("status", podUUID)}.CombinedOutput(t)
got := combinedOutput(t, ctx.ExecCmd("status", podUUID))
if !strings.Contains(got, "state=running") {
t.Errorf("unexpected result, got %q", got)
return
}

testCmd{ctx.ExecCmd("stop", podUUID)}.CombinedOutput(t)
combinedOutput(t, ctx.ExecCmd("stop", podUUID))

waitOrFail(t, child, 0)
}
Expand Down Expand Up @@ -405,16 +472,12 @@ func (r retry) Retry(f func() error) error {
return err
}

type testCmd struct {
*exec.Cmd
}

func (c testCmd) CombinedOutput(t *testing.T) string {
func combinedOutput(t *testing.T, c *exec.Cmd) string {
t.Log("Running", c.Args)
out, err := c.Cmd.CombinedOutput()
out, err := c.CombinedOutput()

if err != nil {
t.Fatal(err, "output", out)
t.Fatal(err, "output", string(out))
}

return string(out)
Expand Down

0 comments on commit b87c707

Please sign in to comment.