github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/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 package main 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 "strings" 23 "testing" 24 25 "github.com/coreos/rkt/tests/testutils" 26 ) 27 28 const ( 29 manifestExportTemplate = `{"acKind":"ImageManifest","acVersion":"0.7.1","name":"IMG_NAME","labels":[{"name":"version","value":"1.0.0"},{"name":"arch","value":"amd64"},{"name":"os","value":"linux"}],"app":{"exec":["/inspect"],"user":"0","group":"0","workingDirectory":"/","environment":[{"name":"VAR_FROM_MANIFEST","value":"manifest"}]}}` 30 ) 31 32 // TestImageExport tests 'rkt image export', it will import some existing 33 // image, export it with rkt image export and check that the exported ACI hash 34 // matches the hash of the imported ACI 35 func TestImageExport(t *testing.T) { 36 testImageName := "coreos.com/rkt-image-export-test" 37 expectManifest := strings.Replace(manifestExportTemplate, "IMG_NAME", testImageName, -1) 38 39 tmpDir := createTempDirOrPanic("rkt-TestImageExport-") 40 defer os.RemoveAll(tmpDir) 41 42 tmpManifest, err := ioutil.TempFile(tmpDir, "manifest") 43 if err != nil { 44 panic(fmt.Sprintf("Cannot create temp manifest: %v", err)) 45 } 46 defer tmpManifest.Close() 47 tmpManifestName := tmpManifest.Name() 48 if err := ioutil.WriteFile(tmpManifestName, []byte(expectManifest), 0600); err != nil { 49 panic(fmt.Sprintf("Cannot write to temp manifest: %v", err)) 50 } 51 defer os.Remove(tmpManifestName) 52 53 testImage := patchTestACI("rkt-inspect-image-export.aci", "--manifest", tmpManifestName) 54 defer os.Remove(testImage) 55 ctx := testutils.NewRktRunCtx() 56 defer ctx.Cleanup() 57 58 testImageId := importImageAndFetchHash(t, ctx, testImage) 59 60 testImageHash, err := getHash(testImage) 61 if err != nil { 62 panic(fmt.Sprintf("Error getting image hash: %v", err)) 63 } 64 65 tests := []struct { 66 image string 67 shouldFind bool 68 expectedHash string 69 }{ 70 { 71 testImageName, 72 true, 73 testImageHash, 74 }, 75 { 76 testImageId, 77 true, 78 testImageHash, 79 }, 80 { 81 "sha512-not-existed", 82 false, 83 "", 84 }, 85 { 86 "some~random~aci~name", 87 false, 88 "", 89 }, 90 } 91 92 for i, tt := range tests { 93 outputAciPath := filepath.Join(tmpDir, fmt.Sprintf("exported-%d.aci", i)) 94 runCmd := fmt.Sprintf("%s image export %s %s", ctx.Cmd(), tt.image, outputAciPath) 95 t.Logf("Running 'image export' test #%v: %v", i, runCmd) 96 spawnAndWaitOrFail(t, runCmd, tt.shouldFind) 97 98 if !tt.shouldFind { 99 continue 100 } 101 102 exportedHash, err := getHash(outputAciPath) 103 if err != nil { 104 t.Fatalf("Error getting exported image hash: %v", err) 105 } 106 107 if exportedHash != tt.expectedHash { 108 t.Fatalf("Expected hash %q but got %s", tt.expectedHash, exportedHash) 109 } 110 } 111 }