diff --git a/core/application/app.go b/core/application/app.go index 5f70fa62c..cc6fe7074 100644 --- a/core/application/app.go +++ b/core/application/app.go @@ -1,8 +1,6 @@ package application import ( - "time" - "github.com/weaveworks/reignite/core/ports" ) @@ -12,25 +10,18 @@ type App interface { ports.MicroVMCommandUseCases ports.MicroVMQueryUseCases ports.ReconcileMicroVMsUseCase - ports.HasTime } func New(cfg *Config, ports *ports.Collection) App { return &app{ cfg: cfg, ports: ports, - clock: time.Now, } } type app struct { cfg *Config ports *ports.Collection - clock func() time.Time -} - -func (a app) SetClock(clock func() time.Time) { - a.clock = clock } type Config struct { diff --git a/core/application/app_test.go b/core/application/app_test.go index 9838eca24..802f55ae1 100644 --- a/core/application/app_test.go +++ b/core/application/app_test.go @@ -143,13 +143,13 @@ func TestApp_CreateMicroVM(t *testing.T) { NetworkService: ns, ImageService: is, FileSystem: fs, + Clock: frozenTime, } tc.expect(rm.EXPECT(), em.EXPECT(), im.EXPECT(), pm.EXPECT()) ctx := context.Background() app := application.New(&application.Config{}, ports) - app.SetClock(frozenTime) _, err := app.CreateMicroVM(ctx, tc.specToCreate) if tc.expectError { @@ -242,13 +242,13 @@ func TestApp_UpdateMicroVM(t *testing.T) { NetworkService: ns, ImageService: is, FileSystem: fs, + Clock: frozenTime, } tc.expect(rm.EXPECT(), em.EXPECT(), im.EXPECT(), pm.EXPECT()) ctx := context.Background() app := application.New(&application.Config{}, ports) - app.SetClock(frozenTime) _, err := app.UpdateMicroVM(ctx, tc.specToUpdate) if tc.expectError { @@ -354,13 +354,13 @@ func TestApp_DeleteMicroVM(t *testing.T) { NetworkService: ns, ImageService: is, FileSystem: fs, + Clock: frozenTime, } tc.expect(rm.EXPECT(), em.EXPECT(), im.EXPECT(), pm.EXPECT()) ctx := context.Background() app := application.New(&application.Config{}, ports) - app.SetClock(frozenTime) err := app.DeleteMicroVM(ctx, tc.toDeleteID, tc.toDeleteNS) if tc.expectError { diff --git a/core/application/commands.go b/core/application/commands.go index c1c4e1ec5..d93f2d5ac 100644 --- a/core/application/commands.go +++ b/core/application/commands.go @@ -47,7 +47,7 @@ func (a *app) CreateMicroVM(ctx context.Context, mvm *models.MicroVM) (*models.M // TODO: validate the spec // Set the timestamp when the VMspec was created. - mvm.Spec.CreatedAt = a.clock().UnixMilli() + mvm.Spec.CreatedAt = a.ports.Clock().UnixMilli() createdMVM, err := a.ports.Repo.Save(ctx, mvm) if err != nil { @@ -90,7 +90,7 @@ func (a *app) UpdateMicroVM(ctx context.Context, mvm *models.MicroVM) (*models.M // TODO: check if update is valid (i.e. compare existing to requested update) // Set the timestamp when the VMspec was updated. - mvm.Spec.UpdatedAt = a.clock().UnixMilli() + mvm.Spec.UpdatedAt = a.ports.Clock().UnixMilli() updatedMVM, err := a.ports.Repo.Save(ctx, mvm) if err != nil { @@ -123,7 +123,8 @@ func (a *app) DeleteMicroVM(ctx context.Context, id, namespace string) error { return fmt.Errorf("microvm %s/%s doesn't exist, skipping delete", id, namespace) } - foundMvm.Spec.DeletedAt = a.clock().UnixMilli() + // Set the timestamp when the VMspec was deleted. + foundMvm.Spec.DeletedAt = a.ports.Clock().UnixMilli() _, err = a.ports.Repo.Save(ctx, foundMvm) if err != nil { diff --git a/core/ports/collection.go b/core/ports/collection.go index 6d7e4b655..28e5f1e68 100644 --- a/core/ports/collection.go +++ b/core/ports/collection.go @@ -1,6 +1,10 @@ package ports -import "github.com/spf13/afero" +import ( + "time" + + "github.com/spf13/afero" +) type Collection struct { Repo MicroVMRepository @@ -10,4 +14,5 @@ type Collection struct { NetworkService NetworkService ImageService ImageService FileSystem afero.Fs + Clock func() time.Time } diff --git a/internal/inject/wire.go b/internal/inject/wire.go index 6275733db..00a064752 100644 --- a/internal/inject/wire.go +++ b/internal/inject/wire.go @@ -5,6 +5,7 @@ package inject import ( "fmt" + "time" "github.com/google/wire" "github.com/spf13/afero" @@ -93,6 +94,7 @@ func appPorts(repo ports.MicroVMRepository, prov ports.MicroVMService, es ports. NetworkService: ns, ImageService: ims, FileSystem: fs, + Clock: time.Now, } } diff --git a/internal/inject/wire_gen.go b/internal/inject/wire_gen.go index bfbbb3248..8c143a5d4 100644 --- a/internal/inject/wire_gen.go +++ b/internal/inject/wire_gen.go @@ -8,7 +8,6 @@ package inject import ( "fmt" - "github.com/spf13/afero" "github.com/weaveworks/reignite/core/application" "github.com/weaveworks/reignite/core/ports" @@ -19,6 +18,7 @@ import ( "github.com/weaveworks/reignite/infrastructure/network" "github.com/weaveworks/reignite/infrastructure/ulid" "github.com/weaveworks/reignite/internal/config" + "time" ) // Injectors from wire.go: @@ -108,6 +108,7 @@ func appPorts(repo ports.MicroVMRepository, prov ports.MicroVMService, es ports. NetworkService: ns, ImageService: ims, FileSystem: fs, + Clock: time.Now, } }