Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.

Commit

Permalink
Update some UTs (#1442)
Browse files Browse the repository at this point in the history
* update UT

* update UT

* update UT
  • Loading branch information
grac3gao-zz authored Jul 14, 2020
1 parent 6258b1a commit 8f4ccf0
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 98 deletions.
30 changes: 24 additions & 6 deletions pkg/apis/events/v1/cloudstoragesource_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ var (
},
}

storageSourceSpecWithKSA = CloudStorageSourceSpec{
Bucket: "my-test-bucket",
PubSubSpec: gcpduckv1.PubSubSpec{
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "foo",
Kind: "bar",
Namespace: "baz",
Name: "qux",
},
},
},
IdentitySpec: gcpduckv1.IdentitySpec{
ServiceAccountName: "old-service-account",
},
},
}

// Bucket, Sink, Secret, Event Type and Project and ObjectNamePrefix
storageSourceSpec = CloudStorageSourceSpec{
Bucket: "my-test-bucket",
Expand Down Expand Up @@ -435,17 +454,16 @@ func TestCheckImmutableFields(t *testing.T) {
allowed: false,
},
"ServiceAccountName changed": {
orig: &storageSourceSpec,
orig: &storageSourceSpecWithKSA,
updated: CloudStorageSourceSpec{
Bucket: storageSourceSpec.Bucket,
EventTypes: storageSourceSpec.EventTypes,
ObjectNamePrefix: storageSourceSpec.ObjectNamePrefix,
Bucket: storageSourceSpecWithKSA.Bucket,
EventTypes: storageSourceSpecWithKSA.EventTypes,
ObjectNamePrefix: storageSourceSpecWithKSA.ObjectNamePrefix,
PubSubSpec: gcpduckv1.PubSubSpec{
IdentitySpec: gcpduckv1.IdentitySpec{
ServiceAccountName: "new-service-account",
},
SourceSpec: storageSourceSpec.SourceSpec,
Secret: storageSourceSpec.Secret,
SourceSpec: storageSourceSpecWithKSA.SourceSpec,
},
},
allowed: false,
Expand Down
43 changes: 30 additions & 13 deletions pkg/apis/events/v1alpha1/cloudauditlogssource_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
"context"
"testing"

gcpauthtesthelper "github.com/google/knative-gcp/pkg/apis/configs/gcpauth/testhelper"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

gcpauthtesthelper "github.com/google/knative-gcp/pkg/apis/configs/gcpauth/testhelper"
"github.com/google/knative-gcp/pkg/apis/duck"
duckv1alpha1 "github.com/google/knative-gcp/pkg/apis/duck/v1alpha1"
metadatatesting "github.com/google/knative-gcp/pkg/gclient/metadata/testing"
Expand Down Expand Up @@ -57,6 +56,29 @@ var (
Project: "my-eventing-project",
},
}

auditLogsSourceSpecWithKSA = CloudAuditLogsSourceSpec{
ServiceName: "foo",
MethodName: "bar",
ResourceName: "baz",
PubSubSpec: duckv1alpha1.PubSubSpec{
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "foo",
Kind: "bar",
Namespace: "baz",
Name: "qux",
},
},
},
IdentitySpec: duckv1alpha1.IdentitySpec{
ServiceAccountName: "old-service-account",
},
Project: "my-eventing-project",
},
}

validServiceAccountName = "test"
invalidServiceAccountName = "@test"
)
Expand Down Expand Up @@ -220,25 +242,20 @@ func TestCloudAuditLogsSourceCheckImmutableFields(t *testing.T) {
allowed: false,
},
"ServiceAccount changed": {
orig: &auditLogsSourceSpec,
orig: &auditLogsSourceSpecWithKSA,
updated: CloudAuditLogsSourceSpec{
PubSubSpec: duckv1alpha1.PubSubSpec{
IdentitySpec: duckv1alpha1.IdentitySpec{
ServiceAccountName: "new-service-account",
},
Secret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: auditLogsSourceSpec.PubSubSpec.Secret.Name,
},
Key: auditLogsSourceSpec.PubSubSpec.Secret.Key,
},
SourceSpec: duckv1.SourceSpec{
Sink: auditLogsSourceSpec.PubSubSpec.Sink,
Sink: auditLogsSourceSpecWithKSA.PubSubSpec.Sink,
},
Project: auditLogsSourceSpecWithKSA.Project,
},
MethodName: auditLogsSourceSpec.MethodName,
ResourceName: auditLogsSourceSpec.ResourceName,
ServiceName: auditLogsSourceSpec.ServiceName,
MethodName: auditLogsSourceSpecWithKSA.MethodName,
ResourceName: auditLogsSourceSpecWithKSA.ResourceName,
ServiceName: auditLogsSourceSpecWithKSA.ServiceName,
},
allowed: false,
},
Expand Down
36 changes: 25 additions & 11 deletions pkg/apis/events/v1alpha1/cloudbuildsource_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"context"
"testing"

