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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"time"
     7  
     8  	"github.com/mndrix/tap-go"
     9  	rspecs "github.com/opencontainers/runtime-spec/specs-go"
    10  	rfc2119 "github.com/opencontainers/runtime-tools/error"
    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  
    20  	g, err := util.GetDefaultGenerator()
    21  	if err != nil {
    22  		util.Fatal(err)
    23  	}
    24  	g.SetProcessArgs([]string{"true"})
    25  	containerID := uuid.NewV4().String()
    26  
    27  	cases := []struct {
    28  		id          string
    29  		action      util.LifecycleAction
    30  		errExpected bool
    31  		err         *rfc2119.Error
    32  	}{
    33  		{"", util.LifecycleActionNone, false, specerror.NewRFCErrorOrPanic(specerror.QueryWithoutIDGenError, fmt.Errorf("state MUST generate an error if it is not provided the ID of a container"), rspecs.Version)},
    34  		{containerID, util.LifecycleActionNone, false, specerror.NewRFCErrorOrPanic(specerror.QueryNonExistGenError, fmt.Errorf("state MUST generate an error if a container that does not exist"), rspecs.Version)},
    35  		{containerID, util.LifecycleActionCreate | util.LifecycleActionDelete, true, specerror.NewRFCErrorOrPanic(specerror.QueryStateImplement, fmt.Errorf("state MUST return the state of a container as specified in the State section"), rspecs.Version)},
    36  	}
    37  
    38  	for _, c := range cases {
    39  		config := util.LifecycleConfig{
    40  			Config:  g,
    41  			Actions: c.action,
    42  			PreCreate: func(r *util.Runtime) error {
    43  				r.SetID(c.id)
    44  				return nil
    45  			},
    46  			PostCreate: func(r *util.Runtime) error {
    47  				_, err = r.State()
    48  				r.Kill("KILL")
    49  				util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*10, time.Second)
    50  				return err
    51  			},
    52  		}
    53  		err = util.RuntimeLifecycleValidate(config)
    54  		// DefaultStateJSONPattern might returns
    55  		if e, ok := err.(*specerror.Error); ok {
    56  			diagnostic := map[string]string{
    57  				"reference": e.Err.Reference,
    58  				"error":     e.Err.Error(),
    59  			}
    60  			t.YAML(diagnostic)
    61  			continue
    62  		}
    63  
    64  		t.Ok((err == nil) == c.errExpected, c.err.Error())
    65  		diagnostic := map[string]string{
    66  			"reference": c.err.Reference,
    67  		}
    68  		if err != nil {
    69  			diagnostic["error"] = err.Error()
    70  			if e, ok := err.(*exec.ExitError); ok {
    71  				if len(e.Stderr) > 0 {
    72  					diagnostic["stderr"] = string(e.Stderr)
    73  				}
    74  			}
    75  		}
    76  		t.YAML(diagnostic)
    77  	}
    78  
    79  	t.AutoPlan()
    80  }