github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_image_extract_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  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/rkt/rkt/tests/testutils"
    26  )
    27  
    28  // TestImageExtract tests 'rkt image extract', it will import some existing
    29  // image with the inspect binary, extract it with rkt image extract and check
    30  // that the exported /inspect hash matches the original inspect binary hash
    31  func TestImageExtract(t *testing.T) {
    32  	testImage := getInspectImagePath()
    33  	testImageName := "coreos.com/rkt-inspect"
    34  
    35  	inspectFile := testutils.GetValueFromEnvOrPanic("INSPECT_BINARY")
    36  	inspectHash := getHashOrPanic(inspectFile)
    37  
    38  	tmpDir := mustTempDir("rkt-TestImageRender-")
    39  	defer os.RemoveAll(tmpDir)
    40  
    41  	ctx := testutils.NewRktRunCtx()
    42  	defer ctx.Cleanup()
    43  
    44  	testImageShortHash, err := importImageAndFetchHash(t, ctx, "", testImage)
    45  	if err != nil {
    46  		t.Fatalf("%v", err)
    47  	}
    48  
    49  	tests := []struct {
    50  		image        string
    51  		shouldFind   bool
    52  		expectedHash string
    53  	}{
    54  		{
    55  			testImageName,
    56  			true,
    57  			inspectHash,
    58  		},
    59  		{
    60  			testImageShortHash,
    61  			true,
    62  			inspectHash,
    63  		},
    64  		{
    65  			"sha512-not-existed",
    66  			false,
    67  			"",
    68  		},
    69  		{
    70  			"some~random~aci~name",
    71  			false,
    72  			"",
    73  		},
    74  	}
    75  
    76  	for i, tt := range tests {
    77  		expectedStatus := 254
    78  		if tt.shouldFind {
    79  			expectedStatus = 0
    80  		}
    81  		outputPath := filepath.Join(tmpDir, fmt.Sprintf("extracted-%d", i))
    82  		runCmd := fmt.Sprintf("%s image extract --rootfs-only %s %s", ctx.Cmd(), tt.image, outputPath)
    83  		t.Logf("Running 'image extract' test #%v: %v", i, runCmd)
    84  		spawnAndWaitOrFail(t, runCmd, expectedStatus)
    85  
    86  		if !tt.shouldFind {
    87  			continue
    88  		}
    89  
    90  		extractedInspectHash, err := getHash(filepath.Join(outputPath, "inspect"))
    91  		if err != nil {
    92  			t.Fatalf("Cannot get rendered inspect binary's hash")
    93  		}
    94  		if extractedInspectHash != tt.expectedHash {
    95  			t.Fatalf("Expected /inspect hash %q but got %s", tt.expectedHash, extractedInspectHash)
    96  		}
    97  	}
    98  }