Skip to content

add tests when prestart/poststart/poststop hooks fail#569

Merged
zhouhao3 merged 1 commit intoopencontainers:masterfrom
liangchenye:master
Feb 8, 2018
Merged

add tests when prestart/poststart/poststop hooks fail#569
zhouhao3 merged 1 commit intoopencontainers:masterfrom
liangchenye:master

Conversation

@liangchenye
Copy link
Copy Markdown
Member

Signed-off-by: Liang Chenye liangchenye@huawei.com

@liangchenye
Copy link
Copy Markdown
Member Author

It should after #567.
PTAL @q384566678 @wking

Copy link
Copy Markdown
Contributor

@wking wking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions for poststart_fail.go. Some of these may apply to the other files you're adding too, but I haven't had time to look those over yet.

r.SetID(uuid.NewV4().String())
g := util.GetDefaultGenerator()
poststart := rspec.Hook{
Path: fmt.Sprintf("%s/%s/bin/false", r.BundleDir, g.Spec().Root.Path),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use filepath.Join here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll update it.

Path: fmt.Sprintf("%s/%s/bin/false", r.BundleDir, g.Spec().Root.Path),
Args: []string{"false"},
}
g.AddPostStartHook(poststart)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you also want a second hook which writes to a file, and a post-RuntimeLifecycleValidate check to ensure that file was never written. Similarly with SetProcessArgs, you'll want to set something there so we can confirm that the runtime didn't actually execute the user-specified program.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this, I'll update these.

t.Header(0)

config := util.LifecycleConfig{
Actions: util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete,
Copy link
Copy Markdown
Contributor

@wking wking Feb 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to set util.LifecycleActionDelete or the PreDelete code below, because the start call should exit nonzero after the hook failure. That means RuntimeLifecycleValidate will exit here, before any of its delete-related code fires. You will need to explicitly delete the container in this file, and not rely on RuntimeLifecycleValidate for deletion.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/opencontainers/runtime-spec/blob/master/runtime.md#lifecycle
In poststart fail test, it will continue to work. 'start' should not exit.

Also with 'ActionDelete' set, even there is no chance to get 'r.Delete', the container will be deleted in the 'defer' function.

defer func() {


err := util.RuntimeLifecycleValidate(nil, config)
if err != nil {
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of assuming any error is the error we're trying to trigger, we should add a PostCreate action that flips a boolean:

successfulCreate := false
config := util.LifecycleConfig{
  …
  PostCreate: func(r *util.Runtime) error {
    successfulCreate = true
    return nil
  },
}
…
if !successfulCreate {
  …fail the test…
} else if err != nil {
  …what you have here…
}

Copy link
Copy Markdown
Member Author

@liangchenye liangchenye Feb 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sequence test is covered by 'prestart.go/poststart.go/poststop.go'. Here we just need to verify if the hook and lifecycle after 'poststart' will still work even if 'poststart' fails.
So we just need to verify if 'poststop' hook works.

@liangchenye
Copy link
Copy Markdown
Member Author

  1. change the RuntimeLifecycleValidation by
    1.1 add a 'BundleDir' to config
    it will be easier to test output out of the lifecycleValidation function
    1.2 add a 'generator' to config
    we don't need to have an extra parameter
  2. updates testing code by checking the outputs

@liangchenye
Copy link
Copy Markdown
Member Author

One issue found in 'runc': once a hook fail in 'poststart' or 'poststop', the remain hooks will not be excused.

One issue found in our generator: we cannot have hooks with a same Path. https://github.com/opencontainers/runtime-tools/blob/master/generate/generate.go#L918
The spec does not forbid it.

Args: []string{
"sh", "-c", fmt.Sprintf("echo 'post-stop called' >> %s", output),
},
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this poststop is needed. If the final output is not post-start called\npost-stop called\n, it's not immediately clear that it's poststart's error, and further judgment is needed. So I think this poststop should be removed to remove unnecessary effects.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, updated.

}
g.AddPostStopHook(poststop)
poststopOK := rspec.Hook{
Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/echo"),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/bin/echo -> /bin/sh

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@liangchenye
Copy link
Copy Markdown
Member Author

updated PTAL @q384566678

  1. Remove the unnecessary 'poststop' hook check in poststart_fail.
  2. Correct the wrong path

// if runErr is not nil, it means the runtime generates an error
// if outputData is not equal to the expected content, it means there is something wrong with the remaining hooks and lifecycle
// if runErr != nil || string(outputData) != "post-stop called\n" {
if runErr != nil || string(outputData) != "" {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the line you commented out is correct.

//	if runErr != nil || string(outputData) != "post-stop called\n" {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ops, big mistake.
Updated

Signed-off-by: Liang Chenye <liangchenye@huawei.com>
@liangchenye liangchenye force-pushed the master branch 2 times, most recently from 4b08dde to 9177741 Compare February 7, 2018 09:05
@zhouhao3
Copy link
Copy Markdown

zhouhao3 commented Feb 7, 2018

LGTM

Approved with PullApprove

@zhouhao3 zhouhao3 merged commit dc61225 into opencontainers:master Feb 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants