// NewEmpire returns a new Empire instance suitable for testing. It ensures that // the database is clean before returning. func NewEmpire(t testing.TB) *empire.Empire { db, err := empire.OpenDB(DatabaseURL) if err != nil { t.Fatal(err) } if err := db.MigrateUp(); err != nil { t.Fatal(err) } // Log queries if verbose mode is set. if testing.Verbose() { db.Debug() } e := empire.New(db) e.Scheduler = scheduler.NewFakeScheduler() e.ProcfileExtractor = ExtractProcfile(nil) e.RunRecorder = empire.RecordTo(ioutil.Discard) if err := e.Reset(); err != nil { t.Fatal(err) } return e }
func TestEmpire_Deploy_Concurrent(t *testing.T) { e := empiretest.NewEmpire(t) s := new(mockScheduler) e.Scheduler = scheduler.NewFakeScheduler() e.ExtractProcfile = func(ctx context.Context, img image.Image, w io.Writer) (procfile.Procfile, error) { return nil, nil } user := &empire.User{Name: "ejholmes"} // Create the first release for this app. r, err := e.Deploy(context.Background(), empire.DeploymentsCreateOpts{ User: user, Output: 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.ExtractProcfile = func(ctx context.Context, img image.Image, w io.Writer) (procfile.Procfile, error) { switch img.Tag { case "v2": close(v2Started) <-v3Started case "v3": close(v3Started) } return nil, nil } v2Done := make(chan struct{}) go func() { r, err = e.Deploy(context.Background(), empire.DeploymentsCreateOpts{ User: user, Output: 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.DeploymentsCreateOpts{ User: user, Output: 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) }
// NewEmpire returns a new Empire instance suitable for testing. It ensures that // the database is clean before returning. func NewEmpire(t testing.TB) *empire.Empire { db, err := empire.OpenDB(DatabaseURL) if err != nil { t.Fatal(err) } // TODO: Run db.MigrateUp once migrations are in the binary. // Log queries if verbose mode is set. if testing.Verbose() { db.Debug() } e := empire.New(db, empire.DefaultOptions) e.Scheduler = scheduler.NewFakeScheduler() e.ExtractProcfile = ExtractProcfile if err := e.Reset(); err != nil { t.Fatal(err) } return e }