gcpauthtesthelper "github.com/google/knative-gcp/pkg/apis/configs/gcpauth/testhelper"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

gcpauthtesthelper "github.com/google/knative-gcp/pkg/apis/configs/gcpauth/testhelper"

"github.com/google/knative-gcp/pkg/apis/duck"
duckv1alpha1 "github.com/google/knative-gcp/pkg/apis/duck/v1alpha1"
metadatatesting "github.com/google/knative-gcp/pkg/gclient/metadata/testing"
Expand Down Expand Up @@ -58,6 +58,25 @@ var (
},
Topic: ptr.String(topic),
}

buildSourceSpecWithKSA = CloudBuildSourceSpec{
PubSubSpec: duckv1alpha1.PubSubSpec{
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "foo",
Kind: "bar",
Namespace: "baz",
Name: "qux",
},
},
},
IdentitySpec: duckv1alpha1.IdentitySpec{
ServiceAccountName: "old-service-account",
},
Project: "my-eventing-project",
},
}
)

func TestCloudBuildSourceCheckValidationFields(t *testing.T) {
Expand Down Expand Up @@ -283,23 +302,18 @@ func TestCloudBuildSourceCheckImmutableFields(t *testing.T) {
allowed: false,
},
"ServiceAccount changed": {
orig: &buildSourceSpec,
orig: &buildSourceSpecWithKSA,
updated: CloudBuildSourceSpec{
PubSubSpec: duckv1alpha1.PubSubSpec{
IdentitySpec: duckv1alpha1.IdentitySpec{
ServiceAccountName: "new-service-account",
},
Secret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: buildSourceSpec.Secret.Name,
},
Key: buildSourceSpec.Secret.Key,
},
SourceSpec: duckv1.SourceSpec{
Sink: buildSourceSpec.Sink,
Sink: buildSourceSpecWithKSA.Sink,
},
Project: buildSourceSpecWithKSA.Project,
},
Topic: buildSourceSpec.Topic,
Topic: buildSourceSpecWithKSA.Topic,
},
allowed: false,
},
Expand Down
33 changes: 24 additions & 9 deletions pkg/apis/events/v1alpha1/cloudpubsubsource_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ var (
},
Topic: "pubsub-topic",
}

pubSubSourceSpecWithKSA = CloudPubSubSourceSpec{
PubSubSpec: duckv1alpha1.PubSubSpec{
IdentitySpec: duckv1alpha1.IdentitySpec{
ServiceAccountName: "old-service-account",
},
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "foo",
Kind: "bar",
Namespace: "baz",
Name: "qux",
},
},
},
Project: "my-eventing-project",
},
Topic: "pubsub-topic",
}
)

func TestCloudPubSubSourceCheckValidationFields(t *testing.T) {
Expand Down Expand Up @@ -309,23 +329,18 @@ func TestCloudPubSubSourceCheckImmutableFields(t *testing.T) {
allowed: false,
},
"ServiceAccount changed": {
orig: &pubSubSourceSpec,
orig: &pubSubSourceSpecWithKSA,
updated: CloudPubSubSourceSpec{
PubSubSpec: duckv1alpha1.PubSubSpec{
IdentitySpec: duckv1alpha1.IdentitySpec{
ServiceAccountName: "new-service-account",
},
Secret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: pubSubSourceSpec.Secret.Name,
},
Key: pubSubSourceSpec.Secret.Key,
},
SourceSpec: duckv1.SourceSpec{
Sink: pubSubSourceSpec.Sink,
Sink: pubSubSourceSpecWithKSA.Sink,
},
Project: pubSubSourceSpecWithKSA.Project,
},
Topic: pubSubSourceSpec.Topic,
Topic: pubSubSourceSpecWithKSA.Topic,
},
allowed: false,
},
Expand Down
32 changes: 26 additions & 6 deletions pkg/apis/events/v1alpha1/cloudschedulersource_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ var (
},
},
}

