github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/tests/rkt_exec_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  	"io/ioutil"
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/coreos/rkt/tests/testutils"
    24  )
    25  
    26  const (
    27  	noappManifestStr = `{"acKind":"ImageManifest","acVersion":"0.7.4","name":"coreos.com/rkt-inspect","labels":[{"name":"version","value":"1.0.0"},{"name":"arch","value":"amd64"},{"name":"os","value":"linux"}]}`
    28  )
    29  
    30  func TestRunOverrideExec(t *testing.T) {
    31  	noappManifestFile := "noapp-manifest.json"
    32  	if err := ioutil.WriteFile(noappManifestFile, []byte(noappManifestStr), 0600); err != nil {
    33  		t.Fatalf("Cannot write noapp manifest: %v", err)
    34  	}
    35  	defer os.Remove(noappManifestFile)
    36  	noappImage := patchTestACI("rkt-image-without-exec.aci", fmt.Sprintf("--manifest=%s", noappManifestFile))
    37  	defer os.Remove(noappImage)
    38  	execImage := patchTestACI("rkt-exec-override.aci", "--exec=/inspect")
    39  	defer os.Remove(execImage)
    40  	ctx := testutils.NewRktRunCtx()
    41  	defer ctx.Cleanup()
    42  
    43  	for _, tt := range []struct {
    44  		rktCmd       string
    45  		expectedLine string
    46  	}{
    47  		{
    48  			// Sanity check - make sure no --exec override prints the expected exec invocation
    49  			rktCmd:       fmt.Sprintf("%s --insecure-options=image run --mds-register=false %s -- --print-exec", ctx.Cmd(), execImage),
    50  			expectedLine: "inspect execed as: /inspect",
    51  		},
    52  		{
    53  			// Now test overriding the entrypoint (which is a symlink to /inspect so should behave identically)
    54  			rktCmd:       fmt.Sprintf("%s --insecure-options=image run --mds-register=false %s --exec /inspect-link -- --print-exec", ctx.Cmd(), execImage),
    55  			expectedLine: "inspect execed as: /inspect-link",
    56  		},
    57  		{
    58  			// Test overriding the entrypoint with a relative path
    59  			rktCmd:       fmt.Sprintf("%s --insecure-options=image run --mds-register=false %s --exec inspect-link-bin -- --print-exec", ctx.Cmd(), execImage),
    60  			expectedLine: "inspect execed as: inspect-link-bin",
    61  		},
    62  
    63  		{
    64  			// Test overriding the entrypoint with a missing app section
    65  			rktCmd:       fmt.Sprintf("%s --insecure-options=image run --mds-register=false %s --exec /inspect -- --print-exec", ctx.Cmd(), noappImage),
    66  			expectedLine: "inspect execed as: /inspect",
    67  		},
    68  	} {
    69  		runRktAndCheckOutput(t, tt.rktCmd, tt.expectedLine, false)
    70  	}
    71  }
    72  
    73  func TestRunPreparedOverrideExec(t *testing.T) {
    74  	execImage := patchTestACI("rkt-exec-override.aci", "--exec=/inspect")
    75  	defer os.Remove(execImage)
    76  	ctx := testutils.NewRktRunCtx()
    77  	defer ctx.Cleanup()
    78  
    79  	var rktCmd, uuid, expected string
    80  
    81  	// Sanity check - make sure no --exec override prints the expected exec invocation
    82  	rktCmd = fmt.Sprintf("%s prepare --insecure-options=image %s -- --print-exec", ctx.Cmd(), execImage)
    83  	uuid = runRktAndGetUUID(t, rktCmd)
    84  
    85  	rktCmd = fmt.Sprintf("%s run-prepared --mds-register=false %s", ctx.Cmd(), uuid)
    86  	expected = "inspect execed as: /inspect"
    87  	runRktAndCheckOutput(t, rktCmd, expected, false)
    88  
    89  	// Now test overriding the entrypoint (which is a symlink to /inspect so should behave identically)
    90  	rktCmd = fmt.Sprintf("%s prepare --insecure-options=image %s --exec /inspect-link -- --print-exec", ctx.Cmd(), execImage)
    91  	uuid = runRktAndGetUUID(t, rktCmd)
    92  
    93  	rktCmd = fmt.Sprintf("%s run-prepared --mds-register=false %s", ctx.Cmd(), uuid)
    94  	expected = "inspect execed as: /inspect-link"
    95  	runRktAndCheckOutput(t, rktCmd, expected, false)
    96  }