|
| 1 | +package plans |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 12 | + "github.com/google/uuid" |
| 13 | + "github.com/stackitcloud/stackit-sdk-go/services/alb" |
| 14 | +) |
| 15 | + |
| 16 | +type testCtxKey struct{} |
| 17 | + |
| 18 | +var ( |
| 19 | + testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") |
| 20 | + testClient = &alb.APIClient{} |
| 21 | + testProjectId = uuid.NewString() |
| 22 | + testRegion = "eu01" |
| 23 | +) |
| 24 | + |
| 25 | +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { |
| 26 | + flagValues := map[string]string{ |
| 27 | + globalflags.ProjectIdFlag: testProjectId, |
| 28 | + globalflags.RegionFlag: testRegion, |
| 29 | + } |
| 30 | + for _, mod := range mods { |
| 31 | + mod(flagValues) |
| 32 | + } |
| 33 | + return flagValues |
| 34 | +} |
| 35 | + |
| 36 | +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { |
| 37 | + model := &inputModel{ |
| 38 | + GlobalFlagModel: &globalflags.GlobalFlagModel{ProjectId: testProjectId, Region: testRegion, Verbosity: globalflags.VerbosityDefault}, |
| 39 | + } |
| 40 | + for _, mod := range mods { |
| 41 | + mod(model) |
| 42 | + } |
| 43 | + return model |
| 44 | +} |
| 45 | + |
| 46 | +func fixtureRequest(mods ...func(request *alb.ApiListPlansRequest)) alb.ApiListPlansRequest { |
| 47 | + request := testClient.ListPlans(testCtx, testRegion) |
| 48 | + for _, mod := range mods { |
| 49 | + mod(&request) |
| 50 | + } |
| 51 | + return request |
| 52 | +} |
| 53 | + |
| 54 | +func TestParseInput(t *testing.T) { |
| 55 | + tests := []struct { |
| 56 | + description string |
| 57 | + flagValues map[string]string |
| 58 | + isValid bool |
| 59 | + expectedModel *inputModel |
| 60 | + }{ |
| 61 | + { |
| 62 | + description: "base", |
| 63 | + flagValues: fixtureFlagValues(), |
| 64 | + isValid: true, |
| 65 | + expectedModel: fixtureInputModel(), |
| 66 | + }, |
| 67 | + { |
| 68 | + description: "no values", |
| 69 | + flagValues: map[string]string{}, |
| 70 | + isValid: false, |
| 71 | + }, |
| 72 | + { |
| 73 | + description: "project id missing", |
| 74 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 75 | + delete(flagValues, globalflags.ProjectIdFlag) |
| 76 | + }), |
| 77 | + isValid: false, |
| 78 | + }, |
| 79 | + { |
| 80 | + description: "project id invalid 1", |
| 81 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 82 | + flagValues[globalflags.ProjectIdFlag] = "" |
| 83 | + }), |
| 84 | + isValid: false, |
| 85 | + }, |
| 86 | + { |
| 87 | + description: "project id invalid 2", |
| 88 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 89 | + flagValues[globalflags.ProjectIdFlag] = "invalid-uuid" |
| 90 | + }), |
| 91 | + isValid: false, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + for _, tt := range tests { |
| 96 | + t.Run(tt.description, func(t *testing.T) { |
| 97 | + p := print.NewPrinter() |
| 98 | + cmd := NewCmd(p) |
| 99 | + if err := globalflags.Configure(cmd.Flags()); err != nil { |
| 100 | + t.Errorf("cannot configure global flags: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + for flag, value := range tt.flagValues { |
| 104 | + err := cmd.Flags().Set(flag, value) |
| 105 | + if err != nil { |
| 106 | + if !tt.isValid { |
| 107 | + return |
| 108 | + } |
| 109 | + t.Fatalf("setting flag --%s=%s: %v", flag, value, err) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if err := cmd.ValidateRequiredFlags(); err != nil { |
| 114 | + if !tt.isValid { |
| 115 | + return |
| 116 | + } |
| 117 | + t.Fatalf("error validating flags: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | + model, err := parseInput(p, cmd) |
| 121 | + if err != nil { |
| 122 | + if !tt.isValid { |
| 123 | + return |
| 124 | + } |
| 125 | + t.Fatalf("error parsing flags: %v", err) |
| 126 | + } |
| 127 | + |
| 128 | + if !tt.isValid { |
| 129 | + t.Fatalf("did not fail on invalid input") |
| 130 | + } |
| 131 | + diff := cmp.Diff(model, tt.expectedModel) |
| 132 | + if diff != "" { |
| 133 | + t.Fatalf("Data does not match: %s", diff) |
| 134 | + } |
| 135 | + }) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestBuildRequest(t *testing.T) { |
| 140 | + tests := []struct { |
| 141 | + description string |
| 142 | + model *inputModel |
| 143 | + expectedRequest alb.ApiListPlansRequest |
| 144 | + }{ |
| 145 | + { |
| 146 | + description: "base", |
| 147 | + model: fixtureInputModel(), |
| 148 | + expectedRequest: fixtureRequest(), |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + for _, tt := range tests { |
| 153 | + t.Run(tt.description, func(t *testing.T) { |
| 154 | + request := buildRequest(testCtx, tt.model, testClient) |
| 155 | + diff := cmp.Diff(request, tt.expectedRequest, |
| 156 | + cmp.AllowUnexported(tt.expectedRequest), |
| 157 | + cmpopts.EquateComparable(testCtx), |
| 158 | + cmpopts.IgnoreFields(alb.ApiListLoadBalancersRequest{}, "ctx"), |
| 159 | + ) |
| 160 | + if diff != "" { |
| 161 | + t.Fatalf("Data does not match: %s", diff) |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func Test_outputResult(t *testing.T) { |
| 168 | + type args struct { |
| 169 | + outputFormat string |
| 170 | + items []alb.PlanDetails |
| 171 | + } |
| 172 | + tests := []struct { |
| 173 | + name string |
| 174 | + args args |
| 175 | + wantErr bool |
| 176 | + }{ |
| 177 | + { |
| 178 | + name: "empty", |
| 179 | + args: args{ |
| 180 | + outputFormat: "", |
| 181 | + items: []alb.PlanDetails{}, |
| 182 | + }, |
| 183 | + wantErr: false, |
| 184 | + }, |
| 185 | + { |
| 186 | + name: "output format json", |
| 187 | + args: args{ |
| 188 | + outputFormat: print.JSONOutputFormat, |
| 189 | + items: []alb.PlanDetails{}, |
| 190 | + }, |
| 191 | + wantErr: false, |
| 192 | + }, |
| 193 | + } |
| 194 | + p := print.NewPrinter() |
| 195 | + p.Cmd = NewCmd(p) |
| 196 | + for _, tt := range tests { |
| 197 | + t.Run(tt.name, func(t *testing.T) { |
| 198 | + if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr { |
| 199 | + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) |
| 200 | + } |
| 201 | + }) |
| 202 | + } |
| 203 | +} |
0 commit comments