From f363ce804be86ee2c8a99a02a6abfd67e1421091 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Mon, 17 Mar 2014 16:40:15 -0700 Subject: [PATCH] improved coverage of Builder.teardown function in pkg/build --- pkg/build/build.go | 31 +++++++++-------- pkg/build/build_test.go | 73 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 84 insertions(+), 20 deletions(-) diff --git a/pkg/build/build.go b/pkg/build/build.go index 8b39698b1..ffe9ce1d7 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -58,11 +58,13 @@ type Builder struct { Key []byte // Timeout is the maximum amount of to will wait for a process - // to exit. - // - // The default is no timeout. + // to exit. The default is no timeout. Timeout time.Duration + // Privileged indicates the build should be executed in privileged + // mode. The default is false. + Privileged bool + // Stdout specifies the builds's standard output. // // If stdout is nil, Run connects the corresponding file descriptor @@ -306,9 +308,12 @@ func (b *Builder) run() error { AttachStdout: true, AttachStderr: true, } - host := docker.HostConfig{ - Privileged: false, - } + + // configure if Docker should run in privileged mode. + // by default, this is disabled for pull requests for + // security reasons. + host := docker.HostConfig{} + host.Privileged = b.Privileged && len(b.Repo.PR) == 0 // debugging log.Noticef("starting build %s", b.Build.Name) @@ -325,15 +330,15 @@ func (b *Builder) run() error { } // where are temp files going to go? - tmp_path := "/tmp/drone" + tmpPath := "/tmp/drone" if len(os.Getenv("DRONE_TMP")) > 0 { - tmp_path = os.Getenv("DRONE_TMP") + tmpPath = os.Getenv("DRONE_TMP") } - log.Infof("temp directory is %s", tmp_path) + log.Infof("temp directory is %s", tmpPath) - if err := os.MkdirAll(tmp_path, 0777); err != nil { - return fmt.Errorf("Failed to create temp directory at %s: %s", tmp_path, err) + if err := os.MkdirAll(tmpPath, 0777); err != nil { + return fmt.Errorf("Failed to create temp directory at %s: %s", tmpPath, err) } // link cached volumes @@ -352,7 +357,7 @@ func (b *Builder) run() error { // local cache path on the host machine // this path is going to be really long - hostpath := filepath.Join(tmp_path, name, branch, volume) + hostpath := filepath.Join(tmpPath, name, branch, volume) // check if the volume is created if _, err := os.Stat(hostpath); err != nil { @@ -474,7 +479,7 @@ func (b *Builder) writeBuildScript(dir string) error { f.WriteEnv("DRONE_PR", b.Repo.PR) f.WriteEnv("DRONE_BUILD_DIR", b.Repo.Dir) - // add /etc/hosts entries + // add /etc/hosts entries for _, mapping := range b.Build.Hosts { f.WriteHost(mapping) } diff --git a/pkg/build/build_test.go b/pkg/build/build_test.go index 34f2f3c50..b68da7ca2 100644 --- a/pkg/build/build_test.go +++ b/pkg/build/build_test.go @@ -274,15 +274,74 @@ func TestSetupErrorBuildInspect(t *testing.T) { // TestTeardown will test our ability to sucessfully teardown a // Docker-based build environment. -func TestTeardown(t *testing.T) {} +func TestTeardown(t *testing.T) { + setup() + defer teardown() -// TestTeardownContainerFail will test our ability to handle a -// failure to stop and remove the build container. -func TestTeardownContainerFail(t *testing.T) {} + var ( + containerStopped = false + containerRemoved = false + serviceStopped = false + serviceRemoved = false + imageRemoved = false + ) -// TestTeardownImageFail will test our ability to handle a -// failure to stop and remove the build image. -func TestTeardownImageFail(t *testing.T) {} + mux.HandleFunc("/v1.9/containers/7bf9ce0ffb/stop", func(w http.ResponseWriter, r *http.Request) { + containerStopped = true + w.WriteHeader(http.StatusOK) + }) + + mux.HandleFunc("/v1.9/containers/7bf9ce0ffb", func(w http.ResponseWriter, r *http.Request) { + containerRemoved = true + w.WriteHeader(http.StatusOK) + }) + + mux.HandleFunc("/v1.9/containers/ec62dcc736/stop", func(w http.ResponseWriter, r *http.Request) { + serviceStopped = true + w.WriteHeader(http.StatusOK) + }) + + mux.HandleFunc("/v1.9/containers/ec62dcc736", func(w http.ResponseWriter, r *http.Request) { + serviceRemoved = true + w.WriteHeader(http.StatusOK) + }) + + mux.HandleFunc("/v1.9/images/c3ab8ff137", func(w http.ResponseWriter, r *http.Request) { + imageRemoved = true + w.Write([]byte(`[{"Untagged":"c3ab8ff137"},{"Deleted":"c3ab8ff137"}]`)) + }) + + b := Builder{} + b.dockerClient = client + b.services = append(b.services, &docker.Container{ID: "ec62dcc736"}) + b.container = &docker.Run{ID: "7bf9ce0ffb"} + b.image = &docker.Image{ID: "c3ab8ff137"} + b.Build = &script.Build{Services: []string{"mysql"}} + b.teardown() + + if !containerStopped { + t.Errorf("Expected Docker container was stopped") + } + + if !containerRemoved { + t.Errorf("Expected Docker container was removed") + } + + if !serviceStopped { + t.Errorf("Expected Docker mysql container was stopped") + } + + if !serviceRemoved { + t.Errorf("Expected Docker mysql container was removed") + } + + if !imageRemoved { + t.Errorf("Expected Docker image was removed") + } + + // TODO test service container stop + // TODO test service container remove +} func TestWriteIdentifyFile(t *testing.T) { // temporary directory to store file