github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_gc_nspawn_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
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/rkt/rkt/tests/testutils"
    27  )
    28  
    29  func getPodCgroups(shortUUID string) ([]string, error) {
    30  	var podCgroups []string
    31  	walkCgroups := func(path string, info os.FileInfo, err error) error {
    32  		// Maybe the file doesn't exist anymore. We've seen this during tests,
    33  		// not sure about the reason though.
    34  		if os.IsNotExist(err) {
    35  			return nil
    36  		}
    37  		if err != nil {
    38  			return err
    39  		}
    40  		if info.Mode().IsDir() && strings.Contains(path, shortUUID) {
    41  			podCgroups = append(podCgroups, path)
    42  		}
    43  		return nil
    44  	}
    45  	if err := filepath.Walk("/sys/fs/cgroup", walkCgroups); err != nil {
    46  		return nil, err
    47  	}
    48  	return podCgroups, nil
    49  }
    50  
    51  func TestGCCgroup(t *testing.T) {
    52  	ctx := testutils.NewRktRunCtx()
    53  	defer ctx.Cleanup()
    54  
    55  	imagePath := getInspectImagePath()
    56  	cmd := fmt.Sprintf("%s --insecure-options=image prepare %s", ctx.Cmd(), imagePath)
    57  	uuid := runRktAndGetUUID(t, cmd)
    58  	shortUUID := strings.Split(uuid, "-")[0]
    59  
    60  	cmd = fmt.Sprintf("%s run-prepared %s", ctx.Cmd(), shortUUID)
    61  	runRktAndCheckOutput(t, cmd, "", false)
    62  
    63  	cgs, err := getPodCgroups(shortUUID)
    64  	if err != nil {
    65  		t.Fatalf("error getting pod cgroups: %v", err)
    66  	}
    67  	if len(cgs) == 0 {
    68  		t.Fatalf("expected pod cgroup directories after run, but found none")
    69  	}
    70  
    71  	runGC(t, ctx)
    72  
    73  	cgs, err = getPodCgroups(shortUUID)
    74  	if err != nil {
    75  		t.Fatalf("error getting pod cgroups: %v", err)
    76  	}
    77  	if len(cgs) > 0 {
    78  		t.Fatalf(fmt.Sprintf("expected no pod cgroup directories after GC, but found %d: %v", len(cgs), cgs))
    79  	}
    80  }