Skip to content

Commit f901bcc

Browse files
committed
cdi: test find devices
Signed-off-by: CrazyMax <[email protected]>
1 parent e500309 commit f901bcc

File tree

5 files changed

+131
-3
lines changed

5 files changed

+131
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cdiVersion: "0.6.0"
2+
kind: "vendor1.com/device"
3+
devices:
4+
- name: foo
5+
containerEdits:
6+
env:
7+
- FOO=injected
8+
annotations:
9+
org.mobyproject.buildkit.device.autoallow: true
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cdiVersion: "0.6.0"
2+
kind: "vendor1.com/deviceclass"
3+
annotations:
4+
foo.bar.baz: FOO
5+
org.mobyproject.buildkit.device.autoallow: true
6+
devices:
7+
- name: foo
8+
annotations:
9+
org.mobyproject.buildkit.device.class: class1
10+
containerEdits:
11+
env:
12+
- FOO=injected
13+
- name: bar
14+
annotations:
15+
org.mobyproject.buildkit.device.class: class1
16+
containerEdits:
17+
env:
18+
- BAR=injected
19+
- name: baz
20+
annotations:
21+
org.mobyproject.buildkit.device.class: class2
22+
containerEdits:
23+
env:
24+
- BAZ=injected
25+
- name: qux
26+
containerEdits:
27+
env:
28+
- QUX=injected
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cdiVersion: "0.6.0"
2+
kind: "vendor1.com/devicemulti"
3+
devices:
4+
- name: foo
5+
containerEdits:
6+
env:
7+
- FOO=injected
8+
- name: bar
9+
containerEdits:
10+
env:
11+
- BAR=injected
12+
- name: baz
13+
containerEdits:
14+
env:
15+
- BAZ=injected
16+
- name: qux
17+
containerEdits:
18+
env:
19+
- QUX=injected
20+
annotations:
21+
org.mobyproject.buildkit.device.autoallow: true

solver/llbsolver/cdidevices/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
)
1616

1717
const (
18-
deviceAnnotationClass = "org.mobyproject.buildkit.device.class"
19-
deviceAutoAllow = "org.mobyproject.buildkit.device.autoallow"
18+
deviceAnnotationClass = "org.mobyproject.buildkit.device.class"
19+
deviceAnnotationAutoAllow = "org.mobyproject.buildkit.device.autoallow"
2020
)
2121

2222
var installers = map[string]Setup{}
@@ -63,7 +63,7 @@ func (m *Manager) isAutoAllowed(kind, name string, annotations map[string]string
6363
if _, ok := m.autoAllowed[kind]; ok {
6464
return true
6565
}
66-
if v, ok := annotations[deviceAutoAllow]; ok {
66+
if v, ok := annotations[deviceAnnotationAutoAllow]; ok {
6767
if b, err := strconv.ParseBool(v); err == nil && b {
6868
return true
6969
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cdidevices
2+
3+
import (
4+
"testing"
5+
6+
"github.com/moby/buildkit/solver/pb"
7+
"github.com/stretchr/testify/require"
8+
"tags.cncf.io/container-device-interface/pkg/cdi"
9+
)
10+
11+
func TestFindDevices(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
devices []*pb.CDIDevice
15+
expected []string
16+
err bool
17+
}{
18+
{
19+
name: "Find specific device",
20+
devices: []*pb.CDIDevice{
21+
{Name: "vendor1.com/device=foo"},
22+
},
23+
expected: []string{"vendor1.com/device=foo"},
24+
},
25+
{
26+
name: "Find first devices",
27+
devices: []*pb.CDIDevice{
28+
{Name: "vendor1.com/devicemulti"},
29+
},
30+
expected: []string{"vendor1.com/devicemulti=bar"},
31+
},
32+
{
33+
name: "Find all devices of a kind",
34+
devices: []*pb.CDIDevice{
35+
{Name: "vendor1.com/devicemulti=*"},
36+
},
37+
expected: []string{"vendor1.com/devicemulti=foo", "vendor1.com/devicemulti=bar", "vendor1.com/devicemulti=baz", "vendor1.com/devicemulti=qux"},
38+
},
39+
{
40+
name: "Find devices by class",
41+
devices: []*pb.CDIDevice{
42+
{Name: "vendor1.com/deviceclass=class1"},
43+
},
44+
expected: []string{"vendor1.com/deviceclass=foo", "vendor1.com/deviceclass=bar"},
45+
},
46+
{
47+
name: "Device not found",
48+
devices: []*pb.CDIDevice{
49+
{Name: "vendor1.com/device=unknown"},
50+
},
51+
expected: nil,
52+
err: true,
53+
},
54+
}
55+
56+
for _, tt := range tests {
57+
t.Run(tt.name, func(t *testing.T) {
58+
cache, err := cdi.NewCache(cdi.WithSpecDirs("./fixtures"))
59+
require.NoError(t, err)
60+
manager := NewManager(cache, nil)
61+
result, err := manager.FindDevices(tt.devices...)
62+
if tt.err {
63+
require.Error(t, err)
64+
} else {
65+
require.NoError(t, err)
66+
require.ElementsMatch(t, tt.expected, result)
67+
}
68+
})
69+
}
70+
}

0 commit comments

Comments
 (0)