def test_create_customization_un_customized(self):
     """
         test customization - uncustomized
     """
     fake_ctx = self.generate_context_for_customization()
     fake_client = self.generate_client()
     self.run_with_statuses(
         fake_client, fake_ctx,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS
     )
     fake_client._vapp.customize_on_next_poweron = mock.MagicMock(
         return_value=None
     )
     fake_client._vapp.force_customization = mock.MagicMock(
         return_value=None
     )
     with mock.patch(
         'vcloud_plugin_common.VcloudAirClient.get',
         mock.MagicMock(return_value=fake_client)
     ):
         with self.assertRaises(cfy_exc.NonRecoverableError):
             server.configure(ctx=fake_ctx)
 def test_create_customization(self):
     """
         test customization - task None
     """
     fake_ctx = self.generate_context_for_customization()
     fake_client = self.generate_client()
     self.run_with_statuses(fake_client, fake_ctx,
                            vcloud_plugin_common.TASK_STATUS_SUCCESS,
                            vcloud_plugin_common.TASK_STATUS_SUCCESS,
                            vcloud_plugin_common.TASK_STATUS_SUCCESS)
     with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                     mock.MagicMock(return_value=fake_client)):
         with self.assertRaises(cfy_exc.NonRecoverableError):
             server.configure(ctx=fake_ctx)
 def test_create_cpu_mem_values(self):
     """
         check custom cpu/memmory with error in task
     """
     fake_ctx = self.generate_node_context(
         properties={
             'management_network': '_management_network',
             'vcloud_config': {
                 'vdc': 'vdc_name'
             },
             'server': {
                 'template': 'ubuntu',
                 'catalog': 'public',
                 'hardware': {
                     'cpu': 1,
                     'memory': 512
                 }
             }
         },
         relation_node_properties={"not_test": "not_test"})
     fake_client = self.generate_client()
     self.run_with_statuses(fake_client, fake_ctx,
                            vcloud_plugin_common.TASK_STATUS_SUCCESS,
                            vcloud_plugin_common.TASK_STATUS_SUCCESS,
                            vcloud_plugin_common.TASK_STATUS_SUCCESS,
                            vcloud_plugin_common.TASK_STATUS_SUCCESS)
     with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                     mock.MagicMock(return_value=fake_client)):
         # can't customize memory
         with self.assertRaises(cfy_exc.NonRecoverableError):
             server.configure(ctx=fake_ctx)
         fake_client._vapp.modify_vm_memory.assert_called_with('test', 512)
         fake_client._vapp.modify_vm_memory = mock.MagicMock(
             return_value=self.generate_task(
                 vcloud_plugin_common.TASK_STATUS_SUCCESS))
         # can't customize cpu
         with self.assertRaises(cfy_exc.NonRecoverableError):
             server.configure(ctx=fake_ctx)
         fake_client._vapp.modify_vm_cpu.assert_called_with('test', 1)
         fake_client._vapp.modify_vm_cpu = mock.MagicMock(
             return_value=self.generate_task(
                 vcloud_plugin_common.TASK_STATUS_SUCCESS))
         fake_client._vapp.modify_vm_name = mock.MagicMock(
             return_value=self.generate_task(
                 vcloud_plugin_common.TASK_STATUS_SUCCESS))
         # everything fine
         server.create(ctx=fake_ctx)
         fake_client._vapp.modify_vm_name.assert_called_with(1, 'test')
 def test_create_customization_customized(self):
     """
         test customization - success customization
     """
     fake_ctx = self.generate_context_for_customization()
     fake_client = self.generate_client()
     fake_client._vapp.customize_on_next_poweron = mock.MagicMock(
         return_value=True)
     self.run_with_statuses(fake_client, fake_ctx,
                            vcloud_plugin_common.TASK_STATUS_SUCCESS,
                            vcloud_plugin_common.TASK_STATUS_SUCCESS,
                            vcloud_plugin_common.TASK_STATUS_SUCCESS,
                            vcloud_plugin_common.TASK_STATUS_SUCCESS)
     with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                     mock.MagicMock(return_value=fake_client)):
         server.configure(ctx=fake_ctx)
 def test_create_customization_error(self):
     """
         test customization - task error
     """
     fake_ctx = self.generate_context_for_customization()
     fake_client = self.generate_client()
     self.run_with_statuses(
         fake_client, fake_ctx,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_ERROR
     )
     with mock.patch(
         'vcloud_plugin_common.VcloudAirClient.get',
         mock.MagicMock(return_value=fake_client)
     ):
         with self.assertRaises(cfy_exc.NonRecoverableError):
             server.configure(ctx=fake_ctx)
 def test_create_customization_customized(self):
     """
         test customization - success customization
     """
     fake_ctx = self.generate_context_for_customization()
     fake_client = self.generate_client()
     fake_client._vapp.customize_on_next_poweron = mock.MagicMock(
         return_value=True
     )
     self.run_with_statuses(
         fake_client, fake_ctx,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS
     )
     with mock.patch(
         'vcloud_plugin_common.VcloudAirClient.get',
         mock.MagicMock(return_value=fake_client)
     ):
         server.configure(ctx=fake_ctx)
    def test_create_configure_cpu_mem_values(self):
        """
            check custom cpu/memmory with error in task
        """
        # use existed vm
        fake_ctx = self.generate_node_context(
            properties={
                'management_network': '_management_network',
                'vcloud_config': {
                    'vdc': 'vdc_name'
                },
                'use_external_resource': True,
                'resource_id': 'some_server'
            },
            relation_node_properties={
                "not_test": "not_test"
            }
        )
        fake_client = self.generate_client()
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            server.configure(ctx=fake_ctx)
        # can't get vapp
        fake_ctx = self.generate_node_context(
            properties={
                'management_network': '_management_network',
                'vcloud_config': {
                    'vdc': 'vdc_name'
                },
                'server': {
                    'template': 'ubuntu',
                    'catalog': 'public',
                    'hardware': {
                        'cpu': 1,
                        'memory': 512
                    },
                    'guest_customization': {
                        'pre_script': 'pre_script',
                        'post_script': 'post_script',
                        'admin_password': '******',
                        'computer_name': 'computer'
                    }
                }
            },
            relation_node_properties={
                "not_test": "not_test"
            }
        )
        fake_client = self.generate_client()
        fake_client.get_vapp = mock.MagicMock(return_value=None)
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                server.configure(ctx=fake_ctx)
        # create new vm
        fake_client = self.generate_client()
        self.run_with_statuses(
            fake_client, fake_ctx,
            vcloud_plugin_common.TASK_STATUS_SUCCESS,
            vcloud_plugin_common.TASK_STATUS_SUCCESS,
            vcloud_plugin_common.TASK_STATUS_SUCCESS,
            vcloud_plugin_common.TASK_STATUS_SUCCESS,
            vcloud_plugin_common.TASK_STATUS_SUCCESS,
            vcloud_plugin_common.TASK_STATUS_SUCCESS
        )
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            # can't customize memory
            with self.assertRaises(cfy_exc.NonRecoverableError):
                server.configure(ctx=fake_ctx)
            fake_client._vapp.modify_vm_memory.assert_called_with(
                'test', 512
            )
            fake_client._vapp.modify_vm_memory = mock.MagicMock(
                return_value=self.generate_task(
                    vcloud_plugin_common.TASK_STATUS_SUCCESS
                )
            )

            # can't customize cpu
            with self.assertRaises(cfy_exc.NonRecoverableError):
                server.configure(ctx=fake_ctx)
            fake_client._vapp.modify_vm_cpu.assert_called_with(
                'test', 1
            )
            fake_client._vapp.modify_vm_cpu = mock.MagicMock(
                return_value=self.generate_task(
                    vcloud_plugin_common.TASK_STATUS_SUCCESS
                )
            )
            fake_client._vapp.modify_vm_name = mock.MagicMock(
                return_value=self.generate_task(
                    vcloud_plugin_common.TASK_STATUS_SUCCESS
                )
            )

            # need force customization, successfull customization
            fake_client._vapp.customize_on_next_poweron = mock.MagicMock(
                return_value=False
            )
            server.configure(ctx=fake_ctx)

            # somethin wrong with force_customization
            fake_client._vapp.force_customization = mock.MagicMock(
                return_value=None
            )
            with self.assertRaises(cfy_exc.NonRecoverableError):
                server.configure(ctx=fake_ctx)

            # everything fine
            fake_client._vapp.customize_on_next_poweron = mock.MagicMock(
                return_value=True
            )
            server.configure(ctx=fake_ctx)
            server.create(ctx=fake_ctx)
            fake_client._vapp.modify_vm_name.assert_called_with(
                1, 'test'
            )

            # we dont have connected ip
            fake_client._vapp.get_vms_network_info = mock.MagicMock(
                return_value=[[{'is_connected': False}]]
            )
            fake_client._vapp.me.get_status = mock.MagicMock(
                return_value=vcloud_plugin_common.STATUS_POWERED_ON
            )
            sleep_mock = mock.MagicMock()
            with mock.patch(
                'time.sleep',
                sleep_mock
            ):
                server.configure(ctx=fake_ctx)
            sleep_mock.assert_called_with(
                vcloud_network_plugin.GATEWAY_TIMEOUT
            )
            # after first run we have ip
            vapp_with_network = mock.MagicMock()
            vapp_with_network.get_vms_network_info = mock.MagicMock(
                return_value=[[{
                    'is_connected': True,
                    'is_primary': True,
                    'network_name': 'network_name',
                    'ip': '1.1.1.1'
                }]]
            )
            fake_client.get_vapp = mock.MagicMock(
                side_effect=[fake_client._vapp, vapp_with_network]
            )
            with mock.patch(
                'time.sleep',
                sleep_mock
            ):
                server.configure(ctx=fake_ctx)
 def test_create_cpu_mem_values(self):
     """
         check custom cpu/memmory with error in task
     """
     fake_ctx = self.generate_node_context(
         properties={
             'management_network': '_management_network',
             'vcloud_config': {
                 'vdc': 'vdc_name'
             },
             'server': {
                 'template': 'ubuntu',
                 'catalog': 'public',
                 'hardware': {
                     'cpu': 1,
                     'memory': 512
                 }
             }
         },
         relation_node_properties={
             "not_test": "not_test"
         }
     )
     fake_client = self.generate_client()
     self.run_with_statuses(
         fake_client, fake_ctx,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS,
         vcloud_plugin_common.TASK_STATUS_SUCCESS
     )
     with mock.patch(
         'vcloud_plugin_common.VcloudAirClient.get',
         mock.MagicMock(return_value=fake_client)
     ):
         # can't customize memory
         with self.assertRaises(cfy_exc.NonRecoverableError):
             server.configure(ctx=fake_ctx)
         fake_client._vapp.modify_vm_memory.assert_called_with(
             'test', 512
         )
         fake_client._vapp.modify_vm_memory = mock.MagicMock(
             return_value=self.generate_task(
                 vcloud_plugin_common.TASK_STATUS_SUCCESS
             )
         )
         # can't customize cpu
         with self.assertRaises(cfy_exc.NonRecoverableError):
             server.configure(ctx=fake_ctx)
         fake_client._vapp.modify_vm_cpu.assert_called_with(
             'test', 1
         )
         fake_client._vapp.modify_vm_cpu = mock.MagicMock(
             return_value=self.generate_task(
                 vcloud_plugin_common.TASK_STATUS_SUCCESS
             )
         )
         fake_client._vapp.modify_vm_name = mock.MagicMock(
             return_value=self.generate_task(
                 vcloud_plugin_common.TASK_STATUS_SUCCESS
             )
         )
         # everything fine
         server.create(ctx=fake_ctx)
         fake_client._vapp.modify_vm_name.assert_called_with(
             1, 'test'
         )
    def test_create_configure_cpu_mem_values(self):
        """
            check custom cpu/memmory with error in task
        """
        # use existed vm
        fake_ctx = self.generate_node_context(
            properties={
                'management_network': '_management_network',
                'vcloud_config': {
                    'vdc': 'vdc_name'
                },
                'use_external_resource': True,
                'resource_id': 'some_server'
            },
            relation_node_properties={"not_test": "not_test"})
        fake_client = self.generate_client()
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            server.configure(ctx=fake_ctx)
        # can't get vapp
        fake_ctx = self.generate_node_context(
            properties={
                'management_network': '_management_network',
                'vcloud_config': {
                    'vdc': 'vdc_name'
                },
                'server': {
                    'template': 'ubuntu',
                    'catalog': 'public',
                    'hardware': {
                        'cpu': 1,
                        'memory': 512
                    },
                    'guest_customization': {
                        'pre_script': 'pre_script',
                        'post_script': 'post_script',
                        'admin_password': '******',
                        'computer_name': 'computer'
                    }
                }
            },
            relation_node_properties={"not_test": "not_test"})
        fake_client = self.generate_client()
        fake_client.get_vapp = mock.MagicMock(return_value=None)
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                server.configure(ctx=fake_ctx)
        # create new vm
        fake_client = self.generate_client()
        self.run_with_statuses(fake_client, fake_ctx,
                               vcloud_plugin_common.TASK_STATUS_SUCCESS,
                               vcloud_plugin_common.TASK_STATUS_SUCCESS,
                               vcloud_plugin_common.TASK_STATUS_SUCCESS,
                               vcloud_plugin_common.TASK_STATUS_SUCCESS,
                               vcloud_plugin_common.TASK_STATUS_SUCCESS,
                               vcloud_plugin_common.TASK_STATUS_SUCCESS)
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            # can't customize memory
            with self.assertRaises(cfy_exc.NonRecoverableError):
                server.configure(ctx=fake_ctx)
            fake_client._vapp.modify_vm_memory.assert_called_with('test', 512)
            fake_client._vapp.modify_vm_memory = mock.MagicMock(
                return_value=self.generate_task(
                    vcloud_plugin_common.TASK_STATUS_SUCCESS))

            # can't customize cpu
            with self.assertRaises(cfy_exc.NonRecoverableError):
                server.configure(ctx=fake_ctx)
            fake_client._vapp.modify_vm_cpu.assert_called_with('test', 1)
            fake_client._vapp.modify_vm_cpu = mock.MagicMock(
                return_value=self.generate_task(
                    vcloud_plugin_common.TASK_STATUS_SUCCESS))
            fake_client._vapp.modify_vm_name = mock.MagicMock(
                return_value=self.generate_task(
                    vcloud_plugin_common.TASK_STATUS_SUCCESS))

            # need force customization, successfull customization
            fake_client._vapp.customize_on_next_poweron = mock.MagicMock(
                return_value=False)
            server.configure(ctx=fake_ctx)

            # somethin wrong with force_customization
            fake_client._vapp.force_customization = mock.MagicMock(
                return_value=None)
            with self.assertRaises(cfy_exc.NonRecoverableError):
                server.configure(ctx=fake_ctx)

            # everything fine
            fake_client._vapp.customize_on_next_poweron = mock.MagicMock(
                return_value=True)
            server.configure(ctx=fake_ctx)
            server.create(ctx=fake_ctx)
            fake_client._vapp.modify_vm_name.assert_called_with(1, 'test')

            # we dont have connected ip
            fake_client._vapp.get_vms_network_info = mock.MagicMock(
                return_value=[[{
                    'is_connected': False
                }]])
            fake_client._vapp.me.get_status = mock.MagicMock(
                return_value=vcloud_plugin_common.STATUS_POWERED_ON)
            sleep_mock = mock.MagicMock()
            with mock.patch('time.sleep', sleep_mock):
                server.configure(ctx=fake_ctx)
            sleep_mock.assert_called_with(
                vcloud_network_plugin.GATEWAY_TIMEOUT)
            # after first run we have ip
            vapp_with_network = mock.MagicMock()
            vapp_with_network.get_vms_network_info = mock.MagicMock(
                return_value=[[{
                    'is_connected': True,
                    'is_primary': True,
                    'network_name': 'network_name',
                    'ip': '1.1.1.1'
                }]])
            fake_client.get_vapp = mock.MagicMock(
                side_effect=[fake_client._vapp, vapp_with_network])
            with mock.patch('time.sleep', sleep_mock):
                server.configure(ctx=fake_ctx)