Skip to content

Commit 2ba2520

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

File tree

5 files changed

+54
-80
lines changed

5 files changed

+54
-80
lines changed

cmd/incus/image_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
func TestPrepareImageServerFilters(t *testing.T) {
12+
filters := []string{"foo", "requirements.secureboot=false", "type=container"}
13+
14+
result := prepareImageServerFilters(filters, api.InstanceFull{})
15+
assert.Equal(t, []string{"properties.requirements.secureboot=false", "type=container"}, result)
16+
}

cmd/incus/list_test.go

Lines changed: 15 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,12 @@ 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
)
1315

14-
func TestDotPrefixMatch(t *testing.T) {
15-
list := cmdList{}
16-
17-
pass := true
18-
pass = pass && list.dotPrefixMatch("s.privileged", "security.privileged")
19-
pass = pass && list.dotPrefixMatch("u.blah", "user.blah")
20-
21-
if !pass {
22-
t.Error("failed prefix matching")
23-
}
24-
}
25-
2616
func TestShouldShow(t *testing.T) {
2717
list := cmdList{}
2818
inst := &api.Instance{
@@ -67,85 +57,29 @@ func TestShouldShow(t *testing.T) {
6757
},
6858
}
6959

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) {
60+
if !list.shouldShow([]string{"ipv4=10.29.85.0/24"}, inst, state) {
12361
t.Errorf("net=10.29.85.0/24 filter didn't work")
12462
}
12563

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

130-
if !list.shouldShow([]string{"ipv4=10.29.85.156"}, inst, state, false) {
68+
if !list.shouldShow([]string{"ipv4=10.29.85.156"}, inst, state) {
13169
t.Errorf("net=10.29.85.156 filter did not work")
13270
}
13371

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

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

142-
if !list.shouldShow([]string{"ipv6=fd42:72a:89ac:e457:1266:6aff:fe83:ffff/1"}, inst, state, false) {
80+
if !list.shouldShow([]string{"ipv6=fd42:72a:89ac:e457:1266:6aff:fe83:ffff/1"}, inst, state) {
14381
t.Errorf("net=fd42:72a:89ac:e457:1266:6aff:fe83:ffff/1 filter filter didn't work")
14482
}
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-
}
14983
}
15084

15185
// Used by TestColumns and TestInvalidColumns.
@@ -340,3 +274,10 @@ func TestInvalidColumns(t *testing.T) {
340274
run("config:image")
341275
run("devices:eth0")
342276
}
277+
278+
func TestPrepareInstanceServerFilters(t *testing.T) {
279+
filters := []string{"foo", "user.a=blah", "name=v1", "state=running"}
280+
281+
result := prepareInstanceServerFilters(filters, api.InstanceFull{})
282+
assert.Equal(t, []string{"name=(^foo$|^foo.*)", "expanded_config.user.a=blah", "name=v1", "status=running"}, result)
283+
}

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import (
1010
"github.com/lxc/incus/v6/shared/api"
1111
)
1212

13+
func TestDotPrefixMatch(t *testing.T) {
14+
pass := true
15+
pass = pass && filter.DotPrefixMatch("s.privileged", "security.privileged")
16+
pass = pass && filter.DotPrefixMatch("u.blah", "user.blah")
17+
18+
if !pass {
19+
t.Error("failed prefix matching")
20+
}
21+
}
22+
1323
func TestValueOf_Instance(t *testing.T) {
1424
date := time.Date(2020, 1, 29, 11, 10, 32, 0, time.UTC)
1525
instance := api.Instance{
@@ -41,6 +51,7 @@ func TestValueOf_Instance(t *testing.T) {
4151
cases["config.image.os"] = "BusyBox"
4252
cases["name"] = "c1"
4353
cases["expanded_config.image.os"] = "BusyBox"
54+
cases["expanded_config.im.os"] = "BusyBox"
4455
cases["expanded_devices.root.pool"] = "default"
4556
cases["status"] = "Running"
4657
cases["stateful"] = false

0 commit comments

Comments
 (0)