improved coverage of Builder.teardown function in pkg/build

This commit is contained in:
Brad Rydzewski 2014-03-17 16:40:15 -07:00
parent 854d3443d7
commit f363ce804b
2 changed files with 84 additions and 20 deletions

View file

@ -58,11 +58,13 @@ type Builder struct {
Key []byte Key []byte
// Timeout is the maximum amount of to will wait for a process // Timeout is the maximum amount of to will wait for a process
// to exit. // to exit. The default is no timeout.
//
// The default is no timeout.
Timeout time.Duration 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. // Stdout specifies the builds's standard output.
// //
// If stdout is nil, Run connects the corresponding file descriptor // If stdout is nil, Run connects the corresponding file descriptor
@ -306,9 +308,12 @@ func (b *Builder) run() error {
AttachStdout: true, AttachStdout: true,
AttachStderr: 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 // debugging
log.Noticef("starting build %s", b.Build.Name) log.Noticef("starting build %s", b.Build.Name)
@ -325,15 +330,15 @@ func (b *Builder) run() error {
} }
// where are temp files going to go? // where are temp files going to go?
tmp_path := "/tmp/drone" tmpPath := "/tmp/drone"
if len(os.Getenv("DRONE_TMP")) > 0 { 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 { if err := os.MkdirAll(tmpPath, 0777); err != nil {
return fmt.Errorf("Failed to create temp directory at %s: %s", tmp_path, err) return fmt.Errorf("Failed to create temp directory at %s: %s", tmpPath, err)
} }
// link cached volumes // link cached volumes
@ -352,7 +357,7 @@ func (b *Builder) run() error {
// local cache path on the host machine // local cache path on the host machine
// this path is going to be really long // 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 // check if the volume is created
if _, err := os.Stat(hostpath); err != nil { 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_PR", b.Repo.PR)
f.WriteEnv("DRONE_BUILD_DIR", b.Repo.Dir) f.WriteEnv("DRONE_BUILD_DIR", b.Repo.Dir)
// add /etc/hosts entries // add /etc/hosts entries
for _, mapping := range b.Build.Hosts { for _, mapping := range b.Build.Hosts {
f.WriteHost(mapping) f.WriteHost(mapping)
} }

View file

@ -274,15 +274,74 @@ func TestSetupErrorBuildInspect(t *testing.T) {
// TestTeardown will test our ability to sucessfully teardown a // TestTeardown will test our ability to sucessfully teardown a
// Docker-based build environment. // 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 var (
// failure to stop and remove the build container. containerStopped = false
func TestTeardownContainerFail(t *testing.T) {} containerRemoved = false
serviceStopped = false
serviceRemoved = false
imageRemoved = false
)
// TestTeardownImageFail will test our ability to handle a mux.HandleFunc("/v1.9/containers/7bf9ce0ffb/stop", func(w http.ResponseWriter, r *http.Request) {
// failure to stop and remove the build image. containerStopped = true
func TestTeardownImageFail(t *testing.T) {} 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) { func TestWriteIdentifyFile(t *testing.T) {
// temporary directory to store file // temporary directory to store file