Skip to content

Commit b755d74

Browse files
committed
tests: Added/Fixed tests for server-side filtering
Signed-off-by: Piotr Resztak <[email protected]>
1 parent b112620 commit b755d74

File tree

5 files changed

+45
-68
lines changed

5 files changed

+45
-68
lines changed

cmd/incus/image_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/lxc/incus/v6/shared/api"
9+
)
10+
11+
12+
func TestPrepareImageServerFilters(t *testing.T) {
13+
filters := []string{"foo", "requirements.secureboot=false", "type=container"}
14+
15+
result := prepareImageServerFilters(filters, api.InstanceFull{})
16+
assert.Equal(t, []string{"properties.requirements.secureboot=false", "type=container"}, result)
17+
}

cmd/incus/list_test.go

Lines changed: 15 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"strings"
88
"testing"
99

10+
"github.com/stretchr/testify/assert"
11+
1012
"github.com/lxc/incus/v6/internal/instance"
1113
"github.com/lxc/incus/v6/shared/api"
1214
)
@@ -67,85 +69,29 @@ func TestShouldShow(t *testing.T) {
6769
},
6870
}
6971

70-
if !list.shouldShow([]string{"u.blah=abc"}, inst, nil, false) {
71-
t.Error("u.blah=abc didn't match")
72-
}
73-
74-
if !list.shouldShow([]string{"user.blah=abc"}, inst, nil, false) {
75-
t.Error("user.blah=abc didn't match")
76-
}
77-
78-
if !list.shouldShow([]string{"status=RUNNING", "user.blah=abc"}, inst, nil, false) {
79-
t.Error("user.blah=abc status=RUNNING didn't match")
80-
}
81-
82-
if !list.shouldShow([]string{"image.os=Debian", "user.blah=abc"}, inst, nil, false) {
83-
t.Error("user.blah=abc os=debian didn't match")
84-
}
85-
86-
if !list.shouldShow([]string{"location=mem-brain", "user.blah=abc"}, inst, nil, false) {
87-
t.Error("user.blah=abc location=mem-brain didn't match")
88-
}
89-
90-
if !list.shouldShow([]string{"architecture=potato", "user.blah=abc"}, inst, nil, false) {
91-
t.Error("user.blah=abc architecture=potato didn't match")
92-
}
93-
94-
if !list.shouldShow([]string{"foo", "user.blah=abc"}, inst, nil, false) {
95-
t.Error("user.blah=abc foo didn't match")
96-
}
97-
98-
if list.shouldShow([]string{"image.os=temple-os", "user.blah=abc"}, inst, nil, false) {
99-
t.Error("user.blah=abc image.os=temple-os did match")
100-
}
101-
102-
if list.shouldShow([]string{"status=RUNNING", "type=virtual-machine", "user.blah=abc"}, inst, nil, false) {
103-
t.Error("user.blah=abc status=RUNNING, type=virtual-machine did match ")
104-
}
105-
106-
if list.shouldShow([]string{"status=FROZEN,STOPPED"}, inst, nil, false) {
107-
t.Error("status=FROZEN,STOPPED did not match ")
108-
}
109-
110-
if !list.shouldShow([]string{"status=RUNNING,STOPPED"}, inst, nil, false) {
111-
t.Error("status=RUNNING,STOPPED did not match ")
112-
}
113-
114-
if !list.shouldShow([]string{"type=container", "user.blah=abc"}, inst, nil, false) {
115-
t.Error("user.blah=abc type=container didn't match")
116-
}
117-
118-
if list.shouldShow([]string{"bar", "u.blah=other"}, inst, nil, false) {
119-
t.Errorf("value filter didn't work")
120-
}
121-
122-
if !list.shouldShow([]string{"ipv4=10.29.85.0/24"}, inst, state, false) {
72+
if !list.shouldShow([]string{"ipv4=10.29.85.0/24"}, inst, state) {
12373
t.Errorf("net=10.29.85.0/24 filter didn't work")
12474
}
12575

126-
if list.shouldShow([]string{"ipv4=10.29.85.0/32"}, inst, state, false) {
76+
if list.shouldShow([]string{"ipv4=10.29.85.0/32"}, inst, state) {
12777
t.Errorf("net=10.29.85.0/32 filter did work but should not")
12878
}
12979

130-
if !list.shouldShow([]string{"ipv4=10.29.85.156"}, inst, state, false) {
80+
if !list.shouldShow([]string{"ipv4=10.29.85.156"}, inst, state) {
13181
t.Errorf("net=10.29.85.156 filter did not work")
13282
}
13383

134-
if !list.shouldShow([]string{"ipv6=fd42:72a:89ac:e457:1266:6aff:fe83:8301"}, inst, state, false) {
84+
if !list.shouldShow([]string{"ipv6=fd42:72a:89ac:e457:1266:6aff:fe83:8301"}, inst, state) {
13585
t.Errorf("net=fd42:72a:89ac:e457:1266:6aff:fe83:8301 filter didn't work")
13686
}
13787

138-
if list.shouldShow([]string{"ipv6=fd42:072a:89ac:e457:1266:6aff:fe83:ffff/128"}, inst, state, false) {
88+
if list.shouldShow([]string{"ipv6=fd42:072a:89ac:e457:1266:6aff:fe83:ffff/128"}, inst, state) {
13989
t.Errorf("net=1net=fd42:072a:89ac:e457:1266:6aff:fe83:ffff/128 filter did work but should not")
14090
}
14191

142-
if !list.shouldShow([]string{"ipv6=fd42:72a:89ac:e457:1266:6aff:fe83:ffff/1"}, inst, state, false) {
92+
if !list.shouldShow([]string{"ipv6=fd42:72a:89ac:e457:1266:6aff:fe83:ffff/1"}, inst, state) {
14393
t.Errorf("net=fd42:72a:89ac:e457:1266:6aff:fe83:ffff/1 filter filter didn't work")
14494
}
145-
146-
if list.shouldShow([]string{"user.blah=abc", "status=stopped"}, inst, nil, false) {
147-
t.Error("user.blah=abc status=stopped did match even though container status is 'running'")
148-
}
14995
}
15096

15197
// Used by TestColumns and TestInvalidColumns.
@@ -340,3 +286,10 @@ func TestInvalidColumns(t *testing.T) {
340286
run("config:image")
341287
run("devices:eth0")
342288
}
289+
290+
func TestPrepareInstanceServerFilters(t *testing.T) {
291+
filters := []string{"foo", "user.a=blah", "name=v1", "state=running"}
292+
293+
result := prepareInstanceServerFilters(filters, api.InstanceFull{})
294+
assert.Equal(t, []string{"name=(^foo$|^foo.*)", "expanded_config.user.a=blah", "name=v1", "status=running"}, result)
295+
}

cmd/incus/utils_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,15 @@ func (s *utilsTestSuite) TestGetServerSupportedFilters() {
7878
"foo", "type=container", "user.blah=a", "status=running,stopped",
7979
}
8080

81-
supportedFilters, unsupportedFilters := getServerSupportedFilters(filters, api.InstanceFull{}, false)
82-
s.Equal([]string{"type=container"}, supportedFilters)
83-
s.Equal([]string{"foo", "user.blah=a", "status=running,stopped"}, unsupportedFilters)
81+
supportedFilters, unsupportedFilters := getServerSupportedFilters(filters, []string{}, false)
82+
s.Equal([]string{"type=container", "user.blah=a", "status=running,stopped"}, supportedFilters)
83+
s.Equal([]string{"foo"}, unsupportedFilters)
8484

85-
supportedFilters, unsupportedFilters = getServerSupportedFilters(filters, api.InstanceFull{}, true)
86-
s.Equal([]string{"foo", "type=container"}, supportedFilters)
87-
s.Equal([]string{"user.blah=a", "status=running,stopped"}, unsupportedFilters)
85+
supportedFilters, unsupportedFilters = getServerSupportedFilters(filters, []string{}, true)
86+
s.Equal([]string{"foo", "type=container", "user.blah=a", "status=running,stopped"}, supportedFilters)
87+
s.Equal([]string{}, unsupportedFilters)
88+
89+
supportedFilters, unsupportedFilters = getServerSupportedFilters(filters, []string{"type", "status"}, true)
90+
s.Equal([]string{"foo", "user.blah=a"}, supportedFilters)
91+
s.Equal([]string{"type=container", "status=running,stopped"}, unsupportedFilters)
8892
}

internal/filter/match_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ func TestMatch_Instance(t *testing.T) {
4242
"config.image.os eq BusyBox and expanded_devices.root.path eq /": true,
4343
"name eq c2 or status eq Running": true,
4444
"name eq c2 or name eq c3": false,
45+
"status eq Running,Stopped": true,
46+
"name eq c2,c3": false,
4547
}
4648

4749
for s := range cases {

internal/filter/value_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestValueOf_Instance(t *testing.T) {
4141
cases["config.image.os"] = "BusyBox"
4242
cases["name"] = "c1"
4343
cases["expanded_config.image.os"] = "BusyBox"
44+
cases["expanded_config.im.os"] = "BusyBox"
4445
cases["expanded_devices.root.pool"] = "default"
4546
cases["status"] = "Running"
4647
cases["stateful"] = false

0 commit comments

Comments
 (0)