-
Notifications
You must be signed in to change notification settings - Fork 143
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
64e1103
commit 3ba4371
Showing
3 changed files
with
141 additions
and
6 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
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,116 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"time" | ||
|
||
"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 main() { | ||
t := tap.New() | ||
t.Header(0) | ||
|
||
bundleDir, err := util.PrepareBundle() | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
defer os.RemoveAll(bundleDir) | ||
|
||
containerID := uuid.NewV4().String() | ||
|
||
r, err := util.NewRuntime(util.RuntimeCommand, bundleDir) | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
g := util.GetDefaultGenerator() | ||
g.SetProcessArgs([]string{"sh", "-c", fmt.Sprintf("echo 'process called' >> %s", "/output")}) | ||
r.SetConfig(g) | ||
output := filepath.Join(bundleDir, g.Spec().Root.Path, "output") | ||
|
||
// start without id | ||
err = r.Start() | ||
util.SpecErrorOK(t, err != nil, specerror.NewError(specerror.StartWithoutIDGenError, fmt.Errorf("start` operation MUST generate an error if it is not provided the container ID"), rspecs.Version), err) | ||
|
||
// set id for the remaining tests | ||
r.SetID(containerID) | ||
|
||
// start a not `created` container - case one: non-exist container | ||
err = r.Start() | ||
util.SpecErrorOK(t, err != nil, specerror.NewError(specerror.StartNotCreatedGenError, fmt.Errorf("attempting to `start` a container that is not `created` MUST generate an error"), rspecs.Version), err) | ||
|
||
err = r.Create() | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
// start a `created` container | ||
err = r.Start() | ||
if err != nil { | ||
util.SpecErrorOK(t, false, specerror.NewError(specerror.StartProcImplement, fmt.Errorf("`start` operation MUST run the user-specified program as specified by `process`"), rspecs.Version), err) | ||
} else { | ||
err := util.WaitingForStatus(r, util.LifecycleStatusStopped, time.Second*10, time.Second*1) | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
outputData, outputErr := ioutil.ReadFile(output) | ||
// check the output | ||
util.SpecErrorOK(t, outputErr == nil && string(outputData) == "process called\n", specerror.NewError(specerror.StartProcImplement, fmt.Errorf("`start` operation MUST run the user-specified program as specified by `process`"), rspecs.Version), outputErr) | ||
} | ||
|
||
// start a not `created` container - case two: exist and `stopped` | ||
err = r.Start() | ||
// must generate an error | ||
util.SpecErrorOK(t, err != nil, specerror.NewError(specerror.StartNotCreatedGenError, fmt.Errorf("attempting to `start` a container that is not `created` MUST generate an error"), rspecs.Version), err) | ||
|
||
err = util.WaitingForStatus(r, util.LifecycleStatusStopped, time.Second*10, time.Second*1) | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
|
||
outputData, outputErr := ioutil.ReadFile(output) | ||
// must have no effect, it will not be something like 'process called\nprocess called\n' | ||
util.SpecErrorOK(t, outputErr == nil && string(outputData) == "process called\n", specerror.NewError(specerror.StartNotCreatedHaveNoEffect, fmt.Errorf("attempting to `start` a container that is not `created` MUST have no effect on the container"), rspecs.Version), outputErr) | ||
|
||
err = r.Delete() | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
|
||
g.Spec().Process = nil | ||
r.SetConfig(g) | ||
err = r.Create() | ||
// 'create' could fail according to the spec when the process is nil | ||
// only do the test when a container is created | ||
if err == nil { | ||
err = r.Start() | ||
util.SpecErrorOK(t, err == nil, specerror.NewError(specerror.StartWithProcUnsetGenError, fmt.Errorf("`start` operation MUST generate an error if `process` was not set"), rspecs.Version), err) | ||
err = util.WaitingForStatus(r, util.LifecycleStatusStopped, time.Second*10, time.Second*1) | ||
if err == nil { | ||
err = r.Delete() | ||
} | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
} else { | ||
diagnostic := map[string]string{ | ||
"error": err.Error(), | ||
} | ||
if e, ok := err.(*exec.ExitError); ok { | ||
if len(e.Stderr) > 0 { | ||
diagnostic["stderr"] = string(e.Stderr) | ||
} | ||
} | ||
t.Skip(1, "`start` operation MUST generate an error if `process` was not set") | ||
t.YAML(diagnostic) | ||
} | ||
|
||
t.AutoPlan() | ||
} |
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