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

Commit

Permalink
functional tests: refactor TestExport
Browse files Browse the repository at this point in the history
So we can test with fly.
  • Loading branch information
iaguis committed Jul 14, 2016
1 parent 615d32a commit 2da24a7
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 87 deletions.
39 changes: 39 additions & 0 deletions tests/rkt_export_container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2016 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !fly

package main

import (
"testing"

"github.com/coreos/rkt/common"
)

func TestExport(t *testing.T) {
testCases := []ExportTestCase{noOverlaySimpleTest}

// Need to do both checks
if common.SupportsUserNS() && checkUserNS() == nil {
testCases = append(testCases, userNS)
}

if common.SupportsOverlay() {
testCases = append(testCases, overlaySimpleTest)
testCases = append(testCases, overlaySimulateReboot)
}

NewTestExport(testCases...).Execute(t)
}
33 changes: 33 additions & 0 deletions tests/rkt_export_fly_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2016 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build fly

package main

import (
"testing"

"github.com/coreos/rkt/common"
)

func TestExport(t *testing.T) {
if !common.SupportsOverlay() {
t.Skip("Overlay fs not supported.")
}

// TODO(iaguis): we need a new function to unmount the fly pod so we can also test
// overlaySimulateReboot
NewTestExport(overlaySimpleTest).Execute(t)
}
174 changes: 87 additions & 87 deletions tests/rkt_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,111 +20,111 @@ import (
"path/filepath"
"testing"

"github.com/coreos/rkt/common"
"github.com/coreos/rkt/tests/testutils"
)

