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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"reflect"
     7  	"time"
     8  
     9  	"github.com/mndrix/tap-go"
    10  	rspecs "github.com/opencontainers/runtime-spec/specs-go"
    11  	"github.com/opencontainers/runtime-tools/specerror"
    12  	"github.com/opencontainers/runtime-tools/validation/util"
    13  	uuid "github.com/satori/go.uuid"
    14  )
    15  
    16  func main() {
    17  	t := tap.New()
    18  	t.Header(0)
    19  	bundleDir, err := util.PrepareBundle()
    20  	if err != nil {
    21  		util.Fatal(err)
    22  	}
    23  	defer os.RemoveAll(bundleDir)
    24  
    25  	targetErr := specerror.NewError(specerror.KillNonCreateRunHaveNoEffect, fmt.Errorf("attempting to send a signal to a container that is neither `created` nor `running` MUST have no effect on the container"), rspecs.Version)
    26  	containerID := uuid.NewV4().String()
    27  	g, err := util.GetDefaultGenerator()
    28  	if err != nil {
    29  		util.Fatal(err)
    30  	}
    31  	g.SetProcessArgs([]string{"true"})
    32  
    33  	config := util.LifecycleConfig{
    34  		Config:    g,
    35  		BundleDir: bundleDir,
    36  		Actions:   util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete,
    37  		PreCreate: func(r *util.Runtime) error {
    38  			r.SetID(containerID)
    39  			return nil
    40  		},
    41  		PreDelete: func(r *util.Runtime) error {
    42  			err := util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*5, time.Second*1)
    43  			if err != nil {
    44  				return err
    45  			}
    46  			currentState, err := r.State()
    47  			if err != nil {
    48  				return err
    49  			}
    50  			r.Kill("KILL")
    51  			newState, err := r.State()
    52  			if err != nil || !reflect.DeepEqual(newState, currentState) {
    53  				return targetErr
    54  			}
    55  			return nil
    56  		},
    57  	}
    58  	err = util.RuntimeLifecycleValidate(config)
    59  	if err != nil && err != targetErr {
    60  		t.Fail(err.Error())
    61  	} else {
    62  		util.SpecErrorOK(t, err == nil, targetErr, nil)
    63  	}
    64  	t.AutoPlan()
    65  }