def test_convert_field_id_to_tag(): fieldTagMap = { 1: Struct(tag='field1'), 2: Struct(tag='field2'), 3: Struct(tag='field3'), } dr = DcgmJsonReader() dr.m_fieldIdToInfo = fieldTagMap for key in fieldTagMap.keys(): assert (dr.ConvertFieldIdToTag(key) == fieldTagMap[key].tag) # pylint: disable=no-member
def test_struct(): s = Struct(field='field') assert s.field == 'field' # pylint: disable=no-member try: i = s.notfield # pylint: disable=no-member assert False # notfield should not exist except: pass
def test_custom_json_handler(): namespace = Struct(arg=None) def MySendToFluentd(json): namespace.arg = json # pylint: disable=no-member dr = DcgmFluentd('FAKE_HOST', 101010) dr.SendToFluentd = MySendToFluentd dr.CustomJsonHandler('value') assert namespace.arg == 'value' # pylint: disable=no-member
def test_custom_data_handler(): namespace = Struct(called=False, result=None) expected = {'fieldName': 'value', 'gpu_uuid': 'this'} # This function tells us that the json callback is called by CustomDataHandler # with the correct data def setCalled(json): namespace.called = True namespace.result = loads(json) gpuUuidMap = {0: 'notthis', 1: 'this'} fvs = {1: create_fv('key', ['not last value', 'value'])} dr = DcgmJsonReader() dr.m_gpuIdToUUId = gpuUuidMap dr.m_fieldIdToInfo = {'key': Struct(tag='fieldName')} dr.CustomJsonHandler = setCalled dr.CustomDataHandler(fvs) assert namespace.called assert expected == namespace.result
def test_send_to_fluentd(): # Can't create a proper closure in Python, so we create an object which acts # as a closure namespace = Struct(message=None, dest=None) def mysendto(_message, _dest): namespace.message = _message namespace.dest = _dest mysock = Struct(sendto=mysendto) dr = DcgmFluentd('FAKE_HOST', 101010) # Assert that we are sending over UDP assert dr.m_sock.family == AF_INET assert dr.m_sock.type == SOCK_DGRAM dr.m_sock = mysock dr.SendToFluentd('message') assert (namespace.message == 'message') assert (namespace.dest == ('FAKE_HOST', 101010))
def create_fv(key, values): fv_values = [Struct(fieldId=key, value=val) for val in values] # Struct(values=values) return {key: Struct(values=fv_values)}
def test_get_log_level(mock_exit): mock_help = maybemock.Mock() assert cli.get_log_level(Struct(loglevel='0')) == logging.CRITICAL assert cli.get_log_level(Struct(loglevel='1')) == logging.ERROR assert cli.get_log_level(Struct(loglevel='2')) == logging.WARNING assert cli.get_log_level(Struct(loglevel='3')) == logging.INFO assert cli.get_log_level(Struct(loglevel='4')) == logging.DEBUG assert cli.get_log_level(Struct(loglevel='critical')) == logging.CRITICAL assert cli.get_log_level(Struct(loglevel='error')) == logging.ERROR assert cli.get_log_level(Struct(loglevel='warning')) == logging.WARNING assert cli.get_log_level(Struct(loglevel='info')) == logging.INFO assert cli.get_log_level(Struct(loglevel='debug')) == logging.DEBUG mock_exit.assert_not_called() try: # It raises an exception because it tries to return an undeclared var cli.get_log_level(Struct(loglevel='wrong', print_help=mock_help)) except: pass mock_exit.assert_called() mock_help.assert_called()
def test_get_field_ids(): assert cli.get_field_ids(Struct(field_ids="1,2,3")) == [1, 2, 3] assert cli.get_field_ids(Struct(field_ids=[1, 2, 3])) == [1, 2, 3]