github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/elasticsearch/elasticsearch_test.go (about) 1 package elasticsearch_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/fastly/go-fastly/v9/fastly" 8 9 "github.com/fastly/cli/pkg/argparser" 10 "github.com/fastly/cli/pkg/commands/logging/elasticsearch" 11 "github.com/fastly/cli/pkg/config" 12 "github.com/fastly/cli/pkg/errors" 13 "github.com/fastly/cli/pkg/global" 14 "github.com/fastly/cli/pkg/manifest" 15 "github.com/fastly/cli/pkg/mock" 16 "github.com/fastly/cli/pkg/testutil" 17 ) 18 19 func TestCreateElasticsearchInput(t *testing.T) { 20 for _, testcase := range []struct { 21 name string 22 cmd *elasticsearch.CreateCommand 23 want *fastly.CreateElasticsearchInput 24 wantError string 25 }{ 26 { 27 name: "required values set flag serviceID", 28 cmd: createCommandRequired(), 29 want: &fastly.CreateElasticsearchInput{ 30 ServiceID: "123", 31 ServiceVersion: 4, 32 Name: fastly.ToPointer("log"), 33 Index: fastly.ToPointer("logs"), 34 URL: fastly.ToPointer("example.com"), 35 }, 36 }, 37 { 38 name: "all values set flag serviceID", 39 cmd: createCommandAll(), 40 want: &fastly.CreateElasticsearchInput{ 41 ServiceID: "123", 42 ServiceVersion: 4, 43 Name: fastly.ToPointer("logs"), 44 ResponseCondition: fastly.ToPointer("Prevent default logging"), 45 Format: fastly.ToPointer(`%h %l %u %t "%r" %>s %b`), 46 Index: fastly.ToPointer("logs"), 47 URL: fastly.ToPointer("example.com"), 48 Pipeline: fastly.ToPointer("my_pipeline_id"), 49 User: fastly.ToPointer("user"), 50 Password: fastly.ToPointer("password"), 51 RequestMaxEntries: fastly.ToPointer(2), 52 RequestMaxBytes: fastly.ToPointer(2), 53 Placement: fastly.ToPointer("none"), 54 TLSCACert: fastly.ToPointer("-----BEGIN CERTIFICATE-----foo"), 55 TLSHostname: fastly.ToPointer("example.com"), 56 TLSClientCert: fastly.ToPointer("-----BEGIN CERTIFICATE-----bar"), 57 TLSClientKey: fastly.ToPointer("-----BEGIN PRIVATE KEY-----bar"), 58 FormatVersion: fastly.ToPointer(2), 59 }, 60 }, 61 { 62 name: "error missing serviceID", 63 cmd: createCommandMissingServiceID(), 64 want: nil, 65 wantError: errors.ErrNoServiceID.Error(), 66 }, 67 } { 68 t.Run(testcase.name, func(t *testing.T) { 69 var bs []byte 70 out := bytes.NewBuffer(bs) 71 verboseMode := true 72 73 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 74 AutoCloneFlag: testcase.cmd.AutoClone, 75 APIClient: testcase.cmd.Globals.APIClient, 76 Manifest: testcase.cmd.Manifest, 77 Out: out, 78 ServiceVersionFlag: testcase.cmd.ServiceVersion, 79 VerboseMode: verboseMode, 80 }) 81 82 switch { 83 case err != nil && testcase.wantError == "": 84 t.Fatalf("unexpected error getting service details: %v", err) 85 return 86 case err != nil && testcase.wantError != "": 87 testutil.AssertErrorContains(t, err, testcase.wantError) 88 return 89 case err == nil && testcase.wantError != "": 90 t.Fatalf("expected error, have nil (service details: %s, %d)", serviceID, serviceVersion.Number) 91 case err == nil && testcase.wantError == "": 92 have, err := testcase.cmd.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 93 testutil.AssertErrorContains(t, err, testcase.wantError) 94 testutil.AssertEqual(t, testcase.want, have) 95 } 96 }) 97 } 98 } 99 100 func TestUpdateElasticsearchInput(t *testing.T) { 101 scenarios := []struct { 102 name string 103 cmd *elasticsearch.UpdateCommand 104 api mock.API 105 want *fastly.UpdateElasticsearchInput 106 wantError string 107 }{ 108 { 109 name: "all values set flag serviceID", 110 cmd: updateCommandAll(), 111 api: mock.API{ 112 ListVersionsFn: testutil.ListVersions, 113 CloneVersionFn: testutil.CloneVersionResult(4), 114 GetElasticsearchFn: getElasticsearchOK, 115 }, 116 want: &fastly.UpdateElasticsearchInput{ 117 ServiceID: "123", 118 ServiceVersion: 4, 119 Name: "log", 120 NewName: fastly.ToPointer("new1"), 121 Index: fastly.ToPointer("new2"), 122 URL: fastly.ToPointer("new3"), 123 Pipeline: fastly.ToPointer("new4"), 124 User: fastly.ToPointer("new5"), 125 Password: fastly.ToPointer("new6"), 126 RequestMaxEntries: fastly.ToPointer(3), 127 RequestMaxBytes: fastly.ToPointer(3), 128 Placement: fastly.ToPointer("new7"), 129 Format: fastly.ToPointer("new8"), 130 FormatVersion: fastly.ToPointer(3), 131 ResponseCondition: fastly.ToPointer("new9"), 132 TLSCACert: fastly.ToPointer("new10"), 133 TLSClientCert: fastly.ToPointer("new11"), 134 TLSClientKey: fastly.ToPointer("new12"), 135 TLSHostname: fastly.ToPointer("new13"), 136 }, 137 }, 138 { 139 name: "no updates", 140 cmd: updateCommandNoUpdates(), 141 api: mock.API{ 142 ListVersionsFn: testutil.ListVersions, 143 CloneVersionFn: testutil.CloneVersionResult(4), 144 GetElasticsearchFn: getElasticsearchOK, 145 }, 146 want: &fastly.UpdateElasticsearchInput{ 147 ServiceID: "123", 148 ServiceVersion: 4, 149 Name: "log", 150 }, 151 }, 152 { 153 name: "error missing serviceID", 154 cmd: updateCommandMissingServiceID(), 155 want: nil, 156 wantError: errors.ErrNoServiceID.Error(), 157 }, 158 } 159 for testcaseIdx := range scenarios { 160 testcase := &scenarios[testcaseIdx] 161 t.Run(testcase.name, func(t *testing.T) { 162 testcase.cmd.Globals.APIClient = testcase.api 163 164 var bs []byte 165 out := bytes.NewBuffer(bs) 166 verboseMode := true 167 168 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 169 AutoCloneFlag: testcase.cmd.AutoClone, 170 APIClient: testcase.api, 171 Manifest: testcase.cmd.Manifest, 172 Out: out, 173 ServiceVersionFlag: testcase.cmd.ServiceVersion, 174 VerboseMode: verboseMode, 175 }) 176 177 switch { 178 case err != nil && testcase.wantError == "": 179 t.Fatalf("unexpected error getting service details: %v", err) 180 return 181 case err != nil && testcase.wantError != "": 182 testutil.AssertErrorContains(t, err, testcase.wantError) 183 return 184 case err == nil && testcase.wantError != "": 185 t.Fatalf("expected error, have nil (service details: %s, %d)", serviceID, serviceVersion.Number) 186 case err == nil && testcase.wantError == "": 187 have, err := testcase.cmd.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 188 testutil.AssertErrorContains(t, err, testcase.wantError) 189 testutil.AssertEqual(t, testcase.want, have) 190 } 191 }) 192 } 193 } 194 195 func createCommandRequired() *elasticsearch.CreateCommand { 196 var b bytes.Buffer 197 198 globals := global.Data{ 199 Config: config.File{}, 200 Env: config.Environment{}, 201 Output: &b, 202 } 203 globals.APIClient, _ = mock.APIClient(mock.API{ 204 ListVersionsFn: testutil.ListVersions, 205 CloneVersionFn: testutil.CloneVersionResult(4), 206 })("token", "endpoint", false) 207 208 return &elasticsearch.CreateCommand{ 209 Base: argparser.Base{ 210 Globals: &globals, 211 }, 212 Manifest: manifest.Data{ 213 Flag: manifest.Flag{ 214 ServiceID: "123", 215 }, 216 }, 217 ServiceVersion: argparser.OptionalServiceVersion{ 218 OptionalString: argparser.OptionalString{Value: "1"}, 219 }, 220 AutoClone: argparser.OptionalAutoClone{ 221 OptionalBool: argparser.OptionalBool{ 222 Optional: argparser.Optional{ 223 WasSet: true, 224 }, 225 Value: true, 226 }, 227 }, 228 EndpointName: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "log"}, 229 Index: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "logs"}, 230 URL: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "example.com"}, 231 } 232 } 233 234 func createCommandAll() *elasticsearch.CreateCommand { 235 var b bytes.Buffer 236 237 g := global.Data{ 238 Config: config.File{}, 239 Env: config.Environment{}, 240 Output: &b, 241 } 242 g.APIClient, _ = mock.APIClient(mock.API{ 243 ListVersionsFn: testutil.ListVersions, 244 CloneVersionFn: testutil.CloneVersionResult(4), 245 })("token", "endpoint", false) 246 247 return &elasticsearch.CreateCommand{ 248 Base: argparser.Base{ 249 Globals: &g, 250 }, 251 Manifest: manifest.Data{ 252 Flag: manifest.Flag{ 253 ServiceID: "123", 254 }, 255 }, 256 ServiceVersion: argparser.OptionalServiceVersion{ 257 OptionalString: argparser.OptionalString{Value: "1"}, 258 }, 259 AutoClone: argparser.OptionalAutoClone{ 260 OptionalBool: argparser.OptionalBool{ 261 Optional: argparser.Optional{ 262 WasSet: true, 263 }, 264 Value: true, 265 }, 266 }, 267 EndpointName: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "logs"}, 268 Index: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "logs"}, 269 URL: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "example.com"}, 270 Pipeline: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "my_pipeline_id"}, 271 RequestMaxEntries: argparser.OptionalInt{Optional: argparser.Optional{WasSet: true}, Value: 2}, 272 RequestMaxBytes: argparser.OptionalInt{Optional: argparser.Optional{WasSet: true}, Value: 2}, 273 Format: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: `%h %l %u %t "%r" %>s %b`}, 274 FormatVersion: argparser.OptionalInt{Optional: argparser.Optional{WasSet: true}, Value: 2}, 275 ResponseCondition: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "Prevent default logging"}, 276 Placement: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "none"}, 277 User: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "user"}, 278 Password: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "password"}, 279 TLSCACert: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "-----BEGIN CERTIFICATE-----foo"}, 280 TLSHostname: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "example.com"}, 281 TLSClientCert: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "-----BEGIN CERTIFICATE-----bar"}, 282 TLSClientKey: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "-----BEGIN PRIVATE KEY-----bar"}, 283 } 284 } 285 286 func createCommandMissingServiceID() *elasticsearch.CreateCommand { 287 res := createCommandAll() 288 res.Manifest = manifest.Data{} 289 return res 290 } 291 292 func updateCommandNoUpdates() *elasticsearch.UpdateCommand { 293 var b bytes.Buffer 294 295 g := global.Data{ 296 Config: config.File{}, 297 Env: config.Environment{}, 298 Output: &b, 299 } 300 301 return &elasticsearch.UpdateCommand{ 302 Base: argparser.Base{ 303 Globals: &g, 304 }, 305 Manifest: manifest.Data{ 306 Flag: manifest.Flag{ 307 ServiceID: "123", 308 }, 309 }, 310 EndpointName: "log", 311 ServiceVersion: argparser.OptionalServiceVersion{ 312 OptionalString: argparser.OptionalString{Value: "1"}, 313 }, 314 AutoClone: argparser.OptionalAutoClone{ 315 OptionalBool: argparser.OptionalBool{ 316 Optional: argparser.Optional{ 317 WasSet: true, 318 }, 319 Value: true, 320 }, 321 }, 322 } 323 } 324 325 func updateCommandAll() *elasticsearch.UpdateCommand { 326 var b bytes.Buffer 327 328 g := global.Data{ 329 Config: config.File{}, 330 Env: config.Environment{}, 331 Output: &b, 332 } 333 334 return &elasticsearch.UpdateCommand{ 335 Base: argparser.Base{ 336 Globals: &g, 337 }, 338 Manifest: manifest.Data{ 339 Flag: manifest.Flag{ 340 ServiceID: "123", 341 }, 342 }, 343 EndpointName: "log", 344 ServiceVersion: argparser.OptionalServiceVersion{ 345 OptionalString: argparser.OptionalString{Value: "1"}, 346 }, 347 AutoClone: argparser.OptionalAutoClone{ 348 OptionalBool: argparser.OptionalBool{ 349 Optional: argparser.Optional{ 350 WasSet: true, 351 }, 352 Value: true, 353 }, 354 }, 355 NewName: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new1"}, 356 Index: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new2"}, 357 URL: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new3"}, 358 Pipeline: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new4"}, 359 RequestMaxEntries: argparser.OptionalInt{Optional: argparser.Optional{WasSet: true}, Value: 3}, 360 RequestMaxBytes: argparser.OptionalInt{Optional: argparser.Optional{WasSet: true}, Value: 3}, 361 User: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new5"}, 362 Password: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new6"}, 363 Placement: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new7"}, 364 Format: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new8"}, 365 FormatVersion: argparser.OptionalInt{Optional: argparser.Optional{WasSet: true}, Value: 3}, 366 ResponseCondition: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new9"}, 367 TLSCACert: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new10"}, 368 TLSClientCert: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new11"}, 369 TLSClientKey: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new12"}, 370 TLSHostname: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new13"}, 371 } 372 } 373 374 func updateCommandMissingServiceID() *elasticsearch.UpdateCommand { 375 res := updateCommandAll() 376 res.Manifest = manifest.Data{} 377 return res 378 }