Exemplo n.º 1
0
 def test_load_empty_yaml(self, mock_yaml_load, mock_isfile):
     """Tests loading of empty configuration."""
     config = dict()
     mock_isfile.return_value = True
     mock_yaml_load.return_value = config
     mock_fh = mock.mock_open()
     with mock.patch('builtins.open', mock_fh, create=False):
         with self.assertRaises(SystemExit) as cm:
             yaml.load_yaml('/tmp')
     self.assertEqual(cm.exception.code, 1)
Exemplo n.º 2
0
 def test_load_net_sum_config(self, mock_yaml_load, mock_isfile):
     """Tests loading of configuration with net_sum entry."""
     config = {
         'influxdb': {
             'host': 'foo',
             'port': 1234,
             'user': '******',
             'password': '******',
             'dbname': 'foodb'
         },
         'mint': {
             'file': 'foobar.csv'
         },
         'net_sum': {
             'exclude': {
                 'account': ['test']
             }
         }
     }
     mock_isfile.return_value = True
     mock_yaml_load.return_value = config
     mock_fh = mock.mock_open()
     with mock.patch('builtins.open', mock_fh, create=False):
         validated_config = yaml.load_yaml('/tmp')
     self.assertEqual(validated_config['net_sum']['exclude']['vendor'],
                      list())
     self.assertEqual(validated_config['net_sum']['exclude']['category'],
                      list())
     self.assertEqual(validated_config['net_sum']['exclude']['account'],
                      ['test'])
Exemplo n.º 3
0
 def test_config_archive_with_dir(self, mock_yaml_load, mock_isfile):
     """Tests configuration load where archive directory requested."""
     config = {
         'influxdb': {
             'host': 'foo',
             'port': 1234,
             'user': '******',
             'password': '******',
             'dbname': 'foodb'
         },
         'mint': {
             'file': 'foobar.csv',
             'archive': {
                 'directory': '/tmp'
             }
         }
     }
     mock_isfile.return_value = True
     mock_yaml_load.return_value = config
     mock_fh = mock.mock_open()
     with mock.patch('builtins.open', mock_fh, create=False):
         x = yaml.load_yaml('/tmp')
     self.assertTrue('influxdb' in x)
     self.assertTrue('mint' in x)
     self.assertTrue('file' in x['mint'])
     self.assertTrue('archive' in x['mint'])
     self.assertEqual('/tmp', x['mint']['archive']['directory'])
Exemplo n.º 4
0
 def test_load_no_mint_file_config(self, mock_yaml_load, mock_isfile):
     """Tests loading of configuration missing mint."""
     config = {
         'influxdb': {
             'host': 'foo',
             'port': 1234,
             'user': '******',
             'password': '******',
             'dbname': 'foodb'
         },
         'mint': {}
     }
     mock_isfile.return_value = True
     mock_yaml_load.return_value = config
     mock_fh = mock.mock_open()
     with mock.patch('builtins.open', mock_fh, create=False):
         with self.assertRaises(SystemExit) as cm:
             yaml.load_yaml('/tmp')
     self.assertEqual(cm.exception.code, 1)
Exemplo n.º 5
0
 def test_min_dir_only(self, mock_yaml_load, mock_isfile):
     """Tests loading of minimum configuration with directory specified."""
     config = {
         'influxdb': {
             'host': 'foo',
             'port': 1234,
             'user': '******',
             'password': '******',
             'dbname': 'foodb'
         },
         'mint': {
             'directory': '/foo/bar'
         }
     }
     mock_isfile.return_value = True
     mock_yaml_load.return_value = config
     mock_fh = mock.mock_open()
     with mock.patch('builtins.open', mock_fh, create=False):
         x = yaml.load_yaml('/tmp')
     self.assertTrue('directory' in x['mint'])
Exemplo n.º 6
0
 def test_load_meas_sum_config(self, mock_yaml_load, mock_isfile):
     """Tests loading of configuration with mint sum defined."""
     config = {
         'influxdb': {
             'host': 'foo',
             'port': 1234,
             'user': '******',
             'password': '******',
             'dbname': 'foodb'
         },
         'mint': {
             'file': 'foo.csv',
             'sum': None
         }
     }
     mock_isfile.return_value = True
     mock_yaml_load.return_value = config
     mock_fh = mock.mock_open()
     with mock.patch('builtins.open', mock_fh, create=False):
         validated_config = yaml.load_yaml('/tmp')
     self.assertTrue('sum' in validated_config['mint'])
