From 8ade2dd316cd9eaf28ed8b344e2b6d6fbc1b5116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Kj=C3=B6lhede?= Date: Fri, 14 Jul 2023 17:00:46 +0200 Subject: [PATCH] wip --- .mockery.yaml | 12 ++++ cmd/deploy/deploy.go | 2 +- cmd/install/install.go | 4 +- cmd/monitor/monitor.go | 2 +- go.mod | 4 ++ go.sum | 7 ++ internal/flycd/deployment_service_test.go | 70 +++++++++---------- internal/flycd/model/deploy_config.go | 2 +- internal/flycd/webhook_service.go | 2 +- main_test.go | 3 + scripts/bump_version.bash | 2 + scripts/publish_latest_img.bash | 2 + scripts/publish_release.bash | 2 + .../merge-cfg/regular/app1/app.yaml | 3 +- 14 files changed, 73 insertions(+), 44 deletions(-) create mode 100644 .mockery.yaml create mode 100644 main_test.go diff --git a/.mockery.yaml b/.mockery.yaml new file mode 100644 index 0000000..9448f23 --- /dev/null +++ b/.mockery.yaml @@ -0,0 +1,12 @@ +# see recommended basic mockery config: +# https://vektra.github.io/mockery/latest/configuration/#recommended-basic-config +with-expecter: True +all: True +filename: "{{.InterfaceName}}.go" +outpkg: mocks +dir: mocks +keeptree: False +packages: + github.com/gigurra/flycd/internal/fly_client: { } + github.com/gigurra/flycd/internal/flycd: { } + diff --git a/cmd/deploy/deploy.go b/cmd/deploy/deploy.go index 0e27ac8..54b45fd 100644 --- a/cmd/deploy/deploy.go +++ b/cmd/deploy/deploy.go @@ -32,7 +32,7 @@ func Cmd(deployService flycd.DeployService) *cobra.Command { fmt.Printf("Deploying from: %s\n", path) deployCfg := model. - NewDeployConfig(). + NewDefaultDeployConfig(). WithRetries(1). WithForce(*flags.force). WithAbortOnFirstError(*flags.abortEarly) diff --git a/cmd/install/install.go b/cmd/install/install.go index 092c0d2..92a9d81 100644 --- a/cmd/install/install.go +++ b/cmd/install/install.go @@ -110,7 +110,7 @@ func Cmd( if !appExists { - deployCfg := model.NewDeployConfig().WithRetries(0) + deployCfg := model.NewDefaultDeployConfig().WithRetries(0) fmt.Printf("Creating a dummy app '%s' to reserve the name\n", appName) _, err := deployService.DeployAppFromInlineConfig(ctx, deployCfg, model.AppConfig{ @@ -197,7 +197,7 @@ func Cmd( // Deploy it! fmt.Printf("Deploying flycd in monitoring mode to fly.io\n") deployCfg := model. - NewDeployConfig(). + NewDefaultDeployConfig(). WithForce(true). WithRetries(0) _, err = deployService.DeployAppFromInlineConfig(ctx, deployCfg, model.AppConfig{ diff --git a/cmd/monitor/monitor.go b/cmd/monitor/monitor.go index 8a7f226..bce8d4d 100644 --- a/cmd/monitor/monitor.go +++ b/cmd/monitor/monitor.go @@ -157,7 +157,7 @@ func Cmd( fmt.Printf("Syncing/Deploying all apps in %s\n", path) deployCfg := model. - NewDeployConfig(). + NewDefaultDeployConfig(). WithAbortOnFirstError(false) _, err := deployService.DeployAll(ctx, path, deployCfg) diff --git a/go.mod b/go.mod index e4dd003..3f154e9 100644 --- a/go.mod +++ b/go.mod @@ -15,12 +15,16 @@ require ( ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/labstack/gommon v0.4.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect golang.org/x/crypto v0.6.0 // indirect diff --git a/go.sum b/go.sum index 75a037e..854fc90 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,15 @@ github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRM github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= diff --git a/internal/flycd/deployment_service_test.go b/internal/flycd/deployment_service_test.go index 67acc38..4ac4205 100644 --- a/internal/flycd/deployment_service_test.go +++ b/internal/flycd/deployment_service_test.go @@ -2,44 +2,42 @@ package flycd import ( "context" - "github.com/gigurra/flycd/internal/fly_client" + "fmt" "github.com/gigurra/flycd/internal/flycd/model" - "github.com/gigurra/flycd/internal/flycd/util/util_work_dir" + "github.com/gigurra/flycd/mocks" + "github.com/stretchr/testify/mock" + "testing" ) -type fakeFlyClient struct { -} - -func (f fakeFlyClient) CreateOrgToken(_ context.Context, _ string) (string, error) { panic("not used") } - -func (f fakeFlyClient) ExistsSecret(ctx context.Context, cmd fly_client.ExistsSecretCmd) (bool, error) { - //TODO implement me - panic("implement me") -} +func TestDeployFromFolder(t *testing.T) { + ctx := context.Background() + flyClient := mocks.NewMockFlyClient(t) + deployService := NewDeployService(flyClient) + deployCfg := model. + NewDefaultDeployConfig(). + WithAbortOnFirstError(true). + WithRetries(0) + + fmt.Printf("flyClient: %+v\n", flyClient) + + flyClient. + EXPECT(). + ExistsApp(mock.Anything, mock.Anything). + Return(false, nil) + + flyClient. + EXPECT(). + CreateNewApp(mock.Anything, mock.Anything, mock.Anything, mock.Anything). + Return(nil) + + flyClient. + EXPECT(). + DeployExistingApp(mock.Anything, mock.Anything, mock.Anything, mock.Anything). + Return(nil) + + _, err := deployService.DeployAppFromFolder(ctx, "../../test/test-projects/merge-cfg/regular/app1", deployCfg) + if err != nil { + t.Fatalf("DeployAppFromFolder failed: %v", err) + } -func (f fakeFlyClient) StoreSecret(ctx context.Context, cmd fly_client.StoreSecretCmd) error { - //TODO implement me - panic("implement me") } - -func (f fakeFlyClient) ExistsApp(ctx context.Context, name string) (bool, error) { - //TODO implement me - panic("implement me") -} - -func (f fakeFlyClient) GetDeployedAppConfig(ctx context.Context, name string) (model.AppConfig, error) { - //TODO implement me - panic("implement me") -} - -func (f fakeFlyClient) CreateNewApp(ctx context.Context, cfg model.AppConfig, tempDir util_work_dir.WorkDir, twoStep bool) error { - //TODO implement me - panic("implement me") -} - -func (f fakeFlyClient) DeployExistingApp(ctx context.Context, cfg model.AppConfig, tempDir util_work_dir.WorkDir, deployCfg model.DeployConfig) error { - //TODO implement me - panic("implement me") -} - -var _ fly_client.FlyClient = &fakeFlyClient{} diff --git a/internal/flycd/model/deploy_config.go b/internal/flycd/model/deploy_config.go index 22e7021..56a2586 100644 --- a/internal/flycd/model/deploy_config.go +++ b/internal/flycd/model/deploy_config.go @@ -9,7 +9,7 @@ type DeployConfig struct { AbortOnFirstError bool } -func NewDeployConfig() DeployConfig { +func NewDefaultDeployConfig() DeployConfig { return DeployConfig{ Force: false, Retries: 2, diff --git a/internal/flycd/webhook_service.go b/internal/flycd/webhook_service.go index ec4d8fa..0647449 100644 --- a/internal/flycd/webhook_service.go +++ b/internal/flycd/webhook_service.go @@ -78,7 +78,7 @@ func (w WebHookServiceImpl) HandleGithubWebhook(payload github.PushWebhookPayloa } deployCfg := model. - NewDeployConfig(). + NewDefaultDeployConfig(). WithRetries(1). WithForce(false) _, err := w.deployService.DeployAppFromFolder(ctx, app.Path, deployCfg) diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..3ac8692 --- /dev/null +++ b/main_test.go @@ -0,0 +1,3 @@ +package main + +//go:generate mockery diff --git a/scripts/bump_version.bash b/scripts/bump_version.bash index 31088b7..cf3f351 100755 --- a/scripts/bump_version.bash +++ b/scripts/bump_version.bash @@ -4,6 +4,8 @@ set -e # check that the build works go clean ./... +rm -rf mocks +go generate ./... go build ./... go test ./... go clean ./... diff --git a/scripts/publish_latest_img.bash b/scripts/publish_latest_img.bash index 6f41a31..14f9e26 100755 --- a/scripts/publish_latest_img.bash +++ b/scripts/publish_latest_img.bash @@ -4,6 +4,8 @@ set -e # check that the build works go clean ./... +rm -rf mocks +go generate ./... go build ./... go test ./... go clean ./... diff --git a/scripts/publish_release.bash b/scripts/publish_release.bash index b905f63..568ff25 100755 --- a/scripts/publish_release.bash +++ b/scripts/publish_release.bash @@ -10,6 +10,8 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" # check that the build works go clean ./... +rm -rf mocks +go generate ./... go build ./... go test ./... go clean ./... diff --git a/test/test-projects/merge-cfg/regular/app1/app.yaml b/test/test-projects/merge-cfg/regular/app1/app.yaml index 146b882..5402751 100644 --- a/test/test-projects/merge-cfg/regular/app1/app.yaml +++ b/test/test-projects/merge-cfg/regular/app1/app.yaml @@ -2,5 +2,4 @@ app: app1 primary_region: arn org: personal source: - type: git - repo: git@github.com:TestUser/TestRepo.git + type: local