github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_list_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  	"strings"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/rkt/rkt/tests/testutils"
    27  )
    28  
    29  const delta = 4 * time.Second
    30  const precision = 2 * time.Second
    31  
    32  // compareTime checks if a and b are roughly equal
    33  func compareTime(a time.Time, b time.Time) bool {
    34  	diff := a.Sub(b)
    35  	if diff < 0 {
    36  		diff = -diff
    37  	}
    38  	return diff < precision
    39  }
    40  
    41  func TestRktList(t *testing.T) {
    42  	const imgName = "rkt-list-test"
    43  
    44  	image := patchTestACI(fmt.Sprintf("%s.aci", imgName), fmt.Sprintf("--name=%s", imgName))
    45  	defer os.Remove(image)
    46  
    47  	imageHash := getHashOrPanic(image)
    48  	imgID := ImageID{image, imageHash}
    49  
    50  	ctx := testutils.NewRktRunCtx()
    51  	defer ctx.Cleanup()
    52  
    53  	// Prepare image
    54  	cmd := fmt.Sprintf("%s --insecure-options=image prepare %s", ctx.Cmd(), imgID.path)
    55  	podUuid := runRktAndGetUUID(t, cmd)
    56  
    57  	// Get hash
    58  	imageID := fmt.Sprintf("sha512-%s", imgID.hash[:12])
    59  
    60  	cmd = fmt.Sprintf("%s --insecure-options=image run-prepared %s", ctx.Cmd(), podUuid)
    61  	nobodyUid, _ := testutils.GetUnprivilegedUidGid()
    62  	_, status := runRkt(t, cmd, nobodyUid, 0)
    63  	if status != 0 {
    64  		panic(fmt.Errorf("expected exit status code 0, got %d", status))
    65  	}
    66  	podInfo := getPodInfo(t, ctx, podUuid)
    67  	ipv4 := podInfo.networks["default"].ipv4
    68  	// Define tests
    69  	tests := []struct {
    70  		cmd           string
    71  		shouldSucceed bool
    72  		expect        string
    73  	}{
    74  		// Test that pod UUID is in output
    75  		{
    76  			"list --full",
    77  			true,
    78  			podUuid,
    79  		},
    80  		// Test that image name is in output
    81  		{
    82  			"list",
    83  			true,
    84  			imgName,
    85  		},
    86  		// Test that imageID is in output
    87  		{
    88  			"list --full",
    89  			true,
    90  			imageID,
    91  		},
    92  		// Remove the image
    93  		{
    94  			fmt.Sprintf("image rm %s", imageID),
    95  			true,
    96  			"successfully removed",
    97  		},
    98  		// Name should still show up in rkt list
    99  		{
   100  			"list",
   101  			true,
   102  			imgName,
   103  		},
   104  		// Test that imageID is still in output
   105  		{
   106  			"list --full",
   107  			true,
   108  			imageID,
   109  		},
   110  		//	Test that ip is in json output
   111  		{
   112  			"list --format=json",
   113  			true,
   114  			ipv4,
   115  		},
   116  	}
   117  
   118  	// Run tests
   119  	for i, tt := range tests {
   120  		runCmd := fmt.Sprintf("%s %s", ctx.Cmd(), tt.cmd)
   121  		t.Logf("Running test #%d, %s", i, runCmd)
   122  		runRktAndCheckOutput(t, runCmd, tt.expect, !tt.shouldSucceed)
   123  	}
   124  }
   125  
   126  func getCreationStartTime(t *testing.T, ctx *testutils.RktRunCtx, imageID string) (creation time.Time, start time.Time) {
   127  	// Run rkt list --full
   128  	rktCmd := fmt.Sprintf("%s list --full", ctx.Cmd())
   129  	child := spawnOrFail(t, rktCmd)
   130  	child.Wait()
   131  
   132  	// Get creation time
   133  	match := fmt.Sprintf(".*%s\t.*\t(.*)\t(.*)\t", imageID)
   134  	result, out, err := expectRegexWithOutput(child, match)
   135  	if err != nil {
   136  		t.Fatalf("%q regex not found, Error: %v\nOutput: %v", match, err, out)
   137  	}
   138  	tmStr := strings.TrimSpace(result[1])
   139  	creation, err = time.Parse(defaultTimeLayout, tmStr)
   140  	if err != nil {
   141  		t.Fatalf("Error parsing creation time: %q", err)
   142  	}
   143  
   144  	tmStr = strings.TrimSpace(result[2])
   145  	start, err = time.Parse(defaultTimeLayout, tmStr)
   146  	if err != nil {
   147  		t.Fatalf("Error parsing start time: %q", err)
   148  	}
   149  
   150  	return creation, start
   151  }
   152  
   153  func TestRktListCreatedStarted(t *testing.T) {
   154  	const imgName = "rkt-list-creation-start-time-test"
   155  
   156  	image := patchTestACI(fmt.Sprintf("%s.aci", imgName), fmt.Sprintf("--exec=/inspect --exit-code=0"))
   157  	defer os.Remove(image)
   158  
   159  	imageHash := getHashOrPanic(image)
   160  	imgID := ImageID{image, imageHash}
   161  
   162  	ctx := testutils.NewRktRunCtx()
   163  	defer ctx.Cleanup()
   164  
   165  	// Prepare image
   166  	cmd := fmt.Sprintf("%s --insecure-options=image prepare %s", ctx.Cmd(), imgID.path)
   167  	podUuid := runRktAndGetUUID(t, cmd)
   168  
   169  	// t0: prepare
   170  	expectPrepare := time.Now()
   171  
   172  	// Get hash
   173  	imageID := fmt.Sprintf("sha512-%s", imgID.hash[:12])
   174  
   175  	tmpDir := mustTempDir(imgName)
   176  	defer os.RemoveAll(tmpDir)
   177  
   178  	time.Sleep(delta)
   179  
   180  	// Run image
   181  	cmd = fmt.Sprintf("%s run-prepared %s", ctx.Cmd(), podUuid)
   182  	rktChild := spawnOrFail(t, cmd)
   183  
   184  	// t1: run
   185  	expectRun := time.Now()
   186  
   187  	waitOrFail(t, rktChild, 0)
   188  
   189  	creation, start := getCreationStartTime(t, ctx, imageID)
   190  	if !compareTime(expectPrepare, creation) {
   191  		t.Fatalf("rkt list returned an incorrect creation time. Got: %q Expect: %q", creation, expectPrepare)
   192  	}
   193  	if !compareTime(expectRun, start) {
   194  		t.Fatalf("rkt list returned an incorrect start time. Got: %q Expect: %q", start, expectRun)
   195  	}
   196  }