Exemplo n.º 7
0
 def test_load_bare_net_sum(self, mock_yaml_load, mock_isfile):
     """Tests loading of config with bare minimum net_sum entry."""
     config = {
         'influxdb': {
             'host': 'foo',
             'port': 1234,
             'user': '******',
             'password': '******',
             'dbname': 'foodb'
         },
         'mint': {
             'file': 'foobar.csv'
         },
         'net_sum': None
     }
     mock_isfile.return_value = True
     mock_yaml_load.return_value = config
     mock_fh = mock.mock_open()
     with mock.patch('builtins.open', mock_fh, create=False):
         validated_config = yaml.load_yaml('/tmp')
     self.assertTrue('net_sum' in validated_config, msg=validated_config)
Exemplo n.º 8
0
def main():
    """Start conversion."""
    args = get_arguments()
    config = yaml.load_yaml(args[ARG_CONFIG])
    util.set_loggers(LOGGER,
                     file=config[CONF_LOGGER][CONF_FILE],
                     level=config[CONF_LOGGER][CONF_LEVEL])
    db_client = dbwrite.InfluxClient(config)
    status = False
    try:
        source = config[CONF_MINT][CONF_FILE]
        LOGGER.debug("Using single file %s", source)
        status = dbwrite.influxdb_write(config,
                                        db_client,
                                        source,
                                        db_skip=args[ARG_NOPUSH])
    except KeyError:
        source = config[CONF_MINT][CONF_DIR]
        LOGGER.debug("Using source dir %s", source)
        filelist = glob.glob('{}/*.csv'.format(source))
        if not filelist:
            LOGGER.warning("No csv files found")
            sys.exit(2)
        status = True
        for file in filelist:
            LOGGER.info("Found %s", file)
            result = dbwrite.influxdb_write(config,
                                            db_client,
                                            file,
                                            db_skip=args[ARG_NOPUSH])
            if not result:
                LOGGER.error("Could not write %s to database", file)
                status = False
                break

    if status and not args[ARG_NOPUSH]:
        LOGGER.info("Databse write successful! :)")
    elif not status:
        LOGGER.error("Database write unsuccessful :(")
        sys.exit(1)
Exemplo n.º 9
0
 def test_load_min_yaml(self, mock_yaml_load, mock_isfile):
     """Tests loading of minimum required configuration."""
     config = {
         'influxdb': {
             'host': 'foo',
             'port': 1234,
             'user': '******',
             'password': '******',
             'dbname': 'foodb'
         },
         'mint': {
             'file': 'foobar.csv'
         }
     }
     mock_isfile.return_value = True
     mock_yaml_load.return_value = config
     mock_fh = mock.mock_open()
     with mock.patch('builtins.open', mock_fh, create=False):
         x = yaml.load_yaml('/tmp')
     self.assertTrue('influxdb' in x)
     self.assertTrue('mint' in x)
     self.assertTrue('file' in x['mint'])
     self.assertFalse('archive' in x['mint'])
Exemplo n.º 10
0
 def test_load_logger_partial_config(self, mock_yaml_load, mock_isfile):
     """Tests loading of configuration with partial logger entry."""
     config = {
         'influxdb': {
             'host': 'foo',
             'port': 1234,
             'user': '******',
             'password': '******',
             'dbname': 'foodb'
         },
         'mint': {
             'file': 'foobar.csv'
         },
         'logger': {
             'level': 'warning'
         }
     }
     mock_isfile.return_value = True
     mock_yaml_load.return_value = config
     mock_fh = mock.mock_open()
     with mock.patch('builtins.open', mock_fh, create=False):
         validated_config = yaml.load_yaml('/tmp')
     self.assertEqual(validated_config['logger']['file'], '')
     self.assertEqual(validated_config['logger']['level'], 'warning')
Exemplo n.º 11
0
 def test_file_not_exist(self, mock_yaml_load, mock_isfile):
     """Tests loading of configuration where mint file does not exist."""
     mock_isfile.return_value = False
     with self.assertRaises(SystemExit) as cm:
         yaml.load_yaml('/tmp/doesnotexist')
     self.assertEqual(cm.exception.code, 1)