class TestInstaller(BaseTest, TestCase):

    def setUp(self):
        super(TestInstaller, self).setUp()
        self._package_url = agent_package.get_package_url()
        self.addCleanup(agent_package.cleanup)

    @get_tenant_mock()
    @patch('cloudify.agent_utils.get_rest_client',
           return_value=MockRestclient())
    @patch('cloudify.utils.get_manager_name', return_value='cloudify')
    def _test_agent_installation(self, agent_config, *_):
        new_ctx = mock_context()
        current_ctx.set(new_ctx)

        self.assert_daemon_dead(agent_config['name'])
        create_agent(agent_config=agent_config)
        self.wait_for_daemon_alive(agent_config['name'])

        new_agent = ctx.instance.runtime_properties['cloudify_agent']

        agent_ssl_cert.verify_remote_cert(new_agent['agent_dir'])

        command_format = 'cfy-agent daemons {0} --name {1}'.format(
            '{0}',
            new_agent['name'])
        runner = LocalCommandRunner()
        runner.run(command_format.format('stop'))
        runner.run(command_format.format('delete'))

        self.assert_daemon_dead(agent_config['name'])
        return new_agent

    def _get_agent_config(self):
        return CloudifyAgentConfig({
            'name': '{0}_{1}'.format('agent_', str(uuid.uuid4())),
            'ip': 'localhost',
            'package_url': self._package_url,
            'rest_host': 'localhost',
            'broker_ip': 'localhost',
            'windows': os.name == 'nt',
            'local': True,
            'ssl_cert_path': self._rest_cert_path
        })

    @only_ci
    def test_installation(self):
        base_dir = tempfile.mkdtemp()
        agent_config = self._get_agent_config()
        agent_config['basedir'] = base_dir
        try:
            self._test_agent_installation(agent_config)
        finally:
            shutil.rmtree(base_dir)

    @only_ci
    def test_installation_no_basedir(self):
        agent_config = self._get_agent_config()
        new_agent = self._test_agent_installation(agent_config)
        self.assertIn('basedir', new_agent)
    _test_local_agent_from_package(agent_name, file_server, agent_ssl_cert,
                                   request)


@pytest.mark.only_nt
@pytest.mark.only_ci
def test_local_agent_from_package_nt(file_server, tmp_path, agent_ssl_cert,
                                     request):
    agent_name = utils.internal.generate_agent_name()
    _test_local_agent_from_package(agent_name, file_server, agent_ssl_cert,
                                   request)


