Skip to content

Commit

Permalink
Merge 565f667 into 49e1d65
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW authored Jun 20, 2022
2 parents 49e1d65 + 565f667 commit e95b2d1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
1 change: 1 addition & 0 deletions book.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type book struct {
failFast bool
skipIncluded bool
runMatch *regexp.Regexp
runSample int
runnerErrs map[string]error
beforeFuncs []func() error
afterFuncs []func() error
Expand Down
20 changes: 19 additions & 1 deletion operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -781,7 +782,7 @@ func Load(pathp string, opts ...Option) (*operators, error) {

for p, o := range om {
if !bk.runMatch.MatchString(p) {
o.Debugf(yellow("Skip %s because it does not match RUNN_RUN\n"), p)
o.Debugf(yellow("Skip %s because it does not match %s\n"), p, bk.runMatch.String())
continue
}
if contains(skipPaths, p) {
Expand All @@ -790,6 +791,9 @@ func Load(pathp string, opts ...Option) (*operators, error) {
}
ops.ops = append(ops.ops, o)
}
if bk.runSample > 0 {
ops.ops = sample(ops.ops, bk.runSample)
}
return ops, nil
}

Expand All @@ -814,6 +818,20 @@ func contains(s []string, e string) bool {
return false
}

func sample(ops []*operator, num int) []*operator {
rand.Seed(time.Now().UnixNano())
var sample []*operator
n := make([]*operator, len(ops))
copy(n, ops)

for i := 0; i < num; i++ {
idx := rand.Intn(len(n))
sample = append(sample, n[idx])
n = append(n[:idx], n[idx+1:]...)
}
return sample
}

func pop(s map[string]interface{}) (string, interface{}, bool) {
for k, v := range s {
delete(s, k)
Expand Down
20 changes: 15 additions & 5 deletions operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,32 @@ func TestLoad(t *testing.T) {
tests := []struct {
path string
RUNN_RUN string
sample int
want int
}{
{
"testdata/book/**/*", "",
"testdata/book/**/*",
"",
0,
func() int {
e, _ := os.ReadDir("testdata/book/")
return len(e)
}(),
},
{"testdata/book/**/*", "initdb", 1},
{"testdata/book/**/*", "nonexistent", 0},
{"testdata/book/**/*", "initdb", 0, 1},
{"testdata/book/**/*", "nonexistent", 0, 0},
{"testdata/book/**/*", "", 3, 3},
}
for _, tt := range tests {

t.Setenv("RUNN_RUN", tt.RUNN_RUN)
ops, err := Load(tt.path, Runner("req", "https://api.github.com"), Runner("db", "sqlite://path/to/test.db"))
opts := []Option{
Runner("req", "https://api.github.com"),
Runner("db", "sqlite://path/to/test.db"),
}
if tt.sample > 0 {
opts = append(opts, RunSample(tt.sample))
}
ops, err := Load(tt.path, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 11 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ func RunMatch(m string) Option {
}
}

// RunSample - Run the specified number of runbooks at random.
func RunSample(n int) Option {
return func(bk *book) error {
if n <= 0 {
return fmt.Errorf("sample must be greater than 0: %d", n)
}
bk.runSample = n
return nil
}
}

func included(included bool) Option {
return func(bk *book) error {
bk.included = included
Expand Down
30 changes: 29 additions & 1 deletion option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func TestOptionIntarval(t *testing.T) {
}
}

func TestRunMatch(t *testing.T) {
func TestOptionRunMatch(t *testing.T) {
tests := []struct {
match string
}{
Expand All @@ -310,3 +310,31 @@ func TestRunMatch(t *testing.T) {
}
}
}

func TestOptionRunSample(t *testing.T) {
tests := []struct {
sample int
wantErr bool
}{
{1, false},
{3, false},
{0, true},
{-1, true},
}
for _, tt := range tests {
bk := newBook()
opt := RunSample(tt.sample)
if err := opt(bk); err != nil {
if !tt.wantErr {
t.Errorf("got error %v", err)
}
continue
}
if tt.wantErr {
t.Error("want error")
}
if bk.runSample != tt.sample {
t.Errorf("got %v\nwant %v", bk.runSample, tt.sample)
}
}
}

0 comments on commit e95b2d1

Please sign in to comment.