Skip to content

Commit

Permalink
OCM-11559 | fix: first check if pool exists
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbranco committed Oct 8, 2024
1 parent 0177b97 commit cc3d4bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 11 additions & 0 deletions cmd/upgrade/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command, argv []string) error {
return fmt.Errorf("Cluster '%s' is not yet ready", clusterKey)
}

if !machinepool.MachinePoolKeyRE.MatchString(machinePoolID) {
return fmt.Errorf("Expected a valid identifier for the machine pool")
}
_, exists, err := r.OCMClient.GetNodePool(cluster.ID(), machinePoolID)
if err != nil {
return fmt.Errorf("Failed to get machine pools for hosted cluster '%s': %v", clusterKey, err)
}
if !exists {
return fmt.Errorf("Machine pool '%s' does not exist for hosted cluster '%s'", machinePoolID, clusterKey)
}

// Enable interactive mode if needed
// We need to specify either both date and time or nothing
if (currentUpgradeScheduling.ScheduleDate != "" && currentUpgradeScheduling.ScheduleTime == "") ||
Expand Down
12 changes: 10 additions & 2 deletions cmd/upgrade/machinepool/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ var _ = Describe("Upgrade machine pool", func() {
err := runWithRuntime(testRuntime.RosaRuntime, Cmd, []string{nodePoolName})
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring(
"Failed to get scheduled upgrades for machine pool 'nodepool85': " +
"Machine pool 'nodepool85' does not exist for hosted cluster 'cluster1'"))
"Machine pool 'nodepool85' does not exist for hosted cluster 'cluster1'"))
})
It("Cluster is ready and there is a scheduled upgraded", func() {
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, nodePoolUpgradePolicy))
_, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Cmd, &[]string{nodePoolName})
Expand All @@ -119,6 +119,7 @@ var _ = Describe("Upgrade machine pool", func() {
It("Succeeds if cluster is ready and there is a scheduled upgraded", func() {
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, nodePoolUpgradePolicy))
_, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Cmd, &[]string{nodePoolName})
Expand All @@ -132,6 +133,7 @@ var _ = Describe("Upgrade machine pool", func() {
Cmd.Flags().Set("interactive", "false")
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, noNodePoolUpgradePolicy))
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Cmd, &[]string{nodePoolName})
Expand All @@ -150,6 +152,7 @@ var _ = Describe("Upgrade machine pool", func() {
Cmd.Flags().Set("interactive", "false")
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, noNodePoolUpgradePolicy))
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Cmd, &[]string{nodePoolName})
Expand All @@ -165,6 +168,7 @@ var _ = Describe("Upgrade machine pool", func() {
Cmd.Flags().Set("interactive", "false")
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, noNodePoolUpgradePolicy))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, ""))
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Expand All @@ -180,6 +184,7 @@ var _ = Describe("Upgrade machine pool", func() {
Cmd.Flags().Set("interactive", "false")
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, noNodePoolUpgradePolicy))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, ""))
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Expand All @@ -195,6 +200,7 @@ var _ = Describe("Upgrade machine pool", func() {
Cmd.Flags().Set("interactive", "false")
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, noNodePoolUpgradePolicy))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusBadRequest, "an error"))
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Expand All @@ -213,6 +219,7 @@ var _ = Describe("Upgrade machine pool", func() {
Cmd.Flags().Set("interactive", "false")
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, noNodePoolUpgradePolicy))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, ""))
_, _, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Expand All @@ -229,6 +236,7 @@ var _ = Describe("Upgrade machine pool", func() {
Cmd.Flags().Set("interactive", "false")
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, test.FormatResource(nodePool)))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, noNodePoolUpgradePolicy))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, ""))
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime,
Expand Down

0 comments on commit cc3d4bf

Please sign in to comment.