|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 14 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 15 | +) |
| 16 | + |
| 17 | +type testCtxKey struct{} |
| 18 | + |
| 19 | +var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") |
| 20 | +var testClient = &iaas.APIClient{} |
| 21 | + |
| 22 | +var testPublicKey = "ssh-rsa <key>" |
| 23 | +var testKeypairName = "foobar_key" |
| 24 | + |
| 25 | +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { |
| 26 | + flagValues := map[string]string{ |
| 27 | + publicKeyFlag: testPublicKey, |
| 28 | + labelFlag: "foo=bar", |
| 29 | + nameFlag: testKeypairName, |
| 30 | + } |
| 31 | + for _, mod := range mods { |
| 32 | + mod(flagValues) |
| 33 | + } |
| 34 | + return flagValues |
| 35 | +} |
| 36 | + |
| 37 | +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { |
| 38 | + model := &inputModel{ |
| 39 | + GlobalFlagModel: &globalflags.GlobalFlagModel{ |
| 40 | + Verbosity: globalflags.VerbosityDefault, |
| 41 | + }, |
| 42 | + Labels: utils.Ptr(map[string]string{ |
| 43 | + "foo": "bar", |
| 44 | + }), |
| 45 | + PublicKey: utils.Ptr(testPublicKey), |
| 46 | + Name: utils.Ptr(testKeypairName), |
| 47 | + } |
| 48 | + for _, mod := range mods { |
| 49 | + mod(model) |
| 50 | + } |
| 51 | + return model |
| 52 | +} |
| 53 | + |
| 54 | +func fixtureRequest(mods ...func(request *iaas.ApiCreateKeyPairRequest)) iaas.ApiCreateKeyPairRequest { |
| 55 | + request := testClient.CreateKeyPair(testCtx) |
| 56 | + request = request.CreateKeyPairPayload(fixturePayload()) |
| 57 | + for _, mod := range mods { |
| 58 | + mod(&request) |
| 59 | + } |
| 60 | + return request |
| 61 | +} |
| 62 | + |
| 63 | +func fixturePayload(mods ...func(payload *iaas.CreateKeyPairPayload)) iaas.CreateKeyPairPayload { |
| 64 | + payload := iaas.CreateKeyPairPayload{ |
| 65 | + Labels: utils.Ptr(map[string]interface{}{ |
| 66 | + "foo": "bar", |
| 67 | + }), |
| 68 | + PublicKey: utils.Ptr(testPublicKey), |
| 69 | + Name: utils.Ptr(testKeypairName), |
| 70 | + } |
| 71 | + for _, mod := range mods { |
| 72 | + mod(&payload) |
| 73 | + } |
| 74 | + return payload |
| 75 | +} |
| 76 | + |
| 77 | +func TestParseInput(t *testing.T) { |
| 78 | + tests := []struct { |
| 79 | + description string |
| 80 | + flagValues map[string]string |
| 81 | + isValid bool |
| 82 | + expectedModel *inputModel |
| 83 | + }{ |
| 84 | + { |
| 85 | + description: "base", |
| 86 | + flagValues: fixtureFlagValues(), |
| 87 | + isValid: true, |
| 88 | + expectedModel: fixtureInputModel(), |
| 89 | + }, |
| 90 | + { |
| 91 | + description: "required only", |
| 92 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 93 | + delete(flagValues, nameFlag) |
| 94 | + delete(flagValues, labelFlag) |
| 95 | + }), |
| 96 | + isValid: true, |
| 97 | + expectedModel: fixtureInputModel(func(model *inputModel) { |
| 98 | + model.Name = nil |
| 99 | + model.Labels = nil |
| 100 | + }), |
| 101 | + }, |
| 102 | + { |
| 103 | + description: "read public key from file", |
| 104 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 105 | + flagValues[publicKeyFlag] = "@./create_test.go" |
| 106 | + }), |
| 107 | + isValid: true, |
| 108 | + expectedModel: fixtureInputModel(func(model *inputModel) { |
| 109 | + file, err := os.ReadFile("./create_test.go") |
| 110 | + if err != nil { |
| 111 | + t.Fatal("could not create expected Model", err) |
| 112 | + } |
| 113 | + model.PublicKey = utils.Ptr(string(file)) |
| 114 | + }), |
| 115 | + }, |
| 116 | + { |
| 117 | + description: "no values", |
| 118 | + flagValues: map[string]string{}, |
| 119 | + isValid: false, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, tt := range tests { |
| 124 | + t.Run(tt.description, func(t *testing.T) { |
| 125 | + p := print.NewPrinter() |
| 126 | + cmd := NewCmd(p) |
| 127 | + err := globalflags.Configure(cmd.Flags()) |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("configure global flags: %v", err) |
| 130 | + } |
| 131 | + |
| 132 | + for flag, value := range tt.flagValues { |
| 133 | + err = cmd.Flags().Set(flag, value) |
| 134 | + if err != nil { |
| 135 | + if !tt.isValid { |
| 136 | + return |
| 137 | + } |
| 138 | + t.Fatalf("setting flag --%s=%s: %v", flag, value, err) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + err = cmd.ValidateRequiredFlags() |
| 143 | + if err != nil { |
| 144 | + if !tt.isValid { |
| 145 | + return |
| 146 | + } |
| 147 | + t.Fatalf("error validating flags: %v", err) |
| 148 | + } |
| 149 | + |
| 150 | + model, err := parseInput(p, cmd) |
| 151 | + if err != nil { |
| 152 | + if !tt.isValid { |
| 153 | + return |
| 154 | + } |
| 155 | + t.Fatalf("error parsing flags: %v", err) |
| 156 | + } |
| 157 | + |
| 158 | + if !tt.isValid { |
| 159 | + t.Fatalf("did not fail on invalid input") |
| 160 | + } |
| 161 | + diff := cmp.Diff(model, tt.expectedModel) |
| 162 | + if diff != "" { |
| 163 | + t.Fatalf("Data does not match: %s", diff) |
| 164 | + } |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestBuildRequest(t *testing.T) { |
| 170 | + tests := []struct { |
| 171 | + description string |
| 172 | + model *inputModel |
| 173 | + expectedRequest iaas.ApiCreateKeyPairRequest |
| 174 | + }{ |
| 175 | + { |
| 176 | + description: "base", |
| 177 | + model: fixtureInputModel(), |
| 178 | + expectedRequest: fixtureRequest(), |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + for _, tt := range tests { |
| 183 | + t.Run(tt.description, func(t *testing.T) { |
| 184 | + request := buildRequest(testCtx, tt.model, testClient) |
| 185 | + |
| 186 | + diff := cmp.Diff(request, tt.expectedRequest, |
| 187 | + cmp.AllowUnexported(tt.expectedRequest), |
| 188 | + cmpopts.EquateComparable(testCtx), |
| 189 | + cmp.AllowUnexported(iaas.NullableString{}), |
| 190 | + ) |
| 191 | + if diff != "" { |
| 192 | + t.Fatalf("Data does not match: %s", diff) |
| 193 | + } |
| 194 | + }) |
| 195 | + } |
| 196 | +} |
0 commit comments