github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_rm_test.go (about)

     1  // Copyright 2016 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  	"path/filepath"
    24  	"testing"
    25  
    26  	"github.com/rkt/rkt/tests/testutils"
    27  )
    28  
    29  func TestRm(t *testing.T) {
    30  	ctx := testutils.NewRktRunCtx()
    31  	defer ctx.Cleanup()
    32  
    33  	var uuids []string
    34  
    35  	img := getInspectImagePath()
    36  	prepareCmd := fmt.Sprintf("%s --insecure-options=image prepare %s", ctx.Cmd(), img)
    37  
    38  	// Finished pod.
    39  	uuid := runRktAndGetUUID(t, prepareCmd)
    40  	runPreparedCmd := fmt.Sprintf("%s --insecure-options=image run-prepared %s", ctx.Cmd(), uuid)
    41  	runRktAndCheckOutput(t, runPreparedCmd, "", false)
    42  
    43  	uuids = append(uuids, uuid)
    44  
    45  	// Prepared pod.
    46  	uuid = runRktAndGetUUID(t, prepareCmd)
    47  	uuids = append(uuids, uuid)
    48  
    49  	podDirs := []string{
    50  		filepath.Join(ctx.DataDir(), "pods", "run"),
    51  		filepath.Join(ctx.DataDir(), "pods", "prepared"),
    52  	}
    53  
    54  	for _, dir := range podDirs {
    55  		pods, err := ioutil.ReadDir(dir)
    56  		if err != nil {
    57  			t.Fatalf("cannot read pods directory %q: %v", dir, err)
    58  		}
    59  		if len(pods) == 0 {
    60  			t.Fatalf("pods should still exist in directory %q: %v", dir, pods)
    61  		}
    62  	}
    63  
    64  	for _, u := range uuids {
    65  		cmd := fmt.Sprintf("%s rm %s", ctx.Cmd(), u)
    66  		spawnAndWaitOrFail(t, cmd, 0)
    67  	}
    68  
    69  	podDirs = append(podDirs, filepath.Join(ctx.DataDir(), "pods", "exited-garbage"))
    70  
    71  	for _, dir := range podDirs {
    72  		pods, err := ioutil.ReadDir(dir)
    73  		if err != nil {
    74  			t.Fatalf("cannot read pods directory %q: %v", dir, err)
    75  		}
    76  		if len(pods) != 0 {
    77  			t.Errorf("no pods should exist in directory %q, but found: %v", dir, pods)
    78  		}
    79  	}
    80  }
    81  
    82  func TestRmInvalid(t *testing.T) {
    83  	ctx := testutils.NewRktRunCtx()
    84  	defer ctx.Cleanup()
    85  
    86  	nonexistentUUID := "0f746094-3438-42bc-ab37-3cf85f132e60"
    87  	tmpDir := mustTempDir("rkt_rm_test")
    88  	defer os.RemoveAll(tmpDir)
    89  	uuidFile := filepath.Join(tmpDir, "uuid-file")
    90  	if err := ioutil.WriteFile(uuidFile, []byte(nonexistentUUID), 0600); err != nil {
    91  		t.Fatalf("cannot write uuid-file: %v", err)
    92  	}
    93  
    94  	expected := fmt.Sprintf("no matches found for %q", nonexistentUUID)
    95  
    96  	cmd := fmt.Sprintf("%s rm %s", ctx.Cmd(), nonexistentUUID)
    97  	runRktAndCheckOutput(t, cmd, expected, true)
    98  
    99  	cmd = fmt.Sprintf("%s rm --uuid-file=%s", ctx.Cmd(), uuidFile)
   100  	runRktAndCheckOutput(t, cmd, expected, true)
   101  }
   102  
   103  func TestRmEmptyUUID(t *testing.T) {
   104  	ctx := testutils.NewRktRunCtx()
   105  	defer ctx.Cleanup()
   106  
   107  	emptyUUID := "\"\""
   108  	expected := fmt.Sprintf("UUID cannot be empty")
   109  
   110  	cmd := fmt.Sprintf("%s rm %s", ctx.Cmd(), emptyUUID)
   111  	runRktAndCheckOutput(t, cmd, expected, true)
   112  }