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