class TestBadConfig(unittest.TestCase):
    def setUp(self):
        self._temp_conf = Config(BAD_CONFIG)
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config(self._temp_conf._config, tempf.name)

    def test_no_openstack_password(self):
        """ No openstack password defined """
        self.assertFalse(self.conf.getopt('openstack_password'))

    def test_no_landscape_creds(self):
        """ No landscape creds defined """
        self.assertFalse(self.conf.getopt('landscapecreds'))

    def test_no_installer_type(self):
        """ No installer type defined """
        self.assertFalse(self.conf.is_single)
class TestBadConfig(unittest.TestCase):

    def setUp(self):
        self._temp_conf = Config(BAD_CONFIG)
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config(self._temp_conf._config, tempf.name)

    def test_no_openstack_password(self):
        """ No openstack password defined """
        self.assertFalse(self.conf.getopt('openstack_password'))

    def test_no_landscape_creds(self):
        """ No landscape creds defined """
        self.assertFalse(self.conf.getopt('landscapecreds'))

    def test_no_installer_type(self):
        """ No installer type defined """
        self.assertFalse(self.conf.is_single)
class ControllerStateTestCase(unittest.TestCase):
    def setUp(self):
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config({}, tempf.name, save_backups=False)

        self.bad_states_int = [5, 6, 7]
        self.good_states_int = [0, 1, 2]

    def test_set_controller_state(self):
        """ Validate config controller state """

        for i in self.bad_states_int:
            self.conf.setopt('current_state', i)
            with self.assertRaises(ValueError):
                s = self.conf.getopt('current_state')
                ControllerState(s)

        for i in self.good_states_int:
            self.conf.setopt('current_state', i)
            s = self.conf.getopt('current_state')
            self.assertEqual(ControllerState(s), i)
class InstallStateTestCase(unittest.TestCase):
    def setUp(self):
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config({}, tempf.name)

        self.bad_states_int = [5, 6, 7]
        self.good_states_int = [0, 1]

    def test_install_state(self):
        """ Validate config install state """

        for i in self.bad_states_int:
            self.conf.setopt('current_state', i)
            with self.assertRaises(ValueError):
                s = self.conf.getopt('current_state')
                InstallState(s)

        for i in self.good_states_int:
            self.conf.setopt('current_state', i)
            s = self.conf.getopt('current_state')
            self.assertEqual(InstallState(s), i)
class ControllerStateTestCase(unittest.TestCase):

    def setUp(self):
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config({}, tempf.name, save_backups=False)

        self.bad_states_int = [5, 6, 7]
        self.good_states_int = [0, 1, 2]

    def test_set_controller_state(self):
        """ Validate config controller state """

        for i in self.bad_states_int:
            self.conf.setopt('current_state', i)
            with self.assertRaises(ValueError):
                s = self.conf.getopt('current_state')
                ControllerState(s)

        for i in self.good_states_int:
            self.conf.setopt('current_state', i)
            s = self.conf.getopt('current_state')
            self.assertEqual(ControllerState(s), i)
class InstallStateTestCase(unittest.TestCase):

    def setUp(self):
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config({}, tempf.name)

        self.bad_states_int = [5, 6, 7]
        self.good_states_int = [0, 1]

    def test_install_state(self):
        """ Validate config install state """

        for i in self.bad_states_int:
            self.conf.setopt('current_state', i)
            with self.assertRaises(ValueError):
                s = self.conf.getopt('current_state')
                InstallState(s)

        for i in self.good_states_int:
            self.conf.setopt('current_state', i)
            s = self.conf.getopt('current_state')
            self.assertEqual(InstallState(s), i)
class CoreStateTestCase(unittest.TestCase):
    """ Handles validating current state within the controllers
    core
    """
    def setUp(self):
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config({}, tempf.name, save_backups=False)
        self.mock_ui = MagicMock(name='ui')

    @patch('cloudinstall.core.Controller')
    def test_controller_state_init(self, Controller):
        """ Validate controller state in core during class init """
        Controller(self.mock_ui, self.conf)
        self.assertEqual(self.conf.getopt('current_state'),
                         ControllerState.INSTALL_WAIT)
class MultiInstallStateTestCase(unittest.TestCase):
    """ Handles validating current state within a
    multi install
    """
    def setUp(self):
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config({}, tempf.name, save_backups=False)
        self.mock_ui = MagicMock(name='ui')

    @patch('cloudinstall.controllers.install.MultiInstall')
    def test_do_install_sets_state(self, MultiInstall):
        """ Validate installstate in multi install """
        mi = MultiInstall(self.mock_ui, config=self.conf)
        mi.do_install()
        self.assertEqual(self.conf.getopt('current_state'),
                         InstallState.RUNNING)