func TestExport(t *testing.T) {
const (
testAci = "test.aci"
testFile = "test.txt"
testContent = "ThisIsATest"
)
const (
testAci = "test.aci"
testFile = "test.txt"
testContent = "ThisIsATest"
)

ctx := testutils.NewRktRunCtx()
defer ctx.Cleanup()
type ExportTestCase struct {
runArgs string
writeArgs string
readArgs string
expectedResult string
unmountOverlay bool
}

type exportTest []ExportTestCase

type testCfg struct {
runArgs string
writeArgs string
readArgs string
expectedResult string
unmountOverlay bool
var (
noOverlaySimpleTest = ExportTestCase{
"--no-overlay --insecure-options=image",
"--write-file --file-name=" + testFile + " --content=" + testContent,
"--read-file --file-name=" + testFile,
testContent,
false,
}

tests := []testCfg{
{
"--no-overlay --insecure-options=image",
"--write-file --file-name=" + testFile + " --content=" + testContent,
"--read-file --file-name=" + testFile,
testContent,
false,
},
userNS = ExportTestCase{
"--private-users --no-overlay --insecure-options=image",
"--write-file --file-name=" + testFile + " --content=" + testContent,
"--read-file --file-name=" + testFile,
testContent,
false,
}

// Need to do both checks
if common.SupportsUserNS() && checkUserNS() == nil {
tests = append(tests, []testCfg{
{
"--private-users --no-overlay --insecure-options=image",
"--write-file --file-name=" + testFile + " --content=" + testContent,
"--read-file --file-name=" + testFile,
testContent,
false,
},
}...)
overlaySimpleTest = ExportTestCase{
"--insecure-options=image",
"--write-file --file-name=" + testFile + " --content=" + testContent,
"--read-file --file-name=" + testFile,
testContent,
false,
}

if common.SupportsOverlay() {
tests = append(tests, []testCfg{
{
"--insecure-options=image",
"--write-file --file-name=" + testFile + " --content=" + testContent,
"--read-file --file-name=" + testFile,
testContent,
false,
},
// Test unmounting overlay to simulate a reboot
{
"--insecure-options=image",
"--write-file --file-name=" + testFile + " --content=" + testContent,
"--read-file --file-name=" + testFile,
testContent,
true,
},
}...)
overlaySimulateReboot = ExportTestCase{
"--insecure-options=image",
"--write-file --file-name=" + testFile + " --content=" + testContent,
"--read-file --file-name=" + testFile,
testContent,
true,
}
)

func (ct exportTest) Execute(t *testing.T) {
ctx := testutils.NewRktRunCtx()
defer ctx.Cleanup()

for _, testCase := range ct {
testCase.Execute(t, ctx)
}
}

for _, tt := range tests {
tmpDir := createTempDirOrPanic("rkt-TestExport-tmp-")
defer os.RemoveAll(tmpDir)
func (ct ExportTestCase) Execute(t *testing.T, ctx *testutils.RktRunCtx) {
tmpDir := createTempDirOrPanic("rkt-TestExport-tmp-")
defer os.RemoveAll(tmpDir)

tmpTestAci := filepath.Join(tmpDir, testAci)
tmpTestAci := filepath.Join(tmpDir, testAci)

// Prepare the image with modifications
const runInspect = "%s %s %s %s --exec=/inspect -- %s"
prepareCmd := fmt.Sprintf(runInspect, ctx.Cmd(), "prepare", tt.runArgs, getInspectImagePath(), tt.writeArgs)
t.Logf("Preparing 'inspect --write-file'")
uuid := runRktAndGetUUID(t, prepareCmd)
// Prepare the image with modifications
const runInspect = "%s %s %s %s --exec=/inspect -- %s"
prepareCmd := fmt.Sprintf(runInspect, ctx.Cmd(), "prepare", ct.runArgs, getInspectImagePath(), ct.writeArgs)
t.Logf("Preparing 'inspect --write-file'")
uuid := runRktAndGetUUID(t, prepareCmd)

runCmd := fmt.Sprintf("%s run-prepared %s", ctx.Cmd(), uuid)
t.Logf("Running 'inspect --write-file'")
child := spawnOrFail(t, runCmd)
waitOrFail(t, child, 0)
runCmd := fmt.Sprintf("%s run-prepared %s", ctx.Cmd(), uuid)
t.Logf("Running 'inspect --write-file'")
child := spawnOrFail(t, runCmd)
waitOrFail(t, child, 0)

if tt.unmountOverlay {
unmountPod(t, ctx, uuid, true)
}
if ct.unmountOverlay {
unmountPod(t, ctx, uuid, true)
}

// Export the image
exportCmd := fmt.Sprintf("%s export %s %s", ctx.Cmd(), uuid, tmpTestAci)
t.Logf("Running 'export'")
child = spawnOrFail(t, exportCmd)
waitOrFail(t, child, 0)

// Run the newly created ACI and check the output
readCmd := fmt.Sprintf(runInspect, ctx.Cmd(), "run", tt.runArgs, tmpTestAci, tt.readArgs)
t.Logf("Running 'inspect --read-file'")
child = spawnOrFail(t, readCmd)
if tt.expectedResult != "" {
if _, out, err := expectRegexWithOutput(child, tt.expectedResult); err != nil {
t.Fatalf("expected %q but not found: %v\n%s", tt.expectedResult, err, out)
}
// Export the image
exportCmd := fmt.Sprintf("%s export %s %s", ctx.Cmd(), uuid, tmpTestAci)
t.Logf("Running 'export'")
child = spawnOrFail(t, exportCmd)
waitOrFail(t, child, 0)

// Run the newly created ACI and check the output
readCmd := fmt.Sprintf(runInspect, ctx.Cmd(), "run", ct.runArgs, tmpTestAci, ct.readArgs)
t.Logf("Running 'inspect --read-file'")
child = spawnOrFail(t, readCmd)
if ct.expectedResult != "" {
if _, out, err := expectRegexWithOutput(child, ct.expectedResult); err != nil {
t.Fatalf("expected %q but not found: %v\n%s", ct.expectedResult, err, out)
}
waitOrFail(t, child, 0)

// run garbage collector on pods and images
runGC(t, ctx)
runImageGC(t, ctx)
}
waitOrFail(t, child, 0)

// run garbage collector on pods and images
runGC(t, ctx)
runImageGC(t, ctx)
}

func NewTestExport(cases ...ExportTestCase) testutils.Test {
return exportTest(cases)
}

0 comments on commit 2da24a7

Please sign in to comment.