-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Liang Chenye <liangchenye@huawei.com>
- Loading branch information
1 parent
216ebc7
commit 56a596f
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"time" | ||
|
||
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 { | ||
var state rspecs.State | ||
data, err := ioutil.ReadFile(filepath.Join(outputDir, hookName)) | ||
if err != nil { | ||
return err | ||
} | ||
err = json.Unmarshal(data, &state) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
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) | ||
} | ||
|
||
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) | ||
} | ||
|
||
if reflect.DeepEqual(state.Annotations, expectedState.Annotations) { | ||
return fmt.Errorf("wrong annotations \"%v\" in the stdin of %s hook, expected \"%v\"", state.Annotations, hookName, state.Annotations) | ||
} | ||
return nil | ||
} | ||
|
||
func main() { | ||
t := tap.New() | ||
t.Header(0) | ||
|
||
bundleDir, err := util.PrepareBundle() | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
containerID := uuid.NewV4().String() | ||
defer os.RemoveAll(bundleDir) | ||
|
||
g := util.GetDefaultGenerator() | ||
outputDir := filepath.Join(bundleDir, g.Spec().Root.Path) | ||
g.AddAnnotation("org.opencontainers.runtime-tools", "hook stdin test") | ||
g.AddPreStartHook(rspecs.Hook{ | ||
Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/sh"), | ||
Args: []string{ | ||
"sh", "-c", fmt.Sprintf("timeout -t 1 cat > %s", filepath.Join(outputDir, "prestart")), | ||
}, | ||
}) | ||
g.AddPostStartHook(rspecs.Hook{ | ||
Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/sh"), | ||
Args: []string{ | ||
"sh", "-c", fmt.Sprintf("timeout -t 1 cat > %s", filepath.Join(outputDir, "poststart")), | ||
}, | ||
}) | ||
g.AddPostStopHook(rspecs.Hook{ | ||
Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/sh"), | ||
Args: []string{ | ||
"sh", "-c", fmt.Sprintf("timeout -t 1 cat > %s", filepath.Join(outputDir, "poststop")), | ||
}, | ||
}) | ||
g.SetProcessArgs([]string{"true"}) | ||
|
||
config := util.LifecycleConfig{ | ||
BundleDir: bundleDir, | ||
Config: g, | ||
Actions: util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete, | ||
PreCreate: func(r *util.Runtime) error { | ||
r.SetID(containerID) | ||
return nil | ||
}, | ||
PreDelete: func(r *util.Runtime) error { | ||
util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*10, time.Second) | ||
return nil | ||
}, | ||
} | ||
|
||
err = util.RuntimeLifecycleValidate(config) | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
|
||
expectedState := rspecs.State{ | ||
ID: containerID, | ||
Bundle: bundleDir, | ||
Annotations: map[string]string{"org.opencontainers.runtime-tools": "hook stdin test"}, | ||
} | ||
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 so that they may do work appropriate to the current state of the container", file), rspecs.Version), err) | ||
} | ||
|
||
t.AutoPlan() | ||
} |