var outputDir string

		BeforeEach(func() {
			metadata = protocol.DockerImageMetadata{
				ExecutionMetadata: protocol.ExecutionMetadata{
					Cmd:        []string{"fake-arg1", "fake-arg2"},
					Entrypoint: []string{"fake-cmd", "fake-arg0"},
					Workdir:    "/fake-workdir",
				},
				DockerImage: "cloudfoundry/diego-docker-app",
			}
		})

		Context("to an unwritable path on disk", func() {
			It("should error", func() {
				err := helpers.SaveMetadata("////tmp/", &metadata)
				Expect(err).To(HaveOccurred())
			})
		})
		Context("with a writable path on disk", func() {

			BeforeEach(func() {
				var err error
				outputDir, err = ioutil.TempDir(os.TempDir(), "metadata")
				Expect(err).NotTo(HaveOccurred())
			})

			AfterEach(func() {
				os.RemoveAll(outputDir)
			})
func (builder *Builder) build() <-chan error {
	errorChan := make(chan error, 1)

	go func() {
		defer close(errorChan)

		authConfig := &registry.AuthConfig{
			Username:      builder.DockerUser,
			Password:      builder.DockerPassword,
			Email:         builder.DockerEmail,
			ServerAddress: builder.DockerLoginServer,
		}
		img, err := helpers.FetchMetadata(builder.RepoName, builder.Tag, builder.InsecureDockerRegistries, authConfig)
		if err != nil {
			errorChan <- fmt.Errorf(
				"failed to fetch metadata from [%s] with tag [%s] and insecure registries %s due to %s",
				builder.RepoName,
				builder.Tag,
				builder.InsecureDockerRegistries,
				err.Error(),
			)
			return
		}

		info := protocol.DockerImageMetadata{}
		if img.Config != nil {
			info.ExecutionMetadata.Cmd = img.Config.Cmd
			info.ExecutionMetadata.Entrypoint = img.Config.Entrypoint
			info.ExecutionMetadata.Workdir = img.Config.WorkingDir
			info.ExecutionMetadata.User = img.Config.User
			info.ExecutionMetadata.ExposedPorts, err = extractPorts(img.Config.ExposedPorts)
			if err != nil {
				portDetails := fmt.Sprintf("%v", img.Config.ExposedPorts)
				println("failed to parse image ports", portDetails, err.Error())
				errorChan <- err
				return
			}
		}

		dockerImageURL := builder.RepoName
		if len(builder.Tag) > 0 {
			dockerImageURL = dockerImageURL + ":" + builder.Tag
		}
		info.DockerImage = dockerImageURL

		if builder.CacheDockerImage {
			info.DockerImage, err = builder.cacheDockerImage(dockerImageURL)
			if err != nil {
				println("failed to cache image", dockerImageURL, err.Error())
				errorChan <- err
				return
			}

		}

		if err := helpers.SaveMetadata(builder.OutputFilename, &info); err != nil {
			errorChan <- fmt.Errorf(
				"failed to save metadata to [%s] due to %s",
				builder.OutputFilename,
				err.Error(),
			)
			return
		}

		errorChan <- nil
	}()

	return errorChan
}