class CoreStateTestCase(unittest.TestCase):

    """ Handles validating current state within the controllers
    core
    """

    def setUp(self):
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config({}, tempf.name, save_backups=False)
        self.mock_ui = MagicMock(name='ui')

    @patch('cloudinstall.core.Controller')
    def test_controller_state_init(self, Controller):
        """ Validate controller state in core during class init """
        Controller(self.mock_ui, self.conf)
        self.assertEqual(
            self.conf.getopt('current_state'), ControllerState.INSTALL_WAIT)
class MultiInstallStateTestCase(unittest.TestCase):

    """ Handles validating current state within a
    multi install
    """

    def setUp(self):
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config({}, tempf.name, save_backups=False)
        self.mock_ui = MagicMock(name='ui')

    @patch('cloudinstall.controllers.install.MultiInstall')
    def test_do_install_sets_state(self, MultiInstall):
        """ Validate installstate in multi install """
        mi = MultiInstall(self.mock_ui, config=self.conf)
        mi.do_install()
        self.assertEqual(
            self.conf.getopt('current_state'), InstallState.RUNNING)
class TestGoodConfig(unittest.TestCase):

    def setUp(self):
        self._temp_conf = Config(GOOD_CONFIG)
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config(self._temp_conf._config, tempf.name)

    def test_save_openstack_password(self):
        """ Save openstack password to config """
        self.conf.setopt('openstack_password', 'pass')
        self.conf.save()
        self.assertEqual('pass', self.conf.getopt('openstack_password'))

    def test_save_maas_creds(self):
        """ Save maas credentials """
        self.conf.setopt('maascreds', dict(api_host='127.0.0.1',
                                           api_key='1234567'))
        self.conf.save()
        self.assertEqual(
            '127.0.0.1', self.conf.getopt('maascreds')['api_host'])

    def test_save_landscape_creds(self):
        """ Save landscape credentials """
        self.conf.setopt('landscapecreds',
                         dict(admin_name='foo',
                              admin_email='*****@*****.**',
                              system_email='*****@*****.**',
                              maas_server='127.0.0.1',
                              maas_apikey='123457'))
        self.conf.save()
        self.assertEqual(
            '*****@*****.**', self.conf.getopt('landscapecreds')['admin_email'])

    def test_save_installer_type(self):
        """ Save installer type """
        self.conf.setopt("install_type", 'multi')
        self.conf.save()
        self.assertEqual('multi', self.conf.getopt('install_type'))

    @unittest.skip
    def test_cfg_path(self):
        """ Validate current users config path """
        self.assertEqual(
            self.conf.cfg_path, path.join(USER_DIR, '.cloud-install'))

    def test_bin_path(self):
        """ Validate additional tools bin path """
        self.assertEqual(self.conf.bin_path, '/usr/share/openstack/bin')

    @unittest.skip
    def test_juju_environments_path(self):
        """ Validate juju environments path in user dir """
        self.assertEqual(
            self.conf.juju_environments_path,
            path.join(
                USER_DIR, '.cloud-install/juju/environments.yaml'))

    def test_clear_empty_args(self):
        """ Empty cli options are not populated
        in generated config
        """
        cfg_file = path.join(DATA_DIR, 'good_config.yaml')
        cfg = utils.populate_config(parse_opts(['--config', cfg_file]))
        self.assertEqual(True, 'http-proxy' not in cfg)

    def test_config_file_persists(self):
        """ CLI options override options in config file
        """
        cfg_file = path.join(DATA_DIR, 'good_config.yaml')
        cfg = utils.populate_config(
            parse_opts(['--config', cfg_file,
                        '--headless']))
        self.assertEqual(True, cfg['headless'])

    def test_config_file_persists_new_cli_opts(self):
        """ Generated config object appends new options
        passed via cli
        """
        cfg_file = path.join(DATA_DIR, 'good_config.yaml')
        cfg = utils.populate_config(
            parse_opts(['--config', cfg_file,
                        '--install-only',
                        '--killcloud-noprompt']))
        self.assertEqual(True, cfg['install_only'])
        self.assertEqual(True, cfg['killcloud_noprompt'])

    def test_config_overrides_from_cli(self):
        """ Config object item is not overridden
        by unset cli option
        """
        cfg_file = path.join(DATA_DIR, 'good_config.yaml')
        cfg = utils.populate_config(
            parse_opts(['--http-proxy',
                        'http://localhost:2222',
                        '--killcloud-noprompt',
                        '--config', cfg_file]))
        self.assertEqual(cfg['https_proxy'], GOOD_CONFIG['https_proxy'])

    def test_default_opts_not_override_config(self):
        """ Verify that default cli opts that are False
        do not override their config_option whose option
        is True.
        """
        cfg_file = path.join(DATA_DIR, 'good_config.yaml')
        cfg_opts_raw = parse_opts(['--config', cfg_file])
        cfg_opts_raw = vars(cfg_opts_raw)
        self.assertEqual(True, 'headless' not in cfg_opts_raw)

        cfg = utils.populate_config(parse_opts(['--config', cfg_file]))
        self.assertEqual(True, cfg['headless'])

    def test_default_opts_no_config(self):
        """ Verify that default cli opts are sanitized
        and that no options set to False or None exist
        in the config object
        """
        cfg = utils.populate_config(parse_opts([]))
        print(cfg)
        self.assertEqual(True, 'headless' not in cfg)
