Skip to content

Commit

Permalink
fix: add extra tests for verison and check command default policy (#56)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Christopher Phillips <[email protected]>
  • Loading branch information
spiffcs authored Feb 13, 2024
1 parent fd37229 commit 4b6ea0e
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 24 deletions.
8 changes: 6 additions & 2 deletions cmd/grant/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ func New(id clio.Identification) clio.Application {

root := command.Root(app)

root.AddCommand(command.Check(app))
root.AddCommand(command.List(app))
root.AddCommand(
command.Check(app),
command.List(app),
clio.VersionCommand(id),
)

// root.AddCommand(command.Inspect(app))

return app
Expand Down
13 changes: 4 additions & 9 deletions cmd/grant/cli/option/check.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package option

import "github.com/anchore/clio"
import (
"github.com/anchore/clio"
)

type Check struct {
List `json:",inline" yaml:",inline" mapstructure:",squash"`
Expand All @@ -14,14 +16,7 @@ func DefaultCheck() Check {
List: DefaultList(),
Quiet: false,
OsiApproved: false,
Rules: []Rule{
{
Name: "deny-all",
Reason: "grant by default will deny all licenses",
Pattern: "*",
Severity: "high",
},
},
Rules: []Rule{defaultDenyAll},
}
}

Expand Down
7 changes: 7 additions & 0 deletions cmd/grant/cli/option/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ type Rule struct {
Mode string `json:"mode" yaml:"mode" mapstructure:"mode"`
Exceptions []string `json:"exceptions" yaml:"exceptions" mapstructure:"exceptions"`
}

var defaultDenyAll = Rule{
Name: "default-deny-all",
Reason: "grant by default will deny all licenses",
Mode: "deny",
Pattern: "*",
}
1 change: 1 addition & 0 deletions grant/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func NewCases(userInputs ...string) []Case {
log.Errorf("unable to create case handler: %+v", err)
return cases
}

defer ch.Close()
for _, userInput := range userInputs {
c, err := ch.determineRequestCase(userInput)
Expand Down
4 changes: 2 additions & 2 deletions grant/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Policy struct {
}

var DefaultDenyAll = Rule{
Name: "default-deny-all",
Glob: glob.MustCompile("*"),
Exceptions: []glob.Glob{},
Mode: Deny,
Expand Down Expand Up @@ -57,8 +58,7 @@ func (p Policy) IsDenied(license License, pkg *Package) (bool, *Rule) {
var toMatch string
if license.IsSPDX() {
toMatch = strings.ToLower(license.LicenseID)
}
if p.MatchNonSPDX && !license.IsSPDX() {
} else {
toMatch = strings.ToLower(license.Name)
}

Expand Down
41 changes: 41 additions & 0 deletions grant/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func Test_DefaultPolicy(t *testing.T) {
want: Policy{
Rules: []Rule{
{
Name: "default-deny-all",
Glob: glob.MustCompile("*"),
Exceptions: []glob.Glob{},
Mode: Deny,
Expand Down Expand Up @@ -73,3 +74,43 @@ func Test_NewPolicy(t *testing.T) {
})
}
}

func Test_Policy_DenyAll(t *testing.T) {
tests := []struct {
name string
p Policy
want struct {
denied bool
rule *Rule
}
}{
{
name: "Policy Default Deny All denies all licenses",
p: DefaultPolicy(),
want: struct {
denied bool
rule *Rule
}{
denied: true,
rule: &Rule{
Name: "default-deny-all",
Glob: glob.MustCompile("*"),
Exceptions: []glob.Glob{},
Mode: Deny,
Reason: "grant by default will deny all licenses",
},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
denied, rule := tc.p.IsDenied(License{LicenseID: "MIT", SPDXExpression: "MIT"}, nil)
if denied != tc.want.denied {
t.Errorf("Expected %t, got %t", tc.want.denied, denied)
}
if diff := cmp.Diff(tc.want.rule, rule); diff != "" {
t.Errorf("IsDenied() mismatch (-want +got):\n%s", diff)
}
})
}
}
38 changes: 38 additions & 0 deletions test/cli/check_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
package cli

import (
"os/exec"
"strings"
"testing"
)

func Test_CheckCmd(t *testing.T) {
tests := []struct {
name string
args []string
expectedInOutput []string
}{
{
name: "check command will deny all on empty config",
args: []string{"-c", emptyConfigPath, "check", "dir:../../."},
expectedInOutput: []string{
"check failed",
"license matches for rule: default-deny-all; matched with pattern *",
"Apache-2.0",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command(grantTmpPath, tt.args...)
output, err := cmd.CombinedOutput()
if err != nil && !strings.Contains(err.Error(), "exit status 1") {
t.Fatalf("cmd.CombinedOutput() failed with %s\n %s", err, string(output))
}
for _, expected := range tt.expectedInOutput {
if !strings.Contains(string(output), expected) {
t.Errorf("expected %s to be in output, but it wasn't; output: %s", expected, string(output))
}
}
})
}
}
51 changes: 51 additions & 0 deletions test/cli/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cli

import (
"log"
"os"
"os/exec"
"testing"
)

const (
grantTmpPath = "../../.tmp/grant"
emptyConfigPath = "../../.tmp/grant_empty.yaml"
)

func buildBinary() (string, error) {
buildCmd := exec.Command("go", "build", "-o", grantTmpPath, "../../cmd/grant/main.go") // Adjust the last argument to your package path if necessary
err := buildCmd.Run()
return grantTmpPath, err
}

func generateEmptyConfig() (string, error) {
emptyConfigCmd := exec.Command("touch", emptyConfigPath)
err := emptyConfigCmd.Run()
return emptyConfigPath, err
}

// setup function that you want to run before any tests
func setup(m *testing.M) {
_, err := buildBinary()
if err != nil {
log.Fatalf("Failed to build binary: %v", err)
}
_, err = generateEmptyConfig()
if err != nil {
log.Fatalf("Failed to generate empty config: %v", err)
}
}

// teardown function to clean up after the tests
func teardown() {
// Your cleanup code here
println("Running teardown after all tests.")
}

// TestMain is the entry point for testing
func TestMain(m *testing.M) {
setup(m) // Call setup
code := m.Run() // Run the tests and store the result
teardown() // Call teardown
os.Exit(code) // Exit with the result of the tests
}
11 changes: 11 additions & 0 deletions test/cli/utils_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
package cli

import "os"

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
// We also check if the file might actually be a directory.
return !info.IsDir()
}
46 changes: 35 additions & 11 deletions test/cli/version_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
package cli

import "testing"
import (
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
)

// Note main_test.go is used to set up and teardown the tests. This is the entry point for testing and
// responsible for building the most recent version of the grant binary.
func Test_VersionCommand(t *testing.T) {
tests := []struct {
name string
command string
name string
command string
expectedInOutput []string
}{
{
name: "text output",
command: "version",
name: "text output",
command: "--version",
expectedInOutput: []string{"[not provided]"},
},
{
name: "json output",
command: "version -o json",
},
{
name: "root command short version output",
command: "--version",
name: "long form",
command: "version",
expectedInOutput: []string{
"Application:",
"Version:",
"BuildDate:",
"GitCommit:",
"GitDescription:",
"Platform:",
"GoVersion:",
"Compiler:",
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// check if the command is available
cmd := exec.Command(grantTmpPath, test.command)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("command failed: %v: cmd output: %s", err, string(output))
}

for _, expected := range test.expectedInOutput {
assert.Contains(t, string(output), expected, "expected output: %s not found in command output: %s", expected, string(output))
}
})
}
}

0 comments on commit 4b6ea0e

Please sign in to comment.