github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/tests/rkt_exit_test.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/coreos/rkt/tests/testutils"
    24  )
    25  
    26  // TestExitCodeSimple is testing a few exit codes on 1 pod containing just 1 app
    27  func TestExitCodeSimple(t *testing.T) {
    28  	for i := 0; i < 3; i++ {
    29  		t.Logf("%d\n", i)
    30  		imageFile := patchTestACI("rkt-inspect-exit.aci", fmt.Sprintf("--exec=/inspect --print-msg=Hello --exit-code=%d", i))
    31  		defer os.Remove(imageFile)
    32  		ctx := testutils.NewRktRunCtx()
    33  		defer ctx.Cleanup()
    34  
    35  		cmd := fmt.Sprintf(`%s --debug --insecure-options=image run --mds-register=false %s`,
    36  			ctx.Cmd(), imageFile)
    37  		t.Logf("%s\n", cmd)
    38  		spawnAndWaitOrFail(t, cmd, true)
    39  		checkAppStatus(t, ctx, false, "rkt-inspect", fmt.Sprintf("status=%d", i))
    40  	}
    41  }
    42  
    43  // TestExitCodeWithSeveralApps is testing a pod with three apps returning different
    44  // exit codes.
    45  func TestExitCodeWithSeveralApps(t *testing.T) {
    46  	image0File := patchTestACI("rkt-inspect-exit-0.aci", "--name=hello0",
    47  		"--exec=/inspect --print-msg=HelloWorld --exit-code=0")
    48  	defer os.Remove(image0File)
    49  
    50  	image1File := patchTestACI("rkt-inspect-exit-1.aci", "--name=hello1",
    51  		"--exec=/inspect --print-msg=HelloWorld --exit-code=1")
    52  	defer os.Remove(image1File)
    53  
    54  	image2File := patchTestACI("rkt-inspect-exit-2.aci", "--name=hello2",
    55  		"--exec=/inspect --print-msg=HelloWorld --exit-code=2 --sleep=1")
    56  	defer os.Remove(image2File)
    57  
    58  	ctx := testutils.NewRktRunCtx()
    59  	defer ctx.Cleanup()
    60  
    61  	cmd := fmt.Sprintf(`%s --debug --insecure-options=image run --mds-register=false %s %s %s`,
    62  		ctx.Cmd(), image0File, image1File, image2File)
    63  	child := spawnOrFail(t, cmd)
    64  
    65  	for i := 0; i < 3; i++ {
    66  		// The 3 apps print the same message. We don't have any ordering
    67  		// guarantee but we don't need it.
    68  		if err := expectTimeoutWithOutput(child, "HelloWorld", time.Minute); err != nil {
    69  			t.Fatalf("Could not start the app (#%d): %v", i, err)
    70  		}
    71  	}
    72  
    73  	t.Logf("Waiting pod termination\n")
    74  	waitOrFail(t, child, true)
    75  
    76  	t.Logf("Check final status\n")
    77  
    78  	checkAppStatus(t, ctx, true, "hello0", "status=0")
    79  	checkAppStatus(t, ctx, true, "hello1", "status=1")
    80  	// Currently, hello2 should be stop correctly (exit code 0) when hello1
    81  	// failed, so it cannot return its exit code 2. This might change with
    82  	// https://github.com/coreos/rkt/issues/1461
    83  	checkAppStatus(t, ctx, true, "hello2", "status=0")
    84  }