github.com/damirazo/docker@v1.9.0/pkg/parsers/filters/parse_test.go (about) 1 package filters 2 3 import ( 4 "sort" 5 "testing" 6 ) 7 8 func TestParseArgs(t *testing.T) { 9 // equivalent of `docker ps -f 'created=today' -f 'image.name=ubuntu*' -f 'image.name=*untu'` 10 flagArgs := []string{ 11 "created=today", 12 "image.name=ubuntu*", 13 "image.name=*untu", 14 } 15 var ( 16 args = Args{} 17 err error 18 ) 19 for i := range flagArgs { 20 args, err = ParseFlag(flagArgs[i], args) 21 if err != nil { 22 t.Errorf("failed to parse %s: %s", flagArgs[i], err) 23 } 24 } 25 if len(args["created"]) != 1 { 26 t.Errorf("failed to set this arg") 27 } 28 if len(args["image.name"]) != 2 { 29 t.Errorf("the args should have collapsed") 30 } 31 } 32 33 func TestParseArgsEdgeCase(t *testing.T) { 34 var filters Args 35 args, err := ParseFlag("", filters) 36 if err != nil { 37 t.Fatal(err) 38 } 39 if args == nil || len(args) != 0 { 40 t.Fatalf("Expected an empty Args (map), got %v", args) 41 } 42 if args, err = ParseFlag("anything", args); err == nil || err != ErrBadFormat { 43 t.Fatalf("Expected ErrBadFormat, got %v", err) 44 } 45 } 46 47 func TestToParam(t *testing.T) { 48 a := Args{ 49 "created": []string{"today"}, 50 "image.name": []string{"ubuntu*", "*untu"}, 51 } 52 53 _, err := ToParam(a) 54 if err != nil { 55 t.Errorf("failed to marshal the filters: %s", err) 56 } 57 } 58 59 func TestFromParam(t *testing.T) { 60 invalids := []string{ 61 "anything", 62 "['a','list']", 63 "{'key': 'value'}", 64 `{"key": "value"}`, 65 } 66 valids := map[string]Args{ 67 `{"key": ["value"]}`: { 68 "key": {"value"}, 69 }, 70 `{"key": ["value1", "value2"]}`: { 71 "key": {"value1", "value2"}, 72 }, 73 `{"key1": ["value1"], "key2": ["value2"]}`: { 74 "key1": {"value1"}, 75 "key2": {"value2"}, 76 }, 77 } 78 for _, invalid := range invalids { 79 if _, err := FromParam(invalid); err == nil { 80 t.Fatalf("Expected an error with %v, got nothing", invalid) 81 } 82 } 83 for json, expectedArgs := range valids { 84 args, err := FromParam(json) 85 if err != nil { 86 t.Fatal(err) 87 } 88 if len(args) != len(expectedArgs) { 89 t.Fatalf("Expected %v, go %v", expectedArgs, args) 90 } 91 for key, expectedValues := range expectedArgs { 92 values := args[key] 93 sort.Strings(values) 94 sort.Strings(expectedValues) 95 if len(values) != len(expectedValues) { 96 t.Fatalf("Expected %v, go %v", expectedArgs, args) 97 } 98 for index, expectedValue := range expectedValues { 99 if values[index] != expectedValue { 100 t.Fatalf("Expected %v, go %v", expectedArgs, args) 101 } 102 } 103 } 104 } 105 } 106 107 func TestEmpty(t *testing.T) { 108 a := Args{} 109 v, err := ToParam(a) 110 if err != nil { 111 t.Errorf("failed to marshal the filters: %s", err) 112 } 113 v1, err := FromParam(v) 114 if err != nil { 115 t.Errorf("%s", err) 116 } 117 if len(a) != len(v1) { 118 t.Errorf("these should both be empty sets") 119 } 120 } 121 122 func TestArgsMatchKVList(t *testing.T) { 123 // empty sources 124 args := Args{ 125 "created": []string{"today"}, 126 } 127 if args.MatchKVList("created", map[string]string{}) { 128 t.Fatalf("Expected false for (%v,created), got true", args) 129 } 130 // Not empty sources 131 sources := map[string]string{ 132 "key1": "value1", 133 "key2": "value2", 134 "key3": "value3", 135 } 136 matches := map[*Args]string{ 137 &Args{}: "field", 138 &Args{ 139 "created": []string{"today"}, 140 "labels": []string{"key1"}, 141 }: "labels", 142 &Args{ 143 "created": []string{"today"}, 144 "labels": []string{"key1=value1"}, 145 }: "labels", 146 } 147 differs := map[*Args]string{ 148 &Args{ 149 "created": []string{"today"}, 150 }: "created", 151 &Args{ 152 "created": []string{"today"}, 153 "labels": []string{"key4"}, 154 }: "labels", 155 &Args{ 156 "created": []string{"today"}, 157 "labels": []string{"key1=value3"}, 158 }: "labels", 159 } 160 for args, field := range matches { 161 if args.MatchKVList(field, sources) != true { 162 t.Fatalf("Expected true for %v on %v, got false", sources, args) 163 } 164 } 165 for args, field := range differs { 166 if args.MatchKVList(field, sources) != false { 167 t.Fatalf("Expected false for %v on %v, got true", sources, args) 168 } 169 } 170 } 171 172 func TestArgsMatch(t *testing.T) { 173 source := "today" 174 matches := map[*Args]string{ 175 &Args{}: "field", 176 &Args{ 177 "created": []string{"today"}, 178 "labels": []string{"key1"}, 179 }: "today", 180 &Args{ 181 "created": []string{"to*"}, 182 }: "created", 183 &Args{ 184 "created": []string{"to(.*)"}, 185 }: "created", 186 &Args{ 187 "created": []string{"tod"}, 188 }: "created", 189 &Args{ 190 "created": []string{"anything", "to*"}, 191 }: "created", 192 } 193 differs := map[*Args]string{ 194 &Args{ 195 "created": []string{"tomorrow"}, 196 }: "created", 197 &Args{ 198 "created": []string{"to(day"}, 199 }: "created", 200 &Args{ 201 "created": []string{"tom(.*)"}, 202 }: "created", 203 &Args{ 204 "created": []string{"today1"}, 205 "labels": []string{"today"}, 206 }: "created", 207 } 208 for args, field := range matches { 209 if args.Match(field, source) != true { 210 t.Fatalf("Expected true for %v on %v, got false", source, args) 211 } 212 } 213 for args, field := range differs { 214 if args.Match(field, source) != false { 215 t.Fatalf("Expected false for %v on %v, got true", source, args) 216 } 217 } 218 }