Skip to content

Commit

Permalink
Add comment markers for the crd-bumper tool
Browse files Browse the repository at this point in the history
This tool uses "marker comments" in the Go code to indicate where generated
code should be placed.  These markers are used the same way `controller-gen`
uses code markers.

Signed-off-by: Dean Roehrich <[email protected]>
  • Loading branch information
roehrich-hpe committed Aug 14, 2024
1 parent 011194b commit 7717b17
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
26 changes: 16 additions & 10 deletions github/cluster-api/util/conversion/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ import (
)

var (
oldGVK = schema.GroupVersionKind{
oldLustreFileSystemGVK = schema.GroupVersionKind{
Group: lusv1beta1.GroupVersion.Group,
Version: "v1old",
Kind: "LustreFileSystem",
}

// +crdbumper:scaffold:gvk
)

func TestMarshalData(t *testing.T) {
g := NewWithT(t)

t.Run("should write source object to destination", func(*testing.T) {
t.Run("LustreFileSystem should write source object to destination", func(*testing.T) {
src := &lusv1beta1.LustreFileSystem{
ObjectMeta: metav1.ObjectMeta{
Name: "test-1",
Expand All @@ -55,7 +57,7 @@ func TestMarshalData(t *testing.T) {
}

dst := &unstructured.Unstructured{}
dst.SetGroupVersionKind(oldGVK)
dst.SetGroupVersionKind(oldLustreFileSystemGVK)
dst.SetName("test-1")

g.Expect(MarshalData(src, dst)).To(Succeed())
Expand All @@ -69,7 +71,7 @@ func TestMarshalData(t *testing.T) {
g.Expect(dst.GetAnnotations()[DataAnnotation]).To(ContainSubstring("/lus/w0"))
})

t.Run("should append the annotation", func(*testing.T) {
t.Run("LustreFileSystem should append the annotation", func(*testing.T) {
src := &lusv1beta1.LustreFileSystem{
ObjectMeta: metav1.ObjectMeta{
Name: "test-1",
Expand All @@ -85,29 +87,31 @@ func TestMarshalData(t *testing.T) {
g.Expect(MarshalData(src, dst)).To(Succeed())
g.Expect(dst.GetAnnotations()).To(HaveLen(2))
})

// +crdbumper:scaffold:marshaldata
}

func TestUnmarshalData(t *testing.T) {
g := NewWithT(t)

t.Run("should return false without errors if annotation doesn't exist", func(*testing.T) {
t.Run("LustreFileSystem should return false without errors if annotation doesn't exist", func(*testing.T) {
src := &lusv1beta1.LustreFileSystem{
ObjectMeta: metav1.ObjectMeta{
Name: "test-1",
},
}
dst := &unstructured.Unstructured{}
dst.SetGroupVersionKind(oldGVK)
dst.SetGroupVersionKind(oldLustreFileSystemGVK)
dst.SetName("test-1")

ok, err := UnmarshalData(src, dst)
g.Expect(ok).To(BeFalse())
g.Expect(err).ToNot(HaveOccurred())
})

t.Run("should return true when a valid annotation with data exists", func(*testing.T) {
t.Run("LustreFileSystem should return true when a valid annotation with data exists", func(*testing.T) {
src := &unstructured.Unstructured{}
src.SetGroupVersionKind(oldGVK)
src.SetGroupVersionKind(oldLustreFileSystemGVK)
src.SetName("test-1")
src.SetAnnotations(map[string]string{
DataAnnotation: "{\"metadata\":{\"name\":\"test-1\",\"creationTimestamp\":null,\"labels\":{\"label1\":\"\"}},\"spec\":{},\"status\":{}}",
Expand All @@ -129,9 +133,9 @@ func TestUnmarshalData(t *testing.T) {
g.Expect(dst.GetAnnotations()).To(BeEmpty())
})

t.Run("should clean the annotation on successful unmarshal", func(*testing.T) {
t.Run("LustreFileSystem should clean the annotation on successful unmarshal", func(*testing.T) {
src := &unstructured.Unstructured{}
src.SetGroupVersionKind(oldGVK)
src.SetGroupVersionKind(oldLustreFileSystemGVK)
src.SetName("test-1")
src.SetAnnotations(map[string]string{
"annotation-1": "",
Expand All @@ -151,6 +155,8 @@ func TestUnmarshalData(t *testing.T) {
g.Expect(src.GetAnnotations()).ToNot(HaveKey(DataAnnotation))
g.Expect(src.GetAnnotations()).To(HaveLen(1))
})

// +crdbumper:scaffold:unmarshaldata
}

// Just touch ginkgo, so it's here to interpret any ginkgo args from
Expand Down
8 changes: 6 additions & 2 deletions internal/controller/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Hewlett Packard Enterprise Development LP
* Copyright 2023-2024 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
Expand Down Expand Up @@ -75,7 +75,7 @@ var _ = Describe("Conversion Webhook Test", func() {
}
})

It("reads LustreFileSystem resource via hub and via spoke", func() {
It("reads LustreFileSystem resource via hub and via spoke v1alpha1", func() {
// Spoke should have annotation.
resSpoke := &lusv1alpha1.LustreFileSystem{}
Eventually(func(g Gomega) {
Expand All @@ -92,5 +92,9 @@ var _ = Describe("Conversion Webhook Test", func() {
g.Expect(anno).To(HaveLen(0))
}).Should(Succeed())
})

// +crdbumper:scaffold:spoketest="lus.LustreFileSystem"
})

// +crdbumper:scaffold:webhooksuitetest
})
4 changes: 3 additions & 1 deletion internal/controller/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Hewlett Packard Enterprise Development LP
* Copyright 2021-2024 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
Expand Down Expand Up @@ -120,6 +120,8 @@ var _ = BeforeSuite(func() {
err = (&lusv1beta1.LustreFileSystem{}).SetupWebhookWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

// +crdbumper:scaffold:builder

err = (&LustreFileSystemReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Expand Down

0 comments on commit 7717b17

Please sign in to comment.