Skip to content

Commit

Permalink
add integration test for trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Feb 10, 2020
1 parent 50ae82b commit fc76456
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions test/e2e/trigger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2020 The Knative Authors

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or im
// See the License for the specific language governing permissions and
// limitations under the License.

// +build e2e
// +build !serving

package e2e

import (
"strings"
"testing"

"gotest.tools/assert"
"k8s.io/apimachinery/pkg/util/wait"
"knative.dev/client/pkg/util"
)

func TestBrokerTrigger(t *testing.T) {
t.Parallel()
test := NewE2eTest(t)
test.Setup(t)
defer test.Teardown(t)

err := test.lableNamespaceForDefaultBroker(t)
assert.NilError(t, err)
test.serviceCreate(t, "sinksvc0")

t.Run("create trigger to subscribe a broker", func(t *testing.T) {
test.triggerCreate(t, "trigger1", "sinksvc0")
test.triggerCreateWithFilter(t, "trigger2", []string{"type=knative.dev.bar", "source=cronjob"}, "sinksvc0")
verifyTriggerList(t, []string{"trigger1", "trigger2"})
})
}

func (test *e2eTest) lableNamespaceForDefaultBroker(t *testing.T) error {
kubectl := kubectl{t, Logger{}}

_, err := kubectl.RunWithOpts([]string{"label", "namespace", test.kn.namespace, "knative-eventing-injection=enabled"}, runOpts{})
if err != nil {
t.Fatalf("Error executing 'kubectl lable namespace %s knative-eventing-injection=enabled'. Error: %s", test.kn.namespace, err.Error())
}

return wait.PollImmediate(Interval, Timeout, func() (bool, error) {
out, err := kubectl.RunWithOpts([]string{"get", "broker", "-n", test.kn.namespace, "-o=jsonpath='{.items[0].status.conditions[?(@.type==\"Ready\")].status}'"}, runOpts{})
return strings.Contains(out, "True"), err
})
}

func (test *e2eTest) triggerCreate(t *testing.T, name string, sinksvc string) {
out, err := test.kn.RunWithOpts([]string{"trigger", "create", name, "--broker", "default", "--sink", "svc:" + sinksvc}, runOpts{NoNamespace: false})
assert.NilError(t, err)
assert.Check(t, util.ContainsAllIgnoreCase(out, "Trigger", name, "created", "namespace", test.kn.namespace))
}

func (test *e2eTest) triggerCreateWithFilter(t *testing.T, name string, filters []string, sinksvc string) {
filterStr := ""
for _, v := range filters {
filterStr = filterStr + "--filter " + v + " "
}
out, err := test.kn.RunWithOpts([]string{"trigger", "create", name, filterStr, "--broker", "default", "--sink", "svc:" + sinksvc}, runOpts{NoNamespace: false})
assert.NilError(t, err)
assert.Check(t, util.ContainsAllIgnoreCase(out, "Trigger", name, "created", "namespace", test.kn.namespace))
}

func (test *e2eTest) triggerCreateMissingSink(t *testing.T, name string, resources string, sa string, sink string) {
_, err := test.kn.RunWithOpts([]string{"trigger", "create", name, "--broker", "default", "--sink", "svc:" + sink}, runOpts{NoNamespace: false, AllowError: true})
assert.ErrorContains(t, err, "services.serving.knative.dev", "not found")
}

func (test *e2eTest) triggerDelete(t *testing.T, name string) {
out, err := test.kn.RunWithOpts([]string{"trigger", "delete", name}, runOpts{NoNamespace: false})
assert.NilError(t, err)
assert.Check(t, util.ContainsAllIgnoreCase(out, "Trigger", name, "deleted", "namespace", test.kn.namespace))
}

func (test *e2eTest) triggerUpdateFilter(t *testing.T, name string, filter string, sinksvc string) {
out, err := test.kn.RunWithOpts([]string{"trigger", "update", name, "--filter", filter, "--broker", "default", "--sink", "svc:" + sinksvc}, runOpts{NoNamespace: false})
assert.NilError(t, err)
assert.Check(t, util.ContainsAllIgnoreCase(out, "Trigger", name, "updated", "namespace", test.kn.namespace))
}

func (test *e2eTest) triggerUpdateSink(t *testing.T, name string, sinksvc string) {
out, err := test.kn.RunWithOpts([]string{"trigger", "update", name, "--sink", "svc:" + sinksvc}, runOpts{NoNamespace: false})
assert.NilError(t, err)
assert.Check(t, util.ContainsAllIgnoreCase(out, "Trigger", name, "updated", "namespace", test.kn.namespace))
}

func (test *e2eTest) verifyTriggerList(t *testing.T, triggers []string) {
out, err := test.kn.RunWithOpts([]string{"trigger", "list"}, runOpts{NoNamespace: false})
assert.NilError(t, err)
assert.Check(t, util.ContainsAllIgnoreCase(out, triggers...))
}

func (test *e2eTest) verifyTriggerDescribe(t *testing.T, name string, filters []string, broker string, sink string) {
out, err := test.kn.RunWithOpts([]string{"trigger", "describe", name}, runOpts{NoNamespace: false})
assert.NilError(t, err)
assert.Check(t, util.ContainsAllIgnoreCase(out, filters...))
assert.Check(t, util.ContainsAllIgnoreCase(out, name, broker, sink))
}

0 comments on commit fc76456

Please sign in to comment.