github.com/opencontainers/runtime-tools@v0.9.0/validation/poststop_fail/poststop_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  	poststop := rspec.Hook{
    33  		Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/false"),
    34  		Args: []string{"false"},
    35  	}
    36  	g.AddPostStopHook(poststop)
    37  	poststopOK := rspec.Hook{
    38  		Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/sh"),
    39  		Args: []string{
    40  			"sh", "-c", fmt.Sprintf("echo 'post-stop called' >> %s", output),
    41  		},
    42  	}
    43  	g.AddPostStopHook(poststopOK)
    44  	g.SetProcessArgs([]string{"true"})
    45  
    46  	config := util.LifecycleConfig{
    47  		Config:    g,
    48  		BundleDir: bundleDir,
    49  		Actions:   util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete,
    50  		PreCreate: func(r *util.Runtime) error {
    51  			r.SetID(uuid.NewV4().String())
    52  			return nil
    53  		},
    54  		PreDelete: func(r *util.Runtime) error {
    55  			util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*10, time.Second)
    56  			return nil
    57  		},
    58  	}
    59  
    60  	runErr := util.RuntimeLifecycleValidate(config)
    61  	outputData, _ := ioutil.ReadFile(output)
    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-stop called\n" {
    65  		err = specerror.NewError(specerror.PoststopHookFailGenWarn, fmt.Errorf("if any poststop 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  }