|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/cmd/params" |
| 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/google/uuid" |
| 15 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 16 | +) |
| 17 | + |
| 18 | +type testCtxKey struct{} |
| 19 | + |
| 20 | +var ( |
| 21 | + testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") |
| 22 | + testClient = &iaas.APIClient{} |
| 23 | + testProjectId = uuid.NewString() |
| 24 | + testSnapshotId = uuid.NewString() |
| 25 | +) |
| 26 | + |
| 27 | +func fixtureArgValues(mods ...func(argValues []string)) []string { |
| 28 | + argValues := []string{ |
| 29 | + testSnapshotId, |
| 30 | + } |
| 31 | + for _, mod := range mods { |
| 32 | + mod(argValues) |
| 33 | + } |
| 34 | + return argValues |
| 35 | +} |
| 36 | + |
| 37 | +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { |
| 38 | + flagValues := map[string]string{ |
| 39 | + globalflags.ProjectIdFlag: testProjectId, |
| 40 | + } |
| 41 | + for _, mod := range mods { |
| 42 | + mod(flagValues) |
| 43 | + } |
| 44 | + return flagValues |
| 45 | +} |
| 46 | + |
| 47 | +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { |
| 48 | + model := &inputModel{ |
| 49 | + GlobalFlagModel: &globalflags.GlobalFlagModel{ |
| 50 | + ProjectId: testProjectId, |
| 51 | + Verbosity: globalflags.VerbosityDefault, |
| 52 | + }, |
| 53 | + SnapshotId: testSnapshotId, |
| 54 | + } |
| 55 | + for _, mod := range mods { |
| 56 | + mod(model) |
| 57 | + } |
| 58 | + return model |
| 59 | +} |
| 60 | + |
| 61 | +func fixtureRequest(mods ...func(request *iaas.ApiGetSnapshotRequest)) iaas.ApiGetSnapshotRequest { |
| 62 | + request := testClient.GetSnapshot(testCtx, testProjectId, testSnapshotId) |
| 63 | + for _, mod := range mods { |
| 64 | + mod(&request) |
| 65 | + } |
| 66 | + return request |
| 67 | +} |
| 68 | + |
| 69 | +func TestParseInput(t *testing.T) { |
| 70 | + tests := []struct { |
| 71 | + description string |
| 72 | + argValues []string |
| 73 | + flagValues map[string]string |
| 74 | + isValid bool |
| 75 | + expectedModel *inputModel |
| 76 | + }{ |
| 77 | + { |
| 78 | + description: "base", |
| 79 | + argValues: fixtureArgValues(), |
| 80 | + flagValues: fixtureFlagValues(), |
| 81 | + isValid: true, |
| 82 | + expectedModel: fixtureInputModel(), |
| 83 | + }, |
| 84 | + { |
| 85 | + description: "no values", |
| 86 | + argValues: []string{}, |
| 87 | + flagValues: map[string]string{}, |
| 88 | + isValid: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + description: "no arg values", |
| 92 | + argValues: []string{}, |
| 93 | + flagValues: fixtureFlagValues(), |
| 94 | + isValid: false, |
| 95 | + }, |
| 96 | + { |
| 97 | + description: "no flag values", |
| 98 | + argValues: fixtureArgValues(), |
| 99 | + flagValues: map[string]string{}, |
| 100 | + isValid: false, |
| 101 | + }, |
| 102 | + { |
| 103 | + description: "project id missing", |
| 104 | + argValues: fixtureArgValues(), |
| 105 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 106 | + delete(flagValues, globalflags.ProjectIdFlag) |
| 107 | + }), |
| 108 | + isValid: false, |
| 109 | + }, |
| 110 | + { |
| 111 | + description: "project id invalid", |
| 112 | + argValues: fixtureArgValues(), |
| 113 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 114 | + flagValues[globalflags.ProjectIdFlag] = "invalid-uuid" |
| 115 | + }), |
| 116 | + isValid: false, |
| 117 | + }, |
| 118 | + { |
| 119 | + description: "snapshot id invalid", |
| 120 | + argValues: []string{"invalid-uuid"}, |
| 121 | + flagValues: fixtureFlagValues(), |
| 122 | + isValid: false, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + for _, tt := range tests { |
| 127 | + t.Run(tt.description, func(t *testing.T) { |
| 128 | + p := print.NewPrinter() |
| 129 | + cmd := NewCmd(¶ms.CmdParams{Printer: p}) |
| 130 | + err := globalflags.Configure(cmd.Flags()) |
| 131 | + if err != nil { |
| 132 | + t.Fatalf("configure global flags: %v", err) |
| 133 | + } |
| 134 | + |
| 135 | + for flag, value := range tt.flagValues { |
| 136 | + err := cmd.Flags().Set(flag, value) |
| 137 | + if err != nil { |
| 138 | + if !tt.isValid { |
| 139 | + return |
| 140 | + } |
| 141 | + t.Fatalf("setting flag --%s=%s: %v", flag, value, err) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + err = cmd.ValidateArgs(tt.argValues) |
| 146 | + if err != nil { |
| 147 | + if !tt.isValid { |
| 148 | + return |
| 149 | + } |
| 150 | + t.Fatalf("error validating args: %v", err) |
| 151 | + } |
| 152 | + |
| 153 | + model, err := parseInput(p, cmd, tt.argValues) |
| 154 | + if err != nil { |
| 155 | + if !tt.isValid { |
| 156 | + return |
| 157 | + } |
| 158 | + t.Fatalf("error parsing input: %v", err) |
| 159 | + } |
| 160 | + |
| 161 | + if !tt.isValid { |
| 162 | + t.Fatalf("did not fail on invalid input") |
| 163 | + } |
| 164 | + diff := cmp.Diff(model, tt.expectedModel) |
| 165 | + if diff != "" { |
| 166 | + t.Fatalf("Data does not match: %s", diff) |
| 167 | + } |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func TestBuildRequest(t *testing.T) { |
| 173 | + tests := []struct { |
| 174 | + description string |
| 175 | + model *inputModel |
| 176 | + expectedRequest iaas.ApiGetSnapshotRequest |
| 177 | + }{ |
| 178 | + { |
| 179 | + description: "base", |
| 180 | + model: fixtureInputModel(), |
| 181 | + expectedRequest: fixtureRequest(), |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + for _, tt := range tests { |
| 186 | + t.Run(tt.description, func(t *testing.T) { |
| 187 | + request := buildRequest(testCtx, tt.model, testClient) |
| 188 | + |
| 189 | + diff := cmp.Diff(request, tt.expectedRequest, |
| 190 | + cmp.AllowUnexported(tt.expectedRequest), |
| 191 | + cmpopts.EquateComparable(testCtx), |
| 192 | + ) |
| 193 | + if diff != "" { |
| 194 | + t.Fatalf("Data does not match: %s", diff) |
| 195 | + } |
| 196 | + }) |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +func TestOutputResult(t *testing.T) { |
| 201 | + type args struct { |
| 202 | + outputFormat string |
| 203 | + snapshot *iaas.Snapshot |
| 204 | + } |
| 205 | + tests := []struct { |
| 206 | + name string |
| 207 | + args args |
| 208 | + wantErr bool |
| 209 | + }{ |
| 210 | + { |
| 211 | + name: "empty", |
| 212 | + args: args{}, |
| 213 | + wantErr: true, |
| 214 | + }, |
| 215 | + { |
| 216 | + name: "empty snapshot", |
| 217 | + args: args{ |
| 218 | + snapshot: &iaas.Snapshot{}, |
| 219 | + }, |
| 220 | + wantErr: false, |
| 221 | + }, |
| 222 | + { |
| 223 | + name: "snapshot with values", |
| 224 | + args: args{ |
| 225 | + snapshot: &iaas.Snapshot{ |
| 226 | + Id: utils.Ptr("snapshot-1"), |
| 227 | + Name: utils.Ptr("test-snapshot"), |
| 228 | + }, |
| 229 | + }, |
| 230 | + wantErr: false, |
| 231 | + }, |
| 232 | + } |
| 233 | + p := print.NewPrinter() |
| 234 | + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) |
| 235 | + for _, tt := range tests { |
| 236 | + t.Run(tt.name, func(t *testing.T) { |
| 237 | + if err := outputResult(p, tt.args.outputFormat, tt.args.snapshot); (err != nil) != tt.wantErr { |
| 238 | + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) |
| 239 | + } |
| 240 | + }) |
| 241 | + } |
| 242 | +} |
0 commit comments