github.com/opencontainers/runtime-tools@v0.9.0/validation/delete_only_create_resources/delete_only_create_resources.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strconv"
     9  	"time"
    10  
    11  	tap "github.com/mndrix/tap-go"
    12  	"github.com/mrunalp/fileutils"
    13  	rspec "github.com/opencontainers/runtime-spec/specs-go"
    14  	"github.com/opencontainers/runtime-tools/specerror"
    15  	"github.com/opencontainers/runtime-tools/validation/util"
    16  	uuid "github.com/satori/go.uuid"
    17  )
    18  
    19  func main() {
    20  	t := tap.New()
    21  	t.Header(0)
    22  
    23  	// Create a cgroup
    24  	cgPath := "/sys/fs/cgroup"
    25  	testPath := filepath.Join(cgPath, "pids", "cgrouptest")
    26  	os.Mkdir(testPath, 0755)
    27  	defer os.RemoveAll(testPath)
    28  
    29  	bundleDir, err := util.PrepareBundle()
    30  	if err != nil {
    31  		util.Fatal(err)
    32  	}
    33  	defer os.RemoveAll(bundleDir)
    34  
    35  	r, err := util.NewRuntime(util.RuntimeCommand, bundleDir)
    36  	if err != nil {
    37  		util.Fatal(err)
    38  	}
    39  
    40  	r.SetID(uuid.NewV4().String())
    41  	g, err := util.GetDefaultGenerator()
    42  	if err != nil {
    43  		util.Fatal(err)
    44  	}
    45  
    46  	err = r.SetConfig(g)
    47  	if err != nil {
    48  		util.Fatal(err)
    49  	}
    50  	err = fileutils.CopyFile("runtimetest", filepath.Join(r.BundleDir, "runtimetest"))
    51  	if err != nil {
    52  		util.Fatal(err)
    53  	}
    54  
    55  	err = r.Create()
    56  	if err != nil {
    57  		util.Fatal(err)
    58  	}
    59  
    60  	state, err := r.State()
    61  	if err != nil {
    62  		util.Fatal(err)
    63  	}
    64  	// Add the container to the cgroup
    65  	err = ioutil.WriteFile(filepath.Join(testPath, "tasks"), []byte(strconv.Itoa(state.Pid)), 0644)
    66  	if err != nil {
    67  		util.Fatal(err)
    68  	}
    69  
    70  	err = r.Start()
    71  	if err != nil {
    72  		util.Fatal(err)
    73  	}
    74  
    75  	err = util.WaitingForStatus(r, util.LifecycleStatusStopped, time.Second*10, time.Second*1)
    76  	if err == nil {
    77  		err = r.Delete()
    78  	}
    79  	if err != nil {
    80  		t.Fail(err.Error())
    81  	}
    82  
    83  	_, err = os.Stat(testPath)
    84  	fmt.Println(err)
    85  	util.SpecErrorOK(t, err == nil, specerror.NewError(specerror.DeleteOnlyCreatedRes, fmt.Errorf("Note that resources associated with the container, but not created by this container, MUST NOT be deleted"), rspec.Version), nil)
    86  
    87  	t.AutoPlan()
    88  }