@@ -29,11 +29,9 @@ import (
29
29
"k8s.io/apimachinery/pkg/api/meta"
30
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31
31
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
32
- "k8s.io/apimachinery/pkg/labels"
33
32
"k8s.io/apimachinery/pkg/runtime"
34
33
"k8s.io/apimachinery/pkg/runtime/schema"
35
34
dynamicfake "k8s.io/client-go/dynamic/fake"
36
- "k8s.io/client-go/tools/cache"
37
35
"k8s.io/client-go/tools/record"
38
36
"sigs.k8s.io/controller-runtime/pkg/client"
39
37
"sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -634,7 +632,7 @@ func TestLookForMatchedPolicy(t *testing.T) {
634
632
tests := []struct {
635
633
name string
636
634
object * unstructured.Unstructured
637
- policies []* policyv1alpha1. PropagationPolicy
635
+ policies []client. Object
638
636
expectedPolicy * policyv1alpha1.PropagationPolicy
639
637
}{
640
638
{
@@ -649,8 +647,8 @@ func TestLookForMatchedPolicy(t *testing.T) {
649
647
},
650
648
},
651
649
},
652
- policies : []* policyv1alpha1. PropagationPolicy {
653
- {
650
+ policies : []client. Object {
651
+ & policyv1alpha1. PropagationPolicy {
654
652
ObjectMeta : metav1.ObjectMeta {
655
653
Name : "policy-1" ,
656
654
Namespace : "default" ,
@@ -689,9 +687,7 @@ func TestLookForMatchedPolicy(t *testing.T) {
689
687
690
688
d := & ResourceDetector {
691
689
DynamicClient : fakeClient ,
692
- propagationPolicyLister : & mockPropagationPolicyLister {
693
- policies : tt .policies ,
694
- },
690
+ Client : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (tt .policies ... ).Build (),
695
691
}
696
692
697
693
objectKey := keys.ClusterWideKey {
@@ -725,7 +721,7 @@ func TestLookForMatchedClusterPolicy(t *testing.T) {
725
721
tests := []struct {
726
722
name string
727
723
object * unstructured.Unstructured
728
- policies []* policyv1alpha1. ClusterPropagationPolicy
724
+ policies []client. Object
729
725
expectedPolicy * policyv1alpha1.ClusterPropagationPolicy
730
726
}{
731
727
{
@@ -740,8 +736,8 @@ func TestLookForMatchedClusterPolicy(t *testing.T) {
740
736
},
741
737
},
742
738
},
743
- policies : []* policyv1alpha1. ClusterPropagationPolicy {
744
- {
739
+ policies : []client. Object {
740
+ & policyv1alpha1. ClusterPropagationPolicy {
745
741
ObjectMeta : metav1.ObjectMeta {
746
742
Name : "cluster-policy-1" ,
747
743
},
@@ -778,9 +774,7 @@ func TestLookForMatchedClusterPolicy(t *testing.T) {
778
774
779
775
d := & ResourceDetector {
780
776
DynamicClient : fakeClient ,
781
- clusterPropagationPolicyLister : & mockClusterPropagationPolicyLister {
782
- policies : tt .policies ,
783
- },
777
+ Client : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (tt .policies ... ).Build (),
784
778
}
785
779
786
780
objectKey := keys.ClusterWideKey {
@@ -980,6 +974,7 @@ func setupTestScheme() *runtime.Scheme {
980
974
scheme := runtime .NewScheme ()
981
975
_ = workv1alpha2 .Install (scheme )
982
976
_ = corev1 .AddToScheme (scheme )
977
+ _ = policyv1alpha1 .Install (scheme )
983
978
return scheme
984
979
}
985
980
@@ -1046,110 +1041,6 @@ func (m *mockRESTMapper) ResourceSingularizer(resource string) (string, error) {
1046
1041
return resource , nil
1047
1042
}
1048
1043
1049
- // mockPropagationPolicyLister is a mock implementation of the PropagationPolicyLister
1050
- type mockPropagationPolicyLister struct {
1051
- policies []* policyv1alpha1.PropagationPolicy
1052
- }
1053
-
1054
- func (m * mockPropagationPolicyLister ) List (_ labels.Selector ) (ret []runtime.Object , err error ) {
1055
- var result []runtime.Object
1056
- for _ , p := range m .policies {
1057
- u , err := runtime .DefaultUnstructuredConverter .ToUnstructured (p )
1058
- if err != nil {
1059
- return nil , err
1060
- }
1061
- result = append (result , & unstructured.Unstructured {Object : u })
1062
- }
1063
- return result , nil
1064
- }
1065
-
1066
- func (m * mockPropagationPolicyLister ) Get (name string ) (runtime.Object , error ) {
1067
- for _ , p := range m .policies {
1068
- if p .Name == name {
1069
- u , err := runtime .DefaultUnstructuredConverter .ToUnstructured (p )
1070
- if err != nil {
1071
- return nil , err
1072
- }
1073
- return & unstructured.Unstructured {Object : u }, nil
1074
- }
1075
- }
1076
- return nil , nil
1077
- }
1078
-
1079
- func (m * mockPropagationPolicyLister ) ByNamespace (namespace string ) cache.GenericNamespaceLister {
1080
- return & mockGenericNamespaceLister {
1081
- policies : m .policies ,
1082
- namespace : namespace ,
1083
- }
1084
- }
1085
-
1086
- // mockGenericNamespaceLister is a mock implementation of cache.GenericNamespaceLister
1087
- type mockGenericNamespaceLister struct {
1088
- policies []* policyv1alpha1.PropagationPolicy
1089
- namespace string
1090
- }
1091
-
1092
- func (m * mockGenericNamespaceLister ) List (_ labels.Selector ) (ret []runtime.Object , err error ) {
1093
- var result []runtime.Object
1094
- for _ , p := range m .policies {
1095
- if p .Namespace == m .namespace {
1096
- u , err := runtime .DefaultUnstructuredConverter .ToUnstructured (p )
1097
- if err != nil {
1098
- return nil , err
1099
- }
1100
- result = append (result , & unstructured.Unstructured {Object : u })
1101
- }
1102
- }
1103
- return result , nil
1104
- }
1105
-
1106
- func (m * mockGenericNamespaceLister ) Get (name string ) (runtime.Object , error ) {
1107
- for _ , p := range m .policies {
1108
- if p .Name == name && p .Namespace == m .namespace {
1109
- u , err := runtime .DefaultUnstructuredConverter .ToUnstructured (p )
1110
- if err != nil {
1111
- return nil , err
1112
- }
1113
- return & unstructured.Unstructured {Object : u }, nil
1114
- }
1115
- }
1116
- return nil , nil
1117
- }
1118
-
1119
- // mockClusterPropagationPolicyLister is a mock implementation of the ClusterPropagationPolicyLister
1120
- type mockClusterPropagationPolicyLister struct {
1121
- policies []* policyv1alpha1.ClusterPropagationPolicy
1122
- }
1123
-
1124
- func (m * mockClusterPropagationPolicyLister ) List (_ labels.Selector ) (ret []runtime.Object , err error ) {
1125
- var result []runtime.Object
1126
- for _ , p := range m .policies {
1127
- u , err := runtime .DefaultUnstructuredConverter .ToUnstructured (p )
1128
- if err != nil {
1129
- return nil , err
1130
- }
1131
- result = append (result , & unstructured.Unstructured {Object : u })
1132
- }
1133
- return result , nil
1134
- }
1135
-
1136
- func (m * mockClusterPropagationPolicyLister ) Get (name string ) (runtime.Object , error ) {
1137
- for _ , p := range m .policies {
1138
- if p .Name == name {
1139
- u , err := runtime .DefaultUnstructuredConverter .ToUnstructured (p )
1140
- if err != nil {
1141
- return nil , err
1142
- }
1143
- return & unstructured.Unstructured {Object : u }, nil
1144
- }
1145
- }
1146
- return nil , nil
1147
- }
1148
-
1149
- func (m * mockClusterPropagationPolicyLister ) ByNamespace (_ string ) cache.GenericNamespaceLister {
1150
- return nil // ClusterPropagationPolicies are not namespaced
1151
- }
1152
-
1153
1044
// mockResourceInterpreter is a mock implementation of the ResourceInterpreter interface
1154
1045
type mockResourceInterpreter struct {}
1155
1046
0 commit comments