def test_serial_operation_install_workflow(self, _mock, list_fn):

        _ctx = MockCloudifyContext(node_id='test_node',
                                   deployment_id='test_deployment',
                                   index=7)

        # Check that we raise if the preceder is not 'started'.
        _ctx._context['workflow_id'] = 'install'
        current_ctx.set(_ctx)
        _mock.side_effect = lambda: rest_client_mock.MockRestclient()
        _caller = Mock()

        @serial_operation(workflows=['scale'])
        def run_op_neither(ctx=_ctx, **kwargs):
            _caller()

        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n),
                 index=n,
                 state='started' if n == 6 else 'uninitialized')
            for n in range(0, 6)
        ]
        run_op_neither()
        self.assertTrue(_caller.called)
        current_ctx.clear()
def test_policy_get_fail(monkeypatch):
    """test policy_get operation on non dcae.nodes.policy node"""
    tasks.PolicyHandler._url = tasks.PolicyHandler.DEFAULT_URL
    monkeypatch.setattr('requests.get', monkeyed_policy_handler_get)

    node_policy = MonkeyedNode('test_dcae_policy_node_id',
                               'test_dcae_policy_node_name',
                               tasks.DCAE_POLICY_TYPE,
                               {POLICY_ID: MONKEYED_POLICY_ID})

    node_ms = MonkeyedNode('test_ms_id', 'test_ms_name', "ms.nodes.type", None,
                           [{
                               TARGET_NODE_ID: node_policy.node_id,
                               TARGET_NODE_NAME: node_policy.node_name
                           }])

    try:
        current_ctx.set(node_ms.ctx)
        CtxLogger.log_ctx_info("ctx of node_ms not policy type")
        with pytest.raises(NonRecoverableError) as excinfo:
            tasks.policy_get()
        CtxLogger.log_ctx_info("node_ms not policy type boom: {0}".format(
            str(excinfo.value)))
        assert "unexpected node type " in str(excinfo.value)

    finally:
        MockCloudifyContextFull.clear()
        current_ctx.clear()
def test_policies_find_fail(monkeypatch):
    """test policy_get operation on non dcae.nodes.policies node"""
    tasks.PolicyHandler._url = tasks.PolicyHandler.DEFAULT_URL
    monkeypatch.setattr('requests.post', monkeyed_policy_handler_find)

    node_policies = MonkeyedNode(
        'test_dcae_policies_node_id', 'test_dcae_policies_node_name',
        tasks.DCAE_POLICIES_TYPE, {
            tasks.POLICY_FILTER: {
                POLICY_NAME:
                MONKEYED_POLICY_ID,
                tasks.CONFIG_ATTRIBUTES:
                json.dumps({CONFIG_NAME: "alex_config_name"})
            }
        })
    node_ms_multi = MonkeyedNode('test_ms_multi_id', 'test_ms_multi_name',
                                 "ms.nodes.type", None,
                                 [{
                                     TARGET_NODE_ID: node_policies.node_id,
                                     TARGET_NODE_NAME: node_policies.node_name
                                 }])

    try:
        current_ctx.set(node_ms_multi.ctx)
        CtxLogger.log_ctx_info("ctx of node_ms_multi not policy type")
        with pytest.raises(NonRecoverableError) as excinfo:
            tasks.policy_get()
        CtxLogger.log_ctx_info(
            "node_ms_multi not policy type boom: {0}".format(str(
                excinfo.value)))
        assert "unexpected node type " in str(excinfo.value)

    finally:
        MockCloudifyContextFull.clear()
        current_ctx.clear()
def ctx():
    ctxmock = ctx_mock()
    current_ctx.set(ctxmock)

    yield ctxmock

    current_ctx.clear()
def ctx():
    ctx = Mock()
    current_ctx.set(ctx)

    yield ctx

    current_ctx.clear()
def test_policy_get(monkeypatch):
    """test policy_get operation on dcae.nodes.policy node"""
    tasks.PolicyHandler._url = tasks.PolicyHandler.DEFAULT_URL
    monkeypatch.setattr('requests.get', monkeyed_policy_handler_get)

    node_policy = MonkeyedNode('test_dcae_policy_node_id',
                               'test_dcae_policy_node_name',
                               tasks.DCAE_POLICY_TYPE,
                               {POLICY_ID: MONKEYED_POLICY_ID})

    try:
        current_ctx.set(node_policy.ctx)
        CtxLogger.log_ctx_info("before policy_get")
        tasks.policy_get()
        CtxLogger.log_ctx_info("after policy_get")

        expected = {
            POLICY_BODY:
            MonkeyedPolicyBody.create_policy_body(MONKEYED_POLICY_ID)
        }
        result = node_policy.ctx.instance.runtime_properties
        node_policy.ctx.logger.info("expected runtime_properties: {0}".format(
            json.dumps(expected)))
        node_policy.ctx.logger.info("runtime_properties: {0}".format(
            json.dumps(result)))
        assert MonkeyedPolicyBody.is_the_same_dict(result, expected)
        assert MonkeyedPolicyBody.is_the_same_dict(expected, result)

    finally:
        MockCloudifyContextFull.clear()
        current_ctx.clear()
