Exemple #1
0
def test_load_topology_file_error_schema_error(tmpdir):

    path = str(tmpdir / "test.gns3")
    with open(path, "w+") as f:
        json.dump({"revision": GNS3_FILE_FORMAT_REVISION}, f)
    with pytest.raises(aiohttp.web.HTTPConflict):
        load_topology(path)
Exemple #2
0
def test_load_topology_file_error_schema_error(tmpdir):
    path = str(tmpdir / "test.gns3")
    with open(path, "w+") as f:
        json.dump({
            "revision": GNS3_FILE_FORMAT_REVISION
        }, f)
    with pytest.raises(aiohttp.web.HTTPConflict):
        topo = load_topology(path)
Exemple #3
0
def test_convert(directory, tmpdir):
    before_directory = os.path.join(topologies_directory, directory, "before")
    after_directory = os.path.join(topologies_directory, directory, "after")

    assert os.path.exists(before_directory), "No before directory found file for {}".format(directory)
    assert os.path.exists(after_directory), "No after directory found file for {}".format(directory)

    gns3_file = None
    for file in os.listdir(before_directory):
        if file.endswith(".gns3"):
            gns3_file = file

    assert gns3_file, "No .gns3 found file for {}".format(before_directory)

    with open(os.path.join(before_directory, gns3_file)) as f:
        before_topology = json.load(f)

    # We use a temporary directory for conversion operation to not corrupt our files
    work_directory = str(tmpdir / "work")
    shutil.copytree(before_directory, work_directory)

    work_topology = load_topology(os.path.join(work_directory, gns3_file))
    assert work_topology

    if "revision" not in before_topology or before_topology["revision"] < GNS3_FILE_FORMAT_REVISION:
        assert os.path.exists(os.path.join(work_directory, gns3_file + ".backup{}".format(before_topology.get("revision", 0))))

    # We should have the same file in after directory and the work directory
    for root, dirs, files in os.walk(after_directory):
        for file in files:
            directory = os.path.relpath(root, after_directory)
            file_path = os.path.join(work_directory, directory, file)
            assert os.path.exists(file_path), "{} is missing".format(os.path.join(directory, file))

            # For gns3project we check if size are not too much differents
            if file_path.endswith(".gns3project"):
                size = os.stat(file_path).st_size
                other_size = os.stat(os.path.join(os.path.join(root, file))).st_size
                assert size in range(other_size - 100, other_size + 100), "File {} is different".format(os.path.join(directory, file))
            # For non .gns3 file we check if the file are the same
            elif not file_path.endswith(".gns3"):
                assert os.stat(file_path).st_size == os.stat(os.path.join(os.path.join(root, file))).st_size, "File {} is different".format(os.path.join(directory, file))

    # Check if we don't have unexpected file in work directory
    for root, dirs, files in os.walk(work_directory):
        for file in files:
            directory = os.path.relpath(root, work_directory)
            file_path = os.path.join(after_directory, directory, file)
            # .backup are created by the conversion process
            if ".backup" not in file_path:
                assert os.path.exists(file_path), "{} should not be here".format(os.path.join(directory, file))

    with open(os.path.join(after_directory, gns3_file)) as f:
        after_topology = json.load(f)
    compare_dict("/", work_topology, after_topology)
Exemple #4
0
def test_convert(directory, tmpdir):
    before_directory = os.path.join(topologies_directory, directory, "before")
    after_directory = os.path.join(topologies_directory, directory, "after")

    assert os.path.exists(before_directory), "No before directory found file for {}".format(directory)
    assert os.path.exists(after_directory), "No after directory found file for {}".format(directory)

    gns3_file = None
    for file in os.listdir(before_directory):
        if file.endswith(".gns3"):
            gns3_file = file

    assert gns3_file, "No .gns3 found file for {}".format(before_directory)

    with open(os.path.join(before_directory, gns3_file)) as f:
        before_topology = json.load(f)

    # We use a temporary directory for conversion operation to not corrupt our files
    work_directory = str(tmpdir / "work")
    shutil.copytree(before_directory, work_directory)

    work_topology = load_topology(os.path.join(work_directory, gns3_file))
    assert work_topology

    if "revision" not in before_topology or before_topology["revision"] < GNS3_FILE_FORMAT_REVISION:
        assert os.path.exists(os.path.join(work_directory, gns3_file + ".backup{}".format(before_topology.get("revision", 0))))

    # We should have the same file in after directory and the work directory
    for root, dirs, files in os.walk(after_directory):
        for file in files:
            directory = os.path.relpath(root, after_directory)
            file_path = os.path.join(work_directory, directory, file)
            assert os.path.exists(file_path), "{} is missing".format(os.path.join(directory, file))

            # For gns3project we check if size are not too much differents
            if file_path.endswith(".gns3project"):
                size = os.stat(file_path).st_size
                other_size = os.stat(os.path.join(os.path.join(root, file))).st_size
                assert size in range(other_size - 100, other_size + 100), "File {} is different".format(os.path.join(directory, file))
            # For non .gns3 file we check if the file are the same
            elif not file_path.endswith(".gns3"):
                assert os.stat(file_path).st_size == os.stat(os.path.join(os.path.join(root, file))).st_size, "File {} is different".format(os.path.join(directory, file))

    # Check if we don't have unexpected file in work directory
    for root, dirs, files in os.walk(work_directory):
        for file in files:
            directory = os.path.relpath(root, work_directory)
            file_path = os.path.join(after_directory, directory, file)
            # .backup are created by the conversion process
            if ".backup" not in file_path:
                assert os.path.exists(file_path), "{} should not be here".format(os.path.join(directory, file))

    with open(os.path.join(after_directory, gns3_file)) as f:
        after_topology = json.load(f)
    compare_dict("/", work_topology, after_topology)
