github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_image_export_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 "path/filepath" 24 "testing" 25 26 "github.com/rkt/rkt/common" 27 "github.com/rkt/rkt/pkg/aci/acitest" 28 "github.com/rkt/rkt/tests/testutils" 29 30 "github.com/appc/spec/schema" 31 "github.com/appc/spec/schema/types" 32 ) 33 34 // TestImageExport tests 'rkt image export', it will import some existing 35 // image, export it with rkt image export and check that the exported ACI hash 36 // matches the hash of the imported ACI 37 func TestImageExport(t *testing.T) { 38 manifest := schema.ImageManifest{ 39 Name: "coreos.com/rkt-image-export-test", 40 App: &types.App{ 41 Exec: types.Exec{"/inspect"}, 42 User: "0", Group: "0", 43 WorkingDirectory: "/", 44 Environment: types.Environment{ 45 {"VAR_FROM_MANIFEST", "manifest"}, 46 }, 47 }, 48 Labels: types.Labels{ 49 {"version", "1.30.0"}, 50 {"arch", common.GetArch()}, 51 {"os", common.GetOS()}, 52 }, 53 } 54 55 expectManifest, err := acitest.ImageManifestString(&manifest) 56 if err != nil { 57 t.Fatalf("unexpected error: %v", err) 58 } 59 60 tmpDir := mustTempDir("rkt-TestImageExport-") 61 defer os.RemoveAll(tmpDir) 62 63 tmpManifest, err := ioutil.TempFile(tmpDir, "manifest") 64 if err != nil { 65 panic(fmt.Sprintf("Cannot create temp manifest: %v", err)) 66 } 67 defer tmpManifest.Close() 68 tmpManifestName := tmpManifest.Name() 69 if err := ioutil.WriteFile(tmpManifestName, []byte(expectManifest), 0600); err != nil { 70 panic(fmt.Sprintf("Cannot write to temp manifest: %v", err)) 71 } 72 defer os.Remove(tmpManifestName) 73 74 testImage := patchTestACI("rkt-inspect-image-export.aci", "--manifest", tmpManifestName) 75 defer os.Remove(testImage) 76 ctx := testutils.NewRktRunCtx() 77 defer ctx.Cleanup() 78 79 testImageID, err := importImageAndFetchHash(t, ctx, "", testImage) 80 if err != nil { 81 t.Fatalf("%v", err) 82 } 83 84 testImageHash, err := getHash(testImage) 85 if err != nil { 86 panic(fmt.Sprintf("Error getting image hash: %v", err)) 87 } 88 89 tests := []struct { 90 image string 91 shouldFind bool 92 expectedHash string 93 }{ 94 { 95 string(manifest.Name), 96 true, 97 testImageHash, 98 }, 99 { 100 testImageID, 101 true, 102 testImageHash, 103 }, 104 { 105 "sha512-not-existed", 106 false, 107 "", 108 }, 109 { 110 "some~random~aci~name", 111 false, 112 "", 113 }, 114 } 115 116 for i, tt := range tests { 117 expectedStatus := 254 118 if tt.shouldFind { 119 expectedStatus = 0 120 } 121 outputAciPath := filepath.Join(tmpDir, fmt.Sprintf("exported-%d.aci", i)) 122 runCmd := fmt.Sprintf("%s image export %s %s", ctx.Cmd(), tt.image, outputAciPath) 123 t.Logf("Running 'image export' test #%v: %v", i, runCmd) 124 spawnAndWaitOrFail(t, runCmd, expectedStatus) 125 126 if !tt.shouldFind { 127 continue 128 } 129 130 exportedHash, err := getHash(outputAciPath) 131 if err != nil { 132 t.Fatalf("Error getting exported image hash: %v", err) 133 } 134 135 if exportedHash != tt.expectedHash { 136 t.Fatalf("Expected hash %q but got %s", tt.expectedHash, exportedHash) 137 } 138 } 139 }