def test_deploy_rhel_reg(
            self, mock_check_hypervisor_stats, mock_get_key, mock_create_env,
            generate_certs_mock, mock_get_templte_contents,
            mock_process_multiple_env, wait_for_stack_ready_mock,
            mock_remove_known_hosts, mock_keystone_initialize, mock_sleep,
            mock_setup_endpoints, mock_create_overcloudrc,
            mock_generate_overcloud_passwords,
            mock_create_tempest_deployer_input, mock_deploy_postconfig,
            mock_breakpoints_cleanup, mock_events):

        arglist = [
            '--templates', '--rhel-reg', '--reg-sat-url',
            'https://example.com', '--reg-method', 'satellite', '--reg-org',
            '123456789', '--reg-activation-key', 'super-awesome-key'
        ]
        verifylist = [
            ('templates', '/usr/share/openstack-tripleo-heat-templates/'),
            ('rhel_reg', True), ('reg_sat_url', 'https://example.com'),
            ('reg_method', 'satellite'), ('reg_org', '123456789'),
            ('reg_activation_key', 'super-awesome-key')
        ]

        mock_generate_overcloud_passwords.return_value = self._get_passwords()
        mock_process_multiple_env.return_value = [{}, fakes.create_env()]
        mock_get_templte_contents.return_value = [{}, "template"]
        wait_for_stack_ready_mock.return_value = True

        clients = self.app.client_manager
        orchestration_client = clients.tripleoclient.orchestration
        orchestration_client.stacks.get.return_value = fakes.create_tht_stack()
        mock_events.return_value = []

        mock_check_hypervisor_stats.return_value = {
            'count': 4,
            'memory_mb': 4096,
            'vcpus': 8,
        }
        mock_get_key.return_value = "PASSWORD"
        clients.network.api.find_attr.return_value = {"id": "network id"}

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        baremetal = clients.baremetal
        baremetal.node.list.return_value = range(10)

        result = self.cmd.take_action(parsed_args)
        self.assertTrue(result)

        args, kwargs = mock_process_multiple_env.call_args
        self.assertIn(
            '/usr/share/openstack-tripleo-heat-templates/extraconfig/post_dep'
            'loy/rhel-registration/rhel-registration-resource-registry.yaml',
            args[0])
        self.assertIn(
            '/usr/share/openstack-tripleo-heat-templates/extraconfig/post_dep'
            'loy/rhel-registration/environment-rhel-registration.yaml',
            args[0])
    def test_tht_scale(self, mock_time, mock_uuid1, mock_create_cephx_key,
                       mock_check_hypervisor_stats, mock_get_key,
                       mock_create_env, generate_certs_mock,
                       mock_get_templte_contents, mock_process_multiple_env,
                       wait_for_stack_ready_mock,
                       mock_remove_known_hosts, mock_keystone_initialize,
                       mock_sleep, mock_setup_endpoints,
                       mock_create_overcloudrc,
                       mock_generate_overcloud_passwords,
                       mock_create_tempest_deployer_input,
                       mock_deploy_postconfig,
                       mock_create_parameters_env,
                       mock_breakpoints_cleanupm,
                       mock_events):

        arglist = ['--templates', '--ceph-storage-scale', '3']
        verifylist = [
            ('templates', '/usr/share/openstack-tripleo-heat-templates/'),
            ('ceph_storage_scale', 3)
        ]

        mock_create_cephx_key.return_value = "cephx_key"
        mock_uuid1.return_value = "uuid"
        mock_time.return_value = 123456789

        mock_generate_overcloud_passwords.return_value = self._get_passwords()

        clients = self.app.client_manager
        orchestration_client = clients.tripleoclient.orchestration
        orchestration_client.stacks.get.return_value = fakes.create_tht_stack()
        mock_event = mock.Mock()
        mock_event.id = '1234'
        mock_events.return_value = [mock_events]

        mock_check_hypervisor_stats.return_value = {
            'count': 4,
            'memory_mb': 4096,
            'vcpus': 8,
        }
        mock_get_key.return_value = "PASSWORD"
        clients.network.api.find_attr.return_value = {
            "id": "network id"
        }
        mock_create_env.return_value = "/fake/path"
        mock_env = fakes.create_env()
        mock_process_multiple_env.return_value = [{}, mock_env]
        mock_get_templte_contents.return_value = [{}, "template"]
        wait_for_stack_ready_mock.return_value = True

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        baremetal = clients.baremetal
        baremetal.node.list.return_value = range(10)

        expected_parameters = {
            'AdminPassword': '******',
            'AdminToken': 'password',
            'AodhPassword': '******',
            'CeilometerMeteringSecret': 'password',
            'CeilometerPassword': '******',
            'CephStorageCount': 3,
            'CinderPassword': '******',
            'ExtraConfig': '{}',
            'GlancePassword': '******',
            'HAProxyStatsPassword': '******',
            'HeatPassword': '******',
            'HeatStackDomainAdminPassword': '******',
            'HypervisorNeutronPhysicalBridge': 'br-ex',
            'HypervisorNeutronPublicInterface': 'nic1',
            'NeutronAllowL3AgentFailover': False,
            'NeutronDhcpAgentsPerNetwork': 1,
            'NeutronDnsmasqOptions': 'dhcp-option-force=26,1400',
            'NeutronFlatNetworks': 'datacentre',
            'NeutronL3HA': False,
            'NeutronMetadataProxySharedSecret': 'password',
            'NeutronPassword': '******',
            'NeutronPublicInterface': 'nic1',
            'NovaPassword': '******',
            'NtpServer': '',
            'RabbitPassword': '******',
            'RedisPassword': '******',
            'SaharaPassword': '******',
            'SnmpdReadonlyUserPassword': '******',
            'SwiftHashSuffix': 'password',
            'SwiftPassword': '******',
            'DeployIdentifier': 123456789,
        }

        def _custom_create_params_env(parameters):
            for key, value in six.iteritems(parameters):
                self.assertEqual(value, expected_parameters[key])
            parameter_defaults = {"parameter_defaults": parameters}
            with open(self.parameter_defaults_env_file, 'w') as temp_file:
                temp_file.write(json.dumps(parameter_defaults))
            return [self.parameter_defaults_env_file]

        mock_create_parameters_env.side_effect = _custom_create_params_env

        result = self.cmd.take_action(parsed_args)
        self.assertTrue(result)

        args, kwargs = orchestration_client.stacks.update.call_args

        self.assertEqual(args, (orchestration_client.stacks.get().id, ))

        self.assertEqual(kwargs['files'], {})
        self.assertEqual(kwargs['template'], 'template')
        self.assertEqual(kwargs['environment'], mock_env)
        self.assertEqual(kwargs['stack_name'], 'overcloud')
        self.assertEqual(kwargs['clear_parameters'],
                         mock_env['parameter_defaults'].keys())

        mock_get_templte_contents.assert_called_with(
            '/usr/share/openstack-tripleo-heat-templates/' +
            constants.OVERCLOUD_YAML_NAMES[0])

        mock_create_tempest_deployer_input.assert_called_with()
        mock_process_multiple_env.assert_called_with(
            [self.parameter_defaults_env_file])
    def test_deploy_rhel_reg(self, mock_check_hypervisor_stats,
                             mock_get_key,
                             mock_create_env, generate_certs_mock,
                             mock_get_templte_contents,
                             mock_process_multiple_env,
                             wait_for_stack_ready_mock,
                             mock_remove_known_hosts,
                             mock_keystone_initialize,
                             mock_sleep, mock_setup_endpoints,
                             mock_create_overcloudrc,
                             mock_generate_overcloud_passwords,
                             mock_create_tempest_deployer_input,
                             mock_deploy_postconfig,
                             mock_breakpoints_cleanup,
                             mock_events):

        arglist = ['--templates', '--rhel-reg',
                   '--reg-sat-url', 'https://example.com',
                   '--reg-method', 'satellite', '--reg-org', '123456789',
                   '--reg-activation-key', 'super-awesome-key']
        verifylist = [
            ('templates', '/usr/share/openstack-tripleo-heat-templates/'),
            ('rhel_reg', True),
            ('reg_sat_url', 'https://example.com'),
            ('reg_method', 'satellite'),
            ('reg_org', '123456789'),
            ('reg_activation_key', 'super-awesome-key')
        ]

        mock_generate_overcloud_passwords.return_value = self._get_passwords()
        mock_process_multiple_env.return_value = [{}, fakes.create_env()]
        mock_get_templte_contents.return_value = [{}, "template"]
        wait_for_stack_ready_mock.return_value = True

        clients = self.app.client_manager
        orchestration_client = clients.tripleoclient.orchestration
        orchestration_client.stacks.get.return_value = fakes.create_tht_stack()
        mock_events.return_value = []

        mock_check_hypervisor_stats.return_value = {
            'count': 4,
            'memory_mb': 4096,
            'vcpus': 8,
        }
        mock_get_key.return_value = "PASSWORD"
        clients.network.api.find_attr.return_value = {
            "id": "network id"
        }

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        baremetal = clients.baremetal
        baremetal.node.list.return_value = range(10)

        result = self.cmd.take_action(parsed_args)
        self.assertTrue(result)

        args, kwargs = mock_process_multiple_env.call_args
        self.assertIn(
            '/usr/share/openstack-tripleo-heat-templates/extraconfig/post_dep'
            'loy/rhel-registration/rhel-registration-resource-registry.yaml',
            args[0])
        self.assertIn(
            '/usr/share/openstack-tripleo-heat-templates/extraconfig/post_dep'
            'loy/rhel-registration/environment-rhel-registration.yaml',
            args[0])
    def test_deploy_custom_templates(self, mock_check_hypervisor_stats,
                                     mock_get_key,
                                     mock_create_env, generate_certs_mock,
                                     mock_get_templte_contents,
                                     mock_process_multiple_env,
                                     wait_for_stack_ready_mock,
                                     mock_remove_known_hosts,
                                     mock_keystone_initialize,
                                     mock_sleep, mock_setup_endpoints,
                                     mock_create_overcloudrc,
                                     mock_generate_overcloud_passwords,
                                     mock_create_tempest_deployer_input,
                                     mock_deploy_postconfig,
                                     mock_breakpoints_cleanup,
                                     mock_events):

        arglist = ['--templates', '/home/stack/tripleo-heat-templates']
        verifylist = [
            ('templates', '/home/stack/tripleo-heat-templates'),
        ]

        mock_generate_overcloud_passwords.return_value = self._get_passwords()

        clients = self.app.client_manager
        orchestration_client = clients.tripleoclient.orchestration
        orchestration_client.stacks.get.return_value = fakes.create_tht_stack()
        mock_events.return_value = []

        mock_check_hypervisor_stats.return_value = {
            'count': 4,
            'memory_mb': 4096,
            'vcpus': 8,
        }
        mock_get_key.return_value = "PASSWORD"
        clients.network.api.find_attr.return_value = {
            "id": "network id"
        }
        mock_create_env.return_value = "/fake/path"
        mock_env = fakes.create_env()
        mock_process_multiple_env.return_value = [{}, mock_env]
        mock_get_templte_contents.return_value = [{}, "template"]
        wait_for_stack_ready_mock.return_value = True

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        baremetal = clients.baremetal
        baremetal.node.list.return_value = range(10)

        with mock.patch('tempfile.mkstemp') as mkstemp:
            mkstemp.return_value = (os.open(self.parameter_defaults_env_file,
                                            os.O_RDWR),
                                    self.parameter_defaults_env_file)
            result = self.cmd.take_action(parsed_args)
        self.assertTrue(result)

        args, kwargs = orchestration_client.stacks.update.call_args

        self.assertEqual(args, (orchestration_client.stacks.get().id, ))

        self.assertEqual(kwargs['files'], {})
        self.assertEqual(kwargs['template'], 'template')
        self.assertEqual(kwargs['environment'], mock_env)
        self.assertEqual(kwargs['stack_name'], 'overcloud')
        self.assertEqual(kwargs['clear_parameters'],
                         mock_env['parameter_defaults'].keys())

        mock_get_templte_contents.assert_called_with(
            '/home/stack/tripleo-heat-templates/' +
            constants.OVERCLOUD_YAML_NAMES[0])

        mock_create_tempest_deployer_input.assert_called_with()
        mock_process_multiple_env.assert_called_with(
            [self.parameter_defaults_env_file])
    def test_tht_scale(
            self, mock_time, mock_uuid1, mock_check_hypervisor_stats,
            mock_get_key, mock_create_env, generate_certs_mock,
            mock_get_templte_contents, mock_process_multiple_env,
            wait_for_stack_ready_mock, mock_remove_known_hosts,
            mock_keystone_initialize, mock_sleep, mock_setup_endpoints,
            mock_create_overcloudrc, mock_generate_overcloud_passwords,
            mock_create_tempest_deployer_input, mock_deploy_postconfig,
            mock_create_parameters_env, mock_breakpoints_cleanupm,
            mock_events):

        arglist = ['--templates', '--ceph-storage-scale', '3']
        verifylist = [('templates',
                       '/usr/share/openstack-tripleo-heat-templates/'),
                      ('ceph_storage_scale', 3)]

        mock_uuid1.return_value = "uuid"
        mock_time.return_value = 123456789

        mock_generate_overcloud_passwords.return_value = self._get_passwords()

        clients = self.app.client_manager
        orchestration_client = clients.orchestration
        orchestration_client.stacks.get.return_value = fakes.create_tht_stack()
        mock_event = mock.Mock()
        mock_event.id = '1234'
        mock_events.return_value = [mock_events]

        mock_check_hypervisor_stats.return_value = {
            'count': 4,
            'memory_mb': 4096,
            'vcpus': 8,
        }
        mock_get_key.return_value = "PASSWORD"
        clients.network.api.find_attr.return_value = {"id": "network id"}
        mock_create_env.return_value = "/fake/path"
        mock_env = fakes.create_env()
        mock_process_multiple_env.return_value = [{}, mock_env]
        mock_get_templte_contents.return_value = [{}, "template"]
        wait_for_stack_ready_mock.return_value = True

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        baremetal = clients.baremetal
        baremetal.node.list.return_value = range(10)

        expected_parameters = {
            'AdminPassword': '******',
            'AdminToken': 'password',
            'AodhPassword': '******',
            'BarbicanPassword': '******',
            'CeilometerMeteringSecret': 'password',
            'CeilometerPassword': '******',
            'CephAdminKey': 'password',
            'CephClientKey': 'password',
            'CephClusterFSID': 'uuid',
            'CephMonKey': 'password',
            'CephRgwKey': 'password',
            'CephStorageCount': 3,
            'CinderPassword': '******',
            'ExtraConfig': '{}',
            'GlancePassword': '******',
            'GnocchiPassword': '******',
            'HAProxyStatsPassword': '******',
            'HeatPassword': '******',
            'HeatStackDomainAdminPassword': '******',
            'HypervisorNeutronPhysicalBridge': 'br-ex',
            'HypervisorNeutronPublicInterface': 'nic1',
            'IronicPassword': '******',
            'ManilaPassword': '******',
            'MistralPassword': '******',
            'MysqlClustercheckPassword': '******',
            'NeutronDhcpAgentsPerNetwork': 1,
            'NeutronDnsmasqOptions': 'dhcp-option-force=26,1400',
            'NeutronFlatNetworks': 'datacentre',
            'NeutronMetadataProxySharedSecret': 'password',
            'NeutronPassword': '******',
            'NeutronPublicInterface': 'nic1',
            'NovaPassword': '******',
            'NtpServer': '',
            'RabbitPassword': '******',
            'RedisPassword': '******',
            'SaharaPassword': '******',
            'SnmpdReadonlyUserPassword': '******',
            'SwiftHashSuffix': 'password',
            'SwiftPassword': '******',
            'TrovePassword': '******',
            'ZaqarPassword': '******',
            'DeployIdentifier': 123456789,
            'UpdateIdentifier': '',
            'StackAction': 'UPDATE',
        }

        def _custom_create_params_env(parameters):
            for key, value in six.iteritems(parameters):
                self.assertEqual(value, expected_parameters[key])
            parameter_defaults = {"parameter_defaults": parameters}
            with open(self.parameter_defaults_env_file, 'w') as temp_file:
                temp_file.write(json.dumps(parameter_defaults))
            return [self.parameter_defaults_env_file]

        mock_create_parameters_env.side_effect = _custom_create_params_env

        self.cmd.take_action(parsed_args)

        args, kwargs = orchestration_client.stacks.update.call_args

        self.assertEqual(args, (orchestration_client.stacks.get().id, ))

        self.assertEqual(kwargs['files'], {})
        self.assertEqual(kwargs['template'], 'template')
        self.assertEqual(kwargs['environment'], mock_env)
        self.assertEqual(kwargs['stack_name'], 'overcloud')
        self.assertEqual(kwargs['clear_parameters'],
                         mock_env['parameter_defaults'].keys())

        mock_get_templte_contents.assert_called_with(
            '/usr/share/openstack-tripleo-heat-templates/' +
            constants.OVERCLOUD_YAML_NAMES[0])

        mock_create_tempest_deployer_input.assert_called_with()
        mock_process_multiple_env.assert_called_with(
            [self.parameter_defaults_env_file])
    def test_deploy_custom_templates(
            self, mock_check_hypervisor_stats, mock_get_key, mock_create_env,
            generate_certs_mock, mock_get_templte_contents,
            mock_process_multiple_env, wait_for_stack_ready_mock,
            mock_remove_known_hosts, mock_keystone_initialize, mock_sleep,
            mock_setup_endpoints, mock_create_overcloudrc,
            mock_generate_overcloud_passwords,
            mock_create_tempest_deployer_input, mock_deploy_postconfig,
            mock_breakpoints_cleanup, mock_events):

        arglist = ['--templates', '/home/stack/tripleo-heat-templates']
        verifylist = [
            ('templates', '/home/stack/tripleo-heat-templates'),
        ]

        mock_generate_overcloud_passwords.return_value = self._get_passwords()

        clients = self.app.client_manager
        orchestration_client = clients.orchestration
        orchestration_client.stacks.get.return_value = fakes.create_tht_stack()
        mock_events.return_value = []

        mock_check_hypervisor_stats.return_value = {
            'count': 4,
            'memory_mb': 4096,
            'vcpus': 8,
        }
        mock_get_key.return_value = "PASSWORD"
        clients.network.api.find_attr.return_value = {"id": "network id"}
        mock_create_env.return_value = "/fake/path"
        mock_env = fakes.create_env()
        mock_process_multiple_env.return_value = [{}, mock_env]
        mock_get_templte_contents.return_value = [{}, "template"]
        wait_for_stack_ready_mock.return_value = True

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        baremetal = clients.baremetal
        baremetal.node.list.return_value = range(10)

        with mock.patch('tempfile.mkstemp') as mkstemp:
            mkstemp.return_value = (os.open(self.parameter_defaults_env_file,
                                            os.O_RDWR),
                                    self.parameter_defaults_env_file)
            self.cmd.take_action(parsed_args)

        args, kwargs = orchestration_client.stacks.update.call_args

        self.assertEqual(args, (orchestration_client.stacks.get().id, ))

        self.assertEqual(kwargs['files'], {})
        self.assertEqual(kwargs['template'], 'template')
        self.assertEqual(kwargs['environment'], mock_env)
        self.assertEqual(kwargs['stack_name'], 'overcloud')
        self.assertEqual(kwargs['clear_parameters'],
                         mock_env['parameter_defaults'].keys())

        mock_get_templte_contents.assert_called_with(
            '/home/stack/tripleo-heat-templates/' +
            constants.OVERCLOUD_YAML_NAMES[0])

        mock_create_tempest_deployer_input.assert_called_with()
        mock_process_multiple_env.assert_called_with(
            [self.parameter_defaults_env_file])
    def test_tht_scale(self, mock_uuid1, mock_create_cephx_key,
                       mock_check_hypervisor_stats, mock_get_key,
                       mock_create_env, generate_certs_mock,
                       mock_get_templte_contents, mock_process_multiple_env,
                       wait_for_stack_ready_mock,
                       mock_remove_known_hosts, mock_keystone_initialize,
                       mock_sleep, mock_setup_endpoints,
                       mock_create_overcloudrc,
                       mock_generate_overcloud_passwords,
                       mock_create_tempest_deployer_input,
                       mock_deploy_postconfig,
                       mock_create_parameters_env,
                       mock_breakpoints_cleanup):

        arglist = ['--templates', '--ceph-storage-scale', '3']
        verifylist = [
            ('templates', '/usr/share/openstack-tripleo-heat-templates/'),
        ]

        mock_create_cephx_key.return_value = "cephx_key"
        mock_uuid1.return_value = "uuid"

        mock_generate_overcloud_passwords.return_value = self._get_passwords()

        clients = self.app.client_manager
        orchestration_client = clients.tripleoclient.orchestration
        orchestration_client.stacks.get.return_value = fakes.create_tht_stack()

        mock_check_hypervisor_stats.return_value = {
            'count': 4,
            'memory_mb': 4096,
            'vcpus': 8,
        }
        mock_get_key.return_value = "PASSWORD"
        clients.network.api.find_attr.return_value = {
            "id": "network id"
        }
        mock_create_env.return_value = "/fake/path"
        mock_process_multiple_env.return_value = [{}, "env"]
        mock_get_templte_contents.return_value = [{}, "template"]
        wait_for_stack_ready_mock.return_value = True

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        baremetal = clients.tripleoclient.baremetal()
        baremetal.node.list.return_value = range(10)

        expected_parameters = {
            'AdminPassword': '******',
            'AdminToken': 'password',
            'BlockStorageImage': 'overcloud-full',
            'CeilometerMeteringSecret': 'password',
            'CeilometerPassword': '******',
            'CephStorageCount': 3,
            'CephStorageImage': 'overcloud-full',
            'CinderISCSIHelper': 'lioadm',
            'CinderPassword': '******',
            'CloudName': 'overcloud',
            'controllerImage': 'overcloud-full',
            'ExtraConfig': '{}',
            'GlancePassword': '******',
            'HeatPassword': '******',
            'HeatStackDomainAdminPassword': '******',
            'HypervisorNeutronPhysicalBridge': 'br-ex',
            'HypervisorNeutronPublicInterface': 'nic1',
            'NeutronAllowL3AgentFailover': False,
            'NeutronBridgeMappings': 'datacentre:br-ex',
            'NeutronControlPlaneID': 'network id',
            'NeutronDhcpAgentsPerNetwork': 1,
            'NeutronDnsmasqOptions': 'dhcp-option-force=26,1400',
            'NeutronFlatNetworks': 'datacentre',
            'NeutronL3HA': False,
            'NeutronNetworkVLANRanges': 'datacentre:1:1000',
            'NeutronPassword': '******',
            'NeutronPublicInterface': 'nic1',
            'NovaImage': 'overcloud-full',
            'NovaPassword': '******',
            'NtpServer': '',
            'OvercloudBlockStorageFlavor': 'baremetal',
            'OvercloudCephStorageFlavor': 'baremetal',
            'OvercloudComputeFlavor': 'baremetal',
            'OvercloudControlFlavor': 'baremetal',
            'OvercloudSwiftStorageFlavor': 'baremetal',
            'SnmpdReadonlyUserPassword': '******',
            'SwiftHashSuffix': 'password',
            'SwiftPassword': '******',
            'SwiftStorageImage': 'overcloud-full',
        }

        def _custom_create_params_env(parameters):
            for key, value in six.iteritems(parameters):
                self.assertEqual(value, expected_parameters[key])
            parameter_defaults = {"parameter_defaults": parameters}
            with open(self.parameter_defaults_env_file, 'w') as temp_file:
                temp_file.write(json.dumps(parameter_defaults))
            return [self.parameter_defaults_env_file]

        mock_create_parameters_env.side_effect = _custom_create_params_env

        result = self.cmd.take_action(parsed_args)
        self.assertTrue(result)

        args, kwargs = orchestration_client.stacks.update.call_args

        self.assertEqual(args, (orchestration_client.stacks.get().id, ))

        self.assertEqual(kwargs['files'], {})
        self.assertEqual(kwargs['template'], 'template')
        self.assertEqual(kwargs['environment'], 'env')
        self.assertEqual(kwargs['stack_name'], 'overcloud')

        mock_get_templte_contents.assert_called_with(
            ('/usr/share/openstack-tripleo-heat-templates/overcloud-without-'
             'mergepy.yaml'))

        mock_create_tempest_deployer_input.assert_called_with()
        mock_process_multiple_env.assert_called_with(
            [self.parameter_defaults_env_file])
    def test_tht_scale(self, mock_time, mock_uuid1, mock_create_cephx_key,
                       mock_check_hypervisor_stats, mock_get_key,
                       mock_create_env, generate_certs_mock,
                       mock_get_templte_contents, mock_process_multiple_env,
                       wait_for_stack_ready_mock,
                       mock_remove_known_hosts, mock_keystone_initialize,
                       mock_sleep, mock_setup_endpoints,
                       mock_create_overcloudrc,
                       mock_generate_overcloud_passwords,
                       mock_create_tempest_deployer_input,
                       mock_deploy_postconfig,
                       mock_create_parameters_env,
                       mock_breakpoints_cleanupm,
                       mock_events):

        arglist = ['--templates', '--ceph-storage-scale', '3']
        verifylist = [
            ('templates', '/usr/share/openstack-tripleo-heat-templates/'),
        ]

        mock_create_cephx_key.return_value = "cephx_key"
        mock_uuid1.return_value = "uuid"
        mock_time.return_value = 123456789

        mock_generate_overcloud_passwords.return_value = self._get_passwords()

        clients = self.app.client_manager
        orchestration_client = clients.tripleoclient.orchestration
        orchestration_client.stacks.get.return_value = fakes.create_tht_stack()
        mock_event = mock.Mock()
        mock_event.id = '1234'
        mock_events.return_value = [mock_events]

        mock_check_hypervisor_stats.return_value = {
            'count': 4,
            'memory_mb': 4096,
            'vcpus': 8,
        }
        mock_get_key.return_value = "PASSWORD"
        clients.network.api.find_attr.return_value = {
            "id": "network id"
        }
        mock_create_env.return_value = "/fake/path"
        mock_process_multiple_env.return_value = [{}, "env"]
        mock_get_templte_contents.return_value = [{}, "template"]
        wait_for_stack_ready_mock.return_value = True

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        baremetal = clients.tripleoclient.baremetal
        baremetal.node.list.return_value = range(10)

        expected_parameters = {
            'AdminPassword': '******',
            'AdminToken': 'password',
            'BlockStorageImage': 'overcloud-full',
            'CeilometerMeteringSecret': 'password',
            'CeilometerPassword': '******',
            'CephStorageCount': 3,
            'CephStorageImage': 'overcloud-full',
            'CinderISCSIHelper': 'lioadm',
            'CinderPassword': '******',
            'CloudName': 'overcloud',
            'controllerImage': 'overcloud-full',
            'ExtraConfig': '{}',
            'GlancePassword': '******',
            'HeatPassword': '******',
            'HeatStackDomainAdminPassword': '******',
            'HypervisorNeutronPhysicalBridge': 'br-ex',
            'HypervisorNeutronPublicInterface': 'nic1',
            'NeutronAllowL3AgentFailover': False,
            'NeutronBridgeMappings': 'datacentre:br-ex',
            'NeutronControlPlaneID': 'network id',
            'NeutronDhcpAgentsPerNetwork': 1,
            'NeutronDnsmasqOptions': 'dhcp-option-force=26,1400',
            'NeutronFlatNetworks': 'datacentre',
            'NeutronL3HA': False,
            'NeutronNetworkVLANRanges': 'datacentre:1:1000',
            'NeutronPassword': '******',
            'NeutronPublicInterface': 'nic1',
            'NovaImage': 'overcloud-full',
            'NovaPassword': '******',
            'NtpServer': '',
            'OvercloudBlockStorageFlavor': 'baremetal',
            'OvercloudCephStorageFlavor': 'baremetal',
            'OvercloudComputeFlavor': 'baremetal',
            'OvercloudControlFlavor': 'baremetal',
            'OvercloudSwiftStorageFlavor': 'baremetal',
            'SnmpdReadonlyUserPassword': '******',
            'SwiftHashSuffix': 'password',
            'SwiftPassword': '******',
            'SwiftStorageImage': 'overcloud-full',
            'DeployIdentifier': 123456789,
        }

        def _custom_create_params_env(parameters):
            for key, value in six.iteritems(parameters):
                self.assertEqual(value, expected_parameters[key])
            parameter_defaults = {"parameter_defaults": parameters}
            with open(self.parameter_defaults_env_file, 'w') as temp_file:
                temp_file.write(json.dumps(parameter_defaults))
            return [self.parameter_defaults_env_file]

        mock_create_parameters_env.side_effect = _custom_create_params_env

        result = self.cmd.take_action(parsed_args)
        self.assertTrue(result)

        args, kwargs = orchestration_client.stacks.update.call_args

        self.assertEqual(args, (orchestration_client.stacks.get().id, ))

        self.assertEqual(kwargs['files'], {})
        self.assertEqual(kwargs['template'], 'template')
        self.assertEqual(kwargs['environment'], 'env')
        self.assertEqual(kwargs['stack_name'], 'overcloud')

        mock_get_templte_contents.assert_called_with(
            ('/usr/share/openstack-tripleo-heat-templates/overcloud-without-'
             'mergepy.yaml'))

        mock_create_tempest_deployer_input.assert_called_with()
        mock_process_multiple_env.assert_called_with(
            [self.parameter_defaults_env_file])
    def test_tht_deploy(
            self, mock_time, mock_uuid1, mock_create_cephx_key,
            mock_check_hypervisor_stats, mock_get_key, mock_create_env,
            generate_certs_mock, mock_get_templte_contents,
            mock_process_multiple_env, wait_for_stack_ready_mock,
            mock_remove_known_hosts, mock_keystone_initialize, mock_sleep,
            mock_setup_endpoints, mock_get_keystone_client,
            mock_get_nova_bm_client, mock_create_overcloudrc,
            mock_generate_overcloud_passwords,
            mock_create_tempest_deployer_input, mock_create_parameters_env,
            mock_validate_args, mock_breakpoints_cleanup):

        arglist = ['--templates', '--ceph-storage-scale', '3']
        verifylist = [('templates',
                       '/usr/share/openstack-tripleo-heat-templates/'),
                      ('ceph_storage_scale', 3)]

        mock_create_cephx_key.return_value = "cephx_key"
        mock_uuid1.return_value = "uuid"
        mock_time.return_value = 123456789

        mock_generate_overcloud_passwords.return_value = self._get_passwords()

        clients = self.app.client_manager
        orchestration_client = clients.tripleoclient.orchestration
        mock_stack = fakes.create_tht_stack()
        orchestration_client.stacks.get.return_value = None

        def _orch_clt_create(**kwargs):
            orchestration_client.stacks.get.return_value = mock_stack

        orchestration_client.stacks.create.side_effect = _orch_clt_create

        mock_check_hypervisor_stats.return_value = {
            'count': 4,
            'memory_mb': 4096,
            'vcpus': 8,
        }
        mock_get_key.return_value = "PASSWORD"
        clients.network.api.find_attr.return_value = {"id": "network id"}
        mock_create_env.return_value = "/fake/path"
        mock_env = fakes.create_env()
        mock_process_multiple_env.return_value = [{}, mock_env]
        mock_get_templte_contents.return_value = [{}, "template"]
        wait_for_stack_ready_mock.return_value = True

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        baremetal = clients.baremetal
        baremetal.node.list.return_value = range(10)

        expected_parameters = {
            'AdminPassword': '******',
            'AdminToken': 'password',
            'AodhPassword': '******',
            'CeilometerMeteringSecret': 'password',
            'CeilometerPassword': '******',
            'CephAdminKey': 'cephx_key',
            'CephClusterFSID': 'uuid',
            'CephMonKey': 'cephx_key',
            'CephStorageCount': 3,
            'CinderPassword': '******',
            'ExtraConfig': '{}',
            'GlancePassword': '******',
            'HAProxyStatsPassword': '******',
            'HeatPassword': '******',
            'HeatStackDomainAdminPassword': '******',
            'HypervisorNeutronPhysicalBridge': 'br-ex',
            'HypervisorNeutronPublicInterface': 'nic1',
            'NeutronAllowL3AgentFailover': False,
            'NeutronDhcpAgentsPerNetwork': 1,
            'NeutronDnsmasqOptions': 'dhcp-option-force=26,1400',
            'NeutronEnableTunnelling': 'True',
            'NeutronFlatNetworks': 'datacentre',
            'NeutronL3HA': False,
            'NeutronNetworkType': 'gre',
            'NeutronMetadataProxySharedSecret': 'password',
            'NeutronPassword': '******',
            'NeutronPublicInterface': 'nic1',
            'NeutronTunnelTypes': 'gre',
            'NovaPassword': '******',
            'NtpServer': '',
            'RabbitPassword': '******',
            'RedisPassword': '******',
            'SaharaPassword': '******',
            'SnmpdReadonlyUserPassword': '******',
            'SwiftHashSuffix': 'password',
            'SwiftPassword': '******',
            'DeployIdentifier': 123456789,
        }

        def _custom_create_params_env(parameters):
            for key, value in six.iteritems(parameters):
                self.assertEqual(value, expected_parameters[key])
            parameter_defaults = {"parameter_defaults": parameters}
            with open(self.parameter_defaults_env_file, 'w') as temp_file:
                temp_file.write(json.dumps(parameter_defaults))
            return [self.parameter_defaults_env_file]

        mock_create_parameters_env.side_effect = _custom_create_params_env

        result = self.cmd.take_action(parsed_args)
        self.assertTrue(result)

        args, kwargs = orchestration_client.stacks.create.call_args

        self.assertEqual(kwargs['files'], {})
        self.assertEqual(kwargs['template'], 'template')
        self.assertEqual(kwargs['environment'], mock_env)
        self.assertEqual(kwargs['stack_name'], 'overcloud')
        self.assertEqual(kwargs['clear_parameters'],
                         mock_env['parameter_defaults'].keys())

        mock_get_templte_contents.assert_called_with(
            '/usr/share/openstack-tripleo-heat-templates/' +
            constants.OVERCLOUD_YAML_NAMES[0])

        mock_create_tempest_deployer_input.assert_called_with()
        mock_process_multiple_env.assert_called_with([
            '/usr/share/openstack-tripleo-heat-templates/overcloud-resource-'
            'registry-puppet.yaml', '/fake/path',
            self.parameter_defaults_env_file
        ])

        mock_validate_args.assert_called_once_with(parsed_args)

        mock_remove_known_hosts.assert_called_once_with('0.0.0.0')
        mock_get_keystone_client.assert_called_once_with(
            'admin', 'password', 'admin', 'http://0.0.0.0:8000')