gitlab.com/gitlab-org/labkit@v1.21.0/tracing/connstr/connection_string_parser_test.go (about) 1 package connstr 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func Test_Parse(t *testing.T) { 9 tests := []struct { 10 name string 11 connectionString string 12 wantDriverName string 13 wantOptions map[string]string 14 wantErr bool 15 }{ 16 { 17 name: "empty", 18 connectionString: "", 19 wantErr: true, 20 }, 21 { 22 name: "invalid", 23 connectionString: "notopentracing://something", 24 wantErr: true, 25 }, 26 { 27 name: "invalidport", 28 connectionString: "opentracing://something:port", 29 wantErr: true, 30 }, 31 { 32 name: "jaeger", 33 connectionString: "opentracing://jaeger", 34 wantDriverName: "jaeger", 35 wantOptions: map[string]string{}, 36 wantErr: false, 37 }, 38 { 39 name: "datadog", 40 connectionString: "opentracing://datadog", 41 wantDriverName: "datadog", 42 wantOptions: map[string]string{}, 43 wantErr: false, 44 }, 45 { 46 name: "lightstep", 47 connectionString: "opentracing://lightstep?api_key=123", 48 wantDriverName: "lightstep", 49 wantOptions: map[string]string{ 50 "api_key": "123", 51 }, 52 wantErr: false, 53 }, 54 { 55 name: "path", 56 connectionString: "opentracing://lightstep/123?api_key=123", 57 wantErr: true, 58 }, 59 { 60 name: "multiple_params", 61 connectionString: "opentracing://lightstep?api_key=123&host=localhost:1234", 62 wantDriverName: "lightstep", 63 wantOptions: map[string]string{ 64 "api_key": "123", 65 "host": "localhost:1234", 66 }, 67 wantErr: false, 68 }, 69 { 70 name: "repeat_params", 71 connectionString: "opentracing://lightstep?api_key=123&host=localhost:1234&host=moo", 72 wantDriverName: "lightstep", 73 wantOptions: map[string]string{ 74 "api_key": "123", 75 "host": "localhost:1234", 76 }, 77 wantErr: false, 78 }, 79 } 80 for _, tt := range tests { 81 t.Run(tt.name, func(t *testing.T) { 82 gotDriverName, getOptions, err := Parse(tt.connectionString) 83 if (err != nil) != tt.wantErr { 84 t.Errorf("ParseConnectionString() error = %v, wantErr %v", err, tt.wantErr) 85 return 86 } 87 88 if !reflect.DeepEqual(gotDriverName, tt.wantDriverName) { 89 t.Errorf("ParseConnectionString().driverName = %v, want %v", gotDriverName, tt.wantDriverName) 90 } 91 92 if !reflect.DeepEqual(getOptions, tt.wantOptions) { 93 t.Errorf("ParseConnectionString().options = %v, want %v", getOptions, tt.wantOptions) 94 } 95 }) 96 } 97 }