Merge pull request #6106 from ctripcloud/detector-test

remove mockResourceDetector
This commit is contained in:
karmada-bot 2025-02-11 09:40:07 +08:00 committed by GitHub
commit 7cea9dd66f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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,7 +942,6 @@ 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() != "" {
@ -1019,6 +960,7 @@ func TestApplyClusterPolicy(t *testing.T) {
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)
} }
}
}) })
} }
} }
@ -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