schedulerWithKSA = CloudSchedulerSourceSpec{
Location: "mylocation",
Schedule: "* * * * *",
Data: "mydata",
PubSubSpec: duckv1alpha1.PubSubSpec{
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "foo",
Kind: "bar",
Namespace: "baz",
Name: "qux",
},
},
},
IdentitySpec: duckv1alpha1.IdentitySpec{
ServiceAccountName: "old-service-account",
},
},
}
)

func TestCloudSchedulerSourceValidationFields(t *testing.T) {
Expand Down Expand Up @@ -436,17 +457,16 @@ func TestCloudSchedulerSourceSpecCheckImmutableFields(t *testing.T) {
allowed: false,
},
"ServiceAccount changed changed": {
orig: &schedulerWithSecret,
orig: &schedulerWithKSA,
updated: CloudSchedulerSourceSpec{
Location: schedulerWithSecret.Location,
Schedule: schedulerWithSecret.Schedule,
Data: schedulerWithSecret.Data,
Location: schedulerWithKSA.Location,
Schedule: schedulerWithKSA.Schedule,
Data: schedulerWithKSA.Data,
PubSubSpec: duckv1alpha1.PubSubSpec{
IdentitySpec: duckv1alpha1.IdentitySpec{
ServiceAccountName: "new-service-account",
},
SourceSpec: schedulerWithSecret.SourceSpec,
Secret: schedulerWithSecret.Secret,
SourceSpec: schedulerWithKSA.SourceSpec,
},
},
allowed: false,
Expand Down
32 changes: 25 additions & 7 deletions pkg/apis/events/v1alpha1/cloudstoragesource_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ var (
},
}

storageSourceSpecWithKSA = CloudStorageSourceSpec{
Bucket: "my-test-bucket",
PubSubSpec: duckv1alpha1.PubSubSpec{
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "foo",
Kind: "bar",
Namespace: "baz",
Name: "qux",
},
},
},
IdentitySpec: duckv1alpha1.IdentitySpec{
ServiceAccountName: "old-service-account",
},
},
}

// Bucket, Sink, Secret, Event Type and Project, ObjectNamePrefix and PayloadFormat
storageSourceSpec = CloudStorageSourceSpec{
Bucket: "my-test-bucket",
Expand Down Expand Up @@ -465,18 +484,17 @@ func TestCheckImmutableFields(t *testing.T) {
allowed: false,
},
"ServiceAccountName changed": {
orig: &storageSourceSpec,
orig: &storageSourceSpecWithKSA,
updated: CloudStorageSourceSpec{
Bucket: storageSourceSpec.Bucket,
EventTypes: storageSourceSpec.EventTypes,
ObjectNamePrefix: storageSourceSpec.ObjectNamePrefix,
PayloadFormat: storageSourceSpec.PayloadFormat,
Bucket: storageSourceSpecWithKSA.Bucket,
EventTypes: storageSourceSpecWithKSA.EventTypes,
ObjectNamePrefix: storageSourceSpecWithKSA.ObjectNamePrefix,
PayloadFormat: storageSourceSpecWithKSA.PayloadFormat,
PubSubSpec: duckv1alpha1.PubSubSpec{
IdentitySpec: duckv1alpha1.IdentitySpec{
ServiceAccountName: "new-service-account",
},
SourceSpec: storageSourceSpec.SourceSpec,
Secret: storageSourceSpec.Secret,
SourceSpec: storageSourceSpecWithKSA.SourceSpec,
},
},
allowed: false,
Expand Down
Loading

0 comments on commit 8f4ccf0

Please sign in to comment.