Skip to content

Commit

Permalink
add tests for lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
foosinn committed Sep 10, 2019
1 parent 5130727 commit cba4914
Showing 1 changed file with 49 additions and 33 deletions.
82 changes: 49 additions & 33 deletions guard_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

type (
tCase struct {
command string
content string
}

tQuietCase struct {
quiet string
command string
content string
command string
additionalArgs []string
content string
}
)

func guard(t *testing.T, additionalArgs []string, command string, want string) (err error) {
fmt.Printf("running %s\n", command)
tempFile, err := ioutil.TempFile("", "guard")
if err != nil {
t.Errorf("unable to create tmp file (%s): %s", tempFile.Name(), err)
return fmt.Errorf("unable to create tmp file (%s): %s", tempFile.Name(), err)
}

os.Args = []string{
Expand All @@ -43,8 +42,11 @@ func guard(t *testing.T, additionalArgs []string, command string, want string) (
return
}

if strings.ContainsRune(string(got), '\x00') {
return fmt.Errorf("Nullbyte")
}
if diff := cmp.Diff(want, string(got)); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
return fmt.Errorf("mismatch (-want +got):\n%s", diff)
}

tempFile.Close()
Expand All @@ -53,7 +55,7 @@ func guard(t *testing.T, additionalArgs []string, command string, want string) (
return nil
}

return
return err
}

func TestOutput(t *testing.T) {
Expand All @@ -62,45 +64,59 @@ func TestOutput(t *testing.T) {
// test normal
cases := []tCase{
// check exit statuss
{"true", ""},
{"false", "// error: exit status 1\n"},
{"exit 2", "// error: exit status 2\n"},
{"true", []string{}, ""},
{"false", []string{}, "// error: exit status 1\n"},
{"exit 2", []string{}, "// error: exit status 2\n"},

// check output
{"echo fail", "fail\n// error: bad keyword in command output: fail\n"},
{"echo failure", "failure\n// error: bad keyword in command output: failure\n"},
{"echo ERR", "ERR\n// error: bad keyword in command output: ERR\n"},
{"echo ERROR", "ERROR\n// error: bad keyword in command output: ERROR\n"},
{"echo Crit", "Crit\n// error: bad keyword in command output: Crit\n"},
{"echo Critical", "Critical\n// error: bad keyword in command output: Critical\n"},
{"echo fail", []string{}, "fail\n// error: bad keyword in command output: fail\n"},
{"echo failure", []string{}, "failure\n// error: bad keyword in command output: failure\n"},
{"echo ERR", []string{}, "ERR\n// error: bad keyword in command output: ERR\n"},
{"echo ERROR", []string{}, "ERROR\n// error: bad keyword in command output: ERROR\n"},
{"echo Crit", []string{}, "Crit\n// error: bad keyword in command output: Crit\n"},
{"echo Critical", []string{}, "Critical\n// error: bad keyword in command output: Critical\n"},

// check err output
{"echo Hi there 1>&2", "Hi there\n// error: stderr is not empty\n"},
{"echo Hi there 1>&2", []string{}, "Hi there\n// error: stderr is not empty\n"},

// check asci boundaries
{"echo transferred", ""},
{"echo transferred error", "transferred error\n// error: bad keyword in command output: transferred error\n"},
{"echo transferred", []string{}, ""},
{"echo transferred error", []string{}, "transferred error\n// error: bad keyword in command output: transferred error\n"},

// quiet tests
{"false", []string{"-quiet-times", "0 * * * *:1h"}, ""},
{"false", []string{"-quiet-times", "0 0 * * *:0s"}, "// error: exit status 1\n"},

// timeout tests
{"sleep 1", []string{"-timeout", "2s"}, ""},
{"sleep 2", []string{"-timeout", "500ms"}, "// error: context deadline exceeded\n"},
}
for _, c := range cases {
err = guard(t, []string{}, c.command, c.content)
err = guard(t, c.additionalArgs, c.command, c.content)
if err != nil {
t.Error(err)
break
}
}

// test with quiet
qCases := []tQuietCase{
{"0 * * * *:1h", "false", ""},
{"0 0 * * *:0s", "false", "// error: exit status 1\n"},
// paralell tests
cases = []tCase{
// lockfile tests
{"sleep 2; echo failed", []string{"-lockfile", "/tmp/guard.lock"}, "failed\n// error: bad keyword in command output: failed\n"},
{"echo failed this should not run; sleep 3", []string{"-lockfile", "/tmp/guard.lock"}, ""},
}


for _, c := range qCases {
err = guard(t, []string{"-quiet-times", c.quiet}, c.command, c.content)
errChan := make(chan error)
for _, c := range cases {
<-time.After(1 * time.Second)
go func(c tCase) {
err := guard(t, c.additionalArgs, c.command, c.content)
errChan <- err
}(c)
}
for i := 0; i < len(cases); i++ {
err = <-errChan
if err != nil {
t.Error(err)
break
}
}
}

0 comments on commit cba4914

Please sign in to comment.