github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/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  // +build host coreos src kvm
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/rkt/rkt/common"
    26  	"github.com/rkt/rkt/pkg/aci/acitest"
    27  	"github.com/rkt/rkt/tests/testutils"
    28  
    29  	"github.com/appc/spec/schema"
    30  	"github.com/appc/spec/schema/types"
    31  )
    32  
    33  func TestRunOverrideExec(t *testing.T) {
    34  	// noappManifest specifies an image manifest configuration without
    35  	// an application section.
    36  	noappManifest := schema.ImageManifest{
    37  		Name: "coreos.com/rkt-inspect",
    38  		Labels: types.Labels{
    39  			{"version", "1.30.0"},
    40  			{"arch", common.GetArch()},
    41  			{"os", common.GetOS()},
    42  		},
    43  	}
    44  
    45  	noappManifestStr, err := acitest.ImageManifestString(&noappManifest)
    46  	if err != nil {
    47  		t.Fatalf("unexpected error: %v", err)
    48  	}
    49  
    50  	noappManifestFile := "noapp-manifest.json"
    51  	if err := ioutil.WriteFile(noappManifestFile, []byte(noappManifestStr), 0600); err != nil {
    52  		t.Fatalf("Cannot write noapp manifest: %v", err)
    53  	}
    54  	defer os.Remove(noappManifestFile)
    55  	noappImage := patchTestACI("rkt-image-without-exec.aci", fmt.Sprintf("--manifest=%s", noappManifestFile))
    56  	defer os.Remove(noappImage)
    57  	execImage := patchTestACI("rkt-exec-override.aci", "--exec=/inspect")
    58  	defer os.Remove(execImage)
    59  	ctx := testutils.NewRktRunCtx()
    60  	defer ctx.Cleanup()
    61  
    62  	for _, tt := range []struct {
    63  		rktCmd        string
    64  		expectedRegex string
    65  	}{
    66  		{
    67  			// Sanity check - make sure no --exec override prints the expected exec invocation
    68  			rktCmd:        fmt.Sprintf("%s --insecure-options=image run --mds-register=false %s -- --print-exec", ctx.Cmd(), execImage),
    69  			expectedRegex: "inspect exceed as: /inspect",
    70  		},
    71  		{
    72  			// Now test overriding the entrypoint (which is a symlink to /inspect so should behave identically)
    73  			rktCmd:        fmt.Sprintf("%s --insecure-options=image run --mds-register=false %s --exec /inspect-link -- --print-exec", ctx.Cmd(), execImage),
    74  			expectedRegex: "inspect exceed as: /inspect-link",
    75  		},
    76  		{
    77  			// Test overriding the entrypoint with a relative path
    78  			rktCmd:        fmt.Sprintf("%s --insecure-options=image run --mds-register=false %s --exec inspect-link-bin -- --print-exec", ctx.Cmd(), execImage),
    79  			expectedRegex: "inspect exceed as: .*inspect-link-bin",
    80  		},
    81  
    82  		{
    83  			// Test overriding the entrypoint with a missing app section
    84  			rktCmd:        fmt.Sprintf("%s --insecure-options=image run --mds-register=false %s --exec /inspect -- --print-exec", ctx.Cmd(), noappImage),
    85  			expectedRegex: "inspect exceed as: /inspect",
    86  		},
    87  	} {
    88  		runRktAndCheckRegexOutput(t, tt.rktCmd, tt.expectedRegex)
    89  	}
    90  }
    91  
    92  func TestRunPreparedOverrideExec(t *testing.T) {
    93  	execImage := patchTestACI("rkt-exec-override.aci", "--exec=/inspect")
    94  	defer os.Remove(execImage)
    95  	ctx := testutils.NewRktRunCtx()
    96  	defer ctx.Cleanup()
    97  
    98  	var rktCmd, uuid, expected string
    99  
   100  	// Sanity check - make sure no --exec override prints the expected exec invocation
   101  	rktCmd = fmt.Sprintf("%s prepare --insecure-options=image %s -- --print-exec", ctx.Cmd(), execImage)
   102  	uuid = runRktAndGetUUID(t, rktCmd)
   103  
   104  	rktCmd = fmt.Sprintf("%s run-prepared --mds-register=false %s", ctx.Cmd(), uuid)
   105  	expected = "inspect exceed as: /inspect"
   106  	runRktAndCheckOutput(t, rktCmd, expected, false)
   107  
   108  	// Now test overriding the entrypoint (which is a symlink to /inspect so should behave identically)
   109  	rktCmd = fmt.Sprintf("%s prepare --insecure-options=image %s --exec /inspect-link -- --print-exec", ctx.Cmd(), execImage)
   110  	uuid = runRktAndGetUUID(t, rktCmd)
   111  
   112  	rktCmd = fmt.Sprintf("%s run-prepared --mds-register=false %s", ctx.Cmd(), uuid)
   113  	expected = "inspect exceed as: /inspect-link"
   114  	runRktAndCheckOutput(t, rktCmd, expected, false)
   115  }