Exemple #5
0
def test_load_newer_topology(tmpdir):
    """
    If a topology is design for a more recent GNS3 version
    we disallow the loading.
    """

    data = {
        "project_id": "69f26504-7aa3-48aa-9f29-798d44841211",
        "name": "Test",
        "revision": 42,
        "topology": {},
        "type": "topology",
        "version": __version__
    }

    path = str(tmpdir / "test.gns3")
    with open(path, "w+") as f:
        json.dump(data, f)
    with pytest.raises(aiohttp.web.HTTPConflict):
        load_topology(path)
Exemple #6
0
def test_load_newer_topology(tmpdir):
    """
    If a topology is design for a more recent GNS3 version
    we disallow the loading.
    """
    data = {
        "project_id": "69f26504-7aa3-48aa-9f29-798d44841211",
        "name": "Test",
        "revision": 42,
        "topology": {
        },
        "type": "topology",
        "version": __version__}

    path = str(tmpdir / "test.gns3")
    with open(path, "w+") as f:
        json.dump(data, f)
    with pytest.raises(aiohttp.web.HTTPConflict):
        topo = load_topology(path)
Exemple #7
0
def test_load_topology(tmpdir):
    data = {
        "project_id": "69f26504-7aa3-48aa-9f29-798d44841211",
        "name": "Test",
        "revision": GNS3_FILE_FORMAT_REVISION,
        "topology": {
            "nodes": [],
            "links": [],
            "computes": [],
            "drawings": []
        },
        "type": "topology",
        "version": __version__}

    path = str(tmpdir / "test.gns3")
    with open(path, "w+") as f:
        json.dump(data, f)
    topo = load_topology(path)
    assert topo == data
def test_load_topology(tmpdir):
    data = {
        "project_id": "69f26504-7aa3-48aa-9f29-798d44841211",
        "name": "Test",
        "revision": GNS3_FILE_FORMAT_REVISION,
        "topology": {
            "nodes": [],
            "links": [],
            "computes": [],
            "drawings": []
        },
        "type": "topology",
        "version": __version__}

    path = str(tmpdir / "test.gns3")
    with open(path, "w+") as f:
        json.dump(data, f)
    topo = load_topology(path)
    assert topo == data
Exemple #9
0
def test_load_topology_with_supplier(tmpdir):
    supplier = {'logo': 'logo.png', 'url': 'http://example.com'}
    data = {
        "project_id": "69f26504-7aa3-48aa-9f29-798d44841211",
        "name": "Test",
        "revision": GNS3_FILE_FORMAT_REVISION,
        "topology": {
            "nodes": [],
            "links": [],
            "computes": [],
            "drawings": []
        },
        "supplier": supplier,
        "type": "topology",
        "version": __version__
    }

    path = str(tmpdir / "test.gns3")
    with open(path, "w+") as f:
        json.dump(data, f)
    topo = load_topology(path)
    assert topo == data
Exemple #10
0
def test_load_topology_with_supplier(tmpdir):
    supplier = {
        'logo': 'logo.png',
        'url': 'http://example.com'
    }
    data = {
        "project_id": "69f26504-7aa3-48aa-9f29-798d44841211",
        "name": "Test",
        "revision": GNS3_FILE_FORMAT_REVISION,
        "topology": {
            "nodes": [],
            "links": [],
            "computes": [],
            "drawings": []
        },
        "supplier": supplier,
        "type": "topology",
        "version": __version__}

    path = str(tmpdir / "test.gns3")
    with open(path, "w+") as f:
        json.dump(data, f)
    topo = load_topology(path)
    assert topo == data
Exemple #11
0
def test_load_topology_file_error(tmpdir):
    path = str(tmpdir / "test.gns3")
    with pytest.raises(aiohttp.web.HTTPConflict):
        topo = load_topology(path)
def test_load_topology_file_error(tmpdir):
    path = str(tmpdir / "test.gns3")
    with pytest.raises(aiohttp.web.HTTPConflict):
        topo = load_topology(path)