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