remove mockResourceDetector

Signed-off-by: zach593 <zach_li@outlook.com>
This commit is contained in:
zach593 2025-02-10 22:52:17 +08:00
parent 2be704299d
commit 33a9490f1d
1 changed files with 31 additions and 126 deletions

View File

@ -841,36 +841,15 @@ func TestApplyPolicy(t *testing.T) {
fakeRecorder := record.NewFakeRecorder(10) fakeRecorder := record.NewFakeRecorder(10)
fakeDynamicClient := dynamicfake.NewSimpleDynamicClient(scheme) fakeDynamicClient := dynamicfake.NewSimpleDynamicClient(scheme)
mockDetector := &mockResourceDetector{ d := &ResourceDetector{
ResourceDetector: ResourceDetector{ Client: fakeClient,
Client: fakeClient, DynamicClient: fakeDynamicClient,
DynamicClient: fakeDynamicClient, EventRecorder: fakeRecorder,
EventRecorder: fakeRecorder, ResourceInterpreter: &mockResourceInterpreter{},
ResourceInterpreter: &mockResourceInterpreter{}, RESTMapper: &mockRESTMapper{},
RESTMapper: &mockRESTMapper{},
},
mockClaimPolicyForObject: func(_ *unstructured.Unstructured, _ *policyv1alpha1.PropagationPolicy) (string, error) {
return "mocked-policy-id", nil
},
mockBuildResourceBinding: func(object *unstructured.Unstructured, _, _ map[string]string, _ *policyv1alpha1.PropagationSpec) (*workv1alpha2.ResourceBinding, error) {
return &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: object.GetName() + "-" + strings.ToLower(object.GetKind()),
Namespace: object.GetNamespace(),
},
Spec: workv1alpha2.ResourceBindingSpec{
Resource: workv1alpha2.ObjectReference{
APIVersion: object.GetAPIVersion(),
Kind: object.GetKind(),
Name: object.GetName(),
Namespace: object.GetNamespace(),
},
},
}, nil
},
} }
err := mockDetector.ApplyPolicy(tt.object, keys.ClusterWideKey{}, tt.resourceChangeByKarmada, tt.policy) err := d.ApplyPolicy(tt.object, keys.ClusterWideKey{}, tt.resourceChangeByKarmada, tt.policy)
if tt.expectError { if tt.expectError {
assert.Error(t, err) assert.Error(t, err)
@ -949,49 +928,12 @@ func TestApplyClusterPolicy(t *testing.T) {
fakeRecorder := record.NewFakeRecorder(10) fakeRecorder := record.NewFakeRecorder(10)
fakeDynamicClient := dynamicfake.NewSimpleDynamicClient(scheme) fakeDynamicClient := dynamicfake.NewSimpleDynamicClient(scheme)
d := &mockResourceDetector{ d := &ResourceDetector{
ResourceDetector: ResourceDetector{ Client: fakeClient,
Client: fakeClient, DynamicClient: fakeDynamicClient,
DynamicClient: fakeDynamicClient, EventRecorder: fakeRecorder,
EventRecorder: fakeRecorder, ResourceInterpreter: &mockResourceInterpreter{},
ResourceInterpreter: &mockResourceInterpreter{}, RESTMapper: &mockRESTMapper{},
RESTMapper: &mockRESTMapper{},
},
mockClaimClusterPolicyForObject: func(_ *unstructured.Unstructured, _ *policyv1alpha1.ClusterPropagationPolicy) (string, error) {
return "mocked-cluster-policy-id", nil
},
mockBuildResourceBinding: func(object *unstructured.Unstructured, _, _ map[string]string, _ *policyv1alpha1.PropagationSpec) (*workv1alpha2.ResourceBinding, error) {
binding := &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: object.GetName() + "-" + strings.ToLower(object.GetKind()),
Namespace: object.GetNamespace(),
},
Spec: workv1alpha2.ResourceBindingSpec{
Resource: workv1alpha2.ObjectReference{
APIVersion: object.GetAPIVersion(),
Kind: object.GetKind(),
Name: object.GetName(),
Namespace: object.GetNamespace(),
},
},
}
return binding, nil
},
mockBuildClusterResourceBinding: func(object *unstructured.Unstructured, _, _ map[string]string, _ *policyv1alpha1.PropagationSpec) (*workv1alpha2.ClusterResourceBinding, error) {
binding := &workv1alpha2.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: object.GetName() + "-" + strings.ToLower(object.GetKind()),
},
Spec: workv1alpha2.ResourceBindingSpec{
Resource: workv1alpha2.ObjectReference{
APIVersion: object.GetAPIVersion(),
Kind: object.GetKind(),
Name: object.GetName(),
},
},
}
return binding, nil
},
} }
err := d.ApplyClusterPolicy(tt.object, keys.ClusterWideKey{}, tt.resourceChangeByKarmada, tt.policy) err := d.ApplyClusterPolicy(tt.object, keys.ClusterWideKey{}, tt.resourceChangeByKarmada, tt.policy)
@ -1000,30 +942,30 @@ func TestApplyClusterPolicy(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
} else { } else {
assert.NoError(t, err) assert.NoError(t, err)
}
// Check if ResourceBinding or ClusterResourceBinding was created // Check if ResourceBinding or ClusterResourceBinding was created
if tt.object.GetNamespace() != "" { if tt.object.GetNamespace() != "" {
binding := &workv1alpha2.ResourceBinding{} binding := &workv1alpha2.ResourceBinding{}
err = fakeClient.Get(context.TODO(), client.ObjectKey{ err = fakeClient.Get(context.TODO(), client.ObjectKey{
Namespace: tt.object.GetNamespace(), Namespace: tt.object.GetNamespace(),
Name: tt.object.GetName() + "-" + strings.ToLower(tt.object.GetKind()), Name: tt.object.GetName() + "-" + strings.ToLower(tt.object.GetKind()),
}, binding) }, binding)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, tt.object.GetName(), binding.Spec.Resource.Name) assert.Equal(t, tt.object.GetName(), binding.Spec.Resource.Name)
} else { } else {
binding := &workv1alpha2.ClusterResourceBinding{} binding := &workv1alpha2.ClusterResourceBinding{}
err = fakeClient.Get(context.TODO(), client.ObjectKey{ err = fakeClient.Get(context.TODO(), client.ObjectKey{
Name: tt.object.GetName() + "-" + strings.ToLower(tt.object.GetKind()), Name: tt.object.GetName() + "-" + strings.ToLower(tt.object.GetKind()),
}, binding) }, binding)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, tt.object.GetName(), binding.Spec.Resource.Name) assert.Equal(t, tt.object.GetName(), binding.Spec.Resource.Name)
}
} }
}) })
} }
} }
//Helper Functions // Helper Functions
// setupTestScheme creates a runtime scheme with necessary types for testing // setupTestScheme creates a runtime scheme with necessary types for testing
func setupTestScheme() *runtime.Scheme { func setupTestScheme() *runtime.Scheme {
@ -1096,43 +1038,6 @@ func (m *mockRESTMapper) ResourceSingularizer(resource string) (string, error) {
return resource, nil return resource, nil
} }
// mockResourceDetector is a mock implementation of ResourceDetector
type mockResourceDetector struct {
ResourceDetector
mockClaimPolicyForObject func(object *unstructured.Unstructured, policy *policyv1alpha1.PropagationPolicy) (string, error)
mockClaimClusterPolicyForObject func(object *unstructured.Unstructured, policy *policyv1alpha1.ClusterPropagationPolicy) (string, error)
mockBuildResourceBinding func(object *unstructured.Unstructured, labels, annotations map[string]string, spec *policyv1alpha1.PropagationSpec) (*workv1alpha2.ResourceBinding, error)
mockBuildClusterResourceBinding func(object *unstructured.Unstructured, labels, annotations map[string]string, spec *policyv1alpha1.PropagationSpec) (*workv1alpha2.ClusterResourceBinding, error)
}
func (m *mockResourceDetector) ClaimPolicyForObject(object *unstructured.Unstructured, policy *policyv1alpha1.PropagationPolicy) (string, error) {
if m.mockClaimPolicyForObject != nil {
return m.mockClaimPolicyForObject(object, policy)
}
return "", nil
}
func (m *mockResourceDetector) ClaimClusterPolicyForObject(object *unstructured.Unstructured, policy *policyv1alpha1.ClusterPropagationPolicy) (string, error) {
if m.mockClaimClusterPolicyForObject != nil {
return m.mockClaimClusterPolicyForObject(object, policy)
}
return "", nil
}
func (m *mockResourceDetector) BuildResourceBinding(object *unstructured.Unstructured, labels, annotations map[string]string, spec *policyv1alpha1.PropagationSpec) (*workv1alpha2.ResourceBinding, error) {
if m.mockBuildResourceBinding != nil {
return m.mockBuildResourceBinding(object, labels, annotations, spec)
}
return &workv1alpha2.ResourceBinding{}, nil
}
func (m *mockResourceDetector) BuildClusterResourceBinding(object *unstructured.Unstructured, labels, annotations map[string]string, spec *policyv1alpha1.PropagationSpec) (*workv1alpha2.ClusterResourceBinding, error) {
if m.mockBuildClusterResourceBinding != nil {
return m.mockBuildClusterResourceBinding(object, labels, annotations, spec)
}
return &workv1alpha2.ClusterResourceBinding{}, nil
}
// mockPropagationPolicyLister is a mock implementation of the PropagationPolicyLister // mockPropagationPolicyLister is a mock implementation of the PropagationPolicyLister
type mockPropagationPolicyLister struct { type mockPropagationPolicyLister struct {
policies []*policyv1alpha1.PropagationPolicy policies []*policyv1alpha1.PropagationPolicy