Ejemplo 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
Ejemplo n.º 2
0
    def test_files__missing_file(self):

        query = Query()

        with pytest.raises(QueryExecutionError) as e:
            query.add_query_file(os.path.join(VALID_QUERY_DIRECTORY,
                                              "not_exist.query"))

        assert "File or path not found" in str(e.value)
Ejemplo n.º 3
0
    def test_output__invalid_arg(self):

        query_text = """
            output("foobar")
        """

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

        assert "Invalid argument" in str(e.value)
Ejemplo n.º 4
0
    def test_connect__no_reader(self):

        query_text = """
            reader_object.name
        """

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

        assert "No reader for query" in str(e.value)
Ejemplo n.º 5
0
    def test_variables__undefined(self):

        query_text = """
            test = $unset_variable
        """

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

        assert "Unassigned variable" in str(e.value)
Ejemplo n.º 6
0
    def test_render_yaml__missing_var(self):

        query_text = """
            template = '### {{ variable }} ###'
            render_yaml_template($template)
        """

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

        assert "Undefined" in str(e.value)
Ejemplo n.º 7
0
    def test_reverse__invalid_type(self):

        query_text = """
            list = "not a list"
            reverse($list)
        """

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

        assert "Invalid data type" in str(e.value)
Ejemplo n.º 8
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
Ejemplo n.º 9
0
    def test_directory___success(self):

        query = Query()

        expected_results = [
            {"query_1": "query 1"},
            {"query_2": "query 2"},
            {"query_3": "query 3"},
        ]

        query.add_query_file(VALID_QUERY_DIRECTORY)

        results = query.execute()

        assert results == expected_results
Ejemplo n.º 10
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
    def perform_query_from_file(self, query_file, **query_variables):
        """ Perform Query From File: Gathers data from the current VSD or ES connection using a file in 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_file`` A file containing query language 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)

        query.add_query_file(query_file)
        results = query.execute(None, **query_variables)
        return results
Ejemplo n.º 12
0
    def test_files___success(self):

        query = Query()

        expected_results = [
            {"query_3": "query 3"},
            {"query_1": "query 1"},
        ]

        query.add_query_file(os.path.join(VALID_QUERY_DIRECTORY,
                                          "query_3.query"))
        query.add_query_file(os.path.join(VALID_QUERY_DIRECTORY,
                                          "query_1.query"))

        results = query.execute()

        assert results == expected_results
Ejemplo n.º 13
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)
Ejemplo n.º 14
0
    def test__parse_errors(self, query_text, col):
        query = Query()
        with pytest.raises(QueryParseError) as e:
            query.execute(query_text)

        assert ("line 1 col " + str(col)) in str(e)