Beispiel #1
0
    def test_run_with_calls(self):
        """check connect/call rpc/close connection calls sequence"""
        fake_ctx, node, instance = self._get_mock_context_for_run()

        hello_message = (
            """<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<rfc6020""" +
            """:hello xmlns:a="b" xmlns:d="c" xmlns:rfc6020="urn:ie""" +
            """tf:params:xml:ns:netconf:base:1.0">\n  <rfc6020:capa""" +
            """bilities>\n    <rfc6020:capability>urn:ietf:params:n""" +
            """etconf:base:1.0</rfc6020:capability>\n    <rfc6020:c""" +
            """apability>urn:ietf:params:netconf:base:1.1</rfc6020:""" +
            """capability>\n  </rfc6020:capabilities>\n</rfc6020:he""" +
            """llo>\n""")

        # no calls
        current_ctx.set(fake_ctx)
        rpc.run(ctx=fake_ctx)

        # with empty list of calls
        rpc.run(ctx=fake_ctx, calls=[])

        # with list of calls, but without auth
        with self.assertRaises(cfy_exc.NonRecoverableError):
            rpc.run(ctx=fake_ctx, calls=[{'action': 'get'}])

        node.properties = {
            'netconf_auth': {
                "user": "******",
                "password": "******",
                "ip": "super_secret",
                # save logs
                "store_logs": True
            },
            'metadata': {
                'xmlns': {
                    'd': 'c'
                }
            },
            "base_xmlns": {
                "a": "b"
            }
        }

        # check usage of ip list
        nc_conn = mock.Mock()
        nc_conn.connect = mock.MagicMock(
            side_effect=cfy_exc.NonRecoverableError("Check Exception"))

        with mock.patch(
                'cloudify_terminal_sdk.netconf_connection.NetConfConnection',
                mock.MagicMock(return_value=nc_conn)):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                # we have empty action
                rpc.run(ctx=fake_ctx, calls=[{'unknow': 'get'}])
            nc_conn.connect.assert_called_with('super_secret', 'me',
                                               hello_message, 'secret', None,
                                               830)
            fake_ctx.get_resource.assert_not_called()

        # connect without exception
        nc_conn = self._get_fake_nc_connect()

        with mock.patch(
                'cloudify_terminal_sdk.netconf_connection.NetConfConnection',
                mock.MagicMock(return_value=nc_conn)):
            # we have empty action
            rpc.run(ctx=fake_ctx, calls=[{'unknow': 'get'}])
            nc_conn.connect.assert_called_with('super_secret', 'me',
                                               hello_message, 'secret', None,
                                               830)
            fake_ctx.get_resource.assert_not_called()

            # have some payload
            rpc.run(ctx=fake_ctx,
                    calls=[{
                        'action': 'run_something',
                        'payload': {
                            "a": "b"
                        }
                    }])

            # have lock/unlock operations
            rpc.run(ctx=fake_ctx,
                    calls=[{
                        'action': 'run_something',
                        'payload': {
                            "a": "b"
                        }
                    }],
                    lock=["rfc6020@candidate"])

            # have some copy operations
            rpc.run(ctx=fake_ctx,
                    calls=[{
                        'action': 'run_something',
                        'payload': {
                            "a": "b"
                        }
                    }],
                    back_database="a",
                    front_database="b")

            # check save to runtime properties
            self.assertEqual(instance.runtime_properties, {})
            rpc.run(ctx=fake_ctx,
                    calls=[{
                        'action': 'run_something',
                        'payload': {
                            "c": "d"
                        },
                        'save_to': 'd'
                    }])

            # looks as we save something
            self.assertTrue("d" in instance.runtime_properties)
            self.assertTrue("d_ns" in instance.runtime_properties)
            self.assertTrue("rfc6020@ok" in instance.runtime_properties["d"])

            self.assertEqual(nc_conn.current_level,
                             netconf_connection.NETCONF_1_0_CAPABILITY)

        # version 1.1 and ip from cloudify.relationships.contained_in
        nc_conn.connect = mock.MagicMock(
            return_value=self.CORRECT_HELLO_1_1_REPLY)
        # drop ip from auth dict, lets use 'container' ip
        node.properties['netconf_auth']["ip"] = None
        instance.host_ip = "ip_from_runtime"
        with mock.patch(
                'cloudify_terminal_sdk.netconf_connection.NetConfConnection',
                mock.MagicMock(return_value=nc_conn)):
            rpc.run(ctx=fake_ctx, calls=[{
                'action': 'run_something',
            }])

            # we use correct ip from instance runtime properties
            nc_conn.connect.assert_called_with('ip_from_runtime', 'me',
                                               hello_message, 'secret', None,
                                               830)
            self.assertEqual(nc_conn.current_level,
                             netconf_connection.NETCONF_1_1_CAPABILITY)

        # we have failed operations
        nc_conn = self._get_fake_nc_connect()
        nc_conn.send = mock.Mock(side_effect=[
            # copy-config
            self.CORRECT_REPLY,
            # failed operation
            self.INCORRECT_REPLY,
            # failed reset config
            self.INCORRECT_REPLY
        ])

        with mock.patch(
                'cloudify_terminal_sdk.netconf_connection.NetConfConnection',
                mock.MagicMock(return_value=nc_conn)):
            with self.assertRaises(cfy_exc.RecoverableError):
                # we have some failed operation
                rpc.run(ctx=fake_ctx,
                        calls=[{
                            'action': 'run_something',
                            'payload': {
                                "a": "b"
                            }
                        }],
                        back_database="a",
                        front_database="b")

        # failed operation, but reset successed
        nc_conn.send = mock.Mock(side_effect=[
            # copy-config
            self.CORRECT_REPLY,
            # failed operation
            self.INCORRECT_REPLY,
            # failed reset config
            self.CORRECT_REPLY
        ])
        with mock.patch(
                'cloudify_terminal_sdk.netconf_connection.NetConfConnection',
                mock.MagicMock(return_value=nc_conn)):
            with self.assertRaises(cfy_exc.RecoverableError):
                # we have some failed operation
                rpc.run(ctx=fake_ctx,
                        calls=[{
                            'action': 'run_something',
                            'payload': {
                                "a": "b"
                            }
                        }],
                        back_database="a",
                        front_database="b")
    def test_run(self):
        """check connect/call rpc/close connection sequence"""
        fake_ctx = cfy_mocks.MockCloudifyContext()
        instance = mock.Mock()
        instance.runtime_properties = {}
        fake_ctx._instance = instance
        node = mock.Mock()
        fake_ctx._node = node
        node.properties = {}
        node.runtime_properties = {}

        # no calls
        rpc.run(ctx=fake_ctx)

        # with empty list of calls
        rpc.run(ctx=fake_ctx, calls=[])

        # with list of calls, but without auth
        with self.assertRaises(cfy_exc.NonRecoverableError):
            rpc.run(ctx=fake_ctx, calls=[{'action': 'get'}])

        # netconf connection mock
        nc_conn = mock.Mock()
        nc_conn.connect = mock.MagicMock(
            return_value=self.CORRECT_HELLO_REPLY
        )
        nc_conn.send = mock.MagicMock(
            return_value=self.CORRECT_REPLY
        )
        nc_conn.close = mock.MagicMock(
            return_value=self.CORRECT_CLOSE_REPLY
        )

        nc_conn.current_level = netconf_connection.NETCONF_1_0_CAPABILITY

        node.properties = {
            'netconf_auth': {
                "user": "******",
                "password": "******",
                "ip": "super_secret"
            },
            'metadata': {
                'xmlns': {
                    'd': 'c'
                }
            },
            "base_xmlns": {
                "a": "b"
            }
        }
        with mock.patch(
            'cloudify_netconf.netconf_connection.connection',
            mock.MagicMock(return_value=nc_conn)
        ):
            # we have empty action
            rpc.run(ctx=fake_ctx, calls=[{'unknow': 'get'}])

            # have some payload
            rpc.run(ctx=fake_ctx, calls=[{
                'action': 'run_something',
                'payload': {
                    "a": "b"
                }
            }])

            # have lock/unlock operations
            rpc.run(ctx=fake_ctx, calls=[{
                'action': 'run_something',
                'payload': {
                    "a": "b"
                }
            }], lock=["rfc6020@candidate"])

            # have some copy operations
            rpc.run(ctx=fake_ctx, calls=[{
                'action': 'run_something',
                'payload': {
                    "a": "b"
                }
            }], back_database="a", front_database="b")

            # check save to runtime properties
            self.assertEqual(instance.runtime_properties, {})
            rpc.run(ctx=fake_ctx, calls=[{
                'action': 'run_something',
                'payload': {
                    "c": "d"
                },
                'save_to': 'd'
            }])

            # looks as we save something
            self.assertTrue("d" in instance.runtime_properties)
            self.assertTrue("d_ns" in instance.runtime_properties)
            self.assertTrue(
                "rfc6020@ok" in instance.runtime_properties["d"]
            )

            # validation without relaxng
            rpc.run(ctx=fake_ctx, calls=[{
                'action': 'run_something',
                'payload': {
                    "c": "d"
                },
                'save_to': 'd',
                'validation': {
                    'xpath': 'somepath'
                }
            }])

            self.assertEqual(
                nc_conn.current_level,
                netconf_connection.NETCONF_1_0_CAPABILITY
            )

        # version 1.1
        nc_conn.connect = mock.MagicMock(
            return_value=self.CORRECT_HELLO_1_1_REPLY
        )
        with mock.patch(
            'cloudify_netconf.netconf_connection.connection',
            mock.MagicMock(return_value=nc_conn)
        ):
            rpc.run(ctx=fake_ctx, calls=[{
                'action': 'run_something',
            }])

            self.assertEqual(
                nc_conn.current_level,
                netconf_connection.NETCONF_1_1_CAPABILITY
            )
