Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions validation/hooks_stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,77 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"time"

multierror "github.com/hashicorp/go-multierror"
tap "github.com/mndrix/tap-go"
rspecs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/specerror"
"github.com/opencontainers/runtime-tools/validation/util"
uuid "github.com/satori/go.uuid"
)

func stdinStateCheck(outputDir, hookName string, expectedState rspecs.State) error {
func stdinStateCheck(outputDir, hookName string, expectedState rspecs.State) (errs *multierror.Error) {
var state rspecs.State
data, err := ioutil.ReadFile(filepath.Join(outputDir, hookName))
if err != nil {
return err
errs = multierror.Append(errs, err)
return
}
err = json.Unmarshal(data, &state)
if err != nil {
return err
errs = multierror.Append(errs, err)
return
}

if state.ID != expectedState.ID {
return fmt.Errorf("wrong container ID %q in the stdin of %s hook, expected %q", state.ID, hookName, expectedState.ID)
err = fmt.Errorf("wrong container ID %q in the stdin of %s hook, expected %q", state.ID, hookName, expectedState.ID)
errs = multierror.Append(errs, err)
}

if state.Bundle != expectedState.Bundle {
return fmt.Errorf("wrong bundle directory %q in the stdin of %s hook, expected %q", state.Bundle, hookName, expectedState.Bundle)
err = fmt.Errorf("wrong bundle directory %q in the stdin of %s hook, expected %q", state.Bundle, hookName, expectedState.Bundle)
errs = multierror.Append(errs, err)
}

if hookName != "poststop" && state.Pid != expectedState.Pid {
return fmt.Errorf("wrong container process ID %q in the stdin of %s hook, expected %q", state.Version, hookName, expectedState.Version)
err = fmt.Errorf("wrong container process ID %q in the stdin of %s hook, expected %q", state.Version, hookName, expectedState.Version)
errs = multierror.Append(errs, err)
}

if !reflect.DeepEqual(state.Annotations, expectedState.Annotations) {
return fmt.Errorf("wrong annotations \"%v\" in the stdin of %s hook, expected \"%v\"", state.Annotations, hookName, expectedState.Annotations)
err = fmt.Errorf("wrong annotations \"%v\" in the stdin of %s hook, expected \"%v\"", state.Annotations, hookName, expectedState.Annotations)
errs = multierror.Append(errs, err)
}
return nil

switch hookName {
case "prestart":
if state.Status != "created" {
err = fmt.Errorf("wrong status %q in the stdin of %s hook, expected %q", state.Status, hookName, "created")
errs = multierror.Append(errs, err)
}
case "poststart":
if state.Status != "running" {
err = fmt.Errorf("wrong status %q in the stdin of %s hook, expected %q", state.Status, hookName, "running")
errs = multierror.Append(errs, err)
}
case "poststop":
if state.Status == "" {
err = fmt.Errorf("status in the stdin of %s hook should not be empty", hookName)
errs = multierror.Append(errs, err)
}
default:
err = fmt.Errorf("internal error, unexpected hook name %q", hookName)
errs = multierror.Append(errs, err)
}

return
}

func main() {
Expand Down Expand Up @@ -120,8 +150,8 @@ func main() {
Annotations: map[string]string{annotationKey: annotationValue},
}
for _, file := range []string{"prestart", "poststart", "poststop"} {
err = stdinStateCheck(outputDir, file, expectedState)
util.SpecErrorOK(t, err == nil, specerror.NewError(specerror.PosixHooksStateToStdin, fmt.Errorf("the state of the container MUST be passed to %q hook over stdin", file), rspecs.Version), err)
errs := stdinStateCheck(outputDir, file, expectedState)
util.SpecErrorOK(t, errs.ErrorOrNil() == nil, specerror.NewError(specerror.PosixHooksStateToStdin, fmt.Errorf("the state of the container MUST be passed to %q hook over stdin", file), rspecs.Version), errors.New(errs.Error()))
}

t.AutoPlan()
Expand Down