|
| 1 | +package list |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/google/go-cmp/cmp" |
| 5 | + "github.com/google/uuid" |
| 6 | + "github.com/stackitcloud/stackit-cli/internal/cmd/params" |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func TestParseInput(t *testing.T) { |
| 15 | + projectId := uuid.New().String() |
| 16 | + tests := []struct { |
| 17 | + description string |
| 18 | + globalFlags map[string]string |
| 19 | + expectedModel *inputModel |
| 20 | + isValid bool |
| 21 | + }{ |
| 22 | + { |
| 23 | + description: "valid project id", |
| 24 | + globalFlags: map[string]string{ |
| 25 | + "project-id": projectId, |
| 26 | + }, |
| 27 | + expectedModel: &inputModel{ |
| 28 | + GlobalFlagModel: &globalflags.GlobalFlagModel{ |
| 29 | + ProjectId: projectId, |
| 30 | + Verbosity: globalflags.InfoVerbosity, |
| 31 | + }, |
| 32 | + }, |
| 33 | + isValid: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + description: "missing project id", |
| 37 | + globalFlags: map[string]string{}, |
| 38 | + expectedModel: nil, |
| 39 | + isValid: false, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + for _, tt := range tests { |
| 44 | + t.Run(tt.description, func(t *testing.T) { |
| 45 | + p := print.NewPrinter() |
| 46 | + cmd := NewCmd(¶ms.CmdParams{Printer: p}) |
| 47 | + err := globalflags.Configure(cmd.Flags()) |
| 48 | + if err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + |
| 52 | + for flag, value := range tt.globalFlags { |
| 53 | + if err := cmd.Flags().Set(flag, value); err != nil { |
| 54 | + t.Fatalf("Failed to set global flag %s: %v", flag, err) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + model, err := parseInput(p, cmd) |
| 59 | + if !tt.isValid && err == nil { |
| 60 | + t.Fatalf("parseInput() error = %v, wantErr %v", err, !tt.isValid) |
| 61 | + } |
| 62 | + |
| 63 | + if tt.isValid { |
| 64 | + if diff := cmp.Diff(model, tt.expectedModel); diff != "" { |
| 65 | + t.Fatalf("Model mismatch (-want +got):\n%s", diff) |
| 66 | + } |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestOutputResult(t *testing.T) { |
| 73 | + tests := []struct { |
| 74 | + name string |
| 75 | + outputFormat string |
| 76 | + networkList iaas.PublicNetworkListResponse |
| 77 | + expectedOutput string |
| 78 | + wantErr bool |
| 79 | + }{ |
| 80 | + { |
| 81 | + name: "JSON output single", |
| 82 | + outputFormat: "json", |
| 83 | + networkList: iaas.PublicNetworkListResponse{ |
| 84 | + Items: &[]iaas.PublicNetwork{ |
| 85 | + {Cidr: utils.Ptr("192.168.0.0/24")}, |
| 86 | + }, |
| 87 | + }, |
| 88 | + wantErr: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "JSON output multiple", |
| 92 | + outputFormat: "json", |
| 93 | + networkList: iaas.PublicNetworkListResponse{ |
| 94 | + Items: &[]iaas.PublicNetwork{ |
| 95 | + {Cidr: utils.Ptr("192.168.0.0/24")}, |
| 96 | + {Cidr: utils.Ptr("192.167.0.0/24")}, |
| 97 | + }, |
| 98 | + }, |
| 99 | + wantErr: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "YAML output single", |
| 103 | + outputFormat: "yaml", |
| 104 | + networkList: iaas.PublicNetworkListResponse{ |
| 105 | + Items: &[]iaas.PublicNetwork{ |
| 106 | + {Cidr: utils.Ptr("192.168.0.0/24")}, |
| 107 | + }, |
| 108 | + }, |
| 109 | + wantErr: false, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "YAML output multiple", |
| 113 | + outputFormat: "yaml", |
| 114 | + networkList: iaas.PublicNetworkListResponse{ |
| 115 | + Items: &[]iaas.PublicNetwork{ |
| 116 | + {Cidr: utils.Ptr("192.168.0.0/24")}, |
| 117 | + {Cidr: utils.Ptr("192.167.0.0/24")}, |
| 118 | + }, |
| 119 | + }, |
| 120 | + wantErr: false, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "pretty output single", |
| 124 | + outputFormat: "pretty", |
| 125 | + networkList: iaas.PublicNetworkListResponse{ |
| 126 | + Items: &[]iaas.PublicNetwork{ |
| 127 | + {Cidr: utils.Ptr("192.168.0.0/24")}, |
| 128 | + }, |
| 129 | + }, |
| 130 | + wantErr: false, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "pretty output multiple", |
| 134 | + outputFormat: "pretty", |
| 135 | + networkList: iaas.PublicNetworkListResponse{ |
| 136 | + Items: &[]iaas.PublicNetwork{ |
| 137 | + {Cidr: utils.Ptr("192.168.0.0/24")}, |
| 138 | + {Cidr: utils.Ptr("192.167.0.0/24")}, |
| 139 | + }, |
| 140 | + }, |
| 141 | + wantErr: false, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "default output", |
| 145 | + outputFormat: "", |
| 146 | + networkList: iaas.PublicNetworkListResponse{ |
| 147 | + Items: &[]iaas.PublicNetwork{ |
| 148 | + {Cidr: utils.Ptr("192.168.0.0/24")}, |
| 149 | + }, |
| 150 | + }, |
| 151 | + wantErr: false, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + for _, tt := range tests { |
| 156 | + t.Run(tt.name, func(t *testing.T) { |
| 157 | + p := print.NewPrinter() |
| 158 | + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) |
| 159 | + err := outputResult(p, tt.outputFormat, tt.networkList) |
| 160 | + if (err != nil) != tt.wantErr { |
| 161 | + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments