func TestEmpire_Deploy_ImageNotFound(t *testing.T) { e := empiretest.NewEmpire(t) s := new(mockScheduler) e.Scheduler = s e.ProcfileExtractor = empire.ProcfileExtractorFunc(func(ctx context.Context, img image.Image, w io.Writer) ([]byte, error) { return nil, errors.New("image not found") }) // Deploying an image to an app that doesn't exist will create a new // app. _, err := e.Deploy(context.Background(), empire.DeployOpts{ User: &empire.User{Name: "ejholmes"}, Output: empire.NewDeploymentStream(ioutil.Discard), Image: image.Image{Repository: "remind101/acme-inc"}, }) assert.Error(t, err) // If there's an error deploying, then the transaction should be rolled // backed and no apps should exist. apps, err := e.Apps(empire.AppsQuery{}) assert.NoError(t, err) assert.Equal(t, 0, len(apps)) s.AssertExpectations(t) }
// Returns a function that can be used as a Procfile extract for Empire. It // writes a fake Docker pull to w, and extracts the given Procfile in yaml // format. func ExtractProcfile(pf procfile.Procfile) empire.ProcfileExtractor { if pf == nil { pf = defaultProcfile } return empire.ProcfileExtractorFunc(func(ctx context.Context, img image.Image, w io.Writer) ([]byte, error) { p, err := procfile.Marshal(pf) if err != nil { return nil, err } return p, dockerutil.FakePull(img, w) }) }
func TestEmpire_Deploy_Concurrent(t *testing.T) { e := empiretest.NewEmpire(t) s := new(mockScheduler) e.Scheduler = scheduler.NewFakeScheduler() e.ProcfileExtractor = empiretest.ExtractProcfile(procfile.ExtendedProcfile{ "web": procfile.Process{ Command: []string{"./bin/web"}, }, }) user := &empire.User{Name: "ejholmes"} // Create the first release for this app. r, err := e.Deploy(context.Background(), empire.DeployOpts{ User: user, Output: empire.NewDeploymentStream(ioutil.Discard), Image: image.Image{Repository: "remind101/acme-inc"}, }) assert.NoError(t, err) assert.Equal(t, 1, r.Version) // We'll use the procfile extractor to synchronize two concurrent // deployments. v2Started, v3Started := make(chan struct{}), make(chan struct{}) e.ProcfileExtractor = empire.ProcfileExtractorFunc(func(ctx context.Context, img image.Image, w io.Writer) ([]byte, error) { switch img.Tag { case "v2": close(v2Started) <-v3Started case "v3": close(v3Started) } return procfile.Marshal(procfile.ExtendedProcfile{ "web": procfile.Process{ Command: []string{"./bin/web"}, }, }) }) v2Done := make(chan struct{}) go func() { r, err := e.Deploy(context.Background(), empire.DeployOpts{ User: user, Output: empire.NewDeploymentStream(ioutil.Discard), Image: image.Image{Repository: "remind101/acme-inc", Tag: "v2"}, }) assert.NoError(t, err) assert.Equal(t, 2, r.Version) close(v2Done) }() <-v2Started r, err = e.Deploy(context.Background(), empire.DeployOpts{ User: user, Output: empire.NewDeploymentStream(ioutil.Discard), Image: image.Image{Repository: "remind101/acme-inc", Tag: "v3"}, }) assert.NoError(t, err) assert.Equal(t, 3, r.Version) <-v2Done s.AssertExpectations(t) }