Skip to content

Commit

Permalink
cmd/lava: add tests to "lava scan" command
Browse files Browse the repository at this point in the history
  • Loading branch information
jroimartin committed Nov 9, 2023
1 parent 7278300 commit 9413e3d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cmd/lava/internal/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func init() {
CmdScan.Run = run // Break initialization cycle.
}

// osExit is used by tests to capture the exit code.
var osExit = os.Exit

// run is the entry point of the scan command.
func run(args []string) error {
if len(args) > 0 {
Expand All @@ -69,6 +72,7 @@ func run(args []string) error {
if err = os.Chdir(filepath.Dir(*cfgfile)); err != nil {
return fmt.Errorf("change directory: %w", err)
}

metrics.Collect("lava_version", cfg.LavaVersion)
metrics.Collect("targets", cfg.Targets)

Expand Down Expand Up @@ -98,7 +102,8 @@ func run(args []string) error {
return fmt.Errorf("write metrics: %w", err)
}
}
os.Exit(int(exitCode))

osExit(int(exitCode))

return nil
}
100 changes: 100 additions & 0 deletions cmd/lava/internal/scan/scan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2023 Adevinta

package scan

import (
"flag"
"log/slog"
"os"
"path/filepath"
"testing"

"github.com/jroimartin/clilog"

"github.com/adevinta/lava/internal/gitserver/gittest"
)

func TestMain(m *testing.M) {
flag.Parse()

level := slog.LevelError
if testing.Verbose() {
level = slog.LevelDebug
}

h := clilog.NewCLIHandler(os.Stderr, &clilog.HandlerOptions{Level: level})
slog.SetDefault(slog.New(h))

os.Exit(m.Run())
}

func TestRun(t *testing.T) {
tests := []struct {
name string
wantExitCode int
repo string
}{
{
name: "good repo",
wantExitCode: 0,
repo: "testdata/goodrepo.tar",
},
{
name: "vulnerable repo",
wantExitCode: 103,
repo: "testdata/vulnrepo.tar",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
oldPwd := mustGetwd()
oldCfgfile := *cfgfile
oldOsExit := osExit
defer func() {
mustChdir(oldPwd)
*cfgfile = oldCfgfile
osExit = oldOsExit
}()

tmpPath, err := gittest.ExtractTemp(tt.repo)
if err != nil {
t.Fatalf("unexpected error extracting test repository: %v", err)
}
defer os.RemoveAll(tmpPath)

*cfgfile = filepath.Join(tmpPath, "lava.yaml")

var exitCode int
osExit = func(status int) {
exitCode = status
}

if err := run(nil); err != nil {
t.Fatalf("run error: %v", err)
}

if exitCode != tt.wantExitCode {
t.Errorf("unexpected exit code: got: %v, want: %v", exitCode, tt.wantExitCode)
}
})
}
}

// mustGetwd returns a rooted path name corresponding to the current
// directory. It panics on error.
func mustGetwd() string {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
return wd
}

// mustChdir changes the current working directory to the named
// directory. It panics on error.
func mustChdir(path string) {
if err := os.Chdir(path); err != nil {
panic(err)
}
}
Binary file added cmd/lava/internal/scan/testdata/goodrepo.tar
Binary file not shown.
Binary file added cmd/lava/internal/scan/testdata/vulnrepo.tar
Binary file not shown.

0 comments on commit 9413e3d

Please sign in to comment.