X Tutup
Skip to content

Commit 22cacbf

Browse files
committed
Fixes docker-archive-public#2451 ensure filters <key> and <value> work when case-insensitive
Added unit tests to verify filters Signed-off-by: Anil Belur <askb23@gmail.com>
1 parent d7d6ca2 commit 22cacbf

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

commands/ls.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func parseFilters(filters []string) (FilterOptions, error) {
113113
if len(kv) != 2 {
114114
return options, errors.New("Unsupported filter syntax.")
115115
}
116-
key, value := kv[0], kv[1]
116+
key, value := strings.ToLower(kv[0]), kv[1]
117117

118118
switch key {
119119
case "swarm":
@@ -176,7 +176,7 @@ func matchesSwarmName(host *host.Host, swarmNames []string, swarmMasters map[str
176176
}
177177
for _, n := range swarmNames {
178178
if host.HostOptions.SwarmOptions != nil {
179-
if n == swarmMasters[host.HostOptions.SwarmOptions.Discovery] {
179+
if strings.EqualFold(n, swarmMasters[host.HostOptions.SwarmOptions.Discovery]) {
180180
return true
181181
}
182182
}
@@ -189,7 +189,7 @@ func matchesDriverName(host *host.Host, driverNames []string) bool {
189189
return true
190190
}
191191
for _, n := range driverNames {
192-
if host.DriverName == n {
192+
if strings.EqualFold(host.DriverName, n) {
193193
return true
194194
}
195195
}
@@ -205,7 +205,7 @@ func matchesState(host *host.Host, states []string) bool {
205205
if err != nil {
206206
log.Warn(err)
207207
}
208-
if n == s.String() {
208+
if strings.EqualFold(n, s.String()) {
209209
return true
210210
}
211211
}

commands/ls_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ func TestParseFiltersAll(t *testing.T) {
4343
assert.Equal(t, actual, FilterOptions{SwarmName: []string{"foo"}, DriverName: []string{"bar"}, State: []string{"Stopped"}, Name: []string{"dev"}})
4444
}
4545

46+
func TestParseFiltersAllCase(t *testing.T) {
47+
actual, _ := parseFilters([]string{"sWarM=foo", "DrIver=bar", "StaTe=Stopped", "NAMe=dev"})
48+
assert.Equal(t, actual, FilterOptions{SwarmName: []string{"foo"}, DriverName: []string{"bar"}, State: []string{"Stopped"}, Name: []string{"dev"}})
49+
}
50+
4651
func TestParseFiltersDuplicates(t *testing.T) {
4752
actual, _ := parseFilters([]string{"swarm=foo", "driver=bar", "name=mark", "swarm=baz", "driver=qux", "state=Running", "state=Starting", "name=time"})
4853
assert.Equal(t, actual, FilterOptions{SwarmName: []string{"foo", "baz"}, DriverName: []string{"bar", "qux"}, State: []string{"Running", "Starting"}, Name: []string{"mark", "time"}})
@@ -53,6 +58,16 @@ func TestParseFiltersValueWithEqual(t *testing.T) {
5358
assert.Equal(t, actual, FilterOptions{DriverName: []string{"bar=baz"}})
5459
}
5560

61+
func TestFilterHostsReturnsFiltersValuesCaseInsensitive(t *testing.T) {
62+
opts := FilterOptions{
63+
SwarmName: []string{"fOo"},
64+
DriverName: []string{"ViRtUaLboX"},
65+
State: []string{"StOPpeD"},
66+
}
67+
hosts := []*host.Host{}
68+
actual := filterHosts(hosts, opts)
69+
assert.EqualValues(t, actual, hosts)
70+
}
5671
func TestFilterHostsReturnsSameGivenNoFilters(t *testing.T) {
5772
opts := FilterOptions{}
5873
hosts := []*host.Host{

0 commit comments

Comments
 (0)
X Tutup