1010from datadog_checks .disk import Disk
1111
1212from .common import DEFAULT_DEVICE_NAME , DEFAULT_FILE_SYSTEM , DEFAULT_MOUNT_POINT
13- from .mocks import MockInodesMetrics , mock_df_output
13+ from .mocks import MockInodesMetrics , mock_blkid_output , mock_df_output
1414from .utils import requires_unix
1515
1616
@@ -56,7 +56,7 @@ def test_psutil(aggregator, gauge_metrics, rate_metrics):
5656 Mock psutil and run the check
5757 """
5858 for tag_by in ['true' , 'false' ]:
59- instance = {'tag_by_filesystem' : tag_by }
59+ instance = {'tag_by_filesystem' : tag_by , 'tag_by_label' : False }
6060 c = Disk ('disk' , None , {}, [instance ])
6161 c .check (instance )
6262
@@ -83,7 +83,7 @@ def test_psutil_rw(aggregator):
8383 """
8484 Check for 'ro' option in the mounts
8585 """
86- instance = {'service_check_rw' : 'yes' }
86+ instance = {'service_check_rw' : 'yes' , 'tag_by_label' : False }
8787 c = Disk ('disk' , None , {}, [instance ])
8888 c .check (instance )
8989
@@ -113,24 +113,33 @@ def test_device_tagging(aggregator, gauge_metrics, rate_metrics):
113113 'use_mount' : 'no' ,
114114 'device_tag_re' : {'{}.*' .format (DEFAULT_DEVICE_NAME [:- 1 ]): 'type:dev,tag:two' },
115115 'tags' : ['optional:tags1' ],
116+ 'tag_by_label' : False ,
116117 }
117118 c = Disk ('disk' , None , {}, [instance ])
118- c .check (instance )
119+
120+ with mock .patch ('datadog_checks.disk.disk.Disk._get_devices_label' ):
121+ # _get_devices_label is only called on linux, so devices_label is manually filled
122+ # to make the test run on everything
123+ c .devices_label = {DEFAULT_DEVICE_NAME : 'label:mylab' }
124+ c .check (instance )
119125
120126 # Assert metrics
121- tags = ['type:dev' , 'tag:two' , 'device:{}' .format (DEFAULT_DEVICE_NAME ), 'optional:tags1' ]
127+ tags = ['type:dev' , 'tag:two' , 'device:{}' .format (DEFAULT_DEVICE_NAME ), 'optional:tags1' , 'label:mylab' ]
128+
122129 for name , value in iteritems (gauge_metrics ):
123130 aggregator .assert_metric (name , value = value , tags = tags )
124131
125132 for name , value in iteritems (rate_metrics ):
126- aggregator .assert_metric (name , value = value , tags = ['device:{}' .format (DEFAULT_DEVICE_NAME ), 'optional:tags1' ])
133+ aggregator .assert_metric (
134+ name , value = value , tags = ['device:{}' .format (DEFAULT_DEVICE_NAME ), 'optional:tags1' , 'label:mylab' ]
135+ )
127136
128137 aggregator .assert_all_metrics_covered ()
129138
130139
131140@requires_unix
132141def test_no_psutil_debian (aggregator , gauge_metrics ):
133- instance = {'use_mount' : 'no' , 'excluded_filesystems' : ['tmpfs' ]}
142+ instance = {'use_mount' : 'no' , 'excluded_filesystems' : ['tmpfs' ], 'tag_by_label' : False }
134143 c = Disk ('disk' , None , {}, [instance ])
135144 # disable psutil
136145 c ._psutil = lambda : False
@@ -155,7 +164,12 @@ def test_no_psutil_debian(aggregator, gauge_metrics):
155164
156165@requires_unix
157166def test_no_psutil_freebsd (aggregator , gauge_metrics ):
158- instance = {'use_mount' : 'no' , 'excluded_filesystems' : ['devfs' ], 'excluded_disk_re' : 'zroot/.+' }
167+ instance = {
168+ 'use_mount' : 'no' ,
169+ 'excluded_filesystems' : ['devfs' ],
170+ 'excluded_disk_re' : 'zroot/.+' ,
171+ 'tag_by_label' : False ,
172+ }
159173 c = Disk ('disk' , None , {}, [instance ])
160174 # disable psutil
161175 c ._psutil = lambda : False
@@ -178,7 +192,12 @@ def test_no_psutil_freebsd(aggregator, gauge_metrics):
178192
179193@requires_unix
180194def test_no_psutil_centos (aggregator , gauge_metrics ):
181- instance = {'use_mount' : 'no' , 'excluded_filesystems' : ['devfs' , 'tmpfs' ], 'excluded_disks' : ['/dev/sda1' ]}
195+ instance = {
196+ 'use_mount' : 'no' ,
197+ 'excluded_filesystems' : ['devfs' , 'tmpfs' ],
198+ 'excluded_disks' : ['/dev/sda1' ],
199+ 'tag_by_label' : False ,
200+ }
182201 c = Disk ('disk' , None , {}, [instance ])
183202 # disable psutil
184203 c ._psutil = lambda : False
@@ -198,3 +217,15 @@ def test_no_psutil_centos(aggregator, gauge_metrics):
198217 aggregator .assert_metric (name , tags = ['device:{}' .format (device )])
199218
200219 aggregator .assert_all_metrics_covered ()
220+
221+
222+ def test_get_devices_label ():
223+ c = Disk ('disk' , None , {}, [{}])
224+
225+ with mock .patch (
226+ "datadog_checks.disk.disk.get_subprocess_output" ,
227+ return_value = mock_blkid_output (),
228+ __name__ = 'get_subprocess_output' ,
229+ ):
230+ labels = c ._get_devices_label ()
231+ assert labels .get ("/dev/mapper/vagrant--vg-root" ) == "label:DATA"
0 commit comments