Skip to content

Commit

Permalink
Add NotOwnedFailure test (#628)
Browse files Browse the repository at this point in the history
This patch adds unit test for NotOwnedFailure of VirtualService,
DestinationRule and Certificate.
  • Loading branch information
nak3 authored May 17, 2021
1 parent 0a2badf commit d1305ae
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pkg/reconciler/accessor/istio/destinationrule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
fakeistioclient "knative.dev/net-istio/pkg/client/istio/injection/client/fake"
fakedrinformer "knative.dev/net-istio/pkg/client/istio/injection/informers/networking/v1alpha3/destinationrule/fake"
istiolisters "knative.dev/net-istio/pkg/client/istio/listers/networking/v1alpha3"
kaccessor "knative.dev/net-istio/pkg/reconciler/accessor"

. "knative.dev/pkg/reconciler/testing"
)
Expand All @@ -55,6 +56,16 @@ var (
Host: "desired.example.com",
},
}

notOwnedDR = &v1alpha3.DestinationRule{
ObjectMeta: metav1.ObjectMeta{
Name: "dr",
Namespace: "default",
},
Spec: istiov1alpha3.DestinationRule{
Host: "origin.example.com",
},
}
)

type FakeDestinatioRuleAccessor struct {
Expand Down Expand Up @@ -145,3 +156,35 @@ func TestReconcileDestinationRule_Update(t *testing.T) {
t.Error("Failed to Reconcile DestinationRule:", err)
}
}

func TestReconcileDestinationRule_NotOwnedFailure(t *testing.T) {
ctx, cancel, informers := SetupFakeContextWithCancel(t)

istio := fakeistioclient.Get(ctx)
drInformer := fakedrinformer.Get(ctx)

waitInformers, err := RunAndSyncInformers(ctx, informers...)
if err != nil {
t.Fatal("Failed to start informers")
}
defer func() {
cancel()
waitInformers()
}()

accessor := &FakeDestinatioRuleAccessor{
client: istio,
drLister: drInformer.Lister(),
}

istio.NetworkingV1alpha3().DestinationRules(origin.Namespace).Create(ctx, notOwnedDR, metav1.CreateOptions{})
drInformer.Informer().GetIndexer().Add(notOwnedDR)

_, err = ReconcileDestinationRule(ctx, ownerObj, desiredDR, accessor)
if err == nil {
t.Error("Expected to get error when calling ReconcileDestinationRule, but got no error.")
}
if !kaccessor.IsNotOwned(err) {
t.Error("Expected to get NotOwnedError but got", err)
}
}
32 changes: 32 additions & 0 deletions pkg/reconciler/accessor/istio/virtualservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
istioinformers "knative.dev/net-istio/pkg/client/istio/informers/externalversions"
fakeistioclient "knative.dev/net-istio/pkg/client/istio/injection/client/fake"
istiolisters "knative.dev/net-istio/pkg/client/istio/listers/networking/v1alpha3"
kaccessor "knative.dev/net-istio/pkg/reconciler/accessor"
"knative.dev/pkg/ptr"

. "knative.dev/pkg/reconciler/testing"
Expand Down Expand Up @@ -74,6 +75,16 @@ var (
Hosts: []string{"desired.example.com"},
},
}

notOwned = &v1alpha3.VirtualService{
ObjectMeta: metav1.ObjectMeta{
Name: "vs",
Namespace: "default",
},
Spec: istiov1alpha3.VirtualService{
Hosts: []string{"origin.example.com"},
},
}
)

type FakeAccessor struct {
Expand Down Expand Up @@ -145,6 +156,27 @@ func TestReconcileVirtualService_Update(t *testing.T) {
}
}

func TestReconcileVirtualService_NotOwnedFailure(t *testing.T) {
ctx, _ := SetupFakeContext(t)
ctx, cancel := context.WithCancel(ctx)

istioClient := fakeistioclient.Get(ctx)
accessor, waitInformers := setup(ctx, []*v1alpha3.VirtualService{notOwned}, istioClient, t)
defer func() {
cancel()
waitInformers()
}()

_, err := ReconcileVirtualService(ctx, ownerObj, desired, accessor)
if err == nil {
t.Error("Expected to get error when calling ReconcileVirtualService, but got no error.")
}
if !kaccessor.IsNotOwned(err) {
t.Error("Expected to get NotOwnedError but got", err)
}

}

func setup(ctx context.Context, vses []*v1alpha3.VirtualService,
istioClient istioclientset.Interface, t *testing.T) (*FakeAccessor, func()) {

Expand Down
31 changes: 31 additions & 0 deletions pkg/reconciler/accessor/networking/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

kaccessor "knative.dev/net-istio/pkg/reconciler/accessor"
"knative.dev/networking/pkg/apis/networking/v1alpha1"
clientset "knative.dev/networking/pkg/client/clientset/versioned"
fakenetworkingclient "knative.dev/networking/pkg/client/injection/client/fake"
Expand Down Expand Up @@ -76,6 +77,17 @@ var (
SecretName: "secret0",
},
}

notOwned = &v1alpha1.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "cert",
Namespace: "default",
},
Spec: v1alpha1.CertificateSpec{
DNSNames: []string{"origin.example.com"},
SecretName: "secret0",
},
}
)

type FakeAccessor struct {
Expand Down Expand Up @@ -145,6 +157,25 @@ func TestReconcileCertificateUpdate(t *testing.T) {
}
}

func TestNotOwnedFailure(t *testing.T) {
ctx, cancel, _ := SetupFakeContextWithCancel(t)

client := fakenetworkingclient.Get(ctx)
accessor, waitInformers := setup(ctx, []*v1alpha1.Certificate{notOwned}, client, t)
defer func() {
cancel()
waitInformers()
}()

_, err := ReconcileCertificate(ctx, ownerObj, desired, accessor)
if err == nil {
t.Error("Expected to get error when calling ReconcileCertificate, but got no error.")
}
if !kaccessor.IsNotOwned(err) {
t.Error("Expected to get NotOwnedError but got", err)
}
}

func setup(ctx context.Context, certs []*v1alpha1.Certificate,
client clientset.Interface, t *testing.T) (*FakeAccessor, func()) {

Expand Down

0 comments on commit d1305ae

Please sign in to comment.