Beispiel #3
0
    def test_run_with_template(self):
        """check connect/call rpc/close connection calls sequence"""
        fake_ctx, node, instance = self._get_mock_context_for_run()

        hello_message = (
            """<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<rfc6020""" +
            """:hello xmlns:a="b" xmlns:d="c" xmlns:rfc6020="urn:ie""" +
            """tf:params:xml:ns:netconf:base:1.0">\n  <rfc6020:capa""" +
            """bilities>\n    <rfc6020:capability>urn:ietf:params:n""" +
            """etconf:base:1.0</rfc6020:capability>\n    <rfc6020:c""" +
            """apability>urn:ietf:params:netconf:base:1.1</rfc6020:""" +
            """capability>\n  </rfc6020:capabilities>\n</rfc6020:he""" +
            """llo>\n""")

        # no calls
        current_ctx.set(fake_ctx)

        # with empty list of calls
        rpc.run(ctx=fake_ctx, template=None)
        rpc.run(ctx=fake_ctx, template="")

        # with list of calls, but without auth
        with self.assertRaises(cfy_exc.NonRecoverableError):
            rpc.run(ctx=fake_ctx, template="template.xml")

        nc_conn = self._get_fake_nc_connect()

        node.properties = {
            'netconf_auth': {
                "user": "******",
                "password": "******",
                "ip": "super_secret"
            },
            'metadata': {
                'xmlns': {
                    'd': 'c'
                }
            },
            "base_xmlns": {
                "a": "b"
            }
        }
        with mock.patch(
                'cloudify_terminal_sdk.netconf_connection.NetConfConnection',
                mock.MagicMock(return_value=nc_conn)):
            # we have empty action
            rpc.run(ctx=fake_ctx, template="template.xml")
            nc_conn.connect.assert_called_with('super_secret', 'me',
                                               hello_message, 'secret', None,
                                               830)

            fake_ctx.get_resource.assert_called_with("template.xml")

            # have some params
            fake_ctx.get_resource = mock.MagicMock(return_value="{{ a }}")

            # empty params
            rpc.run(ctx=fake_ctx, template="template.xml", params={})
            rpc.run(ctx=fake_ctx, templates=["template.xml"], params={})

            # real params
            rpc.run(ctx=fake_ctx, template="template.xml", params={'a': 'b'})
            rpc.run(ctx=fake_ctx,
                    templates=["template.xml"],
                    params={'a': 'b'})

            # template with empty commands, must be skiped
            fake_ctx.get_resource = mock.MagicMock(
                return_value="]]>]]>\n]]>]]>")
            rpc.run(ctx=fake_ctx, template="template.xml", params={'a': 'b'})
            rpc.run(ctx=fake_ctx,
                    templates=["template.xml"],
                    params={'a': 'b'})

            template_mock = io.StringIO(u"{{ a }}")
            with mock.patch('cloudify_netconf.xml_rpc.open',
                            return_value=template_mock,
                            create=True) as mocked_open:

                rpc.run(ctx=fake_ctx,
                        templates=["file:///template.xml"],
                        params={'a': 'b'})

                mocked_open.assert_called_once_with("/template.xml")
                nc_conn.send.assert_called_with('b')

            template_mock = mock.Mock()
            template_mock.content = 'b'
            with mock.patch('requests.get',
                            return_value=template_mock,
                            create=True) as mocked_open:

                rpc.run(ctx=fake_ctx,
                        templates=["http://cloudify.co/template.xml"],
                        params={'a': 'b'})

                mocked_open.assert_called_once_with(
                    "http://cloudify.co/template.xml")
                nc_conn.send.assert_called_with('b')
    def test_run(self):
        """check connect/call rpc/close connection sequence"""
        fake_ctx = cfy_mocks.MockCloudifyContext()
        instance = mock.Mock()
        instance.runtime_properties = {}
        fake_ctx._instance = instance
        node = mock.Mock()
        fake_ctx._node = node
        node.properties = {}
        node.runtime_properties = {}

        # no calls
        rpc.run(ctx=fake_ctx)

        # with empty list of calls
        rpc.run(ctx=fake_ctx, calls=[])

        # with list of calls, but without auth
        with self.assertRaises(cfy_exc.NonRecoverableError):
            rpc.run(ctx=fake_ctx, calls=[{'action': 'get'}])

        # netconf connection mock
        nc_conn = mock.Mock()
        nc_conn.connect = mock.MagicMock(return_value=self.CORRECT_HELLO_REPLY)
        nc_conn.send = mock.MagicMock(return_value=self.CORRECT_REPLY)
        nc_conn.close = mock.MagicMock(return_value=self.CORRECT_CLOSE_REPLY)

        nc_conn.current_level = netconf_connection.NETCONF_1_0_CAPABILITY

        node.properties = {
            'netconf_auth': {
                "user": "******",
                "password": "******",
                "ip": "super_secret"
            },
            'metadata': {
                'xmlns': {
                    'd': 'c'
                }
            },
            "base_xmlns": {
                "a": "b"
            }
        }
        with mock.patch('cloudify_netconf.netconf_connection.connection',
                        mock.MagicMock(return_value=nc_conn)):
            # we have empty action
            rpc.run(ctx=fake_ctx, calls=[{'unknow': 'get'}])

            # have some payload
            rpc.run(ctx=fake_ctx,
                    calls=[{
                        'action': 'run_something',
                        'payload': {
                            "a": "b"
                        }
                    }])

            # have lock/unlock operations
            rpc.run(ctx=fake_ctx,
                    calls=[{
                        'action': 'run_something',
                        'payload': {
                            "a": "b"
                        }
                    }],
                    lock=["rfc6020@candidate"])

            # have some copy operations
            rpc.run(ctx=fake_ctx,
                    calls=[{
                        'action': 'run_something',
                        'payload': {
                            "a": "b"
                        }
                    }],
                    back_database="a",
                    front_database="b")

            # check save to runtime properties
            self.assertEqual(instance.runtime_properties, {})
            rpc.run(ctx=fake_ctx,
                    calls=[{
                        'action': 'run_something',
                        'payload': {
                            "c": "d"
                        },
                        'save_to': 'd'
                    }])

            # looks as we save something
            self.assertTrue("d" in instance.runtime_properties)
            self.assertTrue("d_ns" in instance.runtime_properties)
            self.assertTrue("rfc6020@ok" in instance.runtime_properties["d"])

            # validation without relaxng
            rpc.run(ctx=fake_ctx,
                    calls=[{
                        'action': 'run_something',
                        'payload': {
                            "c": "d"
                        },
                        'save_to': 'd',
                        'validation': {
                            'xpath': 'somepath'
                        }
                    }])

            self.assertEqual(nc_conn.current_level,
                             netconf_connection.NETCONF_1_0_CAPABILITY)

        # version 1.1
        nc_conn.connect = mock.MagicMock(
            return_value=self.CORRECT_HELLO_1_1_REPLY)
        with mock.patch('cloudify_netconf.netconf_connection.connection',
                        mock.MagicMock(return_value=nc_conn)):
            rpc.run(ctx=fake_ctx, calls=[{
                'action': 'run_something',
            }])

            self.assertEqual(nc_conn.current_level,
                             netconf_connection.NETCONF_1_1_CAPABILITY)