diff --git a/.github/workflows/go-lint.yaml b/.github/workflows/go-lint.yaml new file mode 100644 index 000000000..b924dffa8 --- /dev/null +++ b/.github/workflows/go-lint.yaml @@ -0,0 +1,49 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: lint +on: + pull_request: + branches: + - master + paths: + - ".github/workflows/go-lint.yaml" + - "helpers/foundation-deployer/**" +permissions: + contents: read + +concurrency: + group: '$${{ github.workflow }}-$${{ github.head_ref || github.ref }}' + cancel-in-progress: true + +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + folder: [helpers/foundation-deployer] + steps: + - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 + - uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 + with: + go-version-file: ${{ matrix.folder }}/go.mod + cache-dependency-path: ${{ matrix.folder }}/go.sum + - name: golangci-lint + uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3.7.0 + with: + version: latest + working-directory: ${{ matrix.folder }} + args: --timeout=5m diff --git a/helpers/foundation-deployer/go.mod b/helpers/foundation-deployer/go.mod index b3f4ec429..cd82def46 100644 --- a/helpers/foundation-deployer/go.mod +++ b/helpers/foundation-deployer/go.mod @@ -1,6 +1,6 @@ module github.com/terraform-google-modules/terraform-example-foundation/helpers/foundation-deployer -go 1.18 +go 1.20 require ( github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test v0.5.0 diff --git a/helpers/foundation-deployer/main.go b/helpers/foundation-deployer/main.go index 852f1a9bc..ed682cfad 100644 --- a/helpers/foundation-deployer/main.go +++ b/helpers/foundation-deployer/main.go @@ -151,7 +151,10 @@ func main() { } if cfg.resetStep != "" { - s.ResetStep(cfg.resetStep) + if err := s.ResetStep(cfg.resetStep); err != nil { + fmt.Printf("# Reset step failed. Error: %s\n", err.Error()) + os.Exit(3) + } return } diff --git a/helpers/foundation-deployer/msg/msg.go b/helpers/foundation-deployer/msg/msg.go index 39265fde5..c2972be2a 100644 --- a/helpers/foundation-deployer/msg/msg.go +++ b/helpers/foundation-deployer/msg/msg.go @@ -35,7 +35,7 @@ var ( bar = strings.Repeat("#", size) ) -func PressEnter(msg string) error { +func PressEnter(msg string) { reader := bufio.NewReader(os.Stdin) t := "# Press Enter to continue" if msg != "" { @@ -43,7 +43,10 @@ func PressEnter(msg string) error { } fmt.Print(t) _, err := reader.ReadString('\n') - return err + if err != nil { + fmt.Printf("# Failed to read string. Error: %s\n", err.Error()) + os.Exit(3) + } } func pad(msg string, size int) string { diff --git a/helpers/foundation-deployer/stages/destroy.go b/helpers/foundation-deployer/stages/destroy.go index 6446d7c33..f9de4880d 100644 --- a/helpers/foundation-deployer/stages/destroy.go +++ b/helpers/foundation-deployer/stages/destroy.go @@ -34,7 +34,9 @@ const ( func DestroyBootstrapStage(t testing.TB, s steps.Steps, c CommonConf) error { - forceBackendMigration(t, BootstrapRepo, "envs", "shared", c) + if err := forceBackendMigration(t, BootstrapRepo, "envs", "shared", c); err != nil { + return err + } stageConf := StageConf{ Stage: BootstrapStep, diff --git a/helpers/foundation-deployer/steps/steps_test.go b/helpers/foundation-deployer/steps/steps_test.go index 748470a29..d87a4a28a 100644 --- a/helpers/foundation-deployer/steps/steps_test.go +++ b/helpers/foundation-deployer/steps/steps_test.go @@ -31,30 +31,37 @@ func TestProcessSteps(t *testing.T) { // Loading a new file s, err := LoadSteps(filepath.Join(t.TempDir(), "new.json")) + assert.NoError(t, err) // CompleteStep assert.False(t, s.IsStepComplete("unit"), "check if 'unit' is 'COMPLETED' should be false") - s.CompleteStep("unit") + err = s.CompleteStep("unit") + assert.NoError(t, err) assert.True(t, s.IsStepComplete("unit"), "check if 'unit' is 'COMPLETED' should be true") // FailStep msg := "step failed" - s.FailStep("fail", msg) + err = s.FailStep("fail", msg) + assert.NoError(t, err) assert.False(t, s.IsStepComplete("fail"), "check if 'fail' is 'COMPLETED' should be false") assert.Equal(t, s.GetStepError("fail"), msg, "step should have failed") // DestroyStep assert.False(t, s.IsStepDestroyed("old"), "check if 'old' is 'DESTROYED' should be false") - s.DestroyStep("old") + err = s.DestroyStep("old") + assert.NoError(t, err) assert.True(t, s.IsStepDestroyed("old"), "check if 'old' is 'DESTROYED' should be true") // ResetStep - s.CompleteStep("reset") - s.CompleteStep("reset.one") + err = s.CompleteStep("reset") + assert.NoError(t, err) + err = s.CompleteStep("reset.one") + assert.NoError(t, err) assert.True(t, s.IsStepComplete("reset"), "check if 'reset is 'COMPLETED' should be true") assert.True(t, s.IsStepComplete("reset.one"), "check if 'reset.one' is 'COMPLETED' should be true") - s.ResetStep("reset.one") + err = s.ResetStep("reset.one") + assert.NoError(t, err) assert.False(t, s.IsStepComplete("reset.one"), "check if 'reset.one' is 'COMPLETED' should be false") assert.False(t, s.IsStepComplete("reset"), "check if 'reset' is 'COMPLETED' should be false") @@ -92,7 +99,8 @@ func TestProcessSteps(t *testing.T) { assert.NoError(t, err) assert.True(t, s.IsStepDestroyed("unit"), "check if 'unit' is 'DESTROYED' should be true") - s.CompleteStep("destroy") + err = s.CompleteStep("destroy") + assert.NoError(t, err) assert.False(t, s.IsStepDestroyed("destroy"), "check if 'destroy' is 'DESTROYED' should be false") err = s.RunDestroyStep("destroy", func() error { return fmt.Errorf(badStepMsg) @@ -101,7 +109,8 @@ func TestProcessSteps(t *testing.T) { assert.False(t, s.IsStepDestroyed("destroy"), "check if 'destroy' is 'DESTROYED' should be false") assert.Equal(t, s.GetStepError("destroy"), badStepMsg, "step 'destroy' should have failed") - s.DestroyStep("gone") + err = s.DestroyStep("gone") + assert.NoError(t, err) assert.True(t, s.IsStepDestroyed("gone"), "check if 'gone' is 'DESTROYED' should be true") err = s.RunDestroyStep("gone", func() error { return fmt.Errorf("will fail if executed") diff --git a/helpers/foundation-deployer/utils/git_test.go b/helpers/foundation-deployer/utils/git_test.go index 88cc8a0c9..c9441ab86 100644 --- a/helpers/foundation-deployer/utils/git_test.go +++ b/helpers/foundation-deployer/utils/git_test.go @@ -55,6 +55,7 @@ func TestGit(t *testing.T) { localBranch, err := local.GetCurrentBranch() assert.Equal(t, localBranch, "unit-test", "current branch should be 'unit-test'") + assert.NoError(t, err) err = os.WriteFile(filepath.Join(repo, "go.mod"), []byte("module example.com/test\n"), 0644) assert.NoError(t, err)