Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return non-zero exit status on no valid objects #209

Merged
merged 3 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions e2etests/sanity_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
//go:build e2e
// +build e2e

package e2etests

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -49,3 +52,29 @@ func TestKubeLinterWithBuiltInChecksDoesntCrashOnHelmChartsRepo(t *testing.T) {
assert.Equal(t, 1, exitErr.ExitCode(), "unexpected exit code: %d; output from kube-linter: %v", exitErr.ExitCode(), outAsStr)
assert.True(t, expectedOutRegex.MatchString(outAsStr), "unexpected output: %s", outAsStr)
}

func TestKubeLinterExitsWithNonZeroCodeOnEmptyDir(t *testing.T) {
kubeLinterBin := os.Getenv(kubeLinterBinEnv)

tmpDir, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(tmpDir))
}()
kubeLinterOut, err := exec.Command(kubeLinterBin, "lint", tmpDir, "--fail-if-no-objects-found").CombinedOutput()

// error is expected
require.Error(t, err)
exitErr, ok := err.(*exec.ExitError)
require.True(t, ok)
outAsStr := string(kubeLinterOut)
assert.Equal(t, 1, exitErr.ExitCode(), "unexpected exit code: %d; output from kube-linter: %v", exitErr.ExitCode(), outAsStr)
msg := "no valid objects found"
assert.True(t, strings.Contains(outAsStr, fmt.Sprintf("Error: %s", msg)), "unexpected output, it should contain: %s", outAsStr)

// without the switch only warning is printed to stderr
kubeLinterOut, err = exec.Command(kubeLinterBin, "lint", tmpDir).CombinedOutput()
require.NoError(t, err)
outAsStr = string(kubeLinterOut)
assert.True(t, strings.Contains(outAsStr, fmt.Sprintf("Warning: %s", msg)), "unexpected output, it should contain: %s", outAsStr)
}
8 changes: 7 additions & 1 deletion pkg/command/lint/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (
// Command is the command for the lint command.
func Command() *cobra.Command {
var configPath string
var failIfNoObjects bool
var verbose bool
format := flagutil.NewEnumFlag("Output format", formatters.GetEnabledFormatters(), common.PlainFormat)

Expand Down Expand Up @@ -95,7 +96,11 @@ func Command() *cobra.Command {
}
}
if !atLeastOneObjectFound {
fmt.Fprintln(os.Stderr, "Warning: no valid objects found.")
msg := "no valid objects found"
if failIfNoObjects {
return errors.New(msg)
}
fmt.Fprintf(os.Stderr, "Warning: %s.\n", msg)
return nil
}
result, err := run.Run(lintCtxs, checkRegistry, enabledChecks)
Expand All @@ -120,6 +125,7 @@ func Command() *cobra.Command {
}

c.Flags().StringVar(&configPath, "config", "", "Path to config file")
c.Flags().BoolVarP(&failIfNoObjects, "fail-if-no-objects-found", "", false, "Return non-zero exit code if no valid objects are found or failed to parse")
c.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
c.Flags().Var(format, "format", format.Usage())

Expand Down