|
| 1 | +package imagemirror |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" |
| 10 | + . "github.com/openshift-online/ocm-sdk-go/testing" |
| 11 | + |
| 12 | + "github.com/openshift/rosa/pkg/test" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + clusterId = "24vf9iitg3p6tlml88iml6j6mu095mh8" |
| 17 | +) |
| 18 | + |
| 19 | +var _ = Describe("Create image mirror", func() { |
| 20 | + Context("Create image mirror command", func() { |
| 21 | + mockHCPClusterReady := test.MockCluster(func(c *cmv1.ClusterBuilder) { |
| 22 | + c.AWS(cmv1.NewAWS().SubnetIDs("subnet-0b761d44d3d9a4663", "subnet-0f87f640e56934cbc")) |
| 23 | + c.Region(cmv1.NewCloudRegion().ID("us-east-1")) |
| 24 | + c.State(cmv1.ClusterStateReady) |
| 25 | + c.Hypershift(cmv1.NewHypershift().Enabled(true)) |
| 26 | + }) |
| 27 | + |
| 28 | + mockClassicCluster := test.MockCluster(func(c *cmv1.ClusterBuilder) { |
| 29 | + c.AWS(cmv1.NewAWS().SubnetIDs("subnet-0b761d44d3d9a4663", "subnet-0f87f640e56934cbc")) |
| 30 | + c.Region(cmv1.NewCloudRegion().ID("us-east-1")) |
| 31 | + c.State(cmv1.ClusterStateReady) |
| 32 | + c.Hypershift(cmv1.NewHypershift().Enabled(false)) |
| 33 | + }) |
| 34 | + |
| 35 | + mockClusterNotReady := test.MockCluster(func(c *cmv1.ClusterBuilder) { |
| 36 | + c.AWS(cmv1.NewAWS().SubnetIDs("subnet-0b761d44d3d9a4663", "subnet-0f87f640e56934cbc")) |
| 37 | + c.Region(cmv1.NewCloudRegion().ID("us-east-1")) |
| 38 | + c.State(cmv1.ClusterStateInstalling) |
| 39 | + c.Hypershift(cmv1.NewHypershift().Enabled(true)) |
| 40 | + }) |
| 41 | + |
| 42 | + hcpClusterReady := test.FormatClusterList([]*cmv1.Cluster{mockHCPClusterReady}) |
| 43 | + classicClusterReady := test.FormatClusterList([]*cmv1.Cluster{mockClassicCluster}) |
| 44 | + clusterNotReady := test.FormatClusterList([]*cmv1.Cluster{mockClusterNotReady}) |
| 45 | + |
| 46 | + var t *test.TestingRuntime |
| 47 | + |
| 48 | + BeforeEach(func() { |
| 49 | + t = test.NewTestRuntime() |
| 50 | + }) |
| 51 | + |
| 52 | + Context("Success scenarios", func() { |
| 53 | + It("Creates image mirror successfully with all required parameters", func() { |
| 54 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hcpClusterReady)) |
| 55 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusCreated, formatCreatedImageMirror())) |
| 56 | + options := NewCreateImageMirrorOptions() |
| 57 | + options.Args().Source = "registry.redhat.io" |
| 58 | + options.Args().Mirrors = []string{"mirror.example.com", "backup.example.com"} |
| 59 | + runner := CreateImageMirrorRunner(options) |
| 60 | + err := t.StdOutReader.Record() |
| 61 | + Expect(err).ToNot(HaveOccurred()) |
| 62 | + cmd := NewCreateImageMirrorCommand() |
| 63 | + err = cmd.Flag("cluster").Value.Set(clusterId) |
| 64 | + Expect(err).ToNot(HaveOccurred()) |
| 65 | + err = runner(context.Background(), t.RosaRuntime, cmd, []string{}) |
| 66 | + Expect(err).ToNot(HaveOccurred()) |
| 67 | + stdout, err := t.StdOutReader.Read() |
| 68 | + Expect(err).ToNot(HaveOccurred()) |
| 69 | + Expect(stdout).To(ContainSubstring("Image mirror with ID 'test-mirror-123' has been created on cluster")) |
| 70 | + Expect(stdout).To(ContainSubstring("Source: registry.redhat.io")) |
| 71 | + Expect(stdout).To(ContainSubstring("Mirrors: [mirror.example.com backup.example.com]")) |
| 72 | + }) |
| 73 | + |
| 74 | + It("Creates image mirror with custom type", func() { |
| 75 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hcpClusterReady)) |
| 76 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusCreated, formatCreatedImageMirror())) |
| 77 | + options := NewCreateImageMirrorOptions() |
| 78 | + options.Args().Type = "digest" |
| 79 | + options.Args().Source = "quay.io/openshift" |
| 80 | + options.Args().Mirrors = []string{"internal.corp.com/openshift"} |
| 81 | + runner := CreateImageMirrorRunner(options) |
| 82 | + cmd := NewCreateImageMirrorCommand() |
| 83 | + err := cmd.Flag("cluster").Value.Set(clusterId) |
| 84 | + Expect(err).ToNot(HaveOccurred()) |
| 85 | + err = runner(context.Background(), t.RosaRuntime, cmd, []string{}) |
| 86 | + Expect(err).ToNot(HaveOccurred()) |
| 87 | + }) |
| 88 | + |
| 89 | + It("Creates image mirror with single mirror", func() { |
| 90 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hcpClusterReady)) |
| 91 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusCreated, formatCreatedImageMirror())) |
| 92 | + options := NewCreateImageMirrorOptions() |
| 93 | + options.Args().Source = "docker.io/library" |
| 94 | + options.Args().Mirrors = []string{"mirror.company.com/dockerhub"} |
| 95 | + runner := CreateImageMirrorRunner(options) |
| 96 | + cmd := NewCreateImageMirrorCommand() |
| 97 | + err := cmd.Flag("cluster").Value.Set(clusterId) |
| 98 | + Expect(err).ToNot(HaveOccurred()) |
| 99 | + err = runner(context.Background(), t.RosaRuntime, cmd, []string{}) |
| 100 | + Expect(err).ToNot(HaveOccurred()) |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + Context("Validation error scenarios", func() { |
| 105 | + It("Returns error when cluster is not ready", func() { |
| 106 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, clusterNotReady)) |
| 107 | + options := NewCreateImageMirrorOptions() |
| 108 | + options.Args().Source = "registry.redhat.io" |
| 109 | + options.Args().Mirrors = []string{"mirror.example.com"} |
| 110 | + runner := CreateImageMirrorRunner(options) |
| 111 | + cmd := NewCreateImageMirrorCommand() |
| 112 | + err := cmd.Flag("cluster").Value.Set(clusterId) |
| 113 | + Expect(err).ToNot(HaveOccurred()) |
| 114 | + err = runner(context.Background(), t.RosaRuntime, cmd, []string{}) |
| 115 | + Expect(err).To(HaveOccurred()) |
| 116 | + Expect(err.Error()).To(ContainSubstring("is not ready. Image mirrors can only be created on ready clusters")) |
| 117 | + }) |
| 118 | + |
| 119 | + It("Returns error when cluster is not Hosted Control Plane", func() { |
| 120 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, classicClusterReady)) |
| 121 | + options := NewCreateImageMirrorOptions() |
| 122 | + options.Args().Source = "registry.redhat.io" |
| 123 | + options.Args().Mirrors = []string{"mirror.example.com"} |
| 124 | + runner := CreateImageMirrorRunner(options) |
| 125 | + cmd := NewCreateImageMirrorCommand() |
| 126 | + err := cmd.Flag("cluster").Value.Set(clusterId) |
| 127 | + Expect(err).ToNot(HaveOccurred()) |
| 128 | + err = runner(context.Background(), t.RosaRuntime, cmd, []string{}) |
| 129 | + Expect(err).To(HaveOccurred()) |
| 130 | + Expect(err.Error()).To(ContainSubstring("Image mirrors are only supported on Hosted Control Plane clusters")) |
| 131 | + }) |
| 132 | + |
| 133 | + It("Returns error when cluster fetch fails", func() { |
| 134 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusNotFound, "{}")) |
| 135 | + options := NewCreateImageMirrorOptions() |
| 136 | + options.Args().Source = "registry.redhat.io" |
| 137 | + options.Args().Mirrors = []string{"mirror.example.com"} |
| 138 | + runner := CreateImageMirrorRunner(options) |
| 139 | + cmd := NewCreateImageMirrorCommand() |
| 140 | + err := cmd.Flag("cluster").Value.Set("nonexistent-cluster") |
| 141 | + Expect(err).ToNot(HaveOccurred()) |
| 142 | + err = runner(context.Background(), t.RosaRuntime, cmd, []string{}) |
| 143 | + Expect(err).To(HaveOccurred()) |
| 144 | + Expect(err.Error()).To(ContainSubstring("status is 404")) |
| 145 | + }) |
| 146 | + |
| 147 | + It("Returns error when CreateImageMirror API call fails", func() { |
| 148 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hcpClusterReady)) |
| 149 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusInternalServerError, "{}")) |
| 150 | + options := NewCreateImageMirrorOptions() |
| 151 | + options.Args().Source = "registry.redhat.io" |
| 152 | + options.Args().Mirrors = []string{"mirror.example.com"} |
| 153 | + runner := CreateImageMirrorRunner(options) |
| 154 | + cmd := NewCreateImageMirrorCommand() |
| 155 | + err := cmd.Flag("cluster").Value.Set(clusterId) |
| 156 | + Expect(err).ToNot(HaveOccurred()) |
| 157 | + err = runner(context.Background(), t.RosaRuntime, cmd, []string{}) |
| 158 | + Expect(err).To(HaveOccurred()) |
| 159 | + Expect(err.Error()).To(ContainSubstring("Failed to create image mirror")) |
| 160 | + }) |
| 161 | + }) |
| 162 | + |
| 163 | + Context("Runtime validation", func() { |
| 164 | + It("Returns error when mirrors array is empty", func() { |
| 165 | + // Test the runtime validation that checks if mirrors slice is empty |
| 166 | + t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hcpClusterReady)) |
| 167 | + options := NewCreateImageMirrorOptions() |
| 168 | + options.Args().Source = "registry.redhat.io" |
| 169 | + options.Args().Mirrors = []string{} // Empty array |
| 170 | + runner := CreateImageMirrorRunner(options) |
| 171 | + cmd := NewCreateImageMirrorCommand() |
| 172 | + err := cmd.Flag("cluster").Value.Set(clusterId) |
| 173 | + Expect(err).ToNot(HaveOccurred()) |
| 174 | + err = runner(context.Background(), t.RosaRuntime, cmd, []string{}) |
| 175 | + Expect(err).To(HaveOccurred()) |
| 176 | + Expect(err.Error()).To(ContainSubstring("At least one mirror registry must be specified")) |
| 177 | + }) |
| 178 | + |
| 179 | + It("Has default type as digest", func() { |
| 180 | + cmd := NewCreateImageMirrorCommand() |
| 181 | + typeFlag := cmd.Flag("type") |
| 182 | + Expect(typeFlag.DefValue).To(Equal("digest")) |
| 183 | + }) |
| 184 | + }) |
| 185 | + |
| 186 | + Context("Command structure", func() { |
| 187 | + It("Has correct command properties", func() { |
| 188 | + cmd := NewCreateImageMirrorCommand() |
| 189 | + Expect(cmd.Use).To(Equal("image-mirror")) |
| 190 | + Expect(cmd.Short).To(Equal("Create image mirror for a cluster")) |
| 191 | + Expect(cmd.Aliases).To(ContainElement("image-mirrors")) |
| 192 | + Expect(cmd.Args).ToNot(BeNil()) |
| 193 | + }) |
| 194 | + |
| 195 | + It("Has expected flags", func() { |
| 196 | + cmd := NewCreateImageMirrorCommand() |
| 197 | + flags := []string{"cluster", "type", "source", "mirrors", "profile", "region"} |
| 198 | + for _, flagName := range flags { |
| 199 | + flag := cmd.Flag(flagName) |
| 200 | + Expect(flag).ToNot(BeNil(), "Flag %s should exist", flagName) |
| 201 | + } |
| 202 | + }) |
| 203 | + }) |
| 204 | + }) |
| 205 | +}) |
| 206 | + |
| 207 | +// formatCreatedImageMirror simulates the response from creating an image mirror |
| 208 | +func formatCreatedImageMirror() string { |
| 209 | + imageMirror, err := cmv1.NewImageMirror(). |
| 210 | + ID("test-mirror-123"). |
| 211 | + Type("digest"). |
| 212 | + Source("registry.redhat.io"). |
| 213 | + Mirrors("mirror.example.com", "backup.example.com"). |
| 214 | + Build() |
| 215 | + Expect(err).ToNot(HaveOccurred()) |
| 216 | + return test.FormatResource(imageMirror) |
| 217 | +} |
0 commit comments