|
| 1 | +package opts |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestParseLogOptions(t *testing.T) { |
| 9 | + type tCases struct { |
| 10 | + driver string |
| 11 | + logOpts []string |
| 12 | + |
| 13 | + hasError bool |
| 14 | + expected map[string]string |
| 15 | + } |
| 16 | + |
| 17 | + for idx, tc := range []tCases{ |
| 18 | + { |
| 19 | + driver: "", |
| 20 | + logOpts: nil, |
| 21 | + hasError: false, |
| 22 | + expected: map[string]string{}, |
| 23 | + }, { |
| 24 | + driver: "none", |
| 25 | + logOpts: nil, |
| 26 | + hasError: false, |
| 27 | + expected: map[string]string{}, |
| 28 | + }, { |
| 29 | + driver: "none", |
| 30 | + logOpts: []string{"haha"}, |
| 31 | + hasError: true, |
| 32 | + expected: nil, |
| 33 | + }, { |
| 34 | + driver: "none", |
| 35 | + logOpts: []string{"test=1"}, |
| 36 | + hasError: true, |
| 37 | + expected: nil, |
| 38 | + }, { |
| 39 | + driver: "json-file", |
| 40 | + logOpts: []string{"test=1"}, |
| 41 | + hasError: false, |
| 42 | + expected: map[string]string{ |
| 43 | + "test": "1", |
| 44 | + }, |
| 45 | + }, { |
| 46 | + driver: "json-file", |
| 47 | + logOpts: []string{"test=1=1"}, |
| 48 | + hasError: false, |
| 49 | + expected: map[string]string{ |
| 50 | + "test": "1=1", |
| 51 | + }, |
| 52 | + }, { |
| 53 | + driver: "json-file", |
| 54 | + logOpts: []string{"test=1", "flag=oops", "test=2"}, |
| 55 | + hasError: false, |
| 56 | + expected: map[string]string{ |
| 57 | + "test": "2", |
| 58 | + "flag": "oops", |
| 59 | + }, |
| 60 | + }, |
| 61 | + } { |
| 62 | + got, err := ParseLogOptions(tc.driver, tc.logOpts) |
| 63 | + if err == nil && tc.hasError { |
| 64 | + t.Fatalf("[%d case] should have error here, but got nothing", idx) |
| 65 | + } |
| 66 | + if err != nil && !tc.hasError { |
| 67 | + t.Fatalf("[%d case] should have no error here, but got error(%v)", idx, err) |
| 68 | + } |
| 69 | + |
| 70 | + if !reflect.DeepEqual(got, tc.expected) { |
| 71 | + t.Fatalf("[%d case] should have (%v), but got (%v)", idx, tc.expected, got) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments