-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add 'Runtime' struct to make test easier; add create test #447
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,124 @@ | ||
package validation | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
rspecs "github.com/opencontainers/runtime-spec/specs-go" | ||
"github.com/opencontainers/runtime-tools/generate" | ||
) | ||
|
||
// Runtime represents the basic requirement of a container runtime | ||
type Runtime struct { | ||
RuntimeCommand string | ||
BundleDir string | ||
ID string | ||
} | ||
|
||
// NewRuntime create a runtime by command and the bundle directory | ||
func NewRuntime(runtimeCommand string, bundleDir string) (Runtime, error) { | ||
var r Runtime | ||
var err error | ||
r.RuntimeCommand, err = exec.LookPath(runtimeCommand) | ||
if err != nil { | ||
return Runtime{}, err | ||
} | ||
|
||
r.BundleDir = bundleDir | ||
return r, err | ||
} | ||
|
||
// SetConfig creates a 'config.json' by the generator | ||
func (r *Runtime) SetConfig(g *generate.Generator) error { | ||
if r.BundleDir == "" { | ||
return errors.New("Please set the bundle directory first") | ||
} | ||
return g.SaveToFile(filepath.Join(r.BundleDir, "config.json"), generate.ExportOptions{}) | ||
} | ||
|
||
// SetID sets the container ID | ||
func (r *Runtime) SetID(id string) { | ||
r.ID = id | ||
} | ||
|
||
// Create a container | ||
func (r *Runtime) Create() error { | ||
var args []string | ||
args = append(args, "create") | ||
if r.ID != "" { | ||
args = append(args, r.ID) | ||
} | ||
|
||
// TODO: following the spec, we need define the bundle, but 'runc' does not.. | ||
// if r.BundleDir != "" { | ||
// args = append(args, r.BundleDir) | ||
// } | ||
cmd := exec.Command(r.RuntimeCommand, args...) | ||
cmd.Dir = r.BundleDir | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
return cmd.Run() | ||
} | ||
|
||
// Start a container | ||
func (r *Runtime) Start() error { | ||
var args []string | ||
args = append(args, "start") | ||
if r.ID != "" { | ||
args = append(args, r.ID) | ||
} | ||
|
||
cmd := exec.Command(r.RuntimeCommand, args...) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
return cmd.Run() | ||
} | ||
|
||
// State a container information | ||
func (r *Runtime) State() (rspecs.State, error) { | ||
var args []string | ||
args = append(args, "state") | ||
if r.ID != "" { | ||
args = append(args, r.ID) | ||
} | ||
|
||
out, err := exec.Command(r.RuntimeCommand, args...).Output() | ||
if err != nil { | ||
return rspecs.State{}, err | ||
} | ||
|
||
var state rspecs.State | ||
err = json.Unmarshal(out, &state) | ||
return state, err | ||
} | ||
|
||
// Delete a container | ||
func (r *Runtime) Delete() error { | ||
var args []string | ||
args = append(args, "delete") | ||
if r.ID != "" { | ||
args = append(args, r.ID) | ||
} | ||
|
||
cmd := exec.Command(r.RuntimeCommand, args...) | ||
return cmd.Run() | ||
} | ||
|
||
// Clean deletes the container and removes the bundle file according to the input parameter | ||
func (r *Runtime) Clean(removeBundle bool) error { | ||
err := r.Delete() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if removeBundle { | ||
os.RemoveAll(r.BundleDir) | ||
} | ||
|
||
return nil | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is broken, since you've deferred a
delete
call (viaClean
) earlier in the function. That doesn't work reliably though, becausedelete
MUST generate an error if the container is notstopped
, and the fact thatstart
completes doesn't mean you'restopped
(you could also berunning
). In order forClean()
to work reliably, you either need to attempt to send aKILL
to the container process withkill
(although the spec is not clear on what signals are supported, you need something like #321 to see thatKILL
MUST be supported) or code that waits for the container process to exit on its own (more on this here, in #305, and in most of the other PRs that attempted to splitrun
intocreate
andstart
, e.g. here and here).