Exemple #7
0
def ctx():
    ctxmock = ctx_mock()
    current_ctx.set(ctxmock)

    yield ctxmock

    current_ctx.clear()
def ctx():
    ctx = Mock()
    current_ctx.set(ctx)

    yield ctx

    current_ctx.clear()
Exemple #9
0
    def test_serial_operation_install(self, list_fn):

        _ctx = MockCloudifyContext(node_id='test_node',
                                   deployment_id='test_deployment',
                                   index=7)

        # Check that we raise if the preceder is not 'started'.
        _ctx._context['workflow_id'] = 'install'
        current_ctx.set(_ctx)
        _caller = Mock()

        @serial_operation()
        def run_op_install_only(ctx=_ctx, **kwargs):
            _caller()

        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n), index=n, state='uninitialized')
            for n in range(0, 6)
        ]
        self.assertRaises(CloudifySerializationRetry, run_op_install_only)
        self.assertFalse(_caller.called)
        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n),
                 index=n,
                 state='started' if n < 7 else 'uninitialized')
            for n in range(3, 10) if n != 7
        ]
        run_op_install_only()
        self.assertTrue(_caller.called)
        current_ctx.clear()
Exemple #10
0
    def test_op_upgrade(self, mock_execute_command):
        # test operation upgrade
        """

        :upgrade operation test:
        """
        props = {
            'component_name': 'test_node',
            'namespace': 'onap',
            'tiller_port': '8888',
            'tiller_ip': '1.1.1.1',
            'tls_enable': 'false',
            'config_dir': '/tmp'
        }
        args = {'revision': '1', 'config': '', 'chart_repo': 'repo', 'chart_version': '2',
                     'config_set': 'config_set', 'config_json': '', 'config_url': '',
                     'config_format': 'format', 'repo_user': '', 'repo_user_passwd': ''}
        mock_ctx = MockCloudifyContext(node_id='test_node_id', node_name='test_node_name',
                                         properties=props)
        try:
            current_ctx.set(mock_ctx)
            with mock.patch('plugin.tasks.get_current_helm_value'):
                with mock.patch('plugin.tasks.get_helm_history'):
                    with mock.patch('plugin.tasks.gen_config_str'):
                        plugin.tasks.upgrade(**args)
        finally:
            current_ctx.clear()
def install_script(name, windows, user, manager_ip):
    ctx = MockCloudifyContext(
        node_id='node',
        properties={'agent_config': {
            'user': user,
            'windows': windows,
            'install_method': 'provided',
            'manager_ip': manager_ip,
            'name': name
        }})
    try:
        current_ctx.set(ctx)
        os.environ['MANAGER_FILE_SERVER_URL'] = 'http://{0}:53229'.format(
            manager_ip)
        init_script = script.init_script(cloudify_agent={})
    finally:
        os.environ.pop('MANAGER_FILE_SERVER_URL')
        current_ctx.clear()
    result = '\n'.join(init_script.split('\n')[:-1])
    if windows:
        return '{0}\n' \
               'DownloadAndExtractAgentPackage\n' \
               'ExportDaemonEnv\n' \
               'ConfigureAgent'.format(result)
    else:
        return '{0}\n' \
               'install_agent'.format(result)
Exemple #12
0
def test_generate():
  mock_ctx = MockCloudifyContext(node_id='test_node_id', node_name='test_node_name', properties={})
  try:
    current_ctx.set(mock_ctx)
    sshkeyshare.keyshare_plugin.generate()
    pub = ctx.instance.runtime_properties['public']
    pvt64 = ctx.instance.runtime_properties['base64private']
  finally:
    current_ctx.clear()
 def test_basic(self):
     self.assertRaises(RuntimeError, current_ctx.get_ctx)
     self.assertRaises(RuntimeError, lambda: ctx.node_id)
     value = MockCloudifyContext(node_id='1')
     current_ctx.set(value)
     self.assertEqual(value, current_ctx.get_ctx())
     self.assertEqual(value.node_id, ctx.node_id)
     current_ctx.clear()
     self.assertRaises(RuntimeError, current_ctx.get_ctx)
     self.assertRaises(RuntimeError, lambda: ctx.node_id)
Exemple #14
0
 def test_basic(self):
     self.assertRaises(RuntimeError, current_ctx.get_ctx)
     self.assertRaises(RuntimeError, lambda: ctx.instance.id)
     value = MockCloudifyContext(node_id='1')
     current_ctx.set(value)
     self.assertEqual(value, current_ctx.get_ctx())
     self.assertEqual(value.instance.id, ctx.instance.id)
     current_ctx.clear()
     self.assertRaises(RuntimeError, current_ctx.get_ctx)
     self.assertRaises(RuntimeError, lambda: ctx.instance.id)