class TestGoodConfig(unittest.TestCase):
    def setUp(self):
        self._temp_conf = Config(GOOD_CONFIG)
        with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
            # Override config file to save to
            self.conf = Config(self._temp_conf._config, tempf.name)

    def test_save_openstack_password(self):
        """ Save openstack password to config """
        self.conf.setopt('openstack_password', 'pass')
        self.conf.save()
        self.assertEqual('pass', self.conf.getopt('openstack_password'))

    def test_save_maas_creds(self):
        """ Save maas credentials """
        self.conf.setopt('maascreds',
                         dict(api_host='127.0.0.1', api_key='1234567'))
        self.conf.save()
        self.assertEqual('127.0.0.1',
                         self.conf.getopt('maascreds')['api_host'])

    def test_save_landscape_creds(self):
        """ Save landscape credentials """
        self.conf.setopt(
            'landscapecreds',
            dict(admin_name='foo',
                 admin_email='*****@*****.**',
                 system_email='*****@*****.**',
                 maas_server='127.0.0.1',
                 maas_apikey='123457'))
        self.conf.save()
        self.assertEqual('*****@*****.**',
                         self.conf.getopt('landscapecreds')['admin_email'])

    def test_save_installer_type(self):
        """ Save installer type """
        self.conf.setopt("install_type", 'multi')
        self.conf.save()
        self.assertEqual('multi', self.conf.getopt('install_type'))

    def test_cfg_path(self):
        """ Validate current users config path """
        self.assertEqual(self.conf.cfg_path,
                         path.join(USER_DIR, '.cloud-install'))

    def test_bin_path(self):
        """ Validate additional tools bin path """
        self.assertEqual(self.conf.bin_path, '/usr/share/openstack/bin')

    def test_juju_environments_path(self):
        """ Validate juju environments path in user dir """
        self.assertEqual(
            self.conf.juju_environments_path,
            path.join(USER_DIR, '.cloud-install/juju/environments.yaml'))

    def test_clear_empty_args(self):
        """ Empty cli options are not populated
        in generated config
        """
        cfg_file = path.join(DATA_DIR, 'good_config.yaml')
        cfg = utils.populate_config(parse_opts(['--config', cfg_file]))
        self.assertEqual(True, 'http-proxy' not in cfg)

    def test_config_file_persists(self):
        """ CLI options override options in config file
        """
        cfg_file = path.join(DATA_DIR, 'good_config.yaml')
        cfg = utils.populate_config(
            parse_opts(['--config', cfg_file, '--headless']))
        self.assertEqual(True, cfg['headless'])

    def test_config_file_persists_new_cli_opts(self):
        """ Generated config object appends new options
        passed via cli
        """
        cfg_file = path.join(DATA_DIR, 'good_config.yaml')
        cfg = utils.populate_config(
            parse_opts([
                '--config', cfg_file, '--install-only', '--killcloud-noprompt'
            ]))
        self.assertEqual(True, cfg['install_only'])
        self.assertEqual(True, cfg['killcloud_noprompt'])

    def test_config_overrides_from_cli(self):
        """ Config object item is not overridden
        by unset cli option
        """
        cfg_file = path.join(DATA_DIR, 'good_config.yaml')
        cfg = utils.populate_config(
            parse_opts([
                '--http-proxy', 'http://localhost:2222',
                '--killcloud-noprompt', '--config', cfg_file
            ]))
        self.assertEqual(cfg['https_proxy'], GOOD_CONFIG['https_proxy'])

    def test_default_opts_not_override_config(self):
        """ Verify that default cli opts that are False
        do not override their config_option whose option
        is True.
        """
        cfg_file = path.join(DATA_DIR, 'good_config.yaml')
        cfg_opts_raw = parse_opts(['--config', cfg_file])
        cfg_opts_raw = vars(cfg_opts_raw)
        self.assertEqual(True, 'headless' not in cfg_opts_raw)

        cfg = utils.populate_config(parse_opts(['--config', cfg_file]))
        self.assertEqual(True, cfg['headless'])

    def test_default_opts_no_config(self):
        """ Verify that default cli opts are sanitized
        and that no options set to False or None exist
        in the config object
        """
        cfg = utils.populate_config(parse_opts([]))
        print(cfg)
        self.assertEqual(True, 'headless' not in cfg)