func getContainer(eng *engine.Engine, id string, t utils.Fataler) *docker.Container { runtime := mkRuntimeFromEngine(eng, t) c := runtime.Get(id) if c == nil { t.Fatal(fmt.Errorf("No such container: %s", id)) } return c }
func createNamedTestContainer(eng *engine.Engine, config *docker.Config, f utils.Fataler, name string) (shortId string) { job := eng.Job("create", name) if err := job.ImportEnv(config); err != nil { f.Fatal(err) } job.Stdout.AddString(&shortId) if err := job.Run(); err != nil { f.Fatal(err) } return }
func containerAttach(eng *engine.Engine, id string, t utils.Fataler) (io.WriteCloser, io.ReadCloser) { c := getContainer(eng, id, t) i, err := c.StdinPipe() if err != nil { t.Fatal(err) } o, err := c.StdoutPipe() if err != nil { t.Fatal(err) } return i, o }
func containerFileExists(eng *engine.Engine, id, dir string, t utils.Fataler) bool { c := getContainer(eng, id, t) if err := c.Mount(); err != nil { t.Fatal(err) } defer c.Unmount() if _, err := os.Stat(path.Join(c.BasefsPath(), dir)); err != nil { if os.IsNotExist(err) { return false } t.Fatal(err) } return true }
func NewTestEngine(t utils.Fataler) *engine.Engine { root, err := newTestDirectory(unitTestStoreBase) if err != nil { t.Fatal(err) } eng, err := engine.New(root) if err != nil { t.Fatal(err) } // Load default plugins // (This is manually copied and modified from main() until we have a more generic plugin system) job := eng.Job("initserver") job.Setenv("Root", root) job.SetenvBool("AutoRestart", false) // TestGetEnabledCors and TestOptionsRoute require EnableCors=true job.SetenvBool("EnableCors", true) if err := job.Run(); err != nil { t.Fatal(err) } return eng }
// Create a temporary runtime suitable for unit testing. // Call t.Fatal() at the first error. func mkRuntime(f utils.Fataler) *docker.Runtime { root, err := newTestDirectory(unitTestStoreBase) if err != nil { f.Fatal(err) } config := &docker.DaemonConfig{ Root: root, AutoRestart: false, Mtu: docker.DefaultNetworkMtu, } eng, err := engine.New(root) if err != nil { f.Fatal(err) } r, err := docker.NewRuntimeFromDirectory(config, eng) if err != nil { f.Fatal(err) } return r }
func startContainer(eng *engine.Engine, id string, t utils.Fataler) { job := eng.Job("start", id) if err := job.Run(); err != nil { t.Fatal(err) } }
// assertHttpError expect the given response to have an error. // Otherwise the it causes the test to fail. func assertHttpError(r *httptest.ResponseRecorder, t utils.Fataler) { // Non-error http status are [200, 400) if !(r.Code < http.StatusOK || r.Code >= http.StatusBadRequest) { t.Fatal(fmt.Errorf("Unexpected http success code: %v", r.Code)) } }
func containerAssertNotExists(eng *engine.Engine, id string, t utils.Fataler) { runtime := mkRuntimeFromEngine(eng, t) if c := runtime.Get(id); c != nil { t.Fatal(fmt.Errorf("Container %s should not exist", id)) } }
func containerKill(eng *engine.Engine, id string, t utils.Fataler) { if err := eng.Job("kill", id).Run(); err != nil { t.Fatal(err) } }