@patch('cloudify.workflows.local._validate_node')
@patch('cloudify_agent.installer.operations.delete_agent_rabbitmq_user')
@patch('cloudify.agent_utils.get_rest_client', return_value=MockRestclient())
@get_tenant_mock()
@patch('cloudify.utils.get_manager_name', return_value='cloudify')
def _test_local_agent_from_package(agent_name, fs, ssl_cert, request, *_):

    agent_queue = '{0}-queue'.format(agent_name)

    blueprint_path = resources.get_resource(
        'blueprints/agent-from-package/local-agent-blueprint.yaml')
    logger.info('Initiating local env')

    inputs = {
        'resource_base': fs.root_path,
        'source_url': get_source_uri(),
        'requirements_file': get_requirements_uri(),
        'name': agent_name,
示例#3
0
class TestInstallNewAgent(BaseDaemonLiveTestCase, TestCase):
    @contextmanager
    def _manager_env(self):
        port = 8756
        fs = FileServer(root_path=self.temp_folder, port=port)
        fs.start()
        self.addCleanup(fs.stop)
        if os.name == 'nt':
            package_name = 'cloudify-windows-agent.exe'
        else:
            dist = platform.dist()
            package_name = '{0}-{1}-agent.tar.gz'.format(
                dist[0].lower(), dist[2].lower())
        resources_dir = os.path.join(self.temp_folder, 'resources')
        agent_dir = os.path.join(resources_dir, 'packages', 'agents')
        agent_script_dir = os.path.join(resources_dir, 'cloudify_agent')
        os.makedirs(agent_dir)
        os.makedirs(agent_script_dir)
        os.makedirs(os.path.join(self.temp_folder, 'cloudify'))

        agent_path = os.path.join(agent_dir, package_name)
        shutil.copyfile(agent_package.get_package_path(), agent_path)
        self.addCleanup(agent_package.cleanup)

        new_env = {
            constants.MANAGER_FILE_SERVER_ROOT_KEY: resources_dir,
            constants.REST_PORT_KEY: str(port),
            constants.MANAGER_NAME: 'cloudify'
        }

        original_create_op_context = operations._get_cloudify_context

        def mock_create_op_context(agent, task_name):
            context = original_create_op_context(agent, task_name)
            context['__cloudify_context']['local'] = True
            return context

        # Need to patch, to avoid broker_ssl_enabled being True
        @contextmanager
        def get_amqp_client(agent):
            yield get_client()

        managers = [
            ManagerItem({
                'networks': {
                    'default': '127.0.0.1'
                },
                'ca_cert_content': agent_ssl_cert.DUMMY_CERT,
                'hostname': 'cloudify'
            })
        ]
        patches = [
            patch.dict(os.environ, new_env),
            patch('cloudify_agent.operations._get_amqp_client',
                  get_amqp_client),
            patch('cloudify.endpoint.LocalEndpoint.get_managers',
                  return_value=managers),
            patch('cloudify_agent.operations._get_cloudify_context',
                  mock_create_op_context),
            get_tenant_mock()
        ]
        for p in patches:
            p.start()
        try:
            yield
        finally:
            for p in patches:
                p.stop()
            fs.stop()

    @patch('cloudify_agent.installer.operations.delete_agent_rabbitmq_user')
    @patch('cloudify.agent_utils.get_rest_client',
           return_value=MockRestclient())
    @only_ci
    def test_install_new_agent(self, *_):
        agent_name = utils.internal.generate_agent_name()

        blueprint_path = resources.get_resource(
            'blueprints/install-new-agent/install-new-agent-blueprint.yaml')
        self.logger.info('Initiating local env')
        inputs = {'name': agent_name, 'ssl_cert_path': self._rest_cert_path}

        with self._manager_env():
            env = local.init_env(name=self._testMethodName,
                                 blueprint_path=blueprint_path,
                                 inputs=inputs)
            env.execute('install', task_retries=0)
            agent_dict = self.get_agent_dict(env, 'new_agent_host')
            agent_ssl_cert.verify_remote_cert(agent_dict['agent_dir'])
            new_agent_name = agent_dict['name']
            self.assertNotEqual(new_agent_name, agent_name)
            self.assert_daemon_alive(new_agent_name)
            env.execute('uninstall', task_retries=1)
            self.wait_for_daemon_dead(name=agent_name)
            self.wait_for_daemon_dead(name=new_agent_name)
示例#4
0
class TestCreateAgentAmqp(BaseTest, TestCase):
    @staticmethod
    @patch('cloudify_agent.installer.config.agent_config.ctx', mock_context())
    @patch('cloudify.utils.ctx', mock_context())
    def _create_agent():
        old_agent = CloudifyAgentConfig({
            'install_method':
            'remote',
            'ip':
            '10.0.4.47',
            'rest_host':
            '10.0.4.46',
            'distro':
            'ubuntu',
            'distro_codename':
            'trusty',
            'basedir':
            '/home/vagrant',
            'user':
            '******',
            'key':
            '~/.ssh/id_rsa',
            'windows':
            False,
            'package_url':
            'http://10.0.4.46:53229/packages/agents/'
            'ubuntu-trusty-agent.tar.gz',
            'version':
            '4.4',
            'broker_config': {
                'broker_ip': '10.0.4.46',
                'broker_pass': '******',
                'broker_user': '******',
                'broker_ssl_cert': ''
            }
        })

        old_agent.set_execution_params()
        old_agent.set_default_values()
        old_agent.set_installation_params(runner=None)
        return old_agent

    @contextmanager
    def _set_context(self, host='localhost'):
        old_context = ctx
        try:
            os.environ[constants.MANAGER_FILE_SERVER_ROOT_KEY] = \
                self.temp_folder
            os.environ[constants.MANAGER_NAME] = 'cloudify'
            properties = {}
            properties['cloudify_agent'] = self._create_agent()
            properties['agent_status'] = {'agent_alive_crossbroker': True}
            mock = mocks.MockCloudifyContext(node_id='host_af231',
                                             runtime_properties=properties,
                                             node_name='host',
                                             properties={'cloudify_agent': {}},
                                             brokers=[{
                                                 'networks': {
                                                     'default': host
                                                 }
                                             }],
                                             managers=[{
                                                 'networks': {
                                                     'default': host
                                                 },
                                                 'hostname': 'cloudify'
                                             }])
            current_ctx.set(mock)
            yield
        finally:
            current_ctx.set(old_context)

    def test_create_agent_dict(self):
        with self._set_context(host='10.0.4.48'):
            old_agent = self._create_agent()
            new_agent = operations.create_new_agent_config(old_agent)
            new_agent['version'] = '3.4'
            third_agent = operations.create_new_agent_config(new_agent)
            equal_keys = ['ip', 'basedir', 'user']
            for k in equal_keys:
                self.assertEqual(old_agent[k], new_agent[k])
                self.assertEqual(old_agent[k], third_agent[k])
            nonequal_keys = [
                'agent_dir', 'workdir', 'envdir', 'name', 'rest_host'
            ]
            for k in nonequal_keys:
                self.assertNotEqual(old_agent[k], new_agent[k])
                self.assertNotEqual(old_agent[k], third_agent[k])
            old_name = old_agent['name']
            new_name = new_agent['name']
            third_name = third_agent['name']
            self.assertIn(old_name, new_name)
            self.assertIn(old_name, third_name)
            self.assertLessEqual(len(third_name), len(new_name))
            new_agent['name'] = '{0}{1}'.format(new_agent['name'], 'not-uuid')
            agent = operations.create_new_agent_config(new_agent)
            self.assertIn(new_agent['name'], agent['name'])

    @patch('cloudify_agent.operations._send_amqp_task')
    @patch('cloudify_agent.api.utils.is_agent_alive',
           MagicMock(return_value=True))
    @get_tenant_mock()
    @get_tenant_mock(patch_where='cloudify_agent.operations.get_tenant')
    @patch('cloudify.agent_utils.get_rest_client',
           return_value=MockRestclient())
    def test_create_agent_from_old_agent(self, *mocks):
        with self._set_context():
            self._create_cloudify_agent_dir()
            old_name = ctx.instance.runtime_properties['cloudify_agent'][
                'name']
            old_agent_dir = ctx.instance.runtime_properties['cloudify_agent'][
                'agent_dir']
            old_queue = ctx.instance.runtime_properties['cloudify_agent'][
                'queue']

            operations.create_agent_amqp()
            new_name = ctx.instance.runtime_properties['cloudify_agent'][
                'name']
            new_agent_dir = ctx.instance.runtime_properties['cloudify_agent'][
                'agent_dir']
            new_queue = ctx.instance.runtime_properties['cloudify_agent'][
                'queue']
            self.assertNotEquals(old_name, new_name)
            self.assertNotEquals(old_agent_dir, new_agent_dir)
            self.assertNotEquals(old_queue, new_queue)

    def _create_cloudify_agent_dir(self):
        agent_script_dir = os.path.join(self.temp_folder, 'cloudify_agent')
        os.makedirs(agent_script_dir)
class TestAgentInstallerLocal(BaseDaemonLiveTestCase, TestCase):
    """
    these tests run local workflows in order to invoke the installer
    operations. the remote use case is tested as part of the system tests.
    """
    @classmethod
    def setUpClass(cls):
        cls.logger = setup_logger(cls.__name__)
        cls.source_url = get_source_uri()
        cls.requirements_file = get_requirements_uri()

    def setUp(self):
        super(TestAgentInstallerLocal, self).setUp()

        self.resource_base = tempfile.mkdtemp(
            prefix='file-server-resource-base')
        self.fs = FileServer(root_path=self.resource_base, ssl=False)
        self.fs.start()

        self.addCleanup(self.fs.stop)
        self.addCleanup(shutil.rmtree, self.resource_base)

    @only_os('posix')
    @only_ci
    def test_local_agent_from_package_posix(self):
        # Check that agent still works with a filepath longer than 128 bytes
        # (paths longer than 128 bytes break shebangs on linux.)
        agent_name = 'agent-' + ''.join(uuid.uuid4().hex for _ in range(4))
        self._test_local_agent_from_package(agent_name)

    @only_os('nt')
    @only_ci
    def test_local_agent_from_package_nt(self):
        agent_name = utils.internal.generate_agent_name()
        self._test_local_agent_from_package(agent_name)

    @patch('cloudify.workflows.local._validate_node')
    @patch('cloudify_agent.installer.operations.delete_agent_rabbitmq_user')
    @patch('cloudify.agent_utils.get_rest_client',
           return_value=MockRestclient())
    @get_tenant_mock()
    @patch('cloudify.utils.get_manager_name', return_value='cloudify')
    def _test_local_agent_from_package(self, agent_name, *_):

        agent_queue = '{0}-queue'.format(agent_name)

        blueprint_path = resources.get_resource(
            'blueprints/agent-from-package/local-agent-blueprint.yaml')
        self.logger.info('Initiating local env')

        inputs = {
            'resource_base': self.resource_base,
            'source_url': self.source_url,
            'requirements_file': self.requirements_file,
            'name': agent_name,
            'queue': agent_queue,
            'file_server_port': self.fs.port,
            'ssl_cert_path': self._rest_cert_path
        }
        managers = [
            ManagerItem({
                'networks': {
                    'default': '127.0.0.1'
                },
                'ca_cert_content': agent_ssl_cert.DUMMY_CERT,
                'hostname': 'cloudify'
            })
        ]

        with patch('cloudify.endpoint.LocalEndpoint.get_managers',
                   return_value=managers):
            env = local.init_env(name=self._testMethodName,
                                 blueprint_path=blueprint_path,
                                 inputs=inputs)

            env.execute('install', task_retries=0)
        agent_dict = self.get_agent_dict(env)
        agent_ssl_cert.verify_remote_cert(agent_dict['agent_dir'])

        env.execute('uninstall', task_retries=1)
        self.wait_for_daemon_dead(agent_queue)
示例#6
0
def mock_get_rest_client():
    with patch('cloudify.agent_utils.get_rest_client',
               return_value=MockRestclient()) as client:
        yield client