Esempio n. 1
0
    def run_execute_test_with_reader(self, reader, query_text,
                                     expected_actions, mock_results,
                                     expected_results, override_vars={}):
        query = Query()
        for mock_result in mock_results:
            reader.add_mock_result(mock_result)

        query.set_reader(reader)

        results = query.execute(query_text, **override_vars)

        if expected_actions is not None:
            expected_actions_formatted = self.format_expected_actions(
                expected_actions)

            print("\nExpected actions:")
            print("\n".join(expected_actions_formatted))

            print("\nRecorded actions:")
            print("\n".join(reader.get_recorded_actions()))

            assert reader.get_recorded_actions() == expected_actions_formatted

        assert results == expected_results

        return query
Esempio n. 2
0
    def run_execute_with_exception(self, query_text, expected_actions,
                                   exception, on_action, mock_results,
                                   expected_results=None, expect_error=True,
                                   override_vars={}):
        query = Query()
        reader = MockReader()
        reader.raise_exception(exception, on_action)
        for mock_result in mock_results:
            reader.add_mock_result(mock_result)

        query.set_reader(reader)

        if expect_error:
            with pytest.raises(exception.__class__) as e:
                query.execute(query_text, **override_vars)
                results = None
        else:
            results = query.execute(query_text, **override_vars)

        expected_actions_formatted = self.format_expected_actions(
            expected_actions)

        print("\nExpected actions:")
        print("\n".join(expected_actions_formatted))

        print("\nRecorded actions:")
        print("\n".join(reader.get_recorded_actions()))

        assert reader.get_recorded_actions() == expected_actions_formatted
        assert results == expected_results

        if expect_error:
            assert e.value == exception
            return e
Esempio n. 3
0
    def test_connect__success(self):

        query_text = """
            first_reader_object.name
            connect("VSD", "https://localhost:8443", "csproot", "csproot", "csp")
            vsd_reader_object.name
            connect("ES", "localhost")
            es_reader_object.name
        """

        expected_results = [
            ["first reader object"],
            ["vsd reader object"],
            ["es reader object"],
        ]

        first_expected_actions = self.format_expected_actions("""
            start-session
            query [first_reader_object (None)] {name}
            stop-session
        """)

        vsd_expected_actions = self.format_expected_actions("""
            connect [https://localhost:8443,csproot,csproot,csp]
            query [vsd_reader_object (None)] {name}
            stop-session
        """)

        es_expected_actions = self.format_expected_actions("""
            connect [localhost]
            query [es_reader_object (None)] {name}
            stop-session
        """)

        query = Query()
        first_reader = MockReader()
        first_reader.add_mock_result(["first reader object"])
        query.set_reader(first_reader)

        vsd_reader = MockReader()
        vsd_reader.add_mock_result(["vsd reader object"])
        query.register_reader("Vsd", vsd_reader)

        es_reader = MockReader()
        es_reader.add_mock_result(["es reader object"])
        query.register_reader("Es", es_reader)

        results = query.execute(query_text)

        assert results == expected_results

        assert first_reader.get_recorded_actions() == first_expected_actions
        assert vsd_reader.get_recorded_actions() == vsd_expected_actions
        assert es_reader.get_recorded_actions() == es_expected_actions
Esempio n. 4
0
    def test_connect__wrong_reader(self):

        query_text = """
            first_reader.name
            connect("invalid", "user", "pass")
        """

        query = Query()
        first_reader = MockReader()
        first_reader.add_mock_result(["first reader object"])

        query.set_reader(first_reader)

        with pytest.raises(QueryExecutionError) as e:
            query.execute(query_text)

        assert "Invalid type for connection" in str(e.value)
    def perform_query(self, query_text, **query_variables):
        """ Perform Query: Gathers data from the current VSD or ES connection using the query language syntax.  Result data will be returned.
                           More information about `query` can be found at:
                           [https://github.com/nuagenetworks/nuage-metroae-config|Nuage MetroAE Config]

        ``query_text`` The query language string to perform on the VSD or ES.
        ``query_variables`` A dictionary of variable values to provide or
                            override during the query.
        """
        reader = self.current_reader
        if reader is None:
            raise Exception("No VSD or ES connection has been made")

        query = Query()
        self.last_query = query
        query.set_logger(logger)
        query.set_reader(reader)
        self._register_query_readers(query)

        results = query.execute(query_text, **query_variables)
        return results