github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_os_arch_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 type osArchTest struct { 34 image string 35 rktCmd string 36 expectedLine string 37 expectError bool 38 } 39 40 func osArchTestRemoveImages(tests []osArchTest) { 41 for _, tt := range tests { 42 os.Remove(tt.image) 43 } 44 } 45 46 func getMissingOrInvalidTests(t *testing.T, ctx *testutils.RktRunCtx) []osArchTest { 47 var tests []osArchTest 48 49 defer osArchTestRemoveImages(tests) 50 51 aci_os, aci_arch := common.GetOSArch() 52 53 manifestOSArch := schema.ImageManifest{ 54 Name: "coreos.com/rkt-missing-os-arch-test", 55 App: &types.App{ 56 Exec: types.Exec{ 57 "/inspect", 58 "--print-msg=HelloWorld", 59 }, 60 User: "0", Group: "0", 61 WorkingDirectory: "/", 62 }, 63 Labels: types.Labels{ 64 {"version", "1.30.0"}, 65 }, 66 } 67 68 // Copy the lables of the image manifest to use the common 69 // part in all test cases. 70 labels := make(types.Labels, len(manifestOSArch.Labels)) 71 copy(labels, manifestOSArch.Labels) 72 73 // Test a valid image as a sanity check 74 manifestOSArch.Labels = append( 75 labels, 76 types.Label{"os", aci_os}, 77 types.Label{"arch", aci_arch}, 78 ) 79 80 goodManifestFile := "good-manifest.json" 81 goodManifestStr, err := acitest.ImageManifestString(&manifestOSArch) 82 if err != nil { 83 t.Fatalf("unexpected error: %v", err) 84 } 85 86 if err := ioutil.WriteFile(goodManifestFile, []byte(goodManifestStr), 0600); err != nil { 87 t.Fatalf("Cannot write good manifest: %v", err) 88 } 89 defer os.Remove(goodManifestFile) 90 91 goodImage := patchTestACI("rkt-good-image.aci", fmt.Sprintf("--manifest=%s", goodManifestFile)) 92 goodTest := osArchTest{ 93 image: goodImage, 94 rktCmd: fmt.Sprintf("%s --insecure-options=image run --mds-register=false %s", ctx.Cmd(), goodImage), 95 expectedLine: "HelloWorld", 96 expectError: false, 97 } 98 tests = append(tests, goodTest) 99 100 // Test an image with a missing os label 101 manifestOSArch.Labels = append(labels, types.Label{"arch", aci_arch}) 102 103 missingOSManifestFile := "missingOS-manifest.json" 104 missingOSManifestStr, err := acitest.ImageManifestString(&manifestOSArch) 105 if err != nil { 106 t.Fatalf("unexpected error: %v", err) 107 } 108 109 if err := ioutil.WriteFile(missingOSManifestFile, []byte(missingOSManifestStr), 0600); err != nil { 110 t.Fatalf("Cannot write missing OS manifest: %v", err) 111 } 112 defer os.Remove(missingOSManifestFile) 113 114 missingOSImage := patchTestACI("rkt-missing-os.aci", fmt.Sprintf("--manifest=%s", missingOSManifestFile)) 115 missingOSTest := osArchTest{ 116 image: missingOSImage, 117 rktCmd: fmt.Sprintf("%s --insecure-options=image run %s", ctx.Cmd(), missingOSImage), 118 expectedLine: "missing os label in the image manifest", 119 expectError: true, 120 } 121 tests = append(tests, missingOSTest) 122 123 // Test an image with a missing arch label 124 manifestOSArch.Labels = append(labels, types.Label{"os", aci_os}) 125 126 missingArchManifestFile := "missingArch-manifest.json" 127 missingArchManifestStr, err := acitest.ImageManifestString(&manifestOSArch) 128 if err != nil { 129 t.Fatalf("unexpected error: %v", err) 130 } 131 132 if err := ioutil.WriteFile(missingArchManifestFile, []byte(missingArchManifestStr), 0600); err != nil { 133 t.Fatalf("Cannot write missing Arch manifest: %v", err) 134 } 135 defer os.Remove(missingArchManifestFile) 136 137 missingArchImage := patchTestACI("rkt-missing-arch.aci", fmt.Sprintf("--manifest=%s", missingArchManifestFile)) 138 missingArchTest := osArchTest{ 139 image: missingArchImage, 140 rktCmd: fmt.Sprintf("%s --insecure-options=image run %s", ctx.Cmd(), missingArchImage), 141 expectedLine: "missing arch label in the image manifest", 142 expectError: true, 143 } 144 tests = append(tests, missingArchTest) 145 146 // Test an image with an invalid os 147 manifestOSArch.Labels = append( 148 labels, 149 types.Label{"os", "freebsd"}, 150 types.Label{"arch", aci_arch}, 151 ) 152 153 invalidOSManifestFile := "invalid-os-manifest.json" 154 invalidOSManifestStr, err := acitest.ImageManifestString(&manifestOSArch) 155 if err != nil { 156 t.Fatalf("unexpected error: %v", err) 157 } 158 159 if err := ioutil.WriteFile(invalidOSManifestFile, []byte(invalidOSManifestStr), 0600); err != nil { 160 t.Fatalf("Cannot write invalid os manifest: %v", err) 161 } 162 defer os.Remove(invalidOSManifestFile) 163 164 invalidOSImage := patchTestACI("rkt-invalid-os.aci", fmt.Sprintf("--manifest=%s", invalidOSManifestFile)) 165 invalidOSTest := osArchTest{ 166 image: invalidOSImage, 167 rktCmd: fmt.Sprintf("%s --insecure-options=image run %s", ctx.Cmd(), invalidOSImage), 168 expectedLine: `bad os "freebsd"`, 169 expectError: true, 170 } 171 172 // only do this test on amd64 since freebsd is not supported on all architectures. 173 if aci_arch == "amd64" { 174 tests = append(tests, invalidOSTest) 175 } 176 177 // Test an image with an invalid arch 178 manifestOSArch.Labels = append( 179 labels, 180 types.Label{"os", aci_os}, 181 types.Label{"arch", "armv5l"}, 182 ) 183 184 invalidArchManifestFile := "invalid-arch-manifest.json" 185 invalidArchManifestStr, err := acitest.ImageManifestString(&manifestOSArch) 186 if err != nil { 187 t.Fatalf("unexpected error: %v", err) 188 } 189 190 if err := ioutil.WriteFile(invalidArchManifestFile, []byte(invalidArchManifestStr), 0600); err != nil { 191 t.Fatalf("Cannot write invalid arch manifest: %v", err) 192 } 193 defer os.Remove(invalidArchManifestFile) 194 195 retTests := tests 196 tests = nil 197 return retTests 198 } 199 200 // TestMissingOrInvalidOSArchRun tests that rkt errors out when it tries to run 201 // an image (not present in the store) with a missing or unsupported os/arch 202 func TestMissingOrInvalidOSArchRun(t *testing.T) { 203 ctx := testutils.NewRktRunCtx() 204 defer ctx.Cleanup() 205 tests := getMissingOrInvalidTests(t, ctx) 206 defer osArchTestRemoveImages(tests) 207 208 for i, tt := range tests { 209 t.Logf("Running test #%v: %v", i, tt.rktCmd) 210 runRktAndCheckOutput(t, tt.rktCmd, tt.expectedLine, tt.expectError) 211 } 212 } 213 214 // TestMissingOrInvalidOSArchFetchRun tests that rkt errors out when it tries 215 // to run an already fetched image with a missing or unsupported os/arch 216 func TestMissingOrInvalidOSArchFetchRun(t *testing.T) { 217 ctx := testutils.NewRktRunCtx() 218 defer ctx.Cleanup() 219 tests := getMissingOrInvalidTests(t, ctx) 220 defer osArchTestRemoveImages(tests) 221 222 for i, tt := range tests { 223 imgHash, err := importImageAndFetchHash(t, ctx, "", tt.image) 224 if err != nil { 225 t.Fatalf("%v", err) 226 } 227 rktCmd := fmt.Sprintf("%s run --mds-register=false %s", ctx.Cmd(), imgHash) 228 t.Logf("Running test #%v: %v", i, rktCmd) 229 runRktAndCheckOutput(t, rktCmd, tt.expectedLine, tt.expectError) 230 } 231 }