def _install_script(name, windows, user, manager, attributes, tmpdir, logger,
                    tenant):
    # Download cert from manager in order to include its content
    # in the init_script.
    local_cert_path = str(tmpdir / 'cloudify_internal_cert.pem')
    logger.info('Downloading internal cert from manager: %s -> %s',
                attributes.LOCAL_REST_CERT_FILE,
                local_cert_path)
    with manager.ssh() as fabric:
        fabric.get(attributes.LOCAL_REST_CERT_FILE, local_cert_path)

    env_vars = {
        constants.REST_HOST_KEY: manager.private_ip_address,
        constants.REST_PORT_KEY: str(defaults.INTERNAL_REST_PORT),
        constants.BROKER_SSL_CERT_PATH: local_cert_path,
        constants.LOCAL_REST_CERT_FILE_KEY: local_cert_path,
        constants.MANAGER_FILE_SERVER_URL_KEY: (
            'https://{0}:{1}/resources'.format(manager.private_ip_address,
                                               defaults.INTERNAL_REST_PORT)
        ),
        constants.MANAGER_FILE_SERVER_ROOT_KEY: str(tmpdir),
        constants.MANAGER_NAME: (
            manager.client.manager.get_managers()[0].hostname
        ),
    }
    (tmpdir / 'cloudify_agent').mkdir()

    ctx = MockCloudifyContext(
        node_id='node',
        tenant={'name': tenant},
        rest_token=manager.client.tokens.get().value,
        managers=manager.client.manager.get_managers(),
        brokers=manager.client.manager.get_brokers(),
        properties={'agent_config': {
            'user': user,
            'windows': windows,
            'install_method': 'init_script',
            'name': name
        }})
    try:
        current_ctx.set(ctx)
        os.environ.update(env_vars)
        script_builder = script._get_script_builder()
        install_script = script_builder.install_script()
    finally:
        for var_name in list(env_vars):
            os.environ.pop(var_name, None)

        current_ctx.clear()

    # Replace the `main` call with an install call - as we only want to
    # install the agent, but not configure/start it
    install_method = 'InstallAgent' if windows else 'install_agent'
    install_script = '\n'.join(install_script.split('\n')[:-1])
    return '{0}\n{1}'.format(install_script, install_method)
Exemple #16
0
 def tearDown(self):
     current_ctx.clear()
     if self.local_env:
         try:
             self.local_env.execute(
                 'uninstall',
                 task_retries=50,
                 task_retry_interval=3,
             )
         except Exception as ex:
             print str(ex)
     super(SecurityTest, self).tearDown()
 def run(queue, value):
     try:
         self.assertRaises(RuntimeError, current_ctx.get_ctx)
         self.assertRaises(RuntimeError, lambda: ctx.node_id)
         current_ctx.set(value)
         self.assertEqual(value, current_ctx.get_ctx())
         self.assertEqual(value.node_id, ctx.node_id)
         current_ctx.clear()
         self.assertRaises(RuntimeError, current_ctx.get_ctx)
         self.assertRaises(RuntimeError, lambda: ctx.node_id)
     except Exception, e:
         queue.put(e)
def _install_script(name, windows, user, manager, attributes, tmpdir, logger,
                    tenant):
    # Download cert from manager in order to include its content
    # in the init_script.
    local_cert_path = str(tmpdir / 'cloudify_internal_cert.pem')
    logger.info('Downloading internal cert from manager: %s -> %s',
                attributes.LOCAL_REST_CERT_FILE,
                local_cert_path)
    with manager.ssh() as fabric:
        fabric.get(attributes.LOCAL_REST_CERT_FILE, local_cert_path)

    env_vars = {
        constants.REST_HOST_KEY: manager.private_ip_address,
        constants.REST_PORT_KEY: str(defaults.INTERNAL_REST_PORT),
        constants.BROKER_SSL_CERT_PATH: local_cert_path,
        constants.LOCAL_REST_CERT_FILE_KEY: local_cert_path,
        constants.MANAGER_FILE_SERVER_URL_KEY: (
            'https://{0}:{1}/resources'.format(manager.private_ip_address,
                                               defaults.INTERNAL_REST_PORT)
        ),
        constants.MANAGER_FILE_SERVER_ROOT_KEY: str(tmpdir),
    }
    (tmpdir / 'cloudify_agent').mkdir()

    ctx = MockCloudifyContext(
        node_id='node',
        tenant={'name': tenant},
        rest_token=manager.client.tokens.get().value,
        properties={'agent_config': {
            'user': user,
            'windows': windows,
            'install_method': 'init_script',
            'rest_host': manager.private_ip_address,
            'broker_ip': manager.private_ip_address,
            'name': name
        }})
    try:
        current_ctx.set(ctx)
        os.environ.update(env_vars)
        script_builder = script._get_script_builder()
        install_script = script_builder.install_script()
    finally:
        for var_name in list(env_vars):
            os.environ.pop(var_name, None)

        current_ctx.clear()

    # Replace the `main` call with an install call - as we only want to
    # install the agent, but not configure/start it
    install_method = 'InstallAgent' if windows else 'install_agent'
    install_script = '\n'.join(install_script.split('\n')[:-1])
    return '{0}\n{1}'.format(install_script, install_method)
