Skip to content

Commit 1ff3b58

Browse files
committed
Add managedosimage controller tests
Signed-off-by: Andrea Mazzotti <[email protected]>
1 parent a3aa877 commit 1ff3b58

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

controllers/managedosimage_controller.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,6 @@ func (r *ManagedOSImageReconciler) objToFleetBundleResources(objs []runtime.Obje
521521
Name: filename,
522522
Content: string(data),
523523
})
524-
525-
fmt.Printf("FILENAMEEE: %s\n", filename)
526-
fmt.Printf("DATAAA: %s\n", string(data))
527-
528524
}
529525
return result, nil
530526
}

controllers/managedosimage_controller_test.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
upgradev1 "github.com/rancher/system-upgrade-controller/pkg/apis/upgrade.cattle.io/v1"
2929
"github.com/rancher/wrangler/v2/pkg/genericcondition"
3030
"github.com/rancher/wrangler/v2/pkg/name"
31-
"gopkg.in/yaml.v3"
3231
corev1 "k8s.io/api/core/v1"
3332
"k8s.io/apimachinery/pkg/api/meta"
3433
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -120,8 +119,9 @@ var _ = Describe("newFleetBundleResources", func() {
120119
},
121120
Spec: elementalv1.ManagedOSVersionSpec{
122121
Type: "container",
123-
UpgradeContainer: &upgradev1.ContainerSpec{
124-
Image: "foo/bar:image",
122+
Metadata: map[string]runtime.RawExtension{
123+
"upgradeImage": {Raw: []byte(`"foo/bar:image"`)},
124+
"displayName": {Raw: []byte(`"Unit Test Image"`)},
125125
},
126126
},
127127
}
@@ -153,8 +153,9 @@ var _ = Describe("newFleetBundleResources", func() {
153153

154154
Expect(bundleResources).To(HaveLen(5))
155155
plan := &upgradev1.Plan{}
156-
Expect(yaml.Unmarshal([]byte(bundleResources[4].Content), plan)).To(Succeed())
157-
Expect(plan.Spec.Upgrade.Image).To(Equal("foo/bar:image"))
156+
Expect(json.Unmarshal([]byte(bundleResources[4].Content), plan)).To(Succeed())
157+
Expect(plan.Spec.Upgrade.Image).To(Equal("foo/bar"))
158+
Expect(plan.Spec.Version).To(Equal("image"))
158159
})
159160

160161
It("return error of managedOSVersion doesn't exist", func() {
@@ -175,6 +176,35 @@ var _ = Describe("newFleetBundleResources", func() {
175176
Expect(bundleResources[3].Name).To(Equal("Secret-cattle-system-os-upgrader-test-name-52e9d8e041f4.yaml"))
176177
Expect(bundleResources[4].Name).To(Equal("Plan-cattle-system-os-upgrader-test-name-8130169de1eb.yaml"))
177178
})
179+
It("should add correlation id and snapshot labels", func() {
180+
wantLabelsEnv := corev1.EnvVar{
181+
Name: "ELEMENTAL_REGISTER_UPGRADE_SNAPSHOT_LABELS",
182+
Value: "managedOSImage=test-name,image=foo/bar:image,managedOSVersion=test",
183+
}
184+
wantCorrelationIDEnv := corev1.EnvVar{
185+
Name: "ELEMENTAL_REGISTER_UPGRADE_CORRELATION_ID",
186+
Value: "6c18cee8fa6945d5661a39aee878c8035780b5d2e62e1509d154a2bd",
187+
}
188+
189+
Expect(r.Create(ctx, managedOSVersion)).To(Succeed())
190+
managedOSImage.Spec.ManagedOSVersionName = "test"
191+
bundleResources, err := r.newFleetBundleResources(ctx, managedOSImage)
192+
Expect(err).ToNot(HaveOccurred())
193+
194+
Expect(bundleResources).To(HaveLen(5))
195+
plan := &upgradev1.Plan{}
196+
Expect(json.Unmarshal([]byte(bundleResources[4].Content), plan)).To(Succeed())
197+
Expect(plan.Spec.Upgrade.Env).Should(ContainElement(wantLabelsEnv))
198+
Expect(plan.Spec.Upgrade.Env).Should(ContainElement(wantCorrelationIDEnv))
199+
200+
value, found := plan.Labels[elementalv1.ElementalUpgradeCorrelationIDLabel]
201+
Expect(found).Should(BeTrue(), "Plan should have correlation ID as label")
202+
Expect(value).Should(Equal("6c18cee8fa6945d5661a39aee878c8035780b5d2e62e1509d154a2bd"))
203+
204+
value, found = managedOSImage.Labels[elementalv1.ElementalUpgradeCorrelationIDLabel]
205+
Expect(found).Should(BeTrue(), "ManagedOSImage should have correlation ID as label")
206+
Expect(value).Should(Equal("6c18cee8fa6945d5661a39aee878c8035780b5d2e62e1509d154a2bd"))
207+
})
178208
})
179209

180210
var _ = Describe("createFleetBundle", func() {

0 commit comments

Comments
 (0)