github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_image_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/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  // TestImageCatManifest tests 'rkt image cat-manifest', it will:
    34  // Read some existing image manifest via the image name, and verify the result.
    35  // Read some existing image manifest via the image hash, and verify the result.
    36  // Read some non-existing image manifest via the image name, and verify nothing is found.
    37  // Read some non-existing image manifest via the image hash, and verify nothing is found.
    38  func TestImageCatManifest(t *testing.T) {
    39  	manifestCat := schema.ImageManifest{
    40  		Name: "coreos.com/rkt-image-cat-manifest-test",
    41  		App: &types.App{
    42  			Exec: types.Exec{"/inspect"},
    43  			User: "0", Group: "0",
    44  			WorkingDirectory: "/",
    45  			Environment: types.Environment{
    46  				{"VAR_FROM_MANIFEST", "manifest"},
    47  			},
    48  		},
    49  		Labels: types.Labels{
    50  			{"version", "1.30.0"},
    51  			{"arch", common.GetArch()},
    52  			{"os", common.GetOS()},
    53  		},
    54  	}
    55  
    56  	expectManifest, err := acitest.ImageManifestString(&manifestCat)
    57  	if err != nil {
    58  		t.Fatalf("unexpected error: %v", err)
    59  	}
    60  
    61  	tmpManifest, err := ioutil.TempFile("", "rkt-TestImageCatManifest-")
    62  	if err != nil {
    63  		t.Fatalf("Cannot create temp manifest: %v", err)
    64  	}
    65  	defer os.Remove(tmpManifest.Name())
    66  	if err := ioutil.WriteFile(tmpManifest.Name(), []byte(expectManifest), 0600); err != nil {
    67  		t.Fatalf("Cannot write to temp manifest: %v", err)
    68  	}
    69  
    70  	testImage := patchTestACI("rkt-inspect-image-cat-manifest.aci", "--manifest", tmpManifest.Name())
    71  	defer os.Remove(testImage)
    72  	ctx := testutils.NewRktRunCtx()
    73  	defer ctx.Cleanup()
    74  
    75  	testImageHash, err := importImageAndFetchHash(t, ctx, "", testImage)
    76  	if err != nil {
    77  		t.Fatalf("%v", err)
    78  	}
    79  
    80  	tests := []struct {
    81  		image      string
    82  		shouldFind bool
    83  		expect     string
    84  	}{
    85  		{
    86  			string(manifestCat.Name),
    87  			true,
    88  			expectManifest,
    89  		},
    90  		{
    91  			testImageHash,
    92  			true,
    93  			expectManifest,
    94  		},
    95  		{
    96  			"sha512-not-existed",
    97  			false,
    98  			"",
    99  		},
   100  		{
   101  			"some~random~aci~name",
   102  			false,
   103  			"",
   104  		},
   105  	}
   106  
   107  	for i, tt := range tests {
   108  		runCmd := fmt.Sprintf("%s image cat-manifest --pretty-print=false %s", ctx.Cmd(), tt.image)
   109  		t.Logf("Running test #%d", i)
   110  		runRktAndCheckOutput(t, runCmd, tt.expect, !tt.shouldFind)
   111  	}
   112  }