Skip to content

Commit

Permalink
Merge pull request openshift#2054 from hunterkepley/ocm-7259
Browse files Browse the repository at this point in the history
OCM-7259 | feat: E2E delete machinepool cmd tests; new runner
  • Loading branch information
openshift-merge-bot[bot] authored May 23, 2024
2 parents 989119c + bc8dcc5 commit 4f218b4
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 78 deletions.
16 changes: 4 additions & 12 deletions cmd/describe/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,15 @@ func NewDescribeMachinePoolCommand() *cobra.Command {
return cmd
}

func DescribeMachinePoolRunner(userOptions DescribeMachinepoolUserOptions) rosa.CommandRunner {
func DescribeMachinePoolRunner(userOptions *DescribeMachinepoolUserOptions) rosa.CommandRunner {
return func(_ context.Context, runtime *rosa.Runtime, cmd *cobra.Command, argv []string) error {
options := NewDescribeMachinepoolOptions()
// Allow the use also directly the machine pool id as positional parameter
if len(argv) == 1 && !cmd.Flag("machinepool").Changed {
userOptions.machinepool = argv[0]
} else {
err := cmd.ParseFlags(argv)
userOptions.machinepool = cmd.Flag("machinepool").Value.String()
if err != nil {
return fmt.Errorf("Unable to parse flags: %s", err)
}
}
err := options.Bind(userOptions)

err := options.Bind(userOptions, argv)
if err != nil {
return err
}

clusterKey := runtime.GetClusterKey()
cluster := runtime.FetchCluster()
if cluster.State() != cmv1.ClusterStateReady {
Expand Down
69 changes: 50 additions & 19 deletions cmd/describe/machinepool/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ var _ = Describe("Upgrade machine pool", func() {
err := t.StdOutReader.Record()
Expect(err).ToNot(HaveOccurred())
cmd := NewDescribeMachinePoolCommand()
cmd.Flag("cluster").Value.Set(clusterId)
err = cmd.Flag("cluster").Value.Set(clusterId)
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, cmd,
[]string{nodePoolName})
Expect(err).To(BeNil())
Expand All @@ -205,8 +206,11 @@ var _ = Describe("Upgrade machine pool", func() {
runner := DescribeMachinePoolRunner(args)
err := t.StdOutReader.Record()
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, NewDescribeMachinePoolCommand(),
[]string{"--machinepool", nodePoolName, "-c", clusterId})
cmd := NewDescribeMachinePoolCommand()
err = cmd.Flag("cluster").Value.Set(clusterId)
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, cmd,
[]string{"--machinepool", nodePoolName})
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("Machine pool '%s' not found", nodePoolName)))
stdout, err := t.StdOutReader.Read()
Expand All @@ -225,8 +229,10 @@ var _ = Describe("Upgrade machine pool", func() {
args := NewDescribeMachinepoolUserOptions()
args.machinepool = nodePoolName
runner := DescribeMachinePoolRunner(args)
err = runner(context.Background(), t.RosaRuntime, NewDescribeMachinePoolCommand(),
[]string{"--machinepool", nodePoolName, "-c", clusterId})
cmd := NewDescribeMachinePoolCommand()
cmd.Flag("cluster").Value.Set(clusterId)
err = runner(context.Background(), t.RosaRuntime, cmd,
[]string{"--machinepool", nodePoolName})
Expect(err).To(BeNil())
stdout, err := t.StdOutReader.Read()
Expect(err).ToNot(HaveOccurred())
Expand All @@ -244,8 +250,11 @@ var _ = Describe("Upgrade machine pool", func() {
runner := DescribeMachinePoolRunner(args)
err := t.StdOutReader.Record()
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, NewDescribeMachinePoolCommand(),
[]string{"--machinepool", nodePoolName, "-c", clusterId})
cmd := NewDescribeMachinePoolCommand()
err = cmd.Flag("cluster").Value.Set(clusterId)
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, cmd,
[]string{"--machinepool", nodePoolName})
Expect(err).To(BeNil())
stdout, err := t.StdOutReader.Read()
Expect(err).ToNot(HaveOccurred())
Expand All @@ -263,8 +272,13 @@ var _ = Describe("Upgrade machine pool", func() {
runner := DescribeMachinePoolRunner(args)
err := t.StdOutReader.Record()
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, NewDescribeMachinePoolCommand(),
[]string{"--output", "yaml", "--machinepool", nodePoolName, "-c", clusterId})
cmd := NewDescribeMachinePoolCommand()
err = cmd.Flag("cluster").Value.Set(clusterId)
Expect(err).ToNot(HaveOccurred())
err = cmd.Flag("output").Value.Set("yaml")
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, cmd,
[]string{"--machinepool", nodePoolName})
Expect(err).To(BeNil())
stdout, err := t.StdOutReader.Read()
Expect(err).ToNot(HaveOccurred())
Expand All @@ -282,8 +296,11 @@ var _ = Describe("Upgrade machine pool", func() {
runner := DescribeMachinePoolRunner(args)
err := t.StdOutReader.Record()
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, NewDescribeMachinePoolCommand(),
[]string{"--machinepool", nodePoolName, "-c", clusterId})
cmd := NewDescribeMachinePoolCommand()
err = cmd.Flag("cluster").Value.Set(clusterId)
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, cmd,
[]string{"--machinepool", nodePoolName})
Expect(err).To(BeNil())
stdout, err := t.StdOutReader.Read()
Expect(err).ToNot(HaveOccurred())
Expand All @@ -299,8 +316,11 @@ var _ = Describe("Upgrade machine pool", func() {
runner := DescribeMachinePoolRunner(args)
err := t.StdOutReader.Record()
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, NewDescribeMachinePoolCommand(),
[]string{"--machinepool", nodePoolName, "-c", clusterId})
cmd := NewDescribeMachinePoolCommand()
err = cmd.Flag("cluster").Value.Set(clusterId)
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, cmd,
[]string{"--machinepool", nodePoolName})
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("Machine pool '%s' not found", nodePoolName)))
stdout, err := t.StdOutReader.Read()
Expand All @@ -315,8 +335,11 @@ var _ = Describe("Upgrade machine pool", func() {
runner := DescribeMachinePoolRunner(args)
err := t.StdOutReader.Record()
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, NewDescribeMachinePoolCommand(),
[]string{"--machinepool", nodePoolName, "-c", clusterId})
cmd := NewDescribeMachinePoolCommand()
err = cmd.Flag("cluster").Value.Set(clusterId)
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, cmd,
[]string{"--machinepool", nodePoolName})
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("Machine pool '%s' not found", nodePoolName)))
stdout, err := t.StdOutReader.Read()
Expand All @@ -331,8 +354,11 @@ var _ = Describe("Upgrade machine pool", func() {
runner := DescribeMachinePoolRunner(args)
err := t.StdOutReader.Record()
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, NewDescribeMachinePoolCommand(),
[]string{"--machinepool", nodePoolName, "-c", clusterId})
cmd := NewDescribeMachinePoolCommand()
err = cmd.Flag("cluster").Value.Set(clusterId)
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, cmd,
[]string{"--machinepool", nodePoolName})
Expect(err).To(BeNil())
stdout, err := t.StdOutReader.Read()
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -361,8 +387,13 @@ var _ = Describe("Upgrade machine pool", func() {
runner := DescribeMachinePoolRunner(args)
err := t.StdOutReader.Record()
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, NewDescribeMachinePoolCommand(),
[]string{"--machinepool", nodePoolName, "--output", "yaml", "-c", clusterId})
cmd := NewDescribeMachinePoolCommand()
err = cmd.Flag("cluster").Value.Set(clusterId)
Expect(err).ToNot(HaveOccurred())
err = cmd.Flag("output").Value.Set("yaml")
Expect(err).ToNot(HaveOccurred())
err = runner(context.Background(), t.RosaRuntime, cmd,
[]string{"--machinepool", nodePoolName})
Expect(err).To(BeNil())
stdout, err := t.StdOutReader.Read()
Expect(err).ToNot(HaveOccurred())
Expand Down
21 changes: 14 additions & 7 deletions cmd/describe/machinepool/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,35 @@ type DescribeMachinepoolUserOptions struct {
type DescribeMachinepoolOptions struct {
reporter *reporter.Object

args DescribeMachinepoolUserOptions
args *DescribeMachinepoolUserOptions
}

func NewDescribeMachinepoolUserOptions() DescribeMachinepoolUserOptions {
return DescribeMachinepoolUserOptions{machinepool: ""}
func NewDescribeMachinepoolUserOptions() *DescribeMachinepoolUserOptions {
return &DescribeMachinepoolUserOptions{machinepool: ""}
}

func NewDescribeMachinepoolOptions() *DescribeMachinepoolOptions {
return &DescribeMachinepoolOptions{
reporter: reporter.CreateReporter(),
args: DescribeMachinepoolUserOptions{},
args: &DescribeMachinepoolUserOptions{},
}
}

func (m *DescribeMachinepoolOptions) Machinepool() string {
return m.args.machinepool
}

func (m *DescribeMachinepoolOptions) Bind(args DescribeMachinepoolUserOptions) error {
if args.machinepool == "" {
func (m *DescribeMachinepoolOptions) Bind(args *DescribeMachinepoolUserOptions, argv []string) error {
m.args = args
if m.args.machinepool == "" {
if len(argv) > 0 {
m.args.machinepool = argv[0]
}
}

if m.args.machinepool == "" {
return fmt.Errorf("you need to specify a machine pool name")
}
m.args.machinepool = args.machinepool

return nil
}
12 changes: 9 additions & 3 deletions cmd/describe/machinepool/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

var _ = Describe("Test describe machinepool options", func() {
var args DescribeMachinepoolUserOptions
var args *DescribeMachinepoolUserOptions
Context("Describe Machinepool User Options", func() {
It("Creates default options", func() {
args = NewDescribeMachinepoolUserOptions()
Expand All @@ -19,15 +19,21 @@ var _ = Describe("Test describe machinepool options", func() {
// Set value then bind
testMachinepool := "test"
args.machinepool = testMachinepool
err := options.Bind(args)
err := options.Bind(args, []string{})
Expect(err).ToNot(HaveOccurred())
Expect(options.Machinepool()).To(Equal(testMachinepool))
})
It("Fail to bind args due to empty machinepool name", func() {
args.machinepool = ""
err := options.Bind(args)
err := options.Bind(args, []string{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("you need to specify a machine pool name"))
})
It("Test Bind with argv instead of normal args (single arg, no flag for machinepool)", func() {
argv := []string{"test-id"}
args.machinepool = ""
options.Bind(args, argv)
Expect(options.Machinepool()).To(Equal(argv[0]))
})
})
})
5 changes: 3 additions & 2 deletions cmd/dlt/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func init() {
Cmd.AddCommand(cluster.Cmd)
Cmd.AddCommand(idp.Cmd)
Cmd.AddCommand(ingress.Cmd)
Cmd.AddCommand(machinepool.Cmd)
machinepoolCommand := machinepool.NewDeleteMachinePoolCommand()
Cmd.AddCommand(machinepoolCommand)
Cmd.AddCommand(upgrade.Cmd)
Cmd.AddCommand(oidcconfig.Cmd)
Cmd.AddCommand(oidcprovider.Cmd)
Expand All @@ -81,7 +82,7 @@ func init() {
oidcprovider.Cmd, upgrade.Cmd, admin.Cmd,
service.Cmd, autoscaler.Cmd, idp.Cmd,
cluster.Cmd, dnsdomains.Cmd, externalauthprovider.Cmd,
kubeletconfig, machinepool.Cmd, tuningconfigs.Cmd,
kubeletconfig, machinepoolCommand, tuningconfigs.Cmd,
}
arguments.MarkRegionDeprecated(Cmd, globallyAvailableCommands)
}
85 changes: 50 additions & 35 deletions cmd/dlt/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
package machinepool

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"

Expand All @@ -28,44 +28,59 @@ import (
"github.com/openshift/rosa/pkg/rosa"
)

var Cmd = &cobra.Command{
Use: "machinepool ID",
Aliases: []string{"machinepools", "machine-pool", "machine-pools"},
Short: "Delete machine pool",
Long: "Delete the additional machine pool from a cluster.",
Example: ` # Delete machine pool with ID mp-1 from a cluster named 'mycluster'
rosa delete machinepool --cluster=mycluster mp-1`,
Run: run,
Args: func(_ *cobra.Command, argv []string) error {
if len(argv) != 1 {
return fmt.Errorf(
"Expected exactly one command line parameter containing the id of the machine pool",
)
}
return nil
},
}
const (
use = "machinepool ID"
short = "Delete machine pool"
long = "Delete the additional machine pool from a cluster."
example = ` # Delete machine pool with ID mp-1 from a cluster named 'mycluster'
rosa delete machinepool --cluster=mycluster mp-1`
)

func init() {
ocm.AddClusterFlag(Cmd)
confirm.AddFlag(Cmd.Flags())
var (
aliases = []string{"machinepools", "machine-pool", "machine-pools"}
)

func NewDeleteMachinePoolCommand() *cobra.Command {
options := NewDeleteMachinepoolUserOptions()
var cmd = &cobra.Command{
Use: use,
Aliases: aliases,
Short: short,
Long: long,
Example: example,
Run: rosa.DefaultRunner(rosa.RuntimeWithOCM(), DeleteMachinePoolRunner(options)),
Args: cobra.MaximumNArgs(1),
}
flags := cmd.Flags()
flags.StringVar(
&options.machinepool,
"machinepool",
"",
"Machine pool of the cluster to target",
)

ocm.AddClusterFlag(cmd)
confirm.AddFlag(cmd.Flags())
return cmd
}

func run(_ *cobra.Command, argv []string) {
r := rosa.NewRuntime().WithAWS().WithOCM()
defer r.Cleanup()
func DeleteMachinePoolRunner(userOptions *DeleteMachinepoolUserOptions) rosa.CommandRunner {
return func(_ context.Context, runtime *rosa.Runtime, cmd *cobra.Command, argv []string) error {
options := NewDeleteMachinepoolOptions()

machinePoolId := argv[0]
if !machinepool.MachinePoolKeyRE.MatchString(machinePoolId) {
r.Reporter.Errorf("Expected a valid identifier for the machine pool")
}
clusterKey := r.GetClusterKey()
cluster := r.FetchCluster()
err := options.Bind(userOptions, argv)
if err != nil {
return err
}

service := machinepool.NewMachinePoolService()
err := service.DeleteMachinePool(r, machinePoolId, clusterKey, cluster)
if err != nil {
r.Reporter.Errorf("Error deleting machinepool: %v", err)
os.Exit(1)
clusterKey := runtime.GetClusterKey()
cluster := runtime.FetchCluster()

service := machinepool.NewMachinePoolService()
err = service.DeleteMachinePool(runtime, options.Machinepool(), clusterKey, cluster)
if err != nil {
return fmt.Errorf("Error deleting machinepool: %v", err)
}
return nil
}
}
Loading

0 comments on commit 4f218b4

Please sign in to comment.