Skip to content

Commit

Permalink
Merge pull request #3116 from schristoff/schristoff_linter_test
Browse files Browse the repository at this point in the history
(chore): Appease GolangLintCI in steps
  • Loading branch information
sgettys authored May 16, 2024
2 parents 2bb8be0 + 30dd4c8 commit cb4db63
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 93 deletions.
12 changes: 7 additions & 5 deletions mage/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ const (
// Build the website in preparation for deploying to the production website on Netlify.
// Uses symlinks so it won't work on Windows. Don't run locally, use DocsPreview instead.
func Docs() {
// Remove the preview container because otherwise it holds a file open and we can't delete the volume mount created at docs/content/operator
mg.SerialDeps(removePreviewContainer, linkOperatorDocs)
mg.SerialDeps(linkOperatorDocs)

cmd := must.Command("hugo", "--source", "docs/")
baseURL := os.Getenv("BASEURL")
Expand All @@ -41,7 +40,8 @@ func Docs() {
}

func removePreviewContainer() {
docker.RemoveContainer(PreviewContainer)
err := docker.RemoveContainer(PreviewContainer)
mgx.Must(err)
}

// Preview the website locally using a Docker container.
Expand All @@ -59,12 +59,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")
Expand All @@ -74,7 +75,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.
Expand Down
12 changes: 8 additions & 4 deletions pkg/build/dockerfile-generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +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)
Expand Down Expand Up @@ -94,7 +95,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)
Expand Down Expand Up @@ -123,7 +125,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)
Expand Down Expand Up @@ -225,7 +228,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)
Expand Down
11 changes: 6 additions & 5 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func TestStoreManifest(t *testing.T) {

func TestCache_StoreBundle_Overwrite(t *testing.T) {
t.Parallel()

var err error
cfg := config.NewTestConfig(t)
home, _ := cfg.Config.GetHomeDir()
cacheDir := filepath.Join(home, "cache")
Expand All @@ -266,12 +266,13 @@ 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())
Expand Down
2 changes: 1 addition & 1 deletion pkg/cnab/config-adapter/stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ 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
Expand Down
5 changes: 3 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ 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))
} else if hardPath != porterPath {
return "", log.Error(fmt.Errorf("WARNING could not resolve %s for symbolic links: %w", porterPath, err))
}
if hardPath != porterPath {
log.Debugf("Resolved porter binary from %s to %s", porterPath, hardPath)
porterPath = hardPath
}
Expand Down
26 changes: 20 additions & 6 deletions pkg/config/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"context"
"log"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -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
Expand Down
30 changes: 23 additions & 7 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"bytes"
"context"
"errors"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -36,13 +37,28 @@ func BindViperToEnvironmentVariables(v *viper.Viper) {

// 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:
Expand Down
8 changes: 6 additions & 2 deletions pkg/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ 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 {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/exec/builder/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ 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()))
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()))
return err
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/exec/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 := `{
Expand Down
10 changes: 7 additions & 3 deletions pkg/pkgmgmt/client/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/pkgmgmt/client/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()
}

Expand Down
Loading

0 comments on commit cb4db63

Please sign in to comment.