github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_env_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 fly 16 17 package main 18 19 import ( 20 "os" 21 "strings" 22 "testing" 23 24 "github.com/rkt/rkt/tests/testutils" 25 ) 26 27 var envTests = []struct { 28 runCmd string 29 runExpect string 30 sleepCmd string 31 enterCmd string 32 }{ 33 { 34 `^RKT_BIN^ --debug --insecure-options=image run --mds-register=false ^PRINT_VAR_FROM_MANIFEST^`, 35 "VAR_FROM_MANIFEST=manifest", 36 `^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --interactive ^SLEEP^`, 37 `/bin/sh -c "^RKT_BIN^ --debug enter $(^RKT_BIN^ list --full|grep running|awk '{print $1}') /inspect --print-env=VAR_FROM_MANIFEST"`, 38 }, 39 { 40 `^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --set-env=VAR_OTHER=setenv ^PRINT_VAR_OTHER^`, 41 "VAR_OTHER=setenv", 42 `^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --interactive --set-env=VAR_OTHER=setenv ^SLEEP^`, 43 `/bin/sh -c "^RKT_BIN^ --debug enter $(^RKT_BIN^ list --full|grep running|awk '{print $1}') /inspect --print-env=VAR_OTHER"`, 44 }, 45 { 46 `^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --set-env=VAR_FROM_MANIFEST=setenv ^PRINT_VAR_FROM_MANIFEST^`, 47 "VAR_FROM_MANIFEST=setenv", 48 `^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --interactive --set-env=VAR_FROM_MANIFEST=setenv ^SLEEP^`, 49 `/bin/sh -c "^RKT_BIN^ --debug enter $(^RKT_BIN^ list --full|grep running|awk '{print $1}') /inspect --print-env=VAR_FROM_MANIFEST"`, 50 }, 51 { 52 `/bin/sh -c "export VAR_OTHER=host ; ^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --inherit-env=true ^PRINT_VAR_OTHER^"`, 53 "VAR_OTHER=host", 54 `/bin/sh -c "export VAR_OTHER=host ; ^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --interactive --inherit-env=true ^SLEEP^"`, 55 `/bin/sh -c "export VAR_OTHER=host ; ^RKT_BIN^ --debug enter $(^RKT_BIN^ list --full|grep running|awk '{print $1}') /inspect --print-env=VAR_OTHER"`, 56 }, 57 { 58 `^RKT_BIN^ --insecure-options=image run --mds-register=false --set-env=TERM=dumb ^PRINT_TERM_HOST^`, 59 "TERM=dumb", 60 `^RKT_BIN^ --insecure-options=image run --mds-register=false --interactive --inherit-env=false ^SLEEP^`, 61 `/bin/sh -c "export TERM=dumb ; ^RKT_BIN^ enter $(^RKT_BIN^ list --full|grep running|awk '{print $1}') /inspect --print-env=TERM"`, 62 }, 63 { 64 `^RKT_BIN^ --insecure-options=image run --mds-register=false --set-env=TERM=^HOST_TERM^ ^PRINT_TERM_HOST^`, 65 "TERM=^HOST_TERM^", 66 `^RKT_BIN^ --insecure-options=image run --mds-register=false --interactive --inherit-env=false ^SLEEP^`, 67 `/bin/sh -c "^RKT_BIN^ enter $(^RKT_BIN^ list --full|grep running|awk '{print $1}') /inspect --print-env=TERM"`, 68 }, 69 { 70 `/bin/sh -c "export VAR_FROM_MANIFEST=host ; ^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --inherit-env=true ^PRINT_VAR_FROM_MANIFEST^"`, 71 "VAR_FROM_MANIFEST=manifest", 72 `/bin/sh -c "export VAR_FROM_MANIFEST=host ; ^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --interactive --inherit-env=true ^SLEEP^"`, 73 `/bin/sh -c "export VAR_FROM_MANIFEST=host ; ^RKT_BIN^ --debug enter $(^RKT_BIN^ list --full|grep running|awk '{print $1}') /inspect --print-env=VAR_FROM_MANIFEST"`, 74 }, 75 { 76 `/bin/sh -c "export VAR_OTHER=host ; ^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --inherit-env=true --set-env=VAR_OTHER=setenv ^PRINT_VAR_OTHER^"`, 77 "VAR_OTHER=setenv", 78 `/bin/sh -c "export VAR_OTHER=host ; ^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --interactive --inherit-env=true --set-env=VAR_OTHER=setenv ^SLEEP^"`, 79 `/bin/sh -c "export VAR_OTHER=host ; ^RKT_BIN^ --debug enter $(^RKT_BIN^ list --full|grep running|awk '{print $1}') /inspect --print-env=VAR_OTHER"`, 80 }, 81 { 82 `/bin/sh -c "^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --set-env=VAR_OTHER=setenv --set-env-file=env_file_test.conf ^PRINT_VAR_OTHER^"`, 83 "VAR_OTHER=setenv", 84 `/bin/sh -c "^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --interactive --set-env=VAR_OTHER=setenv --set-env-file=env_file_test.conf ^SLEEP^"`, 85 `/bin/sh -c "export VAR_OTHER=host ; ^RKT_BIN^ --debug enter $(^RKT_BIN^ list --full|grep running|awk '{print $1}') /inspect --print-env=VAR_OTHER"`, 86 }, 87 { 88 `/bin/sh -c "^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --set-env-file=env_file_test.conf ^PRINT_VAR_OTHER^"`, 89 "VAR_OTHER=file", 90 `/bin/sh -c "^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --interactive --set-env-file=env_file_test.conf ^SLEEP^"`, 91 `/bin/sh -c "^RKT_BIN^ --debug enter $(^RKT_BIN^ list --full|grep running|awk '{print $1}') /inspect --print-env=VAR_OTHER"`, 92 }, 93 { 94 `/bin/sh -c "^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --set-env-file=env_file_test.conf ^CHECK_PATH^"`, 95 "PATH is good", 96 `/bin/sh -c "^RKT_BIN^ --debug --insecure-options=image run --mds-register=false --interactive --set-env-file=env_file_test.conf ^SLEEP^"`, 97 `/bin/sh -c "^RKT_BIN^ --debug enter $(^RKT_BIN^ list --full|grep running|awk '{print $1}') /inspect --check-path"`, 98 }, 99 } 100 101 func TestEnv(t *testing.T) { 102 printVarFromManifestImage := patchTestACI("rkt-inspect-print-var-from-manifest.aci", "--exec=/inspect --print-env=VAR_FROM_MANIFEST") 103 defer os.Remove(printVarFromManifestImage) 104 printVarOtherImage := patchTestACI("rkt-inspect-print-var-other.aci", "--exec=/inspect --print-env=VAR_OTHER") 105 defer os.Remove(printVarOtherImage) 106 printTermHostImage := patchTestACI("rkt-inspect-print-term-host.aci", "--exec=/inspect --print-env=TERM") 107 defer os.Remove(printTermHostImage) 108 checkPathImage := patchTestACI("rkt-inspect-check-path.aci", "--exec=/inspect --check-path") 109 defer os.Remove(checkPathImage) 110 sleepImage := patchTestACI("rkt-inspect-sleep.aci", "--exec=/inspect --read-stdin") 111 defer os.Remove(sleepImage) 112 ctx := testutils.NewRktRunCtx() 113 defer ctx.Cleanup() 114 115 term := testutils.GetValueFromEnvOrPanic("TERM") 116 replacePlaceholders := func(cmd string) string { 117 fixed := cmd 118 fixed = strings.Replace(fixed, "^RKT_BIN^", ctx.Cmd(), -1) 119 fixed = strings.Replace(fixed, "^PRINT_VAR_FROM_MANIFEST^", printVarFromManifestImage, -1) 120 fixed = strings.Replace(fixed, "^PRINT_VAR_OTHER^", printVarOtherImage, -1) 121 fixed = strings.Replace(fixed, "^PRINT_TERM_HOST^", printTermHostImage, -1) 122 fixed = strings.Replace(fixed, "^CHECK_PATH^", checkPathImage, -1) 123 fixed = strings.Replace(fixed, "^SLEEP^", sleepImage, -1) 124 fixed = strings.Replace(fixed, "^HOST_TERM^", term, -1) 125 return fixed 126 } 127 for i, tt := range envTests { 128 // change dynamic variables from expected result 129 tt.runExpect = replacePlaceholders(tt.runExpect) 130 131 // 'run' tests 132 runCmd := replacePlaceholders(tt.runCmd) 133 t.Logf("Running 'run' test #%v", i) 134 runRktAndCheckOutput(t, runCmd, tt.runExpect, false) 135 136 // 'enter' tests 137 sleepCmd := replacePlaceholders(tt.sleepCmd) 138 t.Logf("Running 'enter' test #%v", i) 139 child := spawnOrFail(t, sleepCmd) 140 141 if err := expectWithOutput(child, "Enter text:"); err != nil { 142 t.Fatalf("Waited for the prompt but not found #%v: %v", i, err) 143 } 144 145 enterCmd := replacePlaceholders(tt.enterCmd) 146 t.Logf("Running 'enter' test #%v", i) 147 enterChild := spawnOrFail(t, enterCmd) 148 149 if err := expectWithOutput(enterChild, tt.runExpect); err != nil { 150 t.Fatalf("Expected %q but not found: %v", tt.runExpect, err) 151 } 152 153 waitOrFail(t, enterChild, 0) 154 155 if err := child.SendLine("Bye"); err != nil { 156 t.Fatalf("rkt couldn't write to the container: %v", err) 157 } 158 if err := expectWithOutput(child, "Received text: Bye"); err != nil { 159 t.Fatalf("Expected Bye but not found #%v: %v", i, err) 160 } 161 162 waitOrFail(t, child, 0) 163 ctx.Reset() 164 } 165 }