Skip to content

Commit f68f776

Browse files
committed
add the unit tests for related resources
Signed-off-by: Karol Szwaj <[email protected]> On-behalf-of: @SAP [email protected]
1 parent 779d9ff commit f68f776

File tree

5 files changed

+61
-64
lines changed

5 files changed

+61
-64
lines changed

deploy/crd/kcp.io/syncagent.kcp.io_publishedresources.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ spec:
410410
type: object
411411
type: array
412412
type: object
413+
<<<<<<< HEAD
413414
<<<<<<< HEAD
414415
object:
415416
description: |-
@@ -658,6 +659,8 @@ spec:
658659
description: Optional indicates whether the related resource must be referenced.
659660
type: boolean
660661
>>>>>>> 2f5479b (update codegen)
662+
=======
663+
>>>>>>> 05c1537 (add the unit tests for related resources)
661664
origin:
662665
description: '"service" or "kcp"'
663666
type: string

internal/sync/syncer_related.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,13 @@ import (
4040

4141
func (s *ResourceSyncer) processRelatedResources(log *zap.SugaredLogger, stateStore ObjectStateStore, remote, local syncSide) (requeue bool, err error) {
4242
for _, relatedResource := range s.pubRes.Spec.Related {
43-
if !relatedResource.Optional {
44-
requeue, err := s.processRelatedResource(log.With("identifier", relatedResource.Identifier), stateStore, remote, local, relatedResource)
45-
if err != nil {
46-
return false, fmt.Errorf("failed to process related resource %s: %w", relatedResource.Identifier, err)
47-
}
43+
requeue, err := s.processRelatedResource(log.With("identifier", relatedResource.Identifier), stateStore, remote, local, relatedResource)
44+
if err != nil {
45+
return false, fmt.Errorf("failed to process related resource %s: %w", relatedResource.Identifier, err)
46+
}
4847

49-
if requeue {
50-
return true, nil
51-
}
48+
if requeue {
49+
return true, nil
5250
}
5351
}
5452

internal/sync/syncer_related_test.go

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ import (
3737
"sigs.k8s.io/controller-runtime/pkg/kontext"
3838
)
3939

40+
func newPublishedResources(relatedResources []syncagentv1alpha1.RelatedResourceSpec) *syncagentv1alpha1.PublishedResource {
41+
return &syncagentv1alpha1.PublishedResource{
42+
Spec: syncagentv1alpha1.PublishedResourceSpec{
43+
Resource: syncagentv1alpha1.SourceResourceDescriptor{
44+
APIGroup: dummyv1alpha1.GroupName,
45+
Version: dummyv1alpha1.GroupVersion,
46+
Kind: "NamespacedThing",
47+
},
48+
Projection: &syncagentv1alpha1.ResourceProjection{
49+
Kind: "RemoteThing",
50+
},
51+
Naming: &syncagentv1alpha1.ResourceNaming{
52+
Name: "$remoteClusterName-$remoteName",
53+
},
54+
Related: relatedResources,
55+
},
56+
}
57+
}
58+
4059
func TestSyncerProcessingRelatedResources(t *testing.T) {
4160
const stateNamespace = "kcp-system"
4261

@@ -57,34 +76,12 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
5776

5877
clusterName := logicalcluster.Name("testcluster")
5978

60-
remoteThingPR := &syncagentv1alpha1.PublishedResource{
61-
Spec: syncagentv1alpha1.PublishedResourceSpec{
62-
Resource: syncagentv1alpha1.SourceResourceDescriptor{
63-
APIGroup: dummyv1alpha1.GroupName,
64-
Version: dummyv1alpha1.GroupVersion,
65-
Kind: "NamespacedThing",
66-
},
67-
Projection: &syncagentv1alpha1.ResourceProjection{
68-
Kind: "RemoteThing",
69-
},
70-
// include explicit naming rules to be independent of possible changes to the defaults
71-
Naming: &syncagentv1alpha1.ResourceNaming{
72-
Name: "$remoteClusterName-$remoteName", // Things are Cluster-scoped
73-
},
74-
Related: []syncagentv1alpha1.RelatedResourceSpec{
75-
{
76-
Identifier: "mandatory-credentials",
77-
Origin: "kcp",
78-
Kind: "Secret",
79-
Reference: syncagentv1alpha1.RelatedResourceReference{
80-
Name: syncagentv1alpha1.ResourceLocator{
81-
Path: "metadata.name",
82-
Regex: &syncagentv1alpha1.RegexResourceLocator{
83-
Replacement: "mandatory-credentials",
84-
},
85-
},
86-
},
87-
},
79+
testcases := []testcase{
80+
{
81+
name: "optional related resource does not exist",
82+
remoteAPIGroup: "remote.example.corp",
83+
localCRD: loadCRD("things"),
84+
pubRes: newPublishedResources([]syncagentv1alpha1.RelatedResourceSpec{
8885
{
8986
Identifier: "optional-secret",
9087
Origin: "service",
@@ -97,20 +94,9 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
9794
},
9895
},
9996
},
100-
Optional: true,
10197
},
102-
},
103-
},
104-
}
105-
106-
testcases := []testcase{
107-
{
108-
name: "optional related resource does not exist",
109-
remoteAPIGroup: "remote.example.corp",
110-
localCRD: loadCRD("things"),
111-
pubRes: remoteThingPR,
98+
}),
11299
performRequeues: true,
113-
114100
remoteObject: newUnstructured(&dummyv1alpha1.NamespacedThing{
115101
ObjectMeta: metav1.ObjectMeta{
116102
Name: "my-test-thing",
@@ -173,12 +159,25 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
173159
expectedState: `{"apiVersion":"remote.example.corp/v1alpha1","kind":"RemoteThing","metadata":{"name":"my-test-thing","namespace":"kcp-system"},"spec":{"username":"Colonel Mustard"}}`,
174160
},
175161
{
176-
name: "mandatory related resource does not exist",
177-
remoteAPIGroup: "remote.example.corp",
178-
localCRD: loadCRD("things"),
179-
pubRes: remoteThingPR,
162+
name: "mandatory related resource does not exist",
163+
remoteAPIGroup: "remote.example.corp",
164+
localCRD: loadCRD("things"),
165+
pubRes: newPublishedResources([]syncagentv1alpha1.RelatedResourceSpec{
166+
{
167+
Identifier: "mandatory-credentials",
168+
Origin: "kcp",
169+
Kind: "Secret",
170+
Reference: syncagentv1alpha1.RelatedResourceReference{
171+
Name: syncagentv1alpha1.ResourceLocator{
172+
Path: "metadata.name",
173+
Regex: &syncagentv1alpha1.RegexResourceLocator{
174+
Replacement: "mandatory-credentials",
175+
},
176+
},
177+
},
178+
},
179+
}),
180180
performRequeues: true,
181-
182181
remoteObject: newUnstructured(&dummyv1alpha1.NamespacedThing{
183182
ObjectMeta: metav1.ObjectMeta{
184183
Name: "my-test-thing",

sdk/apis/syncagent/v1alpha1/published_resource.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,6 @@ type RelatedResourceSpec struct {
178178
// Mutation configures optional transformation rules for the related resource.
179179
// Status mutations are only performed when the related resource originates in kcp.
180180
Mutation *ResourceMutationSpec `json:"mutation,omitempty"`
181-
182-
// Optional indicates whether the related resource must be referenced.
183-
Optional bool `json:"optional,omitempty"`
184181
}
185182

186183
// RelatedResourceSource configures how the related resource can be found on the origin side

sdk/applyconfiguration/syncagent/v1alpha1/relatedresourcespec.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)