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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"time"
     9  
    10  	tap "github.com/mndrix/tap-go"
    11  	rspec "github.com/opencontainers/runtime-spec/specs-go"
    12  	"github.com/opencontainers/runtime-tools/specerror"
    13  	"github.com/opencontainers/runtime-tools/validation/util"
    14  	uuid "github.com/satori/go.uuid"
    15  )
    16  
    17  func main() {
    18  	t := tap.New()
    19  	t.Header(0)
    20  
    21  	bundleDir, err := util.PrepareBundle()
    22  	if err != nil {
    23  		return
    24  	}
    25  	defer os.RemoveAll(bundleDir)
    26  
    27  	g, err := util.GetDefaultGenerator()
    28  	if err != nil {
    29  		util.Fatal(err)
    30  	}
    31  	output := filepath.Join(bundleDir, g.Spec().Root.Path, "output")
    32  	poststart := rspec.Hook{
    33  		Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/false"),
    34  		Args: []string{"false"},
    35  	}
    36  	g.AddPostStartHook(poststart)
    37  	poststartOK := rspec.Hook{
    38  		Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/sh"),
    39  		Args: []string{
    40  			"sh", "-c", fmt.Sprintf("echo 'post-start called' >> %s", output),
    41  		},
    42  	}
    43  	g.AddPostStartHook(poststartOK)
    44  	g.SetProcessArgs([]string{"true"})
    45  	config := util.LifecycleConfig{
    46  		Config:    g,
    47  		BundleDir: bundleDir,
    48  		Actions:   util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete,
    49  		PreCreate: func(r *util.Runtime) error {
    50  			r.SetID(uuid.NewV4().String())
    51  			return nil
    52  		},
    53  		PreDelete: func(r *util.Runtime) error {
    54  			util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*10, time.Second)
    55  			return nil
    56  		},
    57  	}
    58  
    59  	runErr := util.RuntimeLifecycleValidate(config)
    60  	outputData, _ := ioutil.ReadFile(output)
    61  
    62  	// if runErr is not nil, it means the runtime generates an error
    63  	// if outputData is not equal to the expected content, it means there is something wrong with the remaining hooks and lifecycle
    64  	if runErr != nil || string(outputData) != "post-start called\n" {
    65  		err = specerror.NewError(specerror.PoststartHookFailGenWarn, fmt.Errorf("if any poststart hook fails, the runtime MUST log a warning, but the remaining hooks and lifecycle continue as if the hook had succeeded"), rspec.Version)
    66  		diagnostic := map[string]string{
    67  			"error": err.Error(),
    68  		}
    69  		t.YAML(diagnostic)
    70  	}
    71  
    72  	t.AutoPlan()
    73  }