Exemplo n.º 1
0
class TestFormatClass(object):
    """Test cases for rptk format classes."""

    data_dir = os.path.join(os.path.dirname(__file__), "data")

    @pytest.mark.parametrize(("format", "path"),
                             default_format_classes().items())
    @pytest.mark.parametrize("objects", objects())
    def test_format_class(self, format, path, objects, validate_schema):
        """Test rptk format class."""
        mod_path, cls_name = path.rsplit(".", 1)
        mod = importlib.import_module(mod_path)
        cls = getattr(mod, cls_name)
        result = dict()
        for obj in objects:
            with open(os.path.join(self.data_dir, "{}.json".format(obj))) as f:
                result.update(json.load(f))
        with cls() as f:
            output = f.format(result=result)
        if format == "json":
            assert validate_schema(json.loads(output),
                                   "get_prefix_list.schema")
        elif format == "yaml":
            assert validate_schema(yaml.load(output, Loader=Loader),
                                   "get_prefix_list.schema")
        else:
            for obj in objects:
                assert obj in output
Exemplo n.º 2
0
class TestWebAPI(object):
    """Test cases for rptk web API."""
    def test_get_formats(self, client, validate_schema):
        """Test get_formats method."""
        uri = "/formats"
        with client() as c:
            resp = c.get(uri)
        assert resp.status_code == 200
        assert resp.content_type == "application/json"
        assert server_re.match(resp.headers["Server"])
        data = resp.json
        assert validate_schema(data, "get_formats.schema")

    def test_get_policies(self, client, validate_schema):
        """Test get_policies method."""
        uri = "/policies"
        with client() as c:
            resp = c.get(uri)
        assert resp.status_code == 200
        assert resp.content_type == "application/json"
        assert server_re.match(resp.headers["Server"])
        data = resp.json
        assert validate_schema(data, "get_policies.schema")

    @pytest.mark.parametrize("f", default_format_classes().keys())
    @pytest.mark.parametrize("objects", objects())
    def test_get_prefix_list(self, client, validate_schema, f, objects):
        """Test get_prefix_list method."""
        base_uris = ["/query?format={}".format(f), "/{}/query?".format(f)]
        uris = list()
        for uri in base_uris:
            for obj in objects:
                uri += "&objects={}".format(obj)
            uris.append(uri)
        if len(objects) == 1:
            uris.append("/{}/{}".format(f, objects[0]))
        print("uris: {}".format(uris))
        for uri in uris:
            with client() as c:
                resp = c.get(uri)
            assert resp.status_code == 200
            assert server_re.match(resp.headers["Server"])
            if f == "json":
                assert resp.content_type == "application/json"
                data = resp.json
                assert validate_schema(data, "get_prefix_list.schema")
            elif f == "yaml":
                assert resp.content_type == "application/x-yaml"
                data = yaml.load(resp.data, Loader=Loader)
                assert validate_schema(data, "get_prefix_list.schema")
            else:
                assert resp.content_type == "text/plain"
                for obj in objects:
                    assert obj in resp.data.decode()
Exemplo n.º 3
0
class TestQueryClass(object):
    """Test cases for rptk query classes."""
    @pytest.mark.parametrize("path", default_query_classes().values())
    @pytest.mark.parametrize("policy", available_policies().keys())
    @pytest.mark.parametrize("objects", objects())
    def test_query_class(self, posix, path, policy, objects, validate_schema):
        """Test rptk query class."""
        mod_path, cls_name = path.rsplit(".", 1)
        mod = importlib.import_module(mod_path)
        cls = getattr(mod, cls_name)
        with cls(host="whois.radb.net", port=43, policy=policy) as q:
            if q.posix_only and not posix:
                pytest.skip("skipping posix only test")
            result = q.query(*objects)
        assert validate_schema(result, "get_prefix_list.schema")
Exemplo n.º 4
0
class TestAPI(object):
    """Test cases for rptk python API."""

    @pytest.mark.parametrize("config_file", config_file_opts)
    @pytest.mark.parametrize("q", default_query_classes().keys())
    @pytest.mark.parametrize("f", default_format_classes().keys())
    @pytest.mark.parametrize("p", available_policies().keys())
    @pytest.mark.parametrize("objects", objects())
    def test_api(self, config_file, q, f, p, objects):
        """Test rptk python API."""
        from rptk import RptkAPI
        with RptkAPI(config_file=config_file, query_class_name=q,
                     format_class_name=f, query_policy=p) as api:
            result = api.query(*objects)
            output = api.format(result=result)
        assert output
Exemplo n.º 5
0
class TestCLI(object):
    """Test cases for rptk command-line tool."""
    @pytest.mark.parametrize("args", args)
    @pytest.mark.parametrize("objects", objects())
    def test_cli(self, capsys, cli_entry_point, args, objects):
        """Test rptk command-line tool."""
        sys.argv[0] = "rptk"
        argv = args + list(objects)
        try:
            cli_entry_point(argv=argv)
        except SystemExit as exit:
            captured = capsys.readouterr()
            assert exit.code == 0
            for obj in objects:
                if "--version" in args:
                    assert version_regexp.match(captured.out)
                else:
                    assert obj in captured.out