def test_load_backup_none(m_utils, m_sh, m_zipf): invoke( backup.load, 'fname --no-load-compose --no-load-datastore --no-load-spark --no-load-node-red --no-update' ) assert m_zipf.read.call_count == 1 assert m_sh.call_count == 1
def test_add_spark_force(m_utils, m_sh, mocker): m_find = mocker.patch(TESTED + '.find_device') m_find.return_value = '280038000847343337373738 192.168.0.55 8332' m_utils.read_compose.side_effect = lambda: {'services': {'testey': {}}} invoke(spark.add_spark, '--name testey', _err=True) invoke(spark.add_spark, '--name testey --force')
def test_load_empty(m_actions, m_utils, m_sh): m_utils.path_exists.return_value = False m_utils.ctx_opts.return_value.dry_run = False # temp dir exists, but was never populated # listdir will return empty invoke(snapshot.load, _err=True)
def test_save_backup(mocker, m_utils, f_save_backup): m_mkdir = mocker.patch(TESTED + '.mkdir') m_zipfile = mocker.patch(TESTED + '.zipfile.ZipFile') invoke(backup.save) m_mkdir.assert_called_once_with(path.abspath('backup/')) m_zipfile.assert_called_once_with( matching(r'^backup/brewblox_backup_\d{8}_\d{4}.zip'), 'w', zipfile.ZIP_DEFLATED) m_zipfile.return_value.write.assert_called_with('docker-compose.yml') assert m_zipfile.return_value.writestr.call_args_list == [ call('brewblox-ui-store.datastore.json', json.dumps([{ 'id': 1 }, { 'id': 2 }, { 'id': 3 }])), call('brewblox-automation.datastore.json', json.dumps([{ 'id': 4 }, { 'id': 5 }, { 'id': 6 }])), call('spark-one.spark.json', 'resp_text'), ]
def test_from_influxdb(m_utils, m_migration): invoke(database.from_influxdb, '--duration=1d --offset s1 1000 --offset s2 5000 s1 s2') m_utils.check_config.assert_called_once() m_utils.confirm_mode.assert_called_once() m_migration.migrate_influxdb.assert_called_once_with( 'victoria', '1d', ['s1', 's2'], [('s1', 1000), ('s2', 5000)])
def test_load_backup_missing(m_utils, m_sh, m_zipf, mocker): m_tmp = mocker.patch(TESTED + '.NamedTemporaryFile', wraps=backup.NamedTemporaryFile) m_zipf.namelist.return_value = zipf_names()[2:] m_zipf.read.side_effect = zipf_read()[2:] invoke(backup.load, 'fname') assert m_zipf.read.call_count == 5 assert m_tmp.call_count == 5
def test_p1_flash(m_usb, m_utils, m_sh): m_dev = Mock() m_dev.idProduct = const.PID_P1 m_usb.core.find.side_effect = [[], [], [m_dev]] invoke(flash.flash, '--release develop --pull') m_sh.assert_called_with( 'SUDO docker run -it --rm --privileged -v /dev:/dev --pull always ' + 'brewblox/firmware-flasher:develop flash')
def test_list_env(m_utils, mocker): m = mocker.patch(TESTED + '.dotenv.dotenv_values') m.return_value = {'k1': 'v1', 'k2': 'v2'} invoke(env.list_env) m.return_value = {} invoke(env.list_env) assert m.call_count == 2
def test_add_node_red(m_utils, m_sh, mocker): m_utils.read_compose.side_effect = lambda: {'services': {}} invoke(add_service.add_node_red) assert m_sh.call_count == 2 m_sh.reset_mock() m_utils.confirm.return_value = False invoke(add_service.add_node_red) assert m_sh.call_count == 1
def test_no_pull(m_utils, m_sh): m_utils.path_exists.return_value = False invoke(setup.setup) num_normal = m_sh.call_count m_sh.reset_mock() invoke(setup.setup, '--no-pull') assert m_sh.call_count == num_normal - 1
def test_esp_flash(m_usb, m_utils, m_sh): m_dev = Mock() m_dev.idProduct = const.PID_ESP32 m_usb.core.find.side_effect = [[], [], [m_dev]] invoke(flash.flash, '--release develop --pull') m_sh.assert_called_with( 'SUDO docker run -it --rm --privileged ' + '-v /dev:/dev -w /app/firmware --entrypoint bash --pull always ' + 'brewblox/brewblox-devcon-spark:develop flash')
def test_install_short(m_utils, m_sh): m_utils.path_exists.return_value = False m_utils.command_exists.side_effect = [ False, # apt True, # docker True, # docker-compose ] invoke(install.install, '--use-defaults') assert m_sh.call_count == 3
def test_install_full(m_utils, m_sh): m_utils.path_exists.return_value = False m_utils.is_docker_user.return_value = False m_utils.command_exists.side_effect = [ True, # apt False, # docker False, # docker-compose ] invoke(install.install) assert m_sh.call_count == 6
def test_pull(m_utils, m_sh, mocker): m_restart = mocker.patch(TESTED + '.restart_services') invoke(service.pull) m_sh.assert_called_with('SUDO docker-compose pull ') invoke(service.pull, 'ui eventbus') m_sh.assert_called_with('SUDO docker-compose pull ui eventbus') assert m_restart.call_count == 2
def test_discover_spark(m_utils, mocker): m_discover = mocker.patch(TESTED + '.list_devices', autospec=True) m_utils.read_compose.return_value = {'services': {}} invoke(add_service.discover_spark) m_discover.assert_called_with('all', {'services': {}}) m_utils.read_compose.side_effect = FileNotFoundError invoke(add_service.discover_spark) m_discover.assert_called_with('all', {})
def test_save_file_exists(m_utils, m_sh): m_utils.path_exists.return_value = True m_utils.confirm.return_value = False invoke(snapshot.save) assert m_sh.call_count == 0 m_utils.confirm.return_value = True invoke(snapshot.save) assert m_sh.call_count == 2
def test_install_basic(m_utils, m_actions, m_input, m_sh, m_opts): invoke(install.install) assert m_sh.call_count == 13 # do everything assert m_input.call_count == 1 # prompt reboot m_sh.reset_mock() m_input.reset_mock() m_opts.prompt_reboot = False invoke(install.install) assert m_sh.call_count == 13 # do everything assert m_input.call_count == 0 # no reboot prompt
def test_coredump(m_utils, m_sh): invoke(diagnostic.coredump) assert m_sh.call_count == 2 assert m_utils.file_netcat.call_count == 1 m_sh.reset_mock() m_utils.file_netcat.reset_mock() m_utils.command_exists.return_value = False invoke(diagnostic.coredump, '--no-upload') assert m_sh.call_count == 3 assert m_utils.file_netcat.call_count == 0
def test_http_body(mock_requests): body = {'var1': 1, 'var2': 'val'} invoke(http.http, ['post', 'url', '-d', json.dumps(body)]) invoke(http.http, ['post', 'url', '-d', json.dumps(body), '--json-body=false']) assert mock_requests.post.call_args_list == [ call('url', headers={}, params={}, verify=False, json=body), call('url', headers={}, params={}, verify=False, data=json.dumps(body)) ]
def test_http_methods(mock_requests): invoke(http.http, ['get', 'url']) assert mock_requests.get.call_args_list == [ call('url', headers={}, params={}, verify=False) ] assert mock_requests.post.call_count == 0 invoke(http.http, ['post', 'url', '--pretty']) assert mock_requests.post.call_args_list == [ call('url', headers={}, params={}, verify=False) ]
def test_install_existing_declined(m_utils, m_sh): m_utils.path_exists.return_value = True m_utils.is_docker_user.return_value = True m_utils.confirm.side_effect = [ True, # Use defaults False, # No to overwrite ] m_utils.command_exists.side_effect = [ True, # apt False, # docker ] invoke(install.install) m_sh.assert_not_called()
def test_http_methods(mock_requests): invoke(http.http, ['get', 'url']) assert mock_requests.get.call_args_list == [ call('url', headers={}, params={}, verify=False) ] assert mock_requests.post.call_count == 0 mock_requests.post.return_value.json.return_value = {'happy': True} invoke(http.http, ['post', 'url', '--pretty']) assert mock_requests.post.call_args_list == [ call('url', headers={}, params={}, verify=False) ]
def test_load_backup_none(m_utils, m_sh, m_zipf): invoke( backup.load, ' '.join([ 'fname', '--no-load-compose', '--no-load-datastore', '--no-load-spark', '--no-load-node-red', '--no-load-mosquitto', '--no-update', ])) assert m_zipf.read.call_count == 1 assert m_sh.call_count == 1
def test_discover_spark(m_utils, mocker): def m_discover_func(discovery_type): yield from [{'desc': 'one'}, {'desc': 'two'}] m_discover = mocker.patch(TESTED + '.discover_device', side_effect=m_discover_func) invoke(add_device.discover_spark) assert m_discover.called_with('all') assert m_utils.info.call_count == 3 invoke(add_device.discover_spark, '--discovery=wifi') assert m_discover.called_with('wifi') assert m_utils.info.call_count == 6
def test_spark_overwrite(m_utils, m_sh, m_choose, mocker): m_utils.read_compose.side_effect = lambda: { 'services': { 'testey': { 'image': 'brewblox/brewblox-devcon-spark:develop' } } } invoke(add_service.add_spark, '--name testey --force') assert m_utils.warn.call_count == 0 invoke(add_service.add_spark, '--name new-testey') assert m_utils.warn.call_count > 0
def test_wifi(m_usb, m_utils, m_sh, mocker): mocker.patch(TESTED + '.LISTEN_MODE_WAIT_S', 0.0001) m_find = mocker.patch(TESTED + '.usb.core.find') m_utils.ctx_opts.return_value.dry_run = False m_find.reset_mock() m_sh.reset_mock() m_find.side_effect = [Mock(), None] # particle invoke(flash.wifi) m_sh.assert_called_once_with( 'pyserial-miniterm -q /dev/ttyACM0 2>/dev/null') m_find.reset_mock() m_sh.reset_mock() m_find.side_effect = [None, Mock()] # ESP invoke(flash.wifi) assert m_sh.call_count == 0 assert m_find.call_count == 2 m_find.reset_mock() m_sh.reset_mock() m_find.side_effect = [None, None, None, Mock()] # ESP, second try invoke(flash.wifi) assert m_sh.call_count == 0 assert m_find.call_count == 4 # No USB calls should be made in dry runs m_utils.ctx_opts.return_value.dry_run = True m_find.reset_mock() m_sh.reset_mock() m_find.side_effect = [Mock(), None] # particle invoke(flash.wifi) m_sh.assert_called_once_with( 'pyserial-miniterm -q /dev/ttyACM0 2>/dev/null') assert m_find.return_value.call_count == 0
def test_install_minimal(m_utils, m_actions, m_input, m_sh, m_opts): m_opts.apt_install = False m_opts.docker_install = False m_opts.docker_group_add = False m_opts.docker_pull = False m_opts.reboot_needed = False m_opts.init_compose = False m_opts.init_datastore = False m_opts.init_history = False m_opts.init_gateway = False m_opts.init_eventbus = False invoke(install.install) assert m_sh.call_count == 3 # Only the bare minimum
def test_http_file_body(mocker, mock_requests): body = {'var1': 1, 'var2': 'val'} open_mock = mocker.patch(TESTED + '.open', mock_open(read_data=json.dumps(body))) invoke(http.http, ['post', 'url', '-f', 'file.json']) invoke(http.http, ['post', 'url', '-f', 'file.json', '--json-body=false']) assert mock_requests.post.call_args_list == [ call('url', headers={}, params={}, verify=False, json=body), call('url', headers={}, params={}, verify=False, data=json.dumps(body)) ] assert open_mock.call_args_list == [ call('file.json'), call('file.json'), ]
def test_wait_timeout(mocker, mock_requests, mock_retry_interval): mocker.patch(TESTED + '.RETRY_COUNT', 5) mock_requests.get.return_value.raise_for_status.side_effect = http.ConnectionError result = invoke(http.http, ['wait', 'url'], _err=True) assert isinstance(result.exception, TimeoutError) assert mock_requests.get.call_count == 5
def test_install_existing_continue(m_utils, m_sh): m_utils.path_exists.return_value = True m_utils.confirm.side_effect = [ True, # ok using dir True, # continue existing dir False, # prompt reboot ] m_utils.is_docker_user.return_value = True m_utils.command_exists.side_effect = [ False, # apt True, # docker ] invoke(install.install, '--no-use-defaults') m_utils.confirm.assert_any_call(matching(r'.*brewblox already exists.*')) assert m_sh.call_count == 3 m_sh.assert_called_with('sudo reboot')