Skip to content

Commit

Permalink
Add managedosimage controller tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Mazzotti <[email protected]>
  • Loading branch information
anmazzotti committed Oct 30, 2024
1 parent 7233bb7 commit 6dbaf10
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
4 changes: 0 additions & 4 deletions controllers/managedosimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,6 @@ func (r *ManagedOSImageReconciler) objToFleetBundleResources(objs []runtime.Obje
Name: filename,
Content: string(data),
})

fmt.Printf("FILENAMEEE: %s\n", filename)
fmt.Printf("DATAAA: %s\n", string(data))

}
return result, nil
}
Expand Down
40 changes: 35 additions & 5 deletions controllers/managedosimage_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
upgradev1 "github.com/rancher/system-upgrade-controller/pkg/apis/upgrade.cattle.io/v1"
"github.com/rancher/wrangler/v2/pkg/genericcondition"
"github.com/rancher/wrangler/v2/pkg/name"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -120,8 +119,9 @@ var _ = Describe("newFleetBundleResources", func() {
},
Spec: elementalv1.ManagedOSVersionSpec{
Type: "container",
UpgradeContainer: &upgradev1.ContainerSpec{
Image: "foo/bar:image",
Metadata: map[string]runtime.RawExtension{
"upgradeImage": {Raw: []byte(`"foo/bar:image"`)},
"displayName": {Raw: []byte(`"Unit Test Image"`)},
},
},
}
Expand Down Expand Up @@ -153,8 +153,9 @@ var _ = Describe("newFleetBundleResources", func() {

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

It("return error of managedOSVersion doesn't exist", func() {
Expand All @@ -175,6 +176,35 @@ var _ = Describe("newFleetBundleResources", func() {
Expect(bundleResources[3].Name).To(Equal("Secret-cattle-system-os-upgrader-test-name-52e9d8e041f4.yaml"))
Expect(bundleResources[4].Name).To(Equal("Plan-cattle-system-os-upgrader-test-name-8130169de1eb.yaml"))
})
It("should add correlation id and snapshot labels", func() {
wantLabelsEnv := corev1.EnvVar{
Name: "ELEMENTAL_REGISTER_UPGRADE_SNAPSHOT_LABELS",
Value: "managedOSImage=test-name,image=foo/bar:image,managedOSVersion=test",
}
wantCorrelationIDEnv := corev1.EnvVar{
Name: "ELEMENTAL_REGISTER_UPGRADE_CORRELATION_ID",
Value: "6c18cee8fa6945d5661a39aee878c8035780b5d2e62e1509d154a2bd",
}

Expect(r.Create(ctx, managedOSVersion)).To(Succeed())
managedOSImage.Spec.ManagedOSVersionName = "test"
bundleResources, err := r.newFleetBundleResources(ctx, managedOSImage)
Expect(err).ToNot(HaveOccurred())

Expect(bundleResources).To(HaveLen(5))
plan := &upgradev1.Plan{}
Expect(json.Unmarshal([]byte(bundleResources[4].Content), plan)).To(Succeed())
Expect(plan.Spec.Upgrade.Env).Should(ContainElement(wantLabelsEnv))
Expect(plan.Spec.Upgrade.Env).Should(ContainElement(wantCorrelationIDEnv))

value, found := plan.Labels[elementalv1.ElementalUpgradeCorrelationIDLabel]
Expect(found).Should(BeTrue(), "Plan should have correlation ID as label")
Expect(value).Should(Equal("6c18cee8fa6945d5661a39aee878c8035780b5d2e62e1509d154a2bd"))

value, found = managedOSImage.Labels[elementalv1.ElementalUpgradeCorrelationIDLabel]
Expect(found).Should(BeTrue(), "ManagedOSImage should have correlation ID as label")
Expect(value).Should(Equal("6c18cee8fa6945d5661a39aee878c8035780b5d2e62e1509d154a2bd"))
})
})

var _ = Describe("createFleetBundle", func() {
Expand Down

0 comments on commit 6dbaf10

Please sign in to comment.