github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_cat_manifest_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/tests/testutils"
    26  )
    27  
    28  // TestCatManifest tests 'rkt cat-manifest', it will:
    29  func TestCatManifest(t *testing.T) {
    30  	const imgName = "rkt-cat-manifest-test"
    31  
    32  	image := patchTestACI(fmt.Sprintf("%s.aci", imgName), fmt.Sprintf("--name=%s", imgName))
    33  	defer os.Remove(image)
    34  
    35  	imageHash := getHashOrPanic(image)
    36  	imgID := ImageID{path: image, hash: imageHash}
    37  
    38  	ctx := testutils.NewRktRunCtx()
    39  	defer ctx.Cleanup()
    40  
    41  	// Prepare image
    42  	cmd := fmt.Sprintf("%s --insecure-options=image prepare %s", ctx.Cmd(), imgID.path)
    43  	podUuid := runRktAndGetUUID(t, cmd)
    44  
    45  	tmpDir := mustTempDir(imgName)
    46  	defer os.RemoveAll(tmpDir)
    47  
    48  	tests := []struct {
    49  		uuid     string
    50  		match    string
    51  		uuidFile bool
    52  	}{
    53  		{
    54  			podUuid,
    55  			imgName,
    56  			false,
    57  		},
    58  		{
    59  			podUuid,
    60  			imageHash[:20],
    61  			false,
    62  		},
    63  		{
    64  			"1234567890abcdef",
    65  			"no matches found for",
    66  			false,
    67  		},
    68  		{
    69  			"",
    70  			imageHash[:20],
    71  			true,
    72  		},
    73  	}
    74  
    75  	for i, tt := range tests {
    76  		if tt.uuidFile == true {
    77  			podUUID := runRktAndGetUUID(t, cmd)
    78  			uuidFile, err := ioutil.TempFile(tmpDir, "uuid-file")
    79  			if err != nil {
    80  				panic(fmt.Sprintf("Cannot create uuid-file: %v", err))
    81  			}
    82  			uuidFilePath := uuidFile.Name()
    83  			if err := ioutil.WriteFile(uuidFilePath, []byte(podUUID), 0600); err != nil {
    84  				panic(fmt.Sprintf("Cannot write pod UUID to uuid-file: %v", err))
    85  			}
    86  			runCmd := fmt.Sprintf("%s cat-manifest --uuid-file=%s --pretty-print=false %s", ctx.Cmd(), uuidFilePath)
    87  			t.Logf("Running test #%d", i)
    88  			runRktAndCheckRegexOutput(t, runCmd, tt.match)
    89  		} else {
    90  			runCmd := fmt.Sprintf("%s cat-manifest --pretty-print=false %s", ctx.Cmd(), tt.uuid)
    91  			t.Logf("Running test #%d", i)
    92  			runRktAndCheckRegexOutput(t, runCmd, tt.match)
    93  		}
    94  	}
    95  }