|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + root "github.com/equinix/metal-cli/internal/cli" |
| 10 | + "github.com/equinix/metal-cli/internal/events" |
| 11 | + outputPkg "github.com/equinix/metal-cli/internal/outputs" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCli_Plans(t *testing.T) { |
| 16 | + subCommand := "event" |
| 17 | + consumerToken := "" |
| 18 | + apiURL := "" |
| 19 | + Version := "metal" |
| 20 | + rootClient := root.NewClient(consumerToken, apiURL, Version) |
| 21 | + type fields struct { |
| 22 | + MainCmd *cobra.Command |
| 23 | + Outputer outputPkg.Outputer |
| 24 | + } |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + fields fields |
| 28 | + want *cobra.Command |
| 29 | + cmdFunc func(*testing.T, *cobra.Command) |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "get", |
| 33 | + fields: fields{ |
| 34 | + MainCmd: events.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), |
| 35 | + Outputer: outputPkg.Outputer(&outputPkg.Standard{}), |
| 36 | + }, |
| 37 | + want: &cobra.Command{}, |
| 38 | + cmdFunc: func(t *testing.T, c *cobra.Command) { |
| 39 | + root := c.Root() |
| 40 | + root.SetArgs([]string{subCommand, "get"}) |
| 41 | + rescueStdout := os.Stdout |
| 42 | + r, w, _ := os.Pipe() |
| 43 | + os.Stdout = w |
| 44 | + if err := root.Execute(); err != nil { |
| 45 | + t.Error(err) |
| 46 | + } |
| 47 | + w.Close() |
| 48 | + out, _ := io.ReadAll(r) |
| 49 | + os.Stdout = rescueStdout |
| 50 | + if !strings.Contains(string(out[:]), "instance.created") && |
| 51 | + !strings.Contains(string(out[:]), "instance.deleted") && |
| 52 | + !strings.Contains(string(out[:]), "ssh_key.created") && |
| 53 | + !strings.Contains(string(out[:]), "ssh_key.deleted") { |
| 54 | + t.Error("expected output should include instance.created, instance.deleted, ssh_key.created, ssh_key.deleted by TYPE") |
| 55 | + } |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "get_by_orgID", |
| 60 | + fields: fields{ |
| 61 | + MainCmd: events.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), |
| 62 | + Outputer: outputPkg.Outputer(&outputPkg.Standard{}), |
| 63 | + }, |
| 64 | + want: &cobra.Command{}, |
| 65 | + cmdFunc: func(t *testing.T, c *cobra.Command) { |
| 66 | + root := c.Root() |
| 67 | + root.SetArgs([]string{subCommand, "get", "--token", os.Getenv("METAL_AUTH_TOKEN"), "-o", os.Getenv("METAL_ORG_ID")}) |
| 68 | + rescueStdout := os.Stdout |
| 69 | + r, w, _ := os.Pipe() |
| 70 | + os.Stdout = w |
| 71 | + if err := root.Execute(); err != nil { |
| 72 | + t.Error(err) |
| 73 | + } |
| 74 | + w.Close() |
| 75 | + out, _ := io.ReadAll(r) |
| 76 | + os.Stdout = rescueStdout |
| 77 | + if !strings.Contains(string(out[:]), "instance.created") && |
| 78 | + !strings.Contains(string(out[:]), "instance.deleted") && |
| 79 | + !strings.Contains(string(out[:]), "ssh_key.created") && |
| 80 | + !strings.Contains(string(out[:]), "ssh_key.deleted") { |
| 81 | + t.Error("expected output should include instance.created, instance.deleted, ssh_key.created, ssh_key.deleted by TYPE") |
| 82 | + } |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "get_by_projID", |
| 87 | + fields: fields{ |
| 88 | + MainCmd: events.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), |
| 89 | + Outputer: outputPkg.Outputer(&outputPkg.Standard{}), |
| 90 | + }, |
| 91 | + want: &cobra.Command{}, |
| 92 | + cmdFunc: func(t *testing.T, c *cobra.Command) { |
| 93 | + root := c.Root() |
| 94 | + root.SetArgs([]string{subCommand, "get", "--token", os.Getenv("METAL_AUTH_TOKEN"), "-p", os.Getenv("METAL_PROJ_ID")}) |
| 95 | + rescueStdout := os.Stdout |
| 96 | + r, w, _ := os.Pipe() |
| 97 | + os.Stdout = w |
| 98 | + if err := root.Execute(); err != nil { |
| 99 | + t.Error(err) |
| 100 | + } |
| 101 | + w.Close() |
| 102 | + out, _ := io.ReadAll(r) |
| 103 | + os.Stdout = rescueStdout |
| 104 | + if !strings.Contains(string(out[:]), "instance.created") && |
| 105 | + !strings.Contains(string(out[:]), "instance.deleted") && |
| 106 | + !strings.Contains(string(out[:]), "ssh_key.created") && |
| 107 | + !strings.Contains(string(out[:]), "ssh_key.deleted") { |
| 108 | + t.Error("expected output should include instance.created, instance.deleted, ssh_key.created, ssh_key.deleted by TYPE") |
| 109 | + } |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "get_by_DeviceID", |
| 114 | + fields: fields{ |
| 115 | + MainCmd: events.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), |
| 116 | + Outputer: outputPkg.Outputer(&outputPkg.Standard{}), |
| 117 | + }, |
| 118 | + want: &cobra.Command{}, |
| 119 | + cmdFunc: func(t *testing.T, c *cobra.Command) { |
| 120 | + root := c.Root() |
| 121 | + root.SetArgs([]string{subCommand, "get", "--token", os.Getenv("METAL_AUTH_TOKEN"), "-p", os.Getenv("METAL_DEVICE_ID")}) |
| 122 | + rescueStdout := os.Stdout |
| 123 | + r, w, _ := os.Pipe() |
| 124 | + os.Stdout = w |
| 125 | + if err := root.Execute(); err != nil { |
| 126 | + t.Error(err) |
| 127 | + } |
| 128 | + w.Close() |
| 129 | + out, _ := io.ReadAll(r) |
| 130 | + os.Stdout = rescueStdout |
| 131 | + if !strings.Contains(string(out[:]), "instance.created") && |
| 132 | + !strings.Contains(string(out[:]), "instance.deleted") && |
| 133 | + !strings.Contains(string(out[:]), "ssh_key.created") && |
| 134 | + !strings.Contains(string(out[:]), "ssh_key.deleted") { |
| 135 | + t.Error("expected output should include instance.created, instance.deleted, ssh_key.created, ssh_key.deleted by TYPE") |
| 136 | + } |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + for _, tt := range tests { |
| 142 | + t.Run(tt.name, func(t *testing.T) { |
| 143 | + rootCmd := rootClient.NewCommand() |
| 144 | + rootCmd.AddCommand(tt.fields.MainCmd) |
| 145 | + tt.cmdFunc(t, tt.fields.MainCmd) |
| 146 | + }) |
| 147 | + } |
| 148 | +} |
0 commit comments