|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/cmd/params" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 13 | + "github.com/stackitcloud/stackit-sdk-go/services/iaasalpha" |
| 14 | +) |
| 15 | + |
| 16 | +var testRegion = "eu01" |
| 17 | +var testOrgId = uuid.NewString() |
| 18 | +var testNetworkAreaId = uuid.NewString() |
| 19 | +var testRoutingTableId = uuid.NewString() |
| 20 | + |
| 21 | +var testLabels = &map[string]string{ |
| 22 | + "key1": "value1", |
| 23 | + "key2": "value2", |
| 24 | +} |
| 25 | + |
| 26 | +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { |
| 27 | + flagValues := map[string]string{ |
| 28 | + globalflags.RegionFlag: testRegion, |
| 29 | + organizationIdFlag: testOrgId, |
| 30 | + networkAreaIdFlag: testNetworkAreaId, |
| 31 | + } |
| 32 | + for _, mod := range mods { |
| 33 | + mod(flagValues) |
| 34 | + } |
| 35 | + return flagValues |
| 36 | +} |
| 37 | + |
| 38 | +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { |
| 39 | + model := &inputModel{ |
| 40 | + GlobalFlagModel: &globalflags.GlobalFlagModel{ |
| 41 | + Verbosity: globalflags.VerbosityDefault, |
| 42 | + Region: testRegion, |
| 43 | + }, |
| 44 | + OrganizationId: utils.Ptr(testOrgId), |
| 45 | + NetworkAreaId: utils.Ptr(testNetworkAreaId), |
| 46 | + RoutingTableId: utils.Ptr(testRoutingTableId), |
| 47 | + } |
| 48 | + for _, mod := range mods { |
| 49 | + mod(model) |
| 50 | + } |
| 51 | + return model |
| 52 | +} |
| 53 | + |
| 54 | +func fixtureArgValues(mods ...func(argValues []string)) []string { |
| 55 | + argValues := []string{ |
| 56 | + testRoutingTableId, |
| 57 | + } |
| 58 | + for _, mod := range mods { |
| 59 | + mod(argValues) |
| 60 | + } |
| 61 | + return argValues |
| 62 | +} |
| 63 | + |
| 64 | +func TestParseInput(t *testing.T) { |
| 65 | + tests := []struct { |
| 66 | + description string |
| 67 | + flagValues map[string]string |
| 68 | + argValues []string |
| 69 | + isValid bool |
| 70 | + expectedModel *inputModel |
| 71 | + }{ |
| 72 | + { |
| 73 | + description: "base", |
| 74 | + flagValues: fixtureFlagValues(), |
| 75 | + argValues: fixtureArgValues(), |
| 76 | + isValid: true, |
| 77 | + expectedModel: fixtureInputModel(), |
| 78 | + }, |
| 79 | + { |
| 80 | + description: "no values", |
| 81 | + argValues: []string{}, |
| 82 | + flagValues: map[string]string{}, |
| 83 | + isValid: false, |
| 84 | + }, |
| 85 | + { |
| 86 | + description: "network-area-id missing", |
| 87 | + argValues: fixtureArgValues(), |
| 88 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 89 | + delete(flagValues, networkAreaIdFlag) |
| 90 | + }), |
| 91 | + isValid: false, |
| 92 | + }, |
| 93 | + { |
| 94 | + description: "org-id missing", |
| 95 | + argValues: fixtureArgValues(), |
| 96 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 97 | + delete(flagValues, organizationIdFlag) |
| 98 | + }), |
| 99 | + isValid: false, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.description, func(t *testing.T) { |
| 105 | + p := print.NewPrinter() |
| 106 | + cmd := NewCmd(¶ms.CmdParams{Printer: p}) |
| 107 | + err := globalflags.Configure(cmd.Flags()) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("configure global flags: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + for flag, value := range tt.flagValues { |
| 113 | + err := cmd.Flags().Set(flag, value) |
| 114 | + if err != nil { |
| 115 | + if !tt.isValid { |
| 116 | + return |
| 117 | + } |
| 118 | + t.Fatalf("setting flag --%s=%s: %v", flag, value, err) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + err = cmd.ValidateRequiredFlags() |
| 123 | + if err != nil { |
| 124 | + if !tt.isValid { |
| 125 | + return |
| 126 | + } |
| 127 | + t.Fatalf("error validating flags: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + model, err := parseInput(p, cmd, tt.argValues) |
| 131 | + if err != nil { |
| 132 | + if !tt.isValid { |
| 133 | + return |
| 134 | + } |
| 135 | + t.Fatalf("error parsing flags: %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + if !tt.isValid { |
| 139 | + t.Fatalf("did not fail on invalid input") |
| 140 | + } |
| 141 | + diff := cmp.Diff(model, tt.expectedModel) |
| 142 | + if diff != "" { |
| 143 | + t.Fatalf("Data does not match: %s", diff) |
| 144 | + } |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestOutputResult(t *testing.T) { |
| 150 | + dummyRouteTable := iaasalpha.RoutingTable{ |
| 151 | + CreatedAt: utils.Ptr(time.Now()), |
| 152 | + Default: nil, |
| 153 | + Description: utils.Ptr("description"), |
| 154 | + Id: utils.Ptr("route-foo"), |
| 155 | + Labels: utils.ConvertStringMapToInterfaceMap(testLabels), |
| 156 | + Name: utils.Ptr("route-foo"), |
| 157 | + SystemRoutes: utils.Ptr(true), |
| 158 | + UpdatedAt: utils.Ptr(time.Now()), |
| 159 | + } |
| 160 | + |
| 161 | + tests := []struct { |
| 162 | + name string |
| 163 | + outputFormat string |
| 164 | + routingTable iaasalpha.RoutingTable |
| 165 | + wantErr bool |
| 166 | + }{ |
| 167 | + { |
| 168 | + name: "json output with one route", |
| 169 | + outputFormat: print.JSONOutputFormat, |
| 170 | + routingTable: dummyRouteTable, |
| 171 | + wantErr: false, |
| 172 | + }, |
| 173 | + { |
| 174 | + name: "yaml output with one route", |
| 175 | + outputFormat: print.YAMLOutputFormat, |
| 176 | + routingTable: dummyRouteTable, |
| 177 | + wantErr: false, |
| 178 | + }, |
| 179 | + } |
| 180 | + |
| 181 | + p := print.NewPrinter() |
| 182 | + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) |
| 183 | + for _, tt := range tests { |
| 184 | + t.Run(tt.name, func(t *testing.T) { |
| 185 | + if err := outputResult(p, tt.outputFormat, &tt.routingTable); (err != nil) != tt.wantErr { |
| 186 | + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) |
| 187 | + } |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
0 commit comments