Skip to content

Commit

Permalink
Fix TestTrigger_Error using deterministic inputs
Browse files Browse the repository at this point in the history
TestTrigger_Error had an error test case that was expecting failure due to a
bad kubeconfig input. The kubeconfig was passed in via a flag/global variable
which means in the test its value is "". This is a problem since if the
kubeconfig is an empty string, one of the underlying functions will try to
authenticate using inClusterConfig which means that the test behaves
differently depending on whether in runs in a k8s pod with access to the
apiserver or not. In local and CI runs, the test passes since its either out of
a k8s cluster or running in a pod which cannot talk to the apiserver directly.
But in our nightly tests running in a tekton cluster, it fails.

The test was also using a golden input to exactly match errors. I modified it
to only match the part of errors which we return from our code.

Fixes #795

Signed-off-by: Dibyo Mukherjee <dibyo@google.com>
  • Loading branch information
dibyom authored and tekton-robot committed Oct 14, 2020
1 parent 408fc25 commit e2e7e38
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cmd/triggerrun/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ func init() {
}

func rootRun(cmd *cobra.Command, args []string) error {
err := trigger(triggerFile, httpPath, action, os.Stdout)
err := trigger(triggerFile, httpPath, action, kubeconfig, os.Stdout)
if err != nil {
return fmt.Errorf("fail to call trigger: %v", err)
}

return nil
}

func trigger(triggerFile, httpPath, action string, writer io.Writer) error {
func trigger(triggerFile, httpPath, action, kubeconfig string, writer io.Writer) error {
// Read HTTP request.
request, body, err := readHTTP(httpPath)
if err != nil {
Expand Down
15 changes: 5 additions & 10 deletions cmd/triggerrun/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"net/http/httputil"
"regexp"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -42,18 +43,12 @@ import (
func TestTrigger_Error(t *testing.T) {
//error case for show feature
buf := new(bytes.Buffer)
err := trigger("../testdata/trigger.yaml", "../testdata/http.txt", "show", buf)
err := trigger("../testdata/trigger.yaml", "../testdata/http.txt", "show", "BAD_KUBECONFIG", buf)

wantError := "fail to get clients: Failed to build config from the flags: invalid configuration: no configuration has been provided, try setting KUBERNETES_MASTER environment variable"
wantError := "fail to get clients: Failed to build config from the flags:"

if err.Error() != wantError {
t.Errorf("Error actual = %v, and Expected = %v.", err, wantError)
}

gotValue := buf.Bytes()
wantValue := []uint8(nil)
if diff := cmp.Diff(wantValue, gotValue); diff != "" {
t.Errorf("Trigger mismatch -want +got: %s", diff)
if !strings.Contains(err.Error(), wantError) {
t.Errorf("Expected actual error to contain %s but got: %s", wantError, err.Error())
}
}

Expand Down

0 comments on commit e2e7e38

Please sign in to comment.