Skip to content
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
14 changes: 12 additions & 2 deletions test/base/generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,26 @@ type MetaResource struct {
metav1.ObjectMeta `json:"metadata,omitempty"`
}

// MetaEventing returns a MetaResource that can represent a Knative Eventing resource.
func MetaEventing(name, namespace, kind string) *MetaResource {
return Meta(name, namespace, kind, eventingAPIVersion)
}

// MetaSource returns a MetaResource that can represent a Knative Sources resource.
func MetaSource(name, namespace, kind string) *MetaResource {
return Meta(name, namespace, kind, sourcesAPIVersion)
}

// Meta returns a MetaResource built from the given name, namespace and kind.
func Meta(name, namespace, kind string) *MetaResource {
func Meta(name, namespace, kind, apiVersion string) *MetaResource {
return &MetaResource{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
TypeMeta: metav1.TypeMeta{
Kind: kind,
APIVersion: EventingAPIVersion,
APIVersion: apiVersion,
},
}
}
Expand Down
112 changes: 77 additions & 35 deletions test/base/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ package base
import (
"fmt"

"github.com/knative/eventing/pkg/apis/eventing/v1alpha1"
eventingv1alpha1 "github.com/knative/eventing/pkg/apis/eventing/v1alpha1"
sourcesv1alpha1 "github.com/knative/eventing/pkg/apis/sources/v1alpha1"
pkgTest "github.com/knative/pkg/test"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand All @@ -30,62 +31,67 @@ import (
"k8s.io/apimachinery/pkg/util/uuid"
)

const EventingAPIVersion = "eventing.knative.dev/v1alpha1"
const eventingAPIVersion = "eventing.knative.dev/v1alpha1"
const sourcesAPIVersion = "sources.eventing.knative.dev/v1alpha1"

// clusterChannelProvisioner returns a ClusterChannelProvisioner for a given name.
func clusterChannelProvisioner(name string) *corev1.ObjectReference {
if name == "" {
return nil
}
return pkgTest.CoreV1ObjectReference("ClusterChannelProvisioner", EventingAPIVersion, name)
return pkgTest.CoreV1ObjectReference("ClusterChannelProvisioner", eventingAPIVersion, name)
}

// channelRef returns an ObjectReference for a given Channel Name.
func channelRef(name string) *corev1.ObjectReference {
return pkgTest.CoreV1ObjectReference("Channel", EventingAPIVersion, name)
return pkgTest.CoreV1ObjectReference("Channel", eventingAPIVersion, name)
}

// Channel returns a Channel with the specified provisioner.
func Channel(name, provisioner string) *v1alpha1.Channel {
return &v1alpha1.Channel{
func Channel(name, provisioner string) *eventingv1alpha1.Channel {
return &eventingv1alpha1.Channel{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.ChannelSpec{
Spec: eventingv1alpha1.ChannelSpec{
Provisioner: clusterChannelProvisioner(provisioner),
},
}
}

// WithSubscriberForSubscription returns an option that adds a Subscriber for the given Subscription.
func WithSubscriberForSubscription(name string) func(*v1alpha1.Subscription) {
return func(s *v1alpha1.Subscription) {
func WithSubscriberForSubscription(name string) func(*eventingv1alpha1.Subscription) {
return func(s *eventingv1alpha1.Subscription) {
if name != "" {
s.Spec.Subscriber = &v1alpha1.SubscriberSpec{
s.Spec.Subscriber = &eventingv1alpha1.SubscriberSpec{
Ref: pkgTest.CoreV1ObjectReference("Service", "v1", name),
}
}
}
}

// WithReply returns an options that adds a ReplyStrategy for the given Subscription.
func WithReply(name string) func(*v1alpha1.Subscription) {
return func(s *v1alpha1.Subscription) {
func WithReply(name string) func(*eventingv1alpha1.Subscription) {
return func(s *eventingv1alpha1.Subscription) {
if name != "" {
s.Spec.Reply = &v1alpha1.ReplyStrategy{
Channel: pkgTest.CoreV1ObjectReference("Channel", EventingAPIVersion, name),
s.Spec.Reply = &eventingv1alpha1.ReplyStrategy{
Channel: pkgTest.CoreV1ObjectReference("Channel", eventingAPIVersion, name),
}
}
}
}

// Subscription returns a Subscription.
func Subscription(name, channelName string, options ...func(*v1alpha1.Subscription)) *v1alpha1.Subscription {
subscription := &v1alpha1.Subscription{
func Subscription(
name,
channelName string,
options ...func(*eventingv1alpha1.Subscription),
) *eventingv1alpha1.Subscription {
subscription := &eventingv1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.SubscriptionSpec{
Spec: eventingv1alpha1.SubscriptionSpec{
Channel: *channelRef(channelName),
},
}
Expand All @@ -96,24 +102,24 @@ func Subscription(name, channelName string, options ...func(*v1alpha1.Subscripti
}

// Broker returns a Broker.
func Broker(name, provisioner string) *v1alpha1.Broker {
return &v1alpha1.Broker{
func Broker(name, provisioner string) *eventingv1alpha1.Broker {
return &eventingv1alpha1.Broker{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.BrokerSpec{
ChannelTemplate: &v1alpha1.ChannelSpec{
Spec: eventingv1alpha1.BrokerSpec{
ChannelTemplate: &eventingv1alpha1.ChannelSpec{
Provisioner: clusterChannelProvisioner(provisioner),
},
},
}
}

// WithTriggerFilter returns an option that adds a TriggerFilter for the given Trigger.
func WithTriggerFilter(eventSource, eventType string) func(*v1alpha1.Trigger) {
return func(t *v1alpha1.Trigger) {
triggerFilter := &v1alpha1.TriggerFilter{
SourceAndType: &v1alpha1.TriggerFilterSourceAndType{
func WithTriggerFilter(eventSource, eventType string) func(*eventingv1alpha1.Trigger) {
return func(t *eventingv1alpha1.Trigger) {
triggerFilter := &eventingv1alpha1.TriggerFilter{
SourceAndType: &eventingv1alpha1.TriggerFilterSourceAndType{
Type: eventType,
Source: eventSource,
},
Expand All @@ -123,35 +129,35 @@ func WithTriggerFilter(eventSource, eventType string) func(*v1alpha1.Trigger) {
}

// WithBroker returns an option that adds a Broker for the given Trigger.
func WithBroker(brokerName string) func(*v1alpha1.Trigger) {
return func(t *v1alpha1.Trigger) {
func WithBroker(brokerName string) func(*eventingv1alpha1.Trigger) {
return func(t *eventingv1alpha1.Trigger) {
t.Spec.Broker = brokerName
}
}

// WithSubscriberRefForTrigger returns an option that adds a Subscriber Ref for the given Trigger.
func WithSubscriberRefForTrigger(name string) func(*v1alpha1.Trigger) {
return func(t *v1alpha1.Trigger) {
func WithSubscriberRefForTrigger(name string) func(*eventingv1alpha1.Trigger) {
return func(t *eventingv1alpha1.Trigger) {
if name != "" {
t.Spec.Subscriber = &v1alpha1.SubscriberSpec{
t.Spec.Subscriber = &eventingv1alpha1.SubscriberSpec{
Ref: pkgTest.CoreV1ObjectReference("Service", "v1", name),
}
}
}
}

// WithSubscriberURIForTrigger returns an option that adds a Subscriber URI for the given Trigger.
func WithSubscriberURIForTrigger(uri string) func(*v1alpha1.Trigger) {
return func(t *v1alpha1.Trigger) {
t.Spec.Subscriber = &v1alpha1.SubscriberSpec{
func WithSubscriberURIForTrigger(uri string) func(*eventingv1alpha1.Trigger) {
return func(t *eventingv1alpha1.Trigger) {
t.Spec.Subscriber = &eventingv1alpha1.SubscriberSpec{
URI: &uri,
}
}
}

// Trigger returns a Trigger.
func Trigger(name string, options ...func(*v1alpha1.Trigger)) *v1alpha1.Trigger {
trigger := &v1alpha1.Trigger{
func Trigger(name string, options ...func(*eventingv1alpha1.Trigger)) *eventingv1alpha1.Trigger {
trigger := &eventingv1alpha1.Trigger{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Expand All @@ -162,6 +168,42 @@ func Trigger(name string, options ...func(*v1alpha1.Trigger)) *v1alpha1.Trigger
return trigger
}

// WithSinkServiceForCronJobSource returns an option that adds a Kubernetes Service sink for the given CronJobSource.
func WithSinkServiceForCronJobSource(name string) func(*sourcesv1alpha1.CronJobSource) {
return func(cjs *sourcesv1alpha1.CronJobSource) {
cjs.Spec.Sink = pkgTest.CoreV1ObjectReference("Service", "v1", name)
}
}

// WithServiceAccountForCronJobSource returns an option that adds a ServiceAccount for the given CronJobSource.
func WithServiceAccountForCronJobSource(saName string) func(*sourcesv1alpha1.CronJobSource) {
return func(cjs *sourcesv1alpha1.CronJobSource) {
cjs.Spec.ServiceAccountName = saName
}
}

// CronJobSource returns a CronJob EventSource.
func CronJobSource(
name,
schedule,
data string,
options ...func(*sourcesv1alpha1.CronJobSource),
) *sourcesv1alpha1.CronJobSource {
cronJobSource := &sourcesv1alpha1.CronJobSource{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: sourcesv1alpha1.CronJobSourceSpec{
Schedule: schedule,
Data: data,
},
}
for _, option := range options {
option(cronJobSource)
}
return cronJobSource
}

// CloudEvent specifies the arguments for a CloudEvent sent by the sendevent
// binary.
type CloudEvent struct {
Expand Down
46 changes: 37 additions & 9 deletions test/common/creation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ limitations under the License.
package common

import (
"github.com/knative/eventing/pkg/apis/eventing/v1alpha1"
eventingv1alpha1 "github.com/knative/eventing/pkg/apis/eventing/v1alpha1"
sourcesv1alpha1 "github.com/knative/eventing/pkg/apis/sources/v1alpha1"
"github.com/knative/eventing/test/base"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand All @@ -28,7 +29,7 @@ var coreAPIVersion = corev1.SchemeGroupVersion.Version
var rbacAPIGroup = rbacv1.SchemeGroupVersion.Group
var rbacAPIVersion = rbacv1.SchemeGroupVersion.Version

// CreateChannelOrFail will create a Channel Resource in Eventing.
// CreateChannelOrFail will create a Channel or fail the test if there is an error.
func (client *Client) CreateChannelOrFail(name, provisonerName string) {
namespace := client.Namespace
channel := base.Channel(name, provisonerName)
Expand All @@ -49,8 +50,12 @@ func (client *Client) CreateChannelsOrFail(names []string, provisionerName strin
}
}

// CreateSubscriptionOrFail will create a Subscription.
func (client *Client) CreateSubscriptionOrFail(name, channelName string, options ...func(*v1alpha1.Subscription)) {
// CreateSubscriptionOrFail will create a Subscription or fail the test if there is an error.
func (client *Client) CreateSubscriptionOrFail(
name,
channelName string,
options ...func(*eventingv1alpha1.Subscription),
) {
namespace := client.Namespace
subscription := base.Subscription(name, channelName, options...)

Expand All @@ -64,13 +69,17 @@ func (client *Client) CreateSubscriptionOrFail(name, channelName string, options
}

// CreateSubscriptionsOrFail will create a list of Subscriptions with the same configuration except the name.
func (client *Client) CreateSubscriptionsOrFail(names []string, channelName string, options ...func(*v1alpha1.Subscription)) {
func (client *Client) CreateSubscriptionsOrFail(
names []string,
channelName string,
options ...func(*eventingv1alpha1.Subscription),
) {
for _, name := range names {
client.CreateSubscriptionOrFail(name, channelName, options...)
}
}

// CreateBrokerOrFail will create a Broker.
// CreateBrokerOrFail will create a Broker or fail the test if there is an error.
func (client *Client) CreateBrokerOrFail(name, provisionerName string) {
namespace := client.Namespace
broker := base.Broker(name, provisionerName)
Expand All @@ -91,8 +100,8 @@ func (client *Client) CreateBrokersOrFail(names []string, provisionerName string
}
}

// CreateTriggerOrFail will create a Trigger.
func (client *Client) CreateTriggerOrFail(name string, options ...func(*v1alpha1.Trigger)) {
// CreateTriggerOrFail will create a Trigger or fail the test if there is an error.
func (client *Client) CreateTriggerOrFail(name string, options ...func(*eventingv1alpha1.Trigger)) {
namespace := client.Namespace
trigger := base.Trigger(name, options...)

Expand All @@ -105,6 +114,25 @@ func (client *Client) CreateTriggerOrFail(name string, options ...func(*v1alpha1
client.Cleaner.AddObj(trigger)
}

// CreateCronJobSourceOrFail will create a CronJobSource or fail the test if there is an error.
func (client *Client) CreateCronJobSourceOrFail(
name,
schedule,
data string,
options ...func(*sourcesv1alpha1.CronJobSource),
) {
namespace := client.Namespace
cronJobSource := base.CronJobSource(name, schedule, data, options...)

cronJobSources := client.Eventing.SourcesV1alpha1().CronJobSources(namespace)
// update cronJobSource with the new reference
cronJobSource, err := cronJobSources.Create(cronJobSource)
if err != nil {
client.T.Fatalf("Failed to create cronjobsource %q: %v", name, err)
}
client.Cleaner.AddObj(cronJobSource)
}

// WithService returns an option that creates a Service binded with the given pod.
func WithService(name string) func(*corev1.Pod, *Client) error {
return func(pod *corev1.Pod, client *Client) error {
Expand All @@ -120,7 +148,7 @@ func WithService(name string) func(*corev1.Pod, *Client) error {
}
}

// CreatePodOrFail will create a Pod.
// CreatePodOrFail will create a Pod or fail the test if there is an error.
func (client *Client) CreatePodOrFail(pod *corev1.Pod, options ...func(*corev1.Pod, *Client) error) {
// set namespace for the pod in case it's empty
namespace := client.Namespace
Expand Down
Loading