Skip to content

Commit 44fd76f

Browse files
committed
use generated fakes
1 parent ad62fba commit 44fd76f

File tree

1 file changed

+21
-62
lines changed

1 file changed

+21
-62
lines changed

internal/controller/state/graph/policy_ancestor_test.go

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
"github.com/go-logr/logr/testr"
88
. "github.com/onsi/gomega"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10-
"k8s.io/apimachinery/pkg/runtime"
1110
"k8s.io/apimachinery/pkg/runtime/schema"
1211
"k8s.io/apimachinery/pkg/types"
1312
v1 "sigs.k8s.io/gateway-api/apis/v1"
1413
"sigs.k8s.io/gateway-api/apis/v1alpha2"
1514

1615
ngfAPIv1alpha2 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha2"
1716
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/nginx/config/policies"
17+
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/nginx/config/policies/policiesfakes"
1818
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/kinds"
1919
)
2020

@@ -278,82 +278,40 @@ func TestGetAncestorName(t *testing.T) {
278278
}
279279
}
280280

281-
type mockPolicy struct {
282-
name string
283-
namespace string
284-
kind string
285-
}
286-
287-
func (m *mockPolicy) GetName() string { return m.name }
288-
func (m *mockPolicy) SetName(name string) { m.name = name }
289-
func (m *mockPolicy) GetNamespace() string { return m.namespace }
290-
func (m *mockPolicy) SetNamespace(namespace string) { m.namespace = namespace }
291-
func (m *mockPolicy) GetObjectKind() schema.ObjectKind {
292-
if m.kind == "" {
293-
return nil
294-
}
295-
return &mockObjectKind{kind: m.kind}
296-
}
297-
func (m *mockPolicy) GetTargetRefs() []v1alpha2.LocalPolicyTargetReference { return nil }
298-
func (m *mockPolicy) GetPolicyStatus() v1alpha2.PolicyStatus { return v1alpha2.PolicyStatus{} }
299-
func (m *mockPolicy) SetPolicyStatus(_ v1alpha2.PolicyStatus) {}
300-
func (m *mockPolicy) DeepCopyObject() runtime.Object { return m }
301-
func (m *mockPolicy) GetUID() types.UID { return "" }
302-
func (m *mockPolicy) GetResourceVersion() string { return "" }
303-
func (m *mockPolicy) SetUID(_ types.UID) {}
304-
func (m *mockPolicy) SetResourceVersion(_ string) {}
305-
func (m *mockPolicy) GetGeneration() int64 { return 0 }
306-
func (m *mockPolicy) SetGeneration(_ int64) {}
307-
func (m *mockPolicy) GetCreationTimestamp() metav1.Time { return metav1.Time{} }
308-
func (m *mockPolicy) SetCreationTimestamp(_ metav1.Time) {}
309-
func (m *mockPolicy) GetDeletionTimestamp() *metav1.Time { return nil }
310-
func (m *mockPolicy) SetDeletionTimestamp(_ *metav1.Time) {}
311-
func (m *mockPolicy) GetDeletionGracePeriodSeconds() *int64 { return nil }
312-
func (m *mockPolicy) SetDeletionGracePeriodSeconds(*int64) {}
313-
func (m *mockPolicy) GetLabels() map[string]string { return nil }
314-
func (m *mockPolicy) SetLabels(_ map[string]string) {}
315-
func (m *mockPolicy) GetAnnotations() map[string]string { return nil }
316-
func (m *mockPolicy) SetAnnotations(_ map[string]string) {}
317-
func (m *mockPolicy) GetFinalizers() []string { return nil }
318-
func (m *mockPolicy) SetFinalizers(_ []string) {}
319-
func (m *mockPolicy) GetOwnerReferences() []metav1.OwnerReference { return nil }
320-
func (m *mockPolicy) SetOwnerReferences([]metav1.OwnerReference) {}
321-
func (m *mockPolicy) GetManagedFields() []metav1.ManagedFieldsEntry { return nil }
322-
func (m *mockPolicy) SetManagedFields(_ []metav1.ManagedFieldsEntry) {}
323-
func (m *mockPolicy) GetGenerateName() string { return "" }
324-
func (m *mockPolicy) SetGenerateName(_ string) {}
325-
func (m *mockPolicy) GetSelfLink() string { return "" }
326-
func (m *mockPolicy) SetSelfLink(_ string) {}
327-
328-
type mockObjectKind struct{ kind string }
329-
330-
func (m *mockObjectKind) GroupVersionKind() schema.GroupVersionKind {
331-
return schema.GroupVersionKind{Kind: m.kind}
332-
}
333-
func (m *mockObjectKind) SetGroupVersionKind(_ schema.GroupVersionKind) {}
334-
335281
func TestGetPolicyName(t *testing.T) {
336282
t.Parallel()
337283
g := NewWithT(t)
338-
policy := &mockPolicy{name: "test-policy", namespace: "test-ns"}
284+
policy := &policiesfakes.FakePolicy{}
285+
policy.GetNameReturns("test-policy")
286+
policy.GetNamespaceReturns("test-ns")
339287
g.Expect(getPolicyName(policy)).To(Equal("test-ns/test-policy"))
340288
}
341289

342290
func TestGetPolicyKind(t *testing.T) {
343291
t.Parallel()
344292
tests := []struct {
345293
name string
346-
policy policies.Policy
294+
setup func() policies.Policy
347295
expected string
348296
}{
349297
{
350-
name: "with kind",
351-
policy: &mockPolicy{kind: "TestPolicy"},
298+
name: "with kind",
299+
setup: func() policies.Policy {
300+
policy := &policiesfakes.FakePolicy{}
301+
objectKind := &policiesfakes.FakeObjectKind{}
302+
objectKind.GroupVersionKindReturns(schema.GroupVersionKind{Kind: "TestPolicy"})
303+
policy.GetObjectKindReturns(objectKind)
304+
return policy
305+
},
352306
expected: "TestPolicy",
353307
},
354308
{
355-
name: "without kind",
356-
policy: &mockPolicy{},
309+
name: "without kind",
310+
setup: func() policies.Policy {
311+
policy := &policiesfakes.FakePolicy{}
312+
policy.GetObjectKindReturns(nil)
313+
return policy
314+
},
357315
expected: "Policy",
358316
},
359317
}
@@ -362,7 +320,8 @@ func TestGetPolicyKind(t *testing.T) {
362320
t.Run(test.name, func(t *testing.T) {
363321
t.Parallel()
364322
g := NewWithT(t)
365-
g.Expect(getPolicyKind(test.policy)).To(Equal(test.expected))
323+
policy := test.setup()
324+
g.Expect(getPolicyKind(policy)).To(Equal(test.expected))
366325
})
367326
}
368327
}

0 commit comments

Comments
 (0)