Exemple #19
0
def test_rm_pgaas_cluster(monkeypatch):
    """
  test rm_pgaas_cluster()
  """
    try:
        set_mock_context('test_rm_pgaas_cluster', monkeypatch)
        pgaas.pgaas_plugin.rm_pgaas_cluster(args={})
    except Exception as e:
        print("Error: {0}".format(e))
        print("Stack: {0}".format(traceback.format_exc()))
        raise
    finally:
        current_ctx.clear()
Exemple #20
0
def test_delete_database(monkeypatch):
    """
  test delete_database()
  """
    try:
        set_mock_context('test_delete_database', monkeypatch)
        pgaas.pgaas_plugin.delete_database(args={})
    except Exception as e:
        print("Error: {0}".format(e))
        print("Stack: {0}".format(traceback.format_exc()))
        raise
    finally:
        current_ctx.clear()
Exemple #21
0
 def run(queue, value):
     try:
         self.assertRaises(RuntimeError, current_ctx.get_ctx)
         self.assertRaises(RuntimeError, lambda: ctx.instance.id)
         current_ctx.set(value)
         self.assertEqual(value, current_ctx.get_ctx())
         self.assertEqual(value.instance.id, ctx.instance.id)
         current_ctx.clear()
         self.assertRaises(RuntimeError, current_ctx.get_ctx)
         self.assertRaises(RuntimeError, lambda: ctx.instance.id)
     except Exception as e:
         queue.put(e)
     else:
         queue.put('ok')
 def run(queue, value):
     try:
         self.assertRaises(RuntimeError, current_ctx.get_ctx)
         self.assertRaises(RuntimeError, lambda: ctx.instance.id)
         current_ctx.set(value)
         self.assertEqual(value, current_ctx.get_ctx())
         self.assertEqual(value.instance.id, ctx.instance.id)
         current_ctx.clear()
         self.assertRaises(RuntimeError, current_ctx.get_ctx)
         self.assertRaises(RuntimeError, lambda: ctx.instance.id)
     except Exception as e:
         queue.put(e)
     else:
         queue.put("ok")
Exemple #23
0
 def newfcn(monkeypatch):
   monkeypatch.setattr(requests, 'delete', _delete)
   monkeypatch.setattr(requests, 'get', _get)
   monkeypatch.setattr(requests, 'post', _post)
   monkeypatch.setattr(requests, 'put', _put)
   properties = { 'fqdn': fqdn, 'openstack': os }
   if ttl is not None:
     properties['ttl'] = ttl
   mock_ctx = MockCloudifyContext(node_id='test_node_id', node_name='test_node_name', properties=properties)
   try:
     current_ctx.set(mock_ctx)
     fcn()
   finally:
     current_ctx.clear()
    def test_create_path(self, mock_put, mock_get):

        # Path
        properties = {}
        props = {
            NAME: 'path_name',
            EP1_NODE_ID: 'openflow:101',
            EP2_NODE_ID: 'openflow:303',
            PROVIDER: 'sr',
            RULES: [],
            CONTRAINTS: {
                WAYPOINT: []
            }
        }
        properties.update(props)

        props = {
            CONTRAINTS: {
                WAYPOINT: ['openflow:102', 'openflow:103']
            },
        }
        properties.update(props)

        ctx = setup_ctx('path_name', properties, PATH_TYPE)
        ctx.instance.runtime_properties['node_type'] = PATH_TYPE

        try:
            ctx.logger.debug("properties: {}".format(properties))
            # controller returns a 201 response when creating a path
            put_resp = MockRequestsResponse()
            mock_put.status_code = 200
            mock_put.return_value = put_resp

            get_resp = MockRequestsResponse()
            get_resp.status_code = 200
            get_resp.content = json.dumps({'path': [{'name': 'path_name'}]})
            mock_get.side_effect = [get_resp]

            path.create(ctx)
            # check mock calls
            self.assertEquals(len(mock_put.mock_calls), 1)
            self.assertEquals(len(mock_get.mock_calls), 1)
            ctx.logger.info("request_calls {}".format(len(
                mock_put.mock_calls)))

            pass

        finally:
            current_ctx.clear()
