From 7d7468330e3fe356bf0d7f111a38000f7a3ba887 Mon Sep 17 00:00:00 2001 From: schristoff <28318173+schristoff@users.noreply.github.com> Date: Tue, 30 Apr 2024 11:28:06 -0600 Subject: [PATCH] (chore): Resolve lint errors Signed-off-by: schristoff <28318173+schristoff@users.noreply.github.com> --- mage/docs/docs.go | 12 +- pkg/build/dockerfile-generator_test.go | 14 +- pkg/cache/cache_test.go | 8 +- pkg/cnab/config-adapter/stamp.go | 3 +- pkg/config/config.go | 2 +- pkg/config/helpers.go | 26 ++- pkg/config/loader.go | 34 ++-- pkg/editor/editor.go | 8 +- pkg/exec/builder/errors.go | 4 +- pkg/exec/version_test.go | 6 +- pkg/pkgmgmt/client/install.go | 10 +- pkg/pkgmgmt/client/runner.go | 4 +- pkg/pkgmgmt/feed/generate_test.go | 173 +++++++++++++++----- pkg/plugins/pluggable/connection.go | 2 +- pkg/portercontext/context_test.go | 6 +- pkg/portercontext/helpers.go | 4 +- pkg/runtime/runtime_manifest.go | 12 +- pkg/runtime/runtime_manifest_test.go | 3 +- pkg/storage/plugins/mongodb/mongodb.go | 4 +- pkg/storage/plugins/mongodb/mongodb_test.go | 8 +- pkg/test/helper.go | 3 +- pkg/test/logger.go | 4 +- workshop/porter-tf-aci/azure/app/main.go | 5 +- 23 files changed, 257 insertions(+), 98 deletions(-) diff --git a/mage/docs/docs.go b/mage/docs/docs.go index 8fd5d4c8a..24bc6cbbc 100644 --- a/mage/docs/docs.go +++ b/mage/docs/docs.go @@ -37,11 +37,13 @@ func Docs() { if baseURL != "" { cmd.Args("-b", baseURL) } - cmd.RunV() + err := cmd.RunV() + mgx.Must(err) } func removePreviewContainer() { - docker.RemoveContainer(PreviewContainer) + err := docker.RemoveContainer(PreviewContainer) + mgx.Must(err) } // Preview the website locally using a Docker container. @@ -59,12 +61,13 @@ func DocsPreview() { } setDockerUser := fmt.Sprintf("--user=%s:%s", currentUser.Uid, currentUser.Gid) pwd, _ := os.Getwd() - must.Run("docker", "run", "-d", "-v", pwd+":/src", + err = must.Run("docker", "run", "-d", "-v", pwd+":/src", "-v", operatorDocs+":/src/docs/content/operator", setDockerUser, "-p", "1313:1313", "--name", PreviewContainer, "-w", "/src/docs", "klakegg/hugo:0.78.1-ext-alpine", "server", "-D", "-F", "--noHTTPCache", "--watch", "--bind=0.0.0.0") + mgx.Must(err) for { output, _ := must.OutputS("docker", "logs", "porter-docs") @@ -74,7 +77,8 @@ func DocsPreview() { time.Sleep(time.Second) } - must.Run("open", "http://localhost:1313/docs/") + err = must.Run("open", "http://localhost:1313/docs/") + mgx.Must(err) } // Build a branch preview of the website. diff --git a/pkg/build/dockerfile-generator_test.go b/pkg/build/dockerfile-generator_test.go index 54cdbcf26..59c430a48 100644 --- a/pkg/build/dockerfile-generator_test.go +++ b/pkg/build/dockerfile-generator_test.go @@ -63,8 +63,8 @@ func TestPorter_buildCustomDockerfile(t *testing.T) { COPY mybin /cnab/app/ ` - c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template") - + err = c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template") + require.NoError(t, err) mp := mixin.NewTestMixinProvider() g := NewDockerfileGenerator(c.Config, m, tmpl, mp) gotlines, err := g.buildDockerfile(context.Background()) @@ -94,8 +94,8 @@ COPY mybin /cnab/app/ COPY mybin /cnab/app/ ` - c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template") - + err = c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template") + require.NoError(t, err) mp := mixin.NewTestMixinProvider() g := NewDockerfileGenerator(c.Config, m, tmpl, mp) gotlines, err := g.buildDockerfile(context.Background()) @@ -123,7 +123,8 @@ COPY mybin /cnab/app/ COPY mybin /cnab/app/ ` - c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template") + err = c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template") + require.NoError(t, err) mp := mixin.NewTestMixinProvider() g := NewDockerfileGenerator(c.Config, m, tmpl, mp) @@ -225,7 +226,8 @@ func TestPorter_appendBuildInstructionsIfMixinTokenIsNotPresent(t *testing.T) { ARG BUNDLE_DIR COPY mybin /cnab/app/ ` - c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template") + err = c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template") + require.NoError(t, err) mp := mixin.NewTestMixinProvider() g := NewDockerfileGenerator(c.Config, m, tmpl, mp) diff --git a/pkg/cache/cache_test.go b/pkg/cache/cache_test.go index d84e1f5d0..2cafb27b2 100644 --- a/pkg/cache/cache_test.go +++ b/pkg/cache/cache_test.go @@ -266,12 +266,14 @@ func TestCache_StoreBundle_Overwrite(t *testing.T) { cb := CachedBundle{BundleReference: cnab.BundleReference{Reference: kahn1dot01}} cb.SetCacheDir(cacheDir) cfg.FileSystem.Create(cb.BuildManifestPath()) - cfg.FileSystem.Create(cb.BuildRelocationFilePath()) + _, err := cfg.FileSystem.Create(cb.BuildRelocationFilePath()) + require.NoError(t, err) junkPath := filepath.Join(cb.cacheDir, "junk.txt") - cfg.FileSystem.Create(junkPath) + _, err = cfg.FileSystem.Create(junkPath) + require.NoError(t, err) // Refresh the cache - cb, err := c.StoreBundle(cb.BundleReference) + cb, err = c.StoreBundle(cb.BundleReference) require.NoError(t, err, "StoreBundle failed") exists, _ := cfg.FileSystem.Exists(cb.BuildBundlePath()) diff --git a/pkg/cnab/config-adapter/stamp.go b/pkg/cnab/config-adapter/stamp.go index 956256f46..07db9bcb7 100644 --- a/pkg/cnab/config-adapter/stamp.go +++ b/pkg/cnab/config-adapter/stamp.go @@ -135,7 +135,8 @@ func (c *ManifestConverter) GenerateStamp(ctx context.Context) (Stamp, error) { if err != nil { // The digest is only used to decide if we need to rebuild, it is not an error condition to not // have a digest. - log.Error(fmt.Errorf("WARNING: Could not digest the porter manifest file: %w", err)) + log.Warn(fmt.Sprint("WARNING: Could not digest the porter manifest file: %w", err)) + stamp.ManifestDigest = "unknown" } else { stamp.ManifestDigest = digest diff --git a/pkg/config/config.go b/pkg/config/config.go index 40c5810c6..bdf60bfae 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -242,7 +242,7 @@ func (c *Config) GetPorterPath(ctx context.Context) (string, error) { // We try to resolve back to the original location hardPath, err := evalSymlinks(porterPath) if err != nil { // if we have trouble resolving symlinks, skip trying to help people who used symlinks - log.Error(fmt.Errorf("WARNING could not resolve %s for symbolic links: %w", porterPath, err)) + return "", log.Error(fmt.Errorf("WARNING could not resolve %s for symbolic links: %w", porterPath, err)) } else if hardPath != porterPath { log.Debugf("Resolved porter binary from %s to %s", porterPath, hardPath) porterPath = hardPath diff --git a/pkg/config/helpers.go b/pkg/config/helpers.go index dafb1e664..988e0d50b 100644 --- a/pkg/config/helpers.go +++ b/pkg/config/helpers.go @@ -2,6 +2,7 @@ package config import ( "context" + "log" "os" "path/filepath" "strconv" @@ -42,14 +43,27 @@ func (c *TestConfig) SetupUnitTest() { c.SetHomeDir(home) // Fake out the porter home directory - c.FileSystem.Create(filepath.Join(home, "porter")) - c.FileSystem.Create(filepath.Join(home, "runtimes", "porter-runtime")) + var err error + if _, err = c.FileSystem.Create(filepath.Join(home, "porter")); err != nil { + log.Fatal(err) + } + if _, err = c.FileSystem.Create(filepath.Join(home, "runtimes", "porter-runtime")); err != nil { + log.Fatal(err) + } mixinsDir := filepath.Join(home, "mixins") - c.FileSystem.Create(filepath.Join(mixinsDir, "exec/exec")) - c.FileSystem.Create(filepath.Join(mixinsDir, "exec/runtimes/exec-runtime")) - c.FileSystem.Create(filepath.Join(mixinsDir, "testmixin/testmixin")) - c.FileSystem.Create(filepath.Join(mixinsDir, "testmixin/runtimes/testmixin-runtime")) + if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "exec/exec")); err != nil { + log.Fatal(err) + } + if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "exec/runtimes/exec-runtime")); err != nil { + log.Fatal(err) + } + if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "testmixin/testmixin")); err != nil { + log.Fatal(err) + } + if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "testmixin/runtimes/testmixin-runtime")); err != nil { + log.Fatal(err) + } } // SetupIntegrationTest initializes the filesystem with the supporting files in diff --git a/pkg/config/loader.go b/pkg/config/loader.go index 190bdc141..a513c8dbf 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -3,9 +3,9 @@ package config import ( "bytes" "context" + "errors" "fmt" "sort" - "strings" "get.porter.sh/porter/pkg/tracing" "github.com/osteele/liquid" @@ -30,19 +30,31 @@ func LoadFromEnvironment() DataStoreLoaderFunc { } func BindViperToEnvironmentVariables(v *viper.Viper) { - v.SetEnvPrefix("PORTER") - v.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_")) - v.AutomaticEnv() // Bind open telemetry environment variables // See https://github.com/open-telemetry/opentelemetry-go/tree/main/exporters/otlp/otlptrace - v.BindEnv("telemetry.endpoint", "OTEL_EXPORTER_OTLP_ENDPOINT", "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT") - v.BindEnv("telemetry.protocol", "OTEL_EXPORTER_OTLP_PROTOCOL", "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL") - v.BindEnv("telemetry.insecure", "OTEL_EXPORTER_OTLP_INSECURE", "OTEL_EXPORTER_OTLP_TRACES_INSECURE") - v.BindEnv("telemetry.certificate", "OTEL_EXPORTER_OTLP_CERTIFICATE", "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE") - v.BindEnv("telemetry.headers", "OTEL_EXPORTER_OTLP_HEADERS", "OTEL_EXPORTER_OTLP_TRACES_HEADERS") - v.BindEnv("telemetry.compression", "OTEL_EXPORTER_OTLP_COMPRESSION", "OTEL_EXPORTER_OTLP_TRACES_COMPRESSION") - v.BindEnv("telemetry.timeout", "OTEL_EXPORTER_OTLP_TIMEOUT", "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT") + var err error + if err = v.BindEnv("telemetry.endpoint", "OTEL_EXPORTER_OTLP_ENDPOINT", "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"); err != nil { + errors.Unwrap(err) + } + if err = v.BindEnv("telemetry.protocol", "OTEL_EXPORTER_OTLP_PROTOCOL", "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"); err != nil { + errors.Unwrap(err) + } + if err = v.BindEnv("telemetry.insecure", "OTEL_EXPORTER_OTLP_INSECURE", "OTEL_EXPORTER_OTLP_TRACES_INSECURE"); err != nil { + errors.Unwrap(err) + } + if err = v.BindEnv("telemetry.certificate", "OTEL_EXPORTER_OTLP_CERTIFICATE", "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE"); err != nil { + errors.Unwrap(err) + } + if err = v.BindEnv("telemetry.headers", "OTEL_EXPORTER_OTLP_HEADERS", "OTEL_EXPORTER_OTLP_TRACES_HEADERS"); err != nil { + errors.Unwrap(err) + } + if err = v.BindEnv("telemetry.compression", "OTEL_EXPORTER_OTLP_COMPRESSION", "OTEL_EXPORTER_OTLP_TRACES_COMPRESSION"); err != nil { + errors.Unwrap(err) + } + if err = v.BindEnv("telemetry.timeout", "OTEL_EXPORTER_OTLP_TIMEOUT", "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT"); err != nil { + errors.Unwrap(err) + } } // LoadFromFilesystem loads data with the following precedence: diff --git a/pkg/editor/editor.go b/pkg/editor/editor.go index b18d5c6a0..aca78f405 100644 --- a/pkg/editor/editor.go +++ b/pkg/editor/editor.go @@ -61,7 +61,13 @@ func (e *Editor) Run(ctx context.Context) ([]byte, error) { if err != nil { return nil, err } - defer e.FileSystem.Remove(tempFile.Name()) + + defer func() error { + if err = e.FileSystem.Remove(tempFile.Name()); err != nil { + return err + } + return nil + }() _, err = tempFile.Write(e.contents) if err != nil { diff --git a/pkg/exec/builder/errors.go b/pkg/exec/builder/errors.go index 68aff8154..4e008a2bf 100644 --- a/pkg/exec/builder/errors.go +++ b/pkg/exec/builder/errors.go @@ -74,7 +74,9 @@ func (h IgnoreErrorHandler) HandleError(ctx context.Context, err ExitError, stdo for _, allowMatch := range h.Output.Regex { expression, regexErr := regexp.Compile(allowMatch) if regexErr != nil { - span.Error(fmt.Errorf("Could not ignore failed command because the Regex specified by the mixin step definition (%q) is invalid:%s", allowMatch, regexErr.Error())) + if err := span.Error(fmt.Errorf("Could not ignore failed command because the Regex specified by the mixin step definition (%q) is invalid:%s", allowMatch, regexErr.Error())); err != nil { + return err + } return err } diff --git a/pkg/exec/version_test.go b/pkg/exec/version_test.go index 4456be08c..b4136ec25 100644 --- a/pkg/exec/version_test.go +++ b/pkg/exec/version_test.go @@ -19,7 +19,8 @@ func TestPrintVersion(t *testing.T) { opts := version.Options{} err := opts.Validate() require.NoError(t, err) - m.PrintVersion(opts) + err = m.PrintVersion(opts) + require.NoError(t, err) gotOutput := m.TestConfig.TestContext.GetOutput() wantOutput := "exec v1.2.3 (abc123) by Porter Authors" @@ -38,7 +39,8 @@ func TestPrintJsonVersion(t *testing.T) { opts.RawFormat = string(printer.FormatJson) err := opts.Validate() require.NoError(t, err) - m.PrintVersion(opts) + err = m.PrintVersion(opts) + require.NoError(t, err) gotOutput := m.TestConfig.TestContext.GetOutput() wantOutput := `{ diff --git a/pkg/pkgmgmt/client/install.go b/pkg/pkgmgmt/client/install.go index 94304c0b4..8d6e28b6d 100644 --- a/pkg/pkgmgmt/client/install.go +++ b/pkg/pkgmgmt/client/install.go @@ -200,14 +200,18 @@ func (fs *FileSystem) downloadFile(ctx context.Context, url url.URL, destPath st return log.Error(fmt.Errorf("unable to check if directory exists %s: %w", parentDir, err)) } - cleanup := func() {} + cleanup := func() error { return nil } if !parentDirExists { err = fs.FileSystem.MkdirAll(parentDir, pkg.FileModeDirectory) if err != nil { return log.Error(fmt.Errorf("unable to create parent directory %s: %w", parentDir, err)) } - cleanup = func() { - fs.FileSystem.RemoveAll(parentDir) // If we can't download the file, don't leave traces of it + cleanup = func() error { + // If we can't download the file, don't leave traces of it + if err = fs.FileSystem.RemoveAll(parentDir); err != nil { + return err + } + return nil } } diff --git a/pkg/pkgmgmt/client/runner.go b/pkg/pkgmgmt/client/runner.go index eded5939a..e56f372d7 100644 --- a/pkg/pkgmgmt/client/runner.go +++ b/pkg/pkgmgmt/client/runner.go @@ -84,7 +84,9 @@ func (r *Runner) Run(ctx context.Context, commandOpts pkgmgmt.CommandOptions) er } go func() { defer stdin.Close() - io.WriteString(stdin, commandOpts.Input) + if _, err := io.WriteString(stdin, commandOpts.Input); err != nil { + span.Error(err) + } }() } diff --git a/pkg/pkgmgmt/feed/generate_test.go b/pkg/pkgmgmt/feed/generate_test.go index f5208e202..4874cc28f 100644 --- a/pkg/pkgmgmt/feed/generate_test.go +++ b/pkg/pkgmgmt/feed/generate_test.go @@ -18,62 +18,140 @@ func TestGenerate(t *testing.T) { ctx := context.Background() tc := portercontext.NewTestContext(t) tc.AddTestFile("testdata/atom-template.xml", "template.xml") - - tc.FileSystem.Create("bin/v1.2.3/helm-darwin-amd64") - tc.FileSystem.Create("bin/v1.2.3/helm-darwin-arm64") - tc.FileSystem.Create("bin/v1.2.3/helm-linux-amd64") - tc.FileSystem.Create("bin/v1.2.3/helm-linux-arm64") - tc.FileSystem.Create("bin/v1.2.3/helm-windows-amd64.exe") - tc.FileSystem.Create("bin/v1.2.3/helm-windows-arm64.exe") + var err error + if _, err = tc.FileSystem.Create("bin/v1.2.3/helm-darwin-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/v1.2.3/helm-darwin-arm64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/v1.2.3/helm-linux-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/v1.2.3/helm-linux-arm64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/v1.2.3/helm-windows-amd64.exe"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/v1.2.3/helm-windows-arm64.exe"); err != nil { + t.Fatal(err) + } // Force the up3 timestamps to stay the same for each test run up3, _ := time.Parse("2006-Jan-02", "2013-Feb-03") - tc.FileSystem.Chtimes("bin/v1.2.3/helm-darwin-amd64", up3, up3) - tc.FileSystem.Chtimes("bin/v1.2.3/helm-darwin-arm64", up3, up3) - tc.FileSystem.Chtimes("bin/v1.2.3/helm-linux-amd64", up3, up3) - tc.FileSystem.Chtimes("bin/v1.2.3/helm-linux-arm64", up3, up3) - tc.FileSystem.Chtimes("bin/v1.2.3/helm-windows-amd64.exe", up3, up3) - tc.FileSystem.Chtimes("bin/v1.2.3/helm-windows-arm64.exe", up3, up3) + if err = tc.FileSystem.Chtimes("bin/v1.2.3/helm-darwin-amd64", up3, up3); err != nil { + t.Fatal(err) + } + if err = tc.FileSystem.Chtimes("bin/v1.2.3/helm-darwin-arm64", up3, up3); err != nil { + t.Fatal(err) + } + if err = tc.FileSystem.Chtimes("bin/v1.2.3/helm-linux-amd64", up3, up3); err != nil { + t.Fatal(err) + } + if err = tc.FileSystem.Chtimes("bin/v1.2.3/helm-linux-arm64", up3, up3); err != nil { + t.Fatal(err) + } + if err = tc.FileSystem.Chtimes("bin/v1.2.3/helm-windows-amd64.exe", up3, up3); err != nil { + t.Fatal(err) + } + if err = tc.FileSystem.Chtimes("bin/v1.2.3/helm-windows-arm64.exe", up3, up3); err != nil { + t.Fatal(err) + } - tc.FileSystem.Create("bin/v1.2.4/helm-darwin-amd64") - tc.FileSystem.Create("bin/v1.2.4/helm-linux-amd64") - tc.FileSystem.Create("bin/v1.2.4/helm-windows-amd64.exe") + if _, err = tc.FileSystem.Create("bin/v1.2.4/helm-darwin-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/v1.2.4/helm-linux-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/v1.2.4/helm-windows-amd64.exe"); err != nil { + t.Fatal(err) + } up4, _ := time.Parse("2006-Jan-02", "2013-Feb-04") - tc.FileSystem.Chtimes("bin/v1.2.4/helm-darwin-amd64", up4, up4) - tc.FileSystem.Chtimes("bin/v1.2.4/helm-linux-amd64", up4, up4) - tc.FileSystem.Chtimes("bin/v1.2.4/helm-windows-amd64.exe", up4, up4) + if err = tc.FileSystem.Chtimes("bin/v1.2.4/helm-darwin-amd64", up4, up4); err != nil { + t.Fatal(err) + } + if err = tc.FileSystem.Chtimes("bin/v1.2.4/helm-linux-amd64", up4, up4); err != nil { + t.Fatal(err) + } + if err = tc.FileSystem.Chtimes("bin/v1.2.4/helm-windows-amd64.exe", up4, up4); err != nil { + t.Fatal(err) + } - tc.FileSystem.Create("bin/v1.2.3/exec-darwin-amd64") - tc.FileSystem.Create("bin/v1.2.3/exec-linux-amd64") - tc.FileSystem.Create("bin/v1.2.3/exec-windows-amd64.exe") + if _, err = tc.FileSystem.Create("bin/v1.2.3/exec-darwin-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/v1.2.3/exec-linux-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/v1.2.3/exec-windows-amd64.exe"); err != nil { + t.Fatal(err) + } up2, _ := time.Parse("2006-Jan-02", "2013-Feb-02") - tc.FileSystem.Chtimes("bin/v1.2.3/exec-darwin-amd64", up2, up2) - tc.FileSystem.Chtimes("bin/v1.2.3/exec-linux-amd64", up2, up2) - tc.FileSystem.Chtimes("bin/v1.2.3/exec-windows-amd64.exe", up2, up2) + if err = tc.FileSystem.Chtimes("bin/v1.2.3/exec-darwin-amd64", up2, up2); err != nil { + t.Fatal(err) + } + if err = tc.FileSystem.Chtimes("bin/v1.2.3/exec-linux-amd64", up2, up2); err != nil { + t.Fatal(err) + } + if err = tc.FileSystem.Chtimes("bin/v1.2.3/exec-windows-amd64.exe", up2, up2); err != nil { + t.Fatal(err) + } - tc.FileSystem.Create("bin/canary/exec-darwin-amd64") - tc.FileSystem.Create("bin/canary/exec-linux-amd64") - tc.FileSystem.Create("bin/canary/exec-windows-amd64.exe") + if _, err = tc.FileSystem.Create("bin/canary/exec-darwin-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/canary/exec-linux-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/canary/exec-windows-amd64.exe"); err != nil { + t.Fatal(err) + } up10, _ := time.Parse("2006-Jan-02", "2013-Feb-10") - tc.FileSystem.Chtimes("bin/canary/exec-darwin-amd64", up10, up10) - tc.FileSystem.Chtimes("bin/canary/exec-linux-amd64", up10, up10) - tc.FileSystem.Chtimes("bin/canary/exec-windows-amd64.exe", up10, up10) + if err = tc.FileSystem.Chtimes("bin/canary/exec-darwin-amd64", up10, up10); err != nil { + t.Fatal(err) + } + if err = tc.FileSystem.Chtimes("bin/canary/exec-linux-amd64", up10, up10); err != nil { + t.Fatal(err) + } + if err = tc.FileSystem.Chtimes("bin/canary/exec-windows-amd64.exe", up10, up10); err != nil { + t.Fatal(err) + } // Create extraneous release directories that should be ignored - tc.FileSystem.Create("bin/v0.34.0-1-gda/helm-darwin-amd64") - tc.FileSystem.Create("bin/v0.34.0-2-g1234567/helm-linux-amd64") - tc.FileSystem.Create("bin/v0.34.0-3-g12345/helm-windows-amd64.exe") + if _, err = tc.FileSystem.Create("bin/v0.34.0-1-gda/helm-darwin-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/v0.34.0-2-g1234567/helm-linux-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/v0.34.0-3-g12345/helm-windows-amd64.exe"); err != nil { + t.Fatal(err) + } - tc.FileSystem.Create("bin/latest/helm-darwin-amd64") - tc.FileSystem.Create("bin/latest/helm-linux-amd64") - tc.FileSystem.Create("bin/latest/helm-windows-amd64.exe") + if _, err = tc.FileSystem.Create("bin/latest/helm-darwin-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/latest/helm-linux-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/latest/helm-windows-amd64.exe"); err != nil { + t.Fatal(err) + } - tc.FileSystem.Create("bin/canary-v1/exec-darwin-amd64") - tc.FileSystem.Create("bin/canary-v1/exec-linux-amd64") - tc.FileSystem.Create("bin/canary-v1/exec-windows-amd64.exe") + if _, err = tc.FileSystem.Create("bin/canary-v1/exec-darwin-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/canary-v1/exec-linux-amd64"); err != nil { + t.Fatal(err) + } + if _, err = tc.FileSystem.Create("bin/canary-v1/exec-windows-amd64.exe"); err != nil { + t.Fatal(err) + } opts := GenerateOptions{ AtomFile: "atom.xml", @@ -81,7 +159,7 @@ func TestGenerate(t *testing.T) { TemplateFile: "template.xml", } f := NewMixinFeed(tc.Context) - err := f.Generate(ctx, opts) + err = f.Generate(ctx, opts) require.NoError(t, err) err = f.Save(opts) require.NoError(t, err) @@ -142,9 +220,16 @@ func TestGenerate_RegexMatch(t *testing.T) { porterCtx.AddTestFile("testdata/atom-template.xml", "template.xml") if tc.mixinName != "" { - porterCtx.FileSystem.Create(fmt.Sprintf("bin/v1.2.3/%s-darwin-amd64", tc.mixinName)) - porterCtx.FileSystem.Create(fmt.Sprintf("bin/v1.2.3/%s-linux-amd64", tc.mixinName)) - porterCtx.FileSystem.Create(fmt.Sprintf("bin/v1.2.3/%s-windows-amd64.exe", tc.mixinName)) + var err error + if _, err = porterCtx.FileSystem.Create(fmt.Sprintf("bin/v1.2.3/%s-darwin-amd64", tc.mixinName)); err != nil { + t.Fatal(err) + } + if _, err = porterCtx.FileSystem.Create(fmt.Sprintf("bin/v1.2.3/%s-linux-amd64", tc.mixinName)); err != nil { + t.Fatal(err) + } + if _, err = porterCtx.FileSystem.Create(fmt.Sprintf("bin/v1.2.3/%s-windows-amd64.exe", tc.mixinName)); err != nil { + t.Fatal(err) + } } opts := GenerateOptions{ diff --git a/pkg/plugins/pluggable/connection.go b/pkg/plugins/pluggable/connection.go index 153803007..7ddaec5c2 100644 --- a/pkg/plugins/pluggable/connection.go +++ b/pkg/plugins/pluggable/connection.go @@ -161,7 +161,7 @@ func (c *PluginConnection) Start(ctx context.Context, pluginCfg io.Reader) error pluginErr = ": plugin stderr was " + pluginErr } err = fmt.Errorf("could not connect to the %s plugin%s: %w", c.key, pluginErr, err) - span.Error(err) // Emit the error before trying to close the connection + err = span.Error(err) // Emit the error before trying to close the connection c.Close(ctx) return err } diff --git a/pkg/portercontext/context_test.go b/pkg/portercontext/context_test.go index f7b4e8516..ccf3f007c 100644 --- a/pkg/portercontext/context_test.go +++ b/pkg/portercontext/context_test.go @@ -40,15 +40,17 @@ func TestContext_LogToFile(t *testing.T) { LogDirectory: "/.porter/logs", }) c.timestampLogs = false // turn off timestamps so we can compare more easily - logfile := c.logFile.Name() _, log := c.StartRootSpan(context.Background(), t.Name()) log.Info("a thing happened") log.Warn("a weird thing happened") - log.Error(errors.New("a bad thing happened")) + err := log.Error(errors.New("a bad thing happened")) + // an error that errors + t.Fatal(err) log.EndSpan() c.Close() // Check that the logs are in json + logfile := c.logFile.Name() logContents, err := c.FileSystem.ReadFile(logfile) require.NoError(t, err) c.CompareGoldenFile("testdata/expected-logs.txt", string(logContents)) diff --git a/pkg/portercontext/helpers.go b/pkg/portercontext/helpers.go index 3e8e14097..d00db2dca 100644 --- a/pkg/portercontext/helpers.go +++ b/pkg/portercontext/helpers.go @@ -129,7 +129,9 @@ func (c *TestContext) AddCleanupDir(dir string) { func (c *TestContext) Close() { for _, dir := range c.cleanupDirs { - c.FileSystem.RemoveAll(dir) + err := c.FileSystem.RemoveAll(dir) + c.T.Fatal((err)) + } } diff --git a/pkg/runtime/runtime_manifest.go b/pkg/runtime/runtime_manifest.go index a630023dc..4127988b4 100644 --- a/pkg/runtime/runtime_manifest.go +++ b/pkg/runtime/runtime_manifest.go @@ -495,7 +495,9 @@ func (m *RuntimeManifest) Initialize(ctx context.Context) error { // that's a cnab change somewhere probably // the problem is in injectParameters in cnab-go if string(bytes) == "null" { - m.config.FileSystem.Remove(param.Destination.Path) + if err := m.config.FileSystem.Remove(param.Destination.Path); err != nil { + return err + } continue } decoded, err := base64.StdEncoding.DecodeString(string(bytes)) @@ -542,7 +544,9 @@ func (m *RuntimeManifest) unpackStateBag(ctx context.Context) error { // the problem is in injectParameters in cnab-go if string(bytes) == "null" { m.debugf(log, "Bundle state file has null content") - m.config.FileSystem.Remove(statePath) + if err := m.config.FileSystem.Remove(statePath); err != nil { + return err + } return nil } // Unpack the state file and copy its contents to where the bundle expects them @@ -598,7 +602,9 @@ func (m *RuntimeManifest) unpackStateBag(ctx context.Context) error { continue } - unpackStateFile(tr, header) + if err = unpackStateFile(tr, header); err != nil { + return err + } } return nil diff --git a/pkg/runtime/runtime_manifest_test.go b/pkg/runtime/runtime_manifest_test.go index ca65ad9fb..c8d8c938d 100644 --- a/pkg/runtime/runtime_manifest_test.go +++ b/pkg/runtime/runtime_manifest_test.go @@ -119,7 +119,8 @@ state: } else { require.Contains(t, err.Error(), test.expErr.Error()) } - pCtx.FileSystem.Remove("/porter/state.tgz") + err = pCtx.FileSystem.Remove("/porter/state.tgz") + require.NoError(t, err) }) } } diff --git a/pkg/storage/plugins/mongodb/mongodb.go b/pkg/storage/plugins/mongodb/mongodb.go index 5f0700fbe..8e2433f53 100644 --- a/pkg/storage/plugins/mongodb/mongodb.go +++ b/pkg/storage/plugins/mongodb/mongodb.go @@ -88,7 +88,9 @@ func (s *Store) Close() error { cxt, cancel := context.WithTimeout(context.Background(), s.timeout) defer cancel() - s.client.Disconnect(cxt) + if err := s.client.Disconnect(cxt); err != nil { + return err + } s.client = nil } return nil diff --git a/pkg/storage/plugins/mongodb/mongodb_test.go b/pkg/storage/plugins/mongodb/mongodb_test.go index 38d57fb54..e9f583e93 100644 --- a/pkg/storage/plugins/mongodb/mongodb_test.go +++ b/pkg/storage/plugins/mongodb/mongodb_test.go @@ -14,14 +14,18 @@ func TestParseDatabase(t *testing.T) { tc := portercontext.NewTestContext(t) t.Run("db specified", func(t *testing.T) { mongo := NewStore(tc.Context, PluginConfig{URL: "mongodb://localhost:27017/test/"}) - mongo.Connect(ctx) + if err := mongo.Connect(ctx); err != nil { + t.Fatal(err) + } defer mongo.Close() assert.Equal(t, "test", mongo.database) }) t.Run("default db", func(t *testing.T) { mongo := NewStore(tc.Context, PluginConfig{URL: "mongodb://localhost:27017"}) - mongo.Connect(ctx) + if err := mongo.Connect(ctx); err != nil { + t.Fatal(err) + } defer mongo.Close() assert.Equal(t, "porter", mongo.database) }) diff --git a/pkg/test/helper.go b/pkg/test/helper.go index 947f4c301..10fa96e5e 100644 --- a/pkg/test/helper.go +++ b/pkg/test/helper.go @@ -70,7 +70,8 @@ func TestMainWithMockedCommandHandlers(m *testing.M) { // the new test output. func CompareGoldenFile(t *testing.T, goldenFile string, got string) { if os.Getenv("PORTER_UPDATE_TEST_FILES") == "true" { - os.MkdirAll(filepath.Dir(goldenFile), pkg.FileModeDirectory) + err := os.MkdirAll(filepath.Dir(goldenFile), pkg.FileModeDirectory) + require.NoError(t, err) t.Logf("Updated test file %s to match latest test output", goldenFile) require.NoError(t, os.WriteFile(goldenFile, []byte(got), pkg.FileModeWritable), "could not update golden file %s", goldenFile) } else { diff --git a/pkg/test/logger.go b/pkg/test/logger.go index 0908f4377..6aebfe2f5 100644 --- a/pkg/test/logger.go +++ b/pkg/test/logger.go @@ -1,6 +1,8 @@ package test -import "testing" +import ( + "testing" +) // Logger helps capture output in a test while still showing it in the console type Logger struct { diff --git a/workshop/porter-tf-aci/azure/app/main.go b/workshop/porter-tf-aci/azure/app/main.go index 6155ae490..fcb9aa052 100644 --- a/workshop/porter-tf-aci/azure/app/main.go +++ b/workshop/porter-tf-aci/azure/app/main.go @@ -13,7 +13,10 @@ func handle(w http.ResponseWriter, r *http.Request) { http.Error(w, "couldn't find MYSQL FQDN", http.StatusInternalServerError) } - w.Write([]byte(fmt.Sprintf("Hello, I'm a webserver that wants to connect to a MYSQL at %s", sqlFQDN))) + _, err := w.Write([]byte(fmt.Sprintf("Hello, I'm a webserver that wants to connect to a MYSQL at %s", sqlFQDN))) + if err != nil { + log.Fatal(err) + } } func main() {