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

refactor test command to add a test #186

Merged
merged 2 commits into from
Nov 8, 2023
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ tmp
testdata/caches
cache
out.yaml
dependabot
./dependabot
13 changes: 9 additions & 4 deletions cmd/dependabot/internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/cobra"
)

var (
type SharedFlags struct {
file string
cache string
debugging bool
Expand All @@ -22,9 +22,14 @@ var (
pullImages bool
volumes []string
timeout time.Duration
updaterImage string
proxyImage string
collectorImage string
local string
}

// root flags
var (
updaterImage string
proxyImage string
collectorImage string
)

// rootCmd represents the base command when called without any subcommands
Expand Down
121 changes: 61 additions & 60 deletions cmd/dependabot/internal/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,72 @@ import (
"gopkg.in/yaml.v3"
)

var (
jobs int
)

var testCmd = &cobra.Command{
Use: "test -f <scenario.yml>",
Short: "Test scenarios",
RunE: func(cmd *cobra.Command, args []string) error {
if jobs < 1 {
return fmt.Errorf("workers must be greater than or equal to 1")
}

if file == "" {
return fmt.Errorf("requires a scenario file")
}
// local variable for testing
var executeTestJob = infra.Run

func NewTestCommand() *cobra.Command {
var flags SharedFlags

cmd := &cobra.Command{
Use: "test -f <scenario.yml>",
Short: "Test scenarios",
RunE: func(cmd *cobra.Command, args []string) error {
if flags.file == "" {
return fmt.Errorf("requires a scenario file")
}

scenario, inputRaw, err := readScenarioFile(flags.file)
if err != nil {
return err
}

processInput(&scenario.Input)

if err := executeTestJob(infra.RunParams{
CacheDir: flags.cache,
CollectorConfigPath: flags.collectorConfigPath,
CollectorImage: collectorImage,
Creds: scenario.Input.Credentials,
Debug: flags.debugging,
Expected: scenario.Output,
ExtraHosts: flags.extraHosts,
InputName: flags.file,
InputRaw: inputRaw,
Job: &scenario.Input.Job,
LocalDir: flags.local,
Output: flags.output,
ProxyCertPath: flags.proxyCertPath,
ProxyImage: proxyImage,
PullImages: flags.pullImages,
Timeout: flags.timeout,
UpdaterImage: updaterImage,
Volumes: flags.volumes,
}); err != nil {
log.Fatal(err)
}

return nil
},
}

scenario, inputRaw, err := readScenarioFile(file)
if err != nil {
return err
}
cmd.Flags().StringVarP(&flags.file, "file", "f", "", "path to scenario file")

processInput(&scenario.Input)

if err := infra.Run(infra.RunParams{
CacheDir: cache,
CollectorConfigPath: collectorConfigPath,
CollectorImage: collectorImage,
Creds: scenario.Input.Credentials,
Debug: debugging,
Expected: scenario.Output,
ExtraHosts: extraHosts,
InputName: file,
InputRaw: inputRaw,
Job: &scenario.Input.Job,
LocalDir: local,
Output: output,
ProxyCertPath: proxyCertPath,
ProxyImage: proxyImage,
PullImages: pullImages,
Timeout: timeout,
UpdaterImage: updaterImage,
Volumes: volumes,
}); err != nil {
log.Fatal(err)
}
cmd.Flags().StringVarP(&flags.output, "output", "o", "", "write scenario to file")
cmd.Flags().StringVar(&flags.cache, "cache", "", "cache import/export directory")
cmd.Flags().StringVar(&flags.local, "local", "", "local directory to use as fetched source")
cmd.Flags().StringVar(&flags.proxyCertPath, "proxy-cert", "", "path to a certificate the proxy will trust")
cmd.Flags().StringVar(&flags.collectorConfigPath, "collector-config", "", "path to an OpenTelemetry collector config file")
cmd.Flags().BoolVar(&flags.pullImages, "pull", true, "pull the image if it isn't present")
cmd.Flags().BoolVar(&flags.debugging, "debug", false, "run an interactive shell inside the updater")
cmd.Flags().StringArrayVarP(&flags.volumes, "volume", "v", nil, "mount volumes in Docker")
cmd.Flags().StringArrayVar(&flags.extraHosts, "extra-hosts", nil, "Docker extra hosts setting on the proxy")
cmd.Flags().DurationVarP(&flags.timeout, "timeout", "t", 0, "max time to run an update")

return nil
},
return cmd
}

var testCmd = NewTestCommand()

func readScenarioFile(file string) (*model.Scenario, []byte, error) {
var scenario model.Scenario

Expand All @@ -81,19 +97,4 @@ func readScenarioFile(file string) (*model.Scenario, []byte, error) {

func init() {
rootCmd.AddCommand(testCmd)

testCmd.Flags().StringVarP(&file, "file", "f", "", "path to scenario file")
testCmd.Flags().IntVarP(&jobs, "jobs", "j", 1, "Number of jobs to run simultaneously")
testCmd.MarkFlagsMutuallyExclusive("jobs", "file")

testCmd.Flags().StringVarP(&output, "output", "o", "", "write scenario to file")
testCmd.Flags().StringVar(&cache, "cache", "", "cache import/export directory")
testCmd.Flags().StringVar(&local, "local", "", "local directory to use as fetched source")
testCmd.Flags().StringVar(&proxyCertPath, "proxy-cert", "", "path to a certificate the proxy will trust")
testCmd.Flags().StringVar(&collectorConfigPath, "collector-config", "", "path to an OpenTelemetry collector config file")
testCmd.Flags().BoolVar(&pullImages, "pull", true, "pull the image if it isn't present")
testCmd.Flags().BoolVar(&debugging, "debug", false, "run an interactive shell inside the updater")
testCmd.Flags().StringArrayVarP(&volumes, "volume", "v", nil, "mount volumes in Docker")
testCmd.Flags().StringArrayVar(&extraHosts, "extra-hosts", nil, "Docker extra hosts setting on the proxy")
testCmd.Flags().DurationVarP(&timeout, "timeout", "t", 0, "max time to run an update")
}
38 changes: 38 additions & 0 deletions cmd/dependabot/internal/cmd/test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"github.com/dependabot/cli/internal/infra"
"testing"
)

func TestTestCommand(t *testing.T) {
t.Cleanup(func() {
executeTestJob = infra.Run
})

t.Run("Read a scenario file", func(t *testing.T) {
var actualParams *infra.RunParams
executeTestJob = func(params infra.RunParams) error {
actualParams = &params
return nil
}
cmd := NewTestCommand()
err := cmd.ParseFlags([]string{"-f", "../../../../testdata/scenario.yml"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = cmd.RunE(cmd, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if actualParams == nil {
t.Fatalf("expected params to be set")
}
if actualParams.InputName == "" {
t.Errorf("expected input name to be set")
}
if actualParams.Job.PackageManager != "go_modules" {
t.Errorf("expected package manager to be set")
}
})
}
Loading