Exemple #25
0
def set_mock_context(agent_ssl_cert, tmp_path, **override_properties):
    node_properties = {
        'agent_config': {
            'user': getpass.getuser(),
            'install_method': 'init_script',
            'rest_host': '127.0.0.1',
            'windows': os.name == 'nt',
            'basedir': str(tmp_path),
        }
    }
    node_properties['agent_config'].update(**override_properties)
    current_ctx.set(
        mock_context(agent_ssl_cert, node_id='d', properties=node_properties))
    yield
    current_ctx.clear()
    def test_create_eline_vlan(self, mock_put, mock_get):

        # Eline
        properties = {}
        props = {
            NAME: 'eline_name',
            PATH_NAME: 'path_name',
            ENDPOINT1: {
                'network_type': 'vlan',
                'switch_id': 'openflow:101',
                'switch_port': '3',
                'segmentation_id': 100
            },
            ENDPOINT2: {
                'network_type': 'vlan',
                'switch_id': 'openflow:303',
                'switch_port': '3',
                'segmentation_id': 100  # range: 0 - 4094
            },
            PROTOCOL_TYPE: ''
        }
        properties.update(props)

        ctx = setup_ctx('eline_name', properties, ELINE_TYPE)

        try:
            ctx.logger.debug("properties: {}".format(properties))
            put_resp = MockRequestsResponse()
            mock_put.status_code = 200
            mock_put.return_value = put_resp

            get_resp = MockRequestsResponse()
            get_resp.status_code = 200
            get_resp.content = json.dumps({'eline': [{'name': 'eline_name'}]})
            mock_get.side_effect = [get_resp]

            eline.create(ctx)

            self.assertEquals(len(mock_put.mock_calls), 1)
            self.assertEquals(len(mock_get.mock_calls), 1)

            ctx.logger.info(
                "request_calls {}".format(len(mock_put.mock_calls)))

            pass

        finally:
            current_ctx.clear()
Exemple #27
0
def test_bad_add_database(monkeypatch):
    """
  test bad_add_database()
  """
    try:
        set_mock_context('test_add_database',
                         monkeypatch,
                         writerfqdn="bad.bar.example.com")
        with pytest.raises(NonRecoverableError):
            pgaas.pgaas_plugin.create_database(args={})
    except Exception as e:
        print("Error: {0}".format(e))
        print("Stack: {0}".format(traceback.format_exc()))
        raise
    finally:
        current_ctx.clear()
Exemple #28
0
def test_discovery_kv(monkeypatch):
    """test finding policy-handler in consul"""
    monkeypatch.setattr('requests.get', monkeyed_discovery_get_kv)

    node_policy = MonkeyedNode('test_dcae_policy_node_id',
                               'test_dcae_policy_node_name',
                               tasks.DCAE_POLICY_TYPE,
                               {POLICY_ID: MONKEYED_POLICY_ID})
    try:
        current_ctx.set(node_policy.ctx)
        tasks.PolicyHandler._lazy_init()
        assert POLICY_HANDLER_FROM_KV == tasks.PolicyHandler._url

    finally:
        tasks.PolicyHandler._url = None
        MockCloudifyContextFull.clear()
        current_ctx.clear()
Exemple #29
0
def test_update_database(monkeypatch):
    """
  test update_database()
  """
    try:
        ########################################################
        # Subtle test implications regarding: update_database  #
        # ---------------------------------------------------  #
        # 1)  update_database is a workflow and the context    #
        #     passed to it has 'nodes' attribute which is not  #
        #     not included in MockCloudifyContext              #
        # 2)  the 'nodes' attribute is a list of contexts so   #
        #     we will have to create a sub-context             #
        # 3)  update_database will iterate through each of the #
        #     nodes contexts looking for the correct one       #
        # 4)  To identify the correct sub-context it will first#
        #     check each sub-context for the existence of      #
        #     properties attribute                             #
        # 5)  ****Mock_context internally saves properties as  #
        #     variable _properties and 'properties' is defined #
        #     as @property...thus it is not recognized as an   #
        #     attribute...this will cause update_database to   #
        #     fail so we need to explicitly create properties  #
        #     properties attribute in the subcontext           #
        ########################################################

        ####################
        # Main context     #
        ####################
        myctx = set_mock_context('test_update_database', monkeypatch)
        ###########################################################
        # Create subcontext and assign it to attribute properties #
        # in main context                                         #
        ###########################################################
        mynode = set_mock_context('test_update_database_node', monkeypatch)
        # pylint: disable=protected-access
        mynode.properties = mynode._properties
        myctx.nodes = [mynode]
        pgaas.pgaas_plugin.update_database(refctx=myctx)
    except Exception as e:
        print("Error: {0}".format(e))
        print("Stack: {0}".format(traceback.format_exc()))
        raise
    finally:
        current_ctx.clear()
