Skip to content

Commit

Permalink
Add missing sync tests to cover all source objects.
Browse files Browse the repository at this point in the history
Reorder tests for consistency (automations first, then sources).
  • Loading branch information
opudrovs committed Jul 21, 2023
1 parent c1beed9 commit a5028f8
Showing 1 changed file with 103 additions and 22 deletions.
125 changes: 103 additions & 22 deletions core/server/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,41 @@ func TestSync(t *testing.T) {
helmRepo := makeHelmRepo(name, *ns)
hr := makeHelmRelease(name, *ns, helmRepo, chart)

g.Expect(k.Create(ctx, gitRepo)).Should(Succeed())
bucket := makeBucket(name, *ns)
ociRepo := makeOCIRepo(name, *ns)

g.Expect(k.Create(ctx, kust)).Should(Succeed())
g.Expect(k.Create(ctx, hr)).Should(Succeed())

g.Expect(k.Create(ctx, bucket)).Should(Succeed())
g.Expect(k.Create(ctx, chart)).Should(Succeed())
g.Expect(k.Create(ctx, helmRepo)).Should(Succeed())
g.Expect(k.Create(ctx, hr)).Should(Succeed())
g.Expect(k.Create(ctx, gitRepo)).Should(Succeed())
g.Expect(k.Create(ctx, ociRepo)).Should(Succeed())

tests := []struct {
name string
msg *pb.SyncFluxObjectRequest
automation fluxsync.Automation
automation fluxsync.Reconcilable
source fluxsync.Reconcilable
}{{
name: "helm release no source",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: helmv2.HelmReleaseKind}},
WithSource: false,
},
automation: fluxsync.HelmReleaseAdapter{HelmRelease: hr},
}, {
name: "helm release with source",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: helmv2.HelmReleaseKind}},
WithSource: true,
},
automation: fluxsync.HelmReleaseAdapter{HelmRelease: hr},
source: fluxsync.NewReconcileable(helmRepo),
}, {
name: "kustomization no source",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Expand All @@ -77,34 +100,56 @@ func TestSync(t *testing.T) {
automation: fluxsync.KustomizationAdapter{Kustomization: kust},
source: fluxsync.NewReconcileable(gitRepo),
}, {
name: "helm release no source",
name: "gitrepository",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: helmv2.HelmReleaseKind}},
Kind: sourcev1.GitRepositoryKind}},
WithSource: false,
},
automation: fluxsync.HelmReleaseAdapter{HelmRelease: hr},
automation: fluxsync.GitRepositoryAdapter{GitRepository: gitRepo},
}, {
name: "helm release with source",
name: "bucket",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: sourcev1.BucketKind}},
WithSource: false,
},
automation: fluxsync.BucketAdapter{Bucket: bucket},
}, {
name: "helmchart",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: sourcev1.HelmChartKind}},
WithSource: false,
},
automation: fluxsync.HelmChartAdapter{HelmChart: chart},
}, {
name: "helmrepository",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: sourcev1.HelmRepositoryKind}},
WithSource: false,
},
automation: fluxsync.HelmRepositoryAdapter{HelmRepository: helmRepo},
}, {
name: "ocirepository",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: sourcev1.OCIRepositoryKind}},
WithSource: false,
},
automation: fluxsync.OCIRepositoryAdapter{OCIRepository: ociRepo},
}, {
name: "multiple objects",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: helmv2.HelmReleaseKind}, {ClusterName: "Default",
Kind: helmv2.HelmReleaseKind}},
WithSource: true,
},
automation: fluxsync.HelmReleaseAdapter{HelmRelease: hr},
source: fluxsync.NewReconcileable(helmRepo),
},
{
name: "multiple objects",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: helmv2.HelmReleaseKind}, {ClusterName: "Default",
Kind: helmv2.HelmReleaseKind}},
WithSource: true,
},
automation: fluxsync.HelmReleaseAdapter{HelmRelease: hr},
source: fluxsync.NewReconcileable(helmRepo),
}}
}}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -161,14 +206,15 @@ func TestSync(t *testing.T) {

func simulateReconcile(ctx context.Context, k client.Client, name types.NamespacedName, o client.Object) error {
switch obj := o.(type) {
case *sourcev1.GitRepository:
case *helmv2.HelmRelease:
if err := k.Get(ctx, name, obj); err != nil {
return err
}

obj.Status.SetLastHandledReconcileRequest(time.Now().Format(time.RFC3339Nano))

return k.Status().Update(ctx, obj)

case *kustomizev1.Kustomization:
if err := k.Get(ctx, name, obj); err != nil {
return err
Expand All @@ -178,7 +224,7 @@ func simulateReconcile(ctx context.Context, k client.Client, name types.Namespac

return k.Status().Update(ctx, obj)

case *sourcev1.HelmRepository:
case *sourcev1.Bucket:
if err := k.Get(ctx, name, obj); err != nil {
return err
}
Expand All @@ -187,7 +233,7 @@ func simulateReconcile(ctx context.Context, k client.Client, name types.Namespac

return k.Status().Update(ctx, obj)

case *helmv2.HelmRelease:
case *sourcev1.GitRepository:
if err := k.Get(ctx, name, obj); err != nil {
return err
}
Expand All @@ -203,6 +249,24 @@ func simulateReconcile(ctx context.Context, k client.Client, name types.Namespac

obj.Status.SetLastHandledReconcileRequest(time.Now().Format(time.RFC3339Nano))

return k.Status().Update(ctx, obj)

case *sourcev1.HelmRepository:
if err := k.Get(ctx, name, obj); err != nil {
return err
}

obj.Status.SetLastHandledReconcileRequest(time.Now().Format(time.RFC3339Nano))

return k.Status().Update(ctx, obj)

case *sourcev1.OCIRepository:
if err := k.Get(ctx, name, obj); err != nil {
return err
}

obj.Status.SetLastHandledReconcileRequest(time.Now().Format(time.RFC3339Nano))

return k.Status().Update(ctx, obj)
}

Expand Down Expand Up @@ -331,3 +395,20 @@ func makeHelmRelease(name string, ns corev1.Namespace, repo *sourcev1.HelmReposi
},
}
}

func makeOCIRepo(name string, ns corev1.Namespace) *sourcev1.OCIRepository {
return &sourcev1.OCIRepository{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns.Name,
},
Spec: sourcev1.OCIRepositorySpec{
URL: "oci://ghcr.io/some/chart",
},
Status: sourcev1.OCIRepositoryStatus{
ReconcileRequestStatus: meta.ReconcileRequestStatus{
LastHandledReconcileAt: time.Now().Format(time.RFC3339Nano),
},
},
}
}

0 comments on commit a5028f8

Please sign in to comment.