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