def install_script(name, windows, user, manager_host):

    env_vars = {}
    env_vars[constants.MANAGER_FILE_SERVER_URL_KEY] = '{0}://{1}:{2}'.format(
        defaults.FILE_SERVER_PROTOCOL, manager_host, defaults.FILE_SERVER_PORT)
    env_vars[constants.FILE_SERVER_PORT_KEY] = str(defaults.FILE_SERVER_PORT)
    env_vars[constants.FILE_SERVER_PROTOCOL_KEY] = \
        defaults.FILE_SERVER_PROTOCOL
    env_vars[constants.REST_PORT_KEY] = str(defaults.REST_PORT)
    env_vars[constants.REST_PROTOCOL_KEY] = defaults.REST_PROTOCOL
    env_vars[constants.SECURITY_ENABLED_KEY] = str(defaults.SECURITY_ENABLED)
    env_vars[constants.VERIFY_REST_CERTIFICATE_KEY] = \
        str(defaults.VERIFY_REST_CERTIFICATE)
    env_vars[constants.REST_CERT_CONTENT_KEY] = ''

    ctx = MockCloudifyContext(
        node_id='node',
        properties={'agent_config': {
            'user': user,
            'windows': windows,
            'install_method': 'provided',
            'rest_host': manager_host,
            'file_server_host': manager_host,
            'broker_ip': manager_host,
            'name': name
        }})
    try:
        current_ctx.set(ctx)
        os.environ.update(env_vars)

        init_script = script.init_script(cloudify_agent={})
    finally:
        for var_name in env_vars.iterkeys():
            os.environ.pop(var_name)

        current_ctx.clear()
    result = '\n'.join(init_script.split('\n')[:-1])
    if windows:
        return '{0}\n' \
               'DownloadAndExtractAgentPackage\n' \
               'ExportDaemonEnv\n' \
               'ConfigureAgent'.format(result)
    else:
        return '{0}\n' \
               'install_agent'.format(result)
 def wrapper(*args, **kwargs):
     ctx = _find_context_arg(args, kwargs, _is_cloudify_context)
     if ctx is None:
         ctx = {}
     if not _is_cloudify_context(ctx):
         ctx = CloudifyContext(ctx)
         kwargs['ctx'] = ctx
     try:
         current_ctx.set(ctx, kwargs)
         result = func(*args, **kwargs)
     except BaseException:
         ctx.logger.error(
             'Exception raised on operation [%s] invocation',
             ctx.task_name, exc_info=True)
         raise
     finally:
         current_ctx.clear()
         ctx.update()
     return result
    def test_serial_operation_uninstall_wait_3(self, _mock, list_fn):

        _ctx = MockCloudifyContext(node_id='test_node',
                                   deployment_id='test_deployment',
                                   index=7)

        # Check that we raise if the preceder is not 'started'.
        _ctx._context['workflow_id'] = 'uninstall'
        current_ctx.set(_ctx)
        _mock.side_effect = lambda: rest_client_mock.MockRestclient()
        _caller = Mock()

        @serial_operation(threshold=3, workflows=['uninstall'])
        def run_op_wait_for_3(ctx=_ctx, **kwargs):
            _caller()

        finished3 = []
        for n in range(1, 8):
            if n == 7:
                continue
            elif n in [1, 2, 3]:
                state = 'started'
            else:
                state = 'uninitialized'
            finished3.append(
                Mock(id='test_node{x}'.format(x=n), index=n, state=state))
        list_fn.return_value = finished3
        self.assertRaises(CloudifySerializationRetry, run_op_wait_for_3)
        self.assertFalse(_caller.called)

        # Check if we cross the serialization_type threshold,
        # then we do not trigger the retry.
        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n),
                 index=n,
                 state='started' if n < 7 else 'uninitialized')
            for n in range(3, 10) if n != 7
        ]
        run_op_wait_for_3()
        self.assertTrue(_caller.called)
        current_ctx.clear()
Exemple #33
0
def test_discovery(monkeypatch):
    """test finding policy-handler in consul"""
    monkeypatch.setattr('requests.get', monkeyed_discovery_get)

    node_policy = MonkeyedNode('test_dcae_policy_node_id',
                               'test_dcae_policy_node_name',
                               tasks.DCAE_POLICY_TYPE,
                               {POLICY_ID: MONKEYED_POLICY_ID})

    try:
        current_ctx.set(node_policy.ctx)
        expected = "http://monkey-policy-handler-address:9999"
        CtxLogger.log_ctx_info("before PolicyHandler._lazy_init")
        tasks.PolicyHandler._lazy_init()
        CtxLogger.log_ctx_info("after PolicyHandler._lazy_init")
        assert expected == tasks.PolicyHandler._url

    finally:
        tasks.PolicyHandler._url = None
        MockCloudifyContextFull.clear()
        current_ctx.clear()
