@@ -4,20 +4,41 @@ package diskio
44
55import (
66 "fmt"
7+ "path/filepath"
78 "testing"
89
910 "github.com/stretchr/testify/require"
11+ "golang.org/x/sys/unix"
1012)
1113
14+ func clearHostEnv (t * testing.T ) {
15+ t .Helper ()
16+
17+ t .Setenv ("HOST_DEV" , "" )
18+ t .Setenv ("HOST_RUN" , "" )
19+ t .Setenv ("HOST_SYS" , "" )
20+ t .Setenv ("HOST_ROOT" , "" )
21+ t .Setenv ("HOST_MOUNT_PREFIX" , "" )
22+ }
23+
24+ func newPlugin (t * testing.T ) * DiskIO {
25+ t .Helper ()
26+
27+ plugin := & DiskIO {}
28+ require .NoError (t , plugin .Init ())
29+ return plugin
30+ }
31+
1232func TestDiskInfo (t * testing.T ) {
13- plugin := & DiskIO {
14- infoCache : map [string ]diskInfoCache {
15- "null" : {
16- modifiedAt : 0 ,
17- udevDataPath : "testdata/udev.txt" ,
18- sysBlockPath : "testdata" ,
19- values : map [string ]string {},
20- },
33+ clearHostEnv (t )
34+
35+ plugin := newPlugin (t )
36+ plugin .infoCache = map [string ]diskInfoCache {
37+ "null" : {
38+ modifiedAt : 0 ,
39+ udevDataPath : "testdata/udev.txt" ,
40+ sysBlockPath : "testdata" ,
41+ values : map [string ]string {},
2142 },
2243 }
2344
@@ -48,15 +69,16 @@ func TestDiskIOStats_diskName(t *testing.T) {
4869
4970 for i , tc := range tests {
5071 t .Run (fmt .Sprintf ("template %d" , i ), func (t * testing.T ) {
51- plugin := DiskIO {
52- NameTemplates : tc .templates ,
53- infoCache : map [string ]diskInfoCache {
54- "null" : {
55- modifiedAt : 0 ,
56- udevDataPath : "testdata/udev.txt" ,
57- sysBlockPath : "testdata" ,
58- values : map [string ]string {},
59- },
72+ clearHostEnv (t )
73+
74+ plugin := newPlugin (t )
75+ plugin .NameTemplates = tc .templates
76+ plugin .infoCache = map [string ]diskInfoCache {
77+ "null" : {
78+ modifiedAt : 0 ,
79+ udevDataPath : "testdata/udev.txt" ,
80+ sysBlockPath : "testdata" ,
81+ values : map [string ]string {},
6082 },
6183 }
6284 name , _ := plugin .diskName ("null" )
@@ -68,17 +90,105 @@ func TestDiskIOStats_diskName(t *testing.T) {
6890// DiskIOStats.diskTags isn't a linux specific function, but dependent
6991// functions are a no-op on non-Linux.
7092func TestDiskIOStats_diskTags (t * testing.T ) {
71- plugin := & DiskIO {
72- DeviceTags : []string {"MY_PARAM_2" },
73- infoCache : map [string ]diskInfoCache {
74- "null" : {
75- modifiedAt : 0 ,
76- udevDataPath : "testdata/udev.txt" ,
77- sysBlockPath : "testdata" ,
78- values : map [string ]string {},
79- },
93+ clearHostEnv (t )
94+
95+ plugin := newPlugin (t )
96+ plugin .DeviceTags = []string {"MY_PARAM_2" }
97+ plugin .infoCache = map [string ]diskInfoCache {
98+ "null" : {
99+ modifiedAt : 0 ,
100+ udevDataPath : "testdata/udev.txt" ,
101+ sysBlockPath : "testdata" ,
102+ values : map [string ]string {},
80103 },
81104 }
82105 dt := plugin .diskTags ("null" )
83106 require .Equal (t , map [string ]string {"MY_PARAM_2" : "myval2" }, dt )
84107}
108+
109+ func TestDiskInfoHonorsHostDev (t * testing.T ) {
110+ hostRoot := filepath .Join ("testdata" , "hostfs" )
111+ t .Setenv ("HOST_DEV" , filepath .Join (hostRoot , "dev" ))
112+ t .Setenv ("HOST_RUN" , "" )
113+ t .Setenv ("HOST_SYS" , "" )
114+ t .Setenv ("HOST_ROOT" , "" )
115+ t .Setenv ("HOST_MOUNT_PREFIX" , "" )
116+
117+ plugin := newPlugin (t )
118+ plugin .infoCache = map [string ]diskInfoCache {
119+ "mockdev" : {
120+ modifiedAt : 0 ,
121+ udevDataPath : "testdata/udev.txt" ,
122+ values : map [string ]string {},
123+ },
124+ }
125+ di , err := plugin .diskInfo ("mockdev" )
126+ require .NoError (t , err )
127+ require .Equal (t , "myval1" , di ["MY_PARAM_1" ])
128+ }
129+
130+ func TestDiskInfoHonorsHostPrefixFallbacksForDev (t * testing.T ) {
131+ tests := []struct {
132+ name string
133+ setupEnv func (* testing.T , string )
134+ createDev func (string ) string
135+ }{
136+ {
137+ name : "host root fallback" ,
138+ setupEnv : func (t * testing.T , hostRoot string ) {
139+ t .Setenv ("HOST_DEV" , "" )
140+ t .Setenv ("HOST_RUN" , "" )
141+ t .Setenv ("HOST_SYS" , "" )
142+ t .Setenv ("HOST_ROOT" , hostRoot )
143+ t .Setenv ("HOST_MOUNT_PREFIX" , filepath .Join (hostRoot , "unused" ))
144+ },
145+ createDev : func (hostRoot string ) string { return filepath .Join (hostRoot , "dev" ) },
146+ },
147+ {
148+ name : "host mount prefix fallback" ,
149+ setupEnv : func (t * testing.T , hostRoot string ) {
150+ t .Setenv ("HOST_DEV" , "" )
151+ t .Setenv ("HOST_RUN" , "" )
152+ t .Setenv ("HOST_SYS" , "" )
153+ t .Setenv ("HOST_ROOT" , "" )
154+ t .Setenv ("HOST_MOUNT_PREFIX" , hostRoot )
155+ },
156+ createDev : func (hostRoot string ) string { return filepath .Join (hostRoot , "dev" ) },
157+ },
158+ }
159+
160+ for _ , tc := range tests {
161+ t .Run (tc .name , func (t * testing.T ) {
162+ hostRoot := filepath .Join ("testdata" , "hostfs" )
163+ devPath := filepath .Join (tc .createDev (hostRoot ), "mockdev" )
164+
165+ var stat unix.Stat_t
166+ require .NoError (t , unix .Stat (devPath , & stat ))
167+ tc .setupEnv (t , hostRoot )
168+
169+ plugin := newPlugin (t )
170+ plugin .infoCache = map [string ]diskInfoCache {
171+ "mockdev" : {
172+ modifiedAt : stat .Mtim .Nano (),
173+ values : map [string ]string {"cached" : "1" },
174+ },
175+ }
176+
177+ di , err := plugin .diskInfo ("mockdev" )
178+ require .NoError (t , err )
179+ require .Equal (t , "1" , di ["cached" ])
180+ })
181+ }
182+ }
183+
184+ func TestGetDeviceWWIDHonorsHostSys (t * testing.T ) {
185+ hostSys := filepath .Join ("testdata" , "hostfs" , "sys" )
186+ t .Setenv ("HOST_DEV" , "" )
187+ t .Setenv ("HOST_RUN" , "" )
188+ t .Setenv ("HOST_SYS" , hostSys )
189+ t .Setenv ("HOST_ROOT" , "" )
190+ t .Setenv ("HOST_MOUNT_PREFIX" , "" )
191+
192+ plugin := newPlugin (t )
193+ require .Equal (t , "my-wwid" , plugin .getDeviceWWID ("sda" ))
194+ }
0 commit comments