From a5269c3e425a899f08cb2326f12301516c690c32 Mon Sep 17 00:00:00 2001 From: Julien Leicher Date: Thu, 10 Oct 2024 08:18:17 +0200 Subject: [PATCH] tests: add deployment fixture tests --- internal/deployment/fixture/app_test.go | 50 ++++++++++++ internal/deployment/fixture/database_test.go | 53 +++++++++++++ .../deployment/fixture/deployment_test.go | 63 +++++++++++++++ internal/deployment/fixture/registry_test.go | 40 ++++++++++ internal/deployment/fixture/target_test.go | 76 +++++++++++++++++++ 5 files changed, 282 insertions(+) create mode 100644 internal/deployment/fixture/app_test.go create mode 100644 internal/deployment/fixture/database_test.go create mode 100644 internal/deployment/fixture/deployment_test.go create mode 100644 internal/deployment/fixture/registry_test.go create mode 100644 internal/deployment/fixture/target_test.go diff --git a/internal/deployment/fixture/app_test.go b/internal/deployment/fixture/app_test.go new file mode 100644 index 0000000..399b82b --- /dev/null +++ b/internal/deployment/fixture/app_test.go @@ -0,0 +1,50 @@ +package fixture_test + +import ( + "testing" + + "github.com/YuukanOO/seelf/internal/deployment/domain" + "github.com/YuukanOO/seelf/internal/deployment/fixture" + "github.com/YuukanOO/seelf/pkg/assert" +) + +func Test_App(t *testing.T) { + t.Run("should be able to build a random app", func(t *testing.T) { + app := fixture.App() + + assert.NotZero(t, app.ID()) + }) + + t.Run("should be able to build an app with a given name", func(t *testing.T) { + app := fixture.App(fixture.WithAppName("foo")) + + created := assert.EventIs[domain.AppCreated](t, &app, 0) + assert.Equal(t, "foo", created.Name) + }) + + t.Run("should be able to build an app created by a specific user id", func(t *testing.T) { + app := fixture.App(fixture.WithAppCreatedBy("uid")) + + created := assert.EventIs[domain.AppCreated](t, &app, 0) + assert.Equal(t, "uid", created.Created.By()) + }) + + t.Run("should be able to build an app with given production and staging configuration", func(t *testing.T) { + production := domain.NewEnvironmentConfig("production_id") + staging := domain.NewEnvironmentConfig("staging_id") + app := fixture.App(fixture.WithEnvironmentConfig(production, staging)) + + created := assert.EventIs[domain.AppCreated](t, &app, 0) + assert.DeepEqual(t, production, created.Production) + assert.DeepEqual(t, staging, created.Staging) + }) + + t.Run("should be able to build an app with given production configuration", func(t *testing.T) { + config := domain.NewEnvironmentConfig("production_id") + app := fixture.App(fixture.WithProductionConfig(config)) + + created := assert.EventIs[domain.AppCreated](t, &app, 0) + assert.DeepEqual(t, config, created.Production) + assert.NotEqual(t, config.Target(), created.Staging.Target()) + }) +} diff --git a/internal/deployment/fixture/database_test.go b/internal/deployment/fixture/database_test.go new file mode 100644 index 0000000..7879c3e --- /dev/null +++ b/internal/deployment/fixture/database_test.go @@ -0,0 +1,53 @@ +package fixture_test + +import ( + "testing" + + auth "github.com/YuukanOO/seelf/internal/auth/domain" + authfixture "github.com/YuukanOO/seelf/internal/auth/fixture" + "github.com/YuukanOO/seelf/internal/deployment/domain" + "github.com/YuukanOO/seelf/internal/deployment/fixture" + "github.com/YuukanOO/seelf/pkg/assert" +) + +func Test_Database(t *testing.T) { + t.Run("should be able to prepare a database without seeding it", func(t *testing.T) { + ctx := fixture.PrepareDatabase(t) + + assert.NotNil(t, ctx) + assert.NotNil(t, ctx.Config) + assert.NotNil(t, ctx.AppsStore) + assert.NotNil(t, ctx.TargetsStore) + assert.NotNil(t, ctx.AppsStore) + assert.NotNil(t, ctx.DeploymentsStore) + assert.NotNil(t, ctx.RegistriesStore) + assert.NotNil(t, ctx.Dispatcher) + assert.HasLength(t, 0, ctx.Dispatcher.Signals()) + assert.HasLength(t, 0, ctx.Dispatcher.Requests()) + }) + + t.Run("should seed correctly and attach the first user id to the created context", func(t *testing.T) { + user := authfixture.User() + target := fixture.Target(fixture.WithTargetCreatedBy(user.ID())) + config := domain.NewEnvironmentConfig(target.ID()) + app := fixture.App( + fixture.WithEnvironmentConfig(config, config), + fixture.WithAppCreatedBy(user.ID()), + ) + registry := fixture.Registry(fixture.WithRegistryCreatedBy(user.ID())) + deployment := fixture.Deployment( + fixture.FromApp(app), + fixture.WithDeploymentRequestedBy(user.ID()), + ) + + ctx := fixture.PrepareDatabase(t, + fixture.WithUsers(&user), + fixture.WithTargets(&target), + fixture.WithApps(&app), + fixture.WithRegistries(®istry), + fixture.WithDeployments(&deployment), + ) + + assert.Equal(t, user.ID(), auth.CurrentUser(ctx.Context).Get("")) + }) +} diff --git a/internal/deployment/fixture/deployment_test.go b/internal/deployment/fixture/deployment_test.go new file mode 100644 index 0000000..fa0ef58 --- /dev/null +++ b/internal/deployment/fixture/deployment_test.go @@ -0,0 +1,63 @@ +package fixture_test + +import ( + "testing" + + "github.com/YuukanOO/seelf/internal/deployment/domain" + "github.com/YuukanOO/seelf/internal/deployment/fixture" + "github.com/YuukanOO/seelf/pkg/assert" +) + +func Test_Deployment(t *testing.T) { + t.Run("should be able to create a new deployment", func(t *testing.T) { + deployment := fixture.Deployment() + + assert.NotZero(t, deployment.ID()) + assert.Equal(t, domain.Production, deployment.Config().Environment()) + }) + + t.Run("should be able to create a new deployment from a given app", func(t *testing.T) { + app := fixture.App() + deployment := fixture.Deployment(fixture.FromApp(app)) + + created := assert.EventIs[domain.DeploymentCreated](t, &deployment, 0) + assert.Equal(t, app.ID(), created.ID.AppID()) + }) + + t.Run("should be able to create a new deployment requested by a given user id", func(t *testing.T) { + deployment := fixture.Deployment(fixture.WithDeploymentRequestedBy("uid")) + + created := assert.EventIs[domain.DeploymentCreated](t, &deployment, 0) + assert.Equal(t, "uid", created.Requested.By()) + }) + + t.Run("should be able to create a new deployment with a given source data", func(t *testing.T) { + source := fixture.SourceData() + deployment := fixture.Deployment(fixture.WithSourceData(source)) + + created := assert.EventIs[domain.DeploymentCreated](t, &deployment, 0) + assert.Equal(t, source, created.Source) + }) + + t.Run("should be able to create a new deployment with a given environment", func(t *testing.T) { + deployment := fixture.Deployment(fixture.ForEnvironment(domain.Staging)) + + created := assert.EventIs[domain.DeploymentCreated](t, &deployment, 0) + assert.Equal(t, domain.Staging, created.Config.Environment()) + }) +} + +func Test_SourceData(t *testing.T) { + t.Run("should be able to create a source data", func(t *testing.T) { + source := fixture.SourceData() + + assert.Equal(t, "test", source.Kind()) + assert.False(t, source.NeedVersionControl()) + }) + + t.Run("should be able to create a source data with version control needed", func(t *testing.T) { + source := fixture.SourceData(fixture.WithVersionControlNeeded()) + + assert.True(t, source.NeedVersionControl()) + }) +} diff --git a/internal/deployment/fixture/registry_test.go b/internal/deployment/fixture/registry_test.go new file mode 100644 index 0000000..2bf497c --- /dev/null +++ b/internal/deployment/fixture/registry_test.go @@ -0,0 +1,40 @@ +package fixture_test + +import ( + "testing" + + "github.com/YuukanOO/seelf/internal/deployment/domain" + "github.com/YuukanOO/seelf/internal/deployment/fixture" + "github.com/YuukanOO/seelf/pkg/assert" + "github.com/YuukanOO/seelf/pkg/must" +) + +func Test_Registry(t *testing.T) { + t.Run("should be able to create a random registry", func(t *testing.T) { + registry := fixture.Registry() + + assert.NotZero(t, registry.ID()) + }) + + t.Run("should be able to create a registry with a given name", func(t *testing.T) { + registry := fixture.Registry(fixture.WithRegistryName("my-registry")) + + created := assert.EventIs[domain.RegistryCreated](t, ®istry, 0) + assert.Equal(t, "my-registry", created.Name) + }) + + t.Run("should be able to create a registry created by a given user id", func(t *testing.T) { + registry := fixture.Registry(fixture.WithRegistryCreatedBy("uid")) + + created := assert.EventIs[domain.RegistryCreated](t, ®istry, 0) + assert.Equal(t, "uid", created.Created.By()) + }) + + t.Run("should be able to create a registry with a given url", func(t *testing.T) { + url := must.Panic(domain.UrlFrom("https://my-registry.com")) + registry := fixture.Registry(fixture.WithUrl(url)) + + created := assert.EventIs[domain.RegistryCreated](t, ®istry, 0) + assert.Equal(t, url, created.Url) + }) +} diff --git a/internal/deployment/fixture/target_test.go b/internal/deployment/fixture/target_test.go new file mode 100644 index 0000000..32ddba7 --- /dev/null +++ b/internal/deployment/fixture/target_test.go @@ -0,0 +1,76 @@ +package fixture_test + +import ( + "testing" + + "github.com/YuukanOO/seelf/internal/deployment/domain" + "github.com/YuukanOO/seelf/internal/deployment/fixture" + "github.com/YuukanOO/seelf/pkg/assert" +) + +func Test_Target(t *testing.T) { + t.Run("should be able to create a target", func(t *testing.T) { + target := fixture.Target() + + assert.NotZero(t, target.ID()) + }) + + t.Run("should be able to create a target with a given name", func(t *testing.T) { + target := fixture.Target(fixture.WithTargetName("name")) + + created := assert.EventIs[domain.TargetCreated](t, &target, 0) + assert.Equal(t, "name", created.Name) + }) + + t.Run("should be able to create a target with a given user id", func(t *testing.T) { + target := fixture.Target(fixture.WithTargetCreatedBy("id")) + + created := assert.EventIs[domain.TargetCreated](t, &target, 0) + assert.Equal(t, "id", created.Created.By()) + }) + + t.Run("should be able to create a target with a given provider config", func(t *testing.T) { + config := fixture.ProviderConfig() + target := fixture.Target(fixture.WithProviderConfig(config)) + + created := assert.EventIs[domain.TargetCreated](t, &target, 0) + assert.DeepEqual(t, config, created.Provider) + }) +} + +func Test_ProviderConfig(t *testing.T) { + t.Run("should be able to create a provider config", func(t *testing.T) { + config := fixture.ProviderConfig() + + assert.NotZero(t, config.Fingerprint()) + assert.NotZero(t, config.Kind()) + }) + + t.Run("should be able to create a provider config with a given fingerprint", func(t *testing.T) { + config := fixture.ProviderConfig(fixture.WithFingerprint("fingerprint")) + + assert.Equal(t, "fingerprint", config.Fingerprint()) + }) + + t.Run("should be able to create a provider config with a given kind", func(t *testing.T) { + config := fixture.ProviderConfig(fixture.WithKind("kind")) + + assert.Equal(t, "kind", config.Kind()) + }) + + t.Run("should be able to create a provider config with a given data", func(t *testing.T) { + one := fixture.ProviderConfig( + fixture.WithKind("kind"), + fixture.WithFingerprint("fingerprint"), + fixture.WithData("data")) + two := fixture.ProviderConfig( + fixture.WithKind("kind"), + fixture.WithFingerprint("fingerprint"), + fixture.WithData("data")) + three := fixture.ProviderConfig() + + assert.True(t, one.Equals(two)) + assert.False(t, one.Equals(three)) + assert.False(t, two.Equals(three)) + }) +}