Exemple #34
0
def test_discovery_failure(monkeypatch):
    """test finding policy-handler in consul"""
    monkeypatch.setattr('requests.get', monkeyed_discovery_get_failure)

    node_policy = MonkeyedNode('test_dcae_policy_node_id',
                               'test_dcae_policy_node_name',
                               tasks.DCAE_POLICY_TYPE,
                               {POLICY_ID: MONKEYED_POLICY_ID})
    try:
        current_ctx.set(node_policy.ctx)
        with pytest.raises(NonRecoverableError) as excinfo:
            tasks.PolicyHandler._lazy_init()

        CtxLogger.log_ctx_info("test_discovery_failure: {0}".format(
            str(excinfo.value)))
        assert str(excinfo.value).startswith("ConnectionError")

    finally:
        tasks.PolicyHandler._url = None
        MockCloudifyContextFull.clear()
        current_ctx.clear()
def test_policies_find(monkeypatch):
    """test policy_get operation on dcae.nodes.policies node"""
    tasks.PolicyHandler._url = tasks.PolicyHandler.DEFAULT_URL
    monkeypatch.setattr('requests.post', monkeyed_policy_handler_find)

    node_policies = MonkeyedNode(
        'test_dcae_policies_node_id', 'test_dcae_policies_node_name',
        tasks.DCAE_POLICIES_TYPE, {
            tasks.POLICY_FILTER: {
                POLICY_NAME:
                MONKEYED_POLICY_ID,
                tasks.CONFIG_ATTRIBUTES:
                json.dumps({CONFIG_NAME: "alex_config_name"})
            }
        })

    try:
        current_ctx.set(node_policies.ctx)
        CtxLogger.log_ctx_info("before policy_get")
        tasks.policy_get()
        CtxLogger.log_ctx_info("after policy_get")

        expected = {
            tasks.POLICIES_FILTERED: {
                MONKEYED_POLICY_ID:
                MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID)
            }
        }

        result = node_policies.ctx.instance.runtime_properties
        node_policies.ctx.logger.info(
            "expected runtime_properties: {0}".format(json.dumps(expected)))
        node_policies.ctx.logger.info("runtime_properties: {0}".format(
            json.dumps(result)))
        assert MonkeyedPolicyBody.is_the_same_dict(result, expected)
        assert MonkeyedPolicyBody.is_the_same_dict(expected, result)

    finally:
        MockCloudifyContextFull.clear()
        current_ctx.clear()
Exemple #36
0
    def test_op_rollback(self, mock_execute_command):
        # test operation rollback
        """

        :rollback operation test:
        """
        props = {
            'component_name': 'test_node',
            'namespace': 'onap',
            'tiller_port': '8888',
            'tiller_ip': '1.1.1.1',
            'tls_enable': 'false'
        }
        args = {'revision': '1'}
        mock_ctx = MockCloudifyContext(node_id='test_node_id', node_name='test_node_name',
                                         properties=props)
        try:
            current_ctx.set(mock_ctx)
            with mock.patch('plugin.tasks.get_current_helm_value'):
                with mock.patch('plugin.tasks.get_helm_history'):
                    plugin.tasks.rollback(**args)
        finally:
            current_ctx.clear()
    def test_delete_eline(self, mock_delete):  # mock_get,
        properties = {}
        props = {
            NAME: 'eline_name',
            PATH_NAME: 'path_name',
        }
        properties.update(props)
        ctx = setup_ctx('eline_name', properties, ELINE_TYPE)
        try:
            # controller returns a 201 response when deleting an eline
            put_resp = MockRequestsResponse()
            mock_delete.status_code = 201
            mock_delete.return_value = put_resp

            eline.delete(ctx)
            self.assertEquals(len(mock_delete.mock_calls), 1)

            ctx.logger.info(
                "request_calls {}".format(len(mock_delete.mock_calls)))
            pass

        finally:
            current_ctx.clear()
    def test_serial_operation_uninstall(self, _mock, list_fn):

        _ctx = MockCloudifyContext(node_id='test_node',
                                   deployment_id='test_deployment',
                                   index=7)

        # Check that we raise if the preceder is not 'started'.
        _ctx._context['workflow_id'] = 'uninstall'
        current_ctx.set(_ctx)
        _mock.side_effect = lambda: rest_client_mock.MockRestclient()
        _caller = Mock()

        @serial_operation(workflows=['uninstall'])
        def run_op_uninstall_only(ctx=_ctx, **kwargs):
            _caller()

        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n), index=n, state='uninitialized')
            for n in range(1, 8) if n != 7
        ]
        self.assertRaises(CloudifySerializationRetry, run_op_uninstall_only)
        self.assertFalse(_caller.called)
        current_ctx.clear()
    def setUp(self):
        super(BaseInitScriptTest, self).setUp()
        ctx = MockCloudifyContext(
            node_id='d',
            properties={'agent_config': {
                'user': self.username,
                'install_method': 'init_script',
                'rest_host': 'localhost',
                'windows': self.windows,
                'basedir': self.temp_folder
            }})
        current_ctx.set(ctx)

        self.addCleanup(lambda: current_ctx.clear())
        self.input_cloudify_agent = {'broker_ip': 'localhost'}
