Skip to content

Commit d1305ae

Browse files
authored
Add NotOwnedFailure test (#628)
This patch adds unit test for NotOwnedFailure of VirtualService, DestinationRule and Certificate.
1 parent 0a2badf commit d1305ae

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

pkg/reconciler/accessor/istio/destinationrule_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
fakeistioclient "knative.dev/net-istio/pkg/client/istio/injection/client/fake"
3030
fakedrinformer "knative.dev/net-istio/pkg/client/istio/injection/informers/networking/v1alpha3/destinationrule/fake"
3131
istiolisters "knative.dev/net-istio/pkg/client/istio/listers/networking/v1alpha3"
32+
kaccessor "knative.dev/net-istio/pkg/reconciler/accessor"
3233

3334
. "knative.dev/pkg/reconciler/testing"
3435
)
@@ -55,6 +56,16 @@ var (
5556
Host: "desired.example.com",
5657
},
5758
}
59+
60+
notOwnedDR = &v1alpha3.DestinationRule{
61+
ObjectMeta: metav1.ObjectMeta{
62+
Name: "dr",
63+
Namespace: "default",
64+
},
65+
Spec: istiov1alpha3.DestinationRule{
66+
Host: "origin.example.com",
67+
},
68+
}
5869
)
5970

6071
type FakeDestinatioRuleAccessor struct {
@@ -145,3 +156,35 @@ func TestReconcileDestinationRule_Update(t *testing.T) {
145156
t.Error("Failed to Reconcile DestinationRule:", err)
146157
}
147158
}
159+
160+
func TestReconcileDestinationRule_NotOwnedFailure(t *testing.T) {
161+
ctx, cancel, informers := SetupFakeContextWithCancel(t)
162+
163+
istio := fakeistioclient.Get(ctx)
164+
drInformer := fakedrinformer.Get(ctx)
165+
166+
waitInformers, err := RunAndSyncInformers(ctx, informers...)
167+
if err != nil {
168+
t.Fatal("Failed to start informers")
169+
}
170+
defer func() {
171+
cancel()
172+
waitInformers()
173+
}()
174+
175+
accessor := &FakeDestinatioRuleAccessor{
176+
client: istio,
177+
drLister: drInformer.Lister(),
178+
}
179+
180+
istio.NetworkingV1alpha3().DestinationRules(origin.Namespace).Create(ctx, notOwnedDR, metav1.CreateOptions{})
181+
drInformer.Informer().GetIndexer().Add(notOwnedDR)
182+
183+
_, err = ReconcileDestinationRule(ctx, ownerObj, desiredDR, accessor)
184+
if err == nil {
185+
t.Error("Expected to get error when calling ReconcileDestinationRule, but got no error.")
186+
}
187+
if !kaccessor.IsNotOwned(err) {
188+
t.Error("Expected to get NotOwnedError but got", err)
189+
}
190+
}

pkg/reconciler/accessor/istio/virtualservice_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
istioinformers "knative.dev/net-istio/pkg/client/istio/informers/externalversions"
3333
fakeistioclient "knative.dev/net-istio/pkg/client/istio/injection/client/fake"
3434
istiolisters "knative.dev/net-istio/pkg/client/istio/listers/networking/v1alpha3"
35+
kaccessor "knative.dev/net-istio/pkg/reconciler/accessor"
3536
"knative.dev/pkg/ptr"
3637

3738
. "knative.dev/pkg/reconciler/testing"
@@ -74,6 +75,16 @@ var (
7475
Hosts: []string{"desired.example.com"},
7576
},
7677
}
78+
79+
notOwned = &v1alpha3.VirtualService{
80+
ObjectMeta: metav1.ObjectMeta{
81+
Name: "vs",
82+
Namespace: "default",
83+
},
84+
Spec: istiov1alpha3.VirtualService{
85+
Hosts: []string{"origin.example.com"},
86+
},
87+
}
7788
)
7889

7990
type FakeAccessor struct {
@@ -145,6 +156,27 @@ func TestReconcileVirtualService_Update(t *testing.T) {
145156
}
146157
}
147158

159+
func TestReconcileVirtualService_NotOwnedFailure(t *testing.T) {
160+
ctx, _ := SetupFakeContext(t)
161+
ctx, cancel := context.WithCancel(ctx)
162+
163+
istioClient := fakeistioclient.Get(ctx)
164+
accessor, waitInformers := setup(ctx, []*v1alpha3.VirtualService{notOwned}, istioClient, t)
165+
defer func() {
166+
cancel()
167+
waitInformers()
168+
}()
169+
170+
_, err := ReconcileVirtualService(ctx, ownerObj, desired, accessor)
171+
if err == nil {
172+
t.Error("Expected to get error when calling ReconcileVirtualService, but got no error.")
173+
}
174+
if !kaccessor.IsNotOwned(err) {
175+
t.Error("Expected to get NotOwnedError but got", err)
176+
}
177+
178+
}
179+
148180
func setup(ctx context.Context, vses []*v1alpha3.VirtualService,
149181
istioClient istioclientset.Interface, t *testing.T) (*FakeAccessor, func()) {
150182

pkg/reconciler/accessor/networking/certificate_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/runtime"
2929

30+
kaccessor "knative.dev/net-istio/pkg/reconciler/accessor"
3031
"knative.dev/networking/pkg/apis/networking/v1alpha1"
3132
clientset "knative.dev/networking/pkg/client/clientset/versioned"
3233
fakenetworkingclient "knative.dev/networking/pkg/client/injection/client/fake"
@@ -76,6 +77,17 @@ var (
7677
SecretName: "secret0",
7778
},
7879
}
80+
81+
notOwned = &v1alpha1.Certificate{
82+
ObjectMeta: metav1.ObjectMeta{
83+
Name: "cert",
84+
Namespace: "default",
85+
},
86+
Spec: v1alpha1.CertificateSpec{
87+
DNSNames: []string{"origin.example.com"},
88+
SecretName: "secret0",
89+
},
90+
}
7991
)
8092

8193
type FakeAccessor struct {
@@ -145,6 +157,25 @@ func TestReconcileCertificateUpdate(t *testing.T) {
145157
}
146158
}
147159

160+
func TestNotOwnedFailure(t *testing.T) {
161+
ctx, cancel, _ := SetupFakeContextWithCancel(t)
162+
163+
client := fakenetworkingclient.Get(ctx)
164+
accessor, waitInformers := setup(ctx, []*v1alpha1.Certificate{notOwned}, client, t)
165+
defer func() {
166+
cancel()
167+
waitInformers()
168+
}()
169+
170+
_, err := ReconcileCertificate(ctx, ownerObj, desired, accessor)
171+
if err == nil {
172+
t.Error("Expected to get error when calling ReconcileCertificate, but got no error.")
173+
}
174+
if !kaccessor.IsNotOwned(err) {
175+
t.Error("Expected to get NotOwnedError but got", err)
176+
}
177+
}
178+
148179
func setup(ctx context.Context, certs []*v1alpha1.Certificate,
149180
client clientset.Interface, t *testing.T) (*FakeAccessor, func()) {
150181

0 commit comments

Comments
 (0)