Exemple #40
0
    def test_serial_operation_install_wait_for_1(self, list_fn):

        _ctx = MockCloudifyContext(node_id='test_node',
                                   deployment_id='test_deployment',
                                   index=7)

        # Check that we raise if the preceder is not 'started'.
        _ctx._context['workflow_id'] = 'install'
        current_ctx.set(_ctx)
        _caller = Mock()

        @serial_operation(threshold=1)
        def run_op_wait_for_1(ctx=_ctx, **kwargs):
            _caller()

        list_fn.return_value = [
            Mock(id='test_node{x}'.format(x=n),
                 index=n,
                 state='started' if n == 6 else 'uninitialized')
            for n in range(0, 6)
        ]
        run_op_wait_for_1()
        self.assertTrue(_caller.called)
        current_ctx.clear()
 def setUp(self):
     super(BaseInitScriptTest, self).setUp()
     ctx = MockCloudifyContext(
         node_id='d',
         properties={'agent_config': {
             'user': self.username,
             'install_method': 'init_script',
             'manager_ip': 'localhost',
             'windows': self.windows,
             'basedir': self.temp_folder
         }})
     current_ctx.set(ctx)
     self.addCleanup(lambda: current_ctx.clear())
     os.environ['MANAGER_FILE_SERVER_URL'] = ''
     self.addCleanup(lambda: os.environ.pop('MANAGER_FILE_SERVER_URL',
                                            None))
     self.input_cloudify_agent = {}
 def tearDown(self):
     current_ctx.clear()
     super(VsphereControllerTest, self).tearDown()
        def wrapper(*args, **kwargs):
            ctx = _find_context_arg(args, kwargs, _is_cloudify_context)
            if ctx is None:
                ctx = {}
            if not _is_cloudify_context(ctx):
                ctx = context.CloudifyContext(ctx)
                # remove __cloudify_context
                raw_context = kwargs.pop(CLOUDIFY_CONTEXT_PROPERTY_KEY, {})
                if ctx.task_target is None:
                    # task is local (not through celery) so we need to
                    # clone kwarg
                    kwargs = copy.deepcopy(kwargs)
                if raw_context.get('has_intrinsic_functions') is True:
                    kwargs = ctx._endpoint.evaluate_functions(payload=kwargs)
                kwargs['ctx'] = ctx
            try:
                current_ctx.set(ctx, kwargs)
                result = func(*args, **kwargs)
            except BaseException as e:
                ctx.logger.error(
                    'Exception raised on operation [%s] invocation',
                    ctx.task_name, exc_info=True)

                if ctx.task_target is None:
                    # local task execution
                    # no serialization issues
                    raise

                # extract exception details
                # type, value, traceback
                tpe, value, tb = sys.exc_info()

                # we re-create the exception here
                # since it will be sent
                # over the wire. And the original exception
                # may cause de-serialization issues
                # on the other side.

                # preserve original type in the message
                message = '{0}: {1}'.format(tpe.__name__, str(e))

                # if the exception type is directly one of our exception
                # than there is no need for conversion and we can just
                # raise the original exception
                if type(e) in [exceptions.OperationRetry,
                               exceptions.RecoverableError,
                               exceptions.NonRecoverableError,
                               exceptions.HttpException]:
                    raise

                # if the exception inherits from our base exceptions, there
                # still might be a de-serialization problem caused by one of
                # the types in the inheritance tree.
                if isinstance(e, exceptions.NonRecoverableError):
                    value = exceptions.NonRecoverableError(message)
                elif isinstance(e, exceptions.OperationRetry):
                    value = exceptions.OperationRetry(message, e.retry_after)
                elif isinstance(e, exceptions.RecoverableError):
                    value = exceptions.RecoverableError(message, e.retry_after)
                else:
                    # convert pure user exceptions
                    # to a RecoverableError
                    value = exceptions.RecoverableError(message)

                raise type(value), value, tb

            finally:
                current_ctx.clear()
                if ctx.type == context.NODE_INSTANCE:
                    ctx.instance.update()
                elif ctx.type == context.RELATIONSHIP_INSTANCE:
                    ctx.source.instance.update()
                    ctx.target.instance.update()
            if ctx.operation._operation_retry:
                raise ctx.operation._operation_retry
            return result
 def tearDown(self):
     current_ctx.clear()
 def tearDown(self):
     current_ctx.clear()
     super(BackupServerTest, self).tearDown()
 def cleanup(self):
     tasks.eval_script = self.original_eval_script
     tasks.execute = self.original_execute
     os.chmod = self.original_os_chmod
     current_ctx.clear()
 def tearDown(self):
     current_ctx.clear()
     if os.path.exists(self.keyfile):
         os.unlink(self.keyfile)
 def tearDown(self):
     current_ctx.clear()
     super(ComponentTestBase, self).tearDown()