Captures written in the captures directory on the controller

This commit is contained in:
Julien Duponchelle committed 2016-04-26 17:10:33 +02:00
1 parent 9a1eeb57e9
commit 264254e657
9 files changed
+155 -40

No files matched your search

+36 -3
View File
@@ -15,7 +15,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import pytest
import asyncio
from unittest.mock import MagicMock
@@ -24,6 +26,8 @@ from gns3server.controller.vm import VM
from gns3server.controller.compute import Compute
from gns3server.controller.project import Project
from tests.utils import AsyncioBytesIO
@pytest.fixture
def project():
@@ -35,6 +39,17 @@ def compute():
return Compute("example.com", controller=MagicMock())
@pytest.fixture
def link(async_run, project, compute):
vm1 = VM(project, compute)
vm2 = VM(project, compute)
link = Link(project)
async_run(link.addVM(vm1, 0, 4))
async_run(link.addVM(vm2, 1, 3))
return link
def test_addVM(async_run, project, compute):
vm1 = VM(project, compute)
@@ -70,15 +85,33 @@ def test_json(async_run, project, compute):
"port_number": 3
}
],
"capturing": False
"capturing": False,
"capture_file_name": None
}
def test_capture_filename(project, compute, async_run):
def test_start_streaming_pcap(link, async_run, tmpdir, project):
@asyncio.coroutine
def fake_reader():
output = AsyncioBytesIO()
yield from output.write(b"hello")
output.seek(0)
return output
link._capture_file_name = "test.pcap"
link._capturing = True
link.read_pcap_from_source = fake_reader
async_run(link._start_streaming_pcap())
with open(os.path.join(project.captures_directory, "test.pcap"), "rb") as f:
c = f.read()
assert c == b"hello"
def test_default_capture_file_name(project, compute, async_run):
vm1 = VM(project, compute, name="Hello@")
vm2 = VM(project, compute, name="w0.rld")
link = Link(project)
async_run(link.addVM(vm1, 0, 4))
async_run(link.addVM(vm2, 1, 3))
assert link.capture_file_name() == "Hello_0-4_to_w0rld_1-3.pcap"
assert link.default_capture_file_name() == "Hello_0-4_to_w0rld_1-3.pcap"
+6
View File
@@ -63,6 +63,12 @@ def test_changing_path_with_quote_not_allowed(tmpdir):
p.path = str(tmpdir / "project\"53")
def test_captures_directory(tmpdir):
p = Project(path=str(tmpdir))
assert p.captures_directory == str(tmpdir / "project-files" / "captures")
assert os.path.exists(p.captures_directory)
def test_addVM(async_run):
compute = MagicMock()
project = Project()
+4 -4
View File
@@ -153,7 +153,7 @@ def test_capture(async_run, project):
assert link.capturing
compute1.post.assert_any_call("/projects/{}/iou/vms/{}/adapters/3/ports/1/start_capture".format(project.id, vm_iou.id), data={
"capture_file_name": link.capture_file_name(),
"capture_file_name": link.default_capture_file_name(),
"data_link_type": "DLT_EN10MB"
})
@@ -163,7 +163,7 @@ def test_capture(async_run, project):
compute1.post.assert_any_call("/projects/{}/iou/vms/{}/adapters/3/ports/1/stop_capture".format(project.id, vm_iou.id))
def test_read_pcap(project, async_run):
def test_read_pcap_from_source(project, async_run):
compute1 = MagicMock()
link = UDPLink(project)
@@ -173,5 +173,5 @@ def test_read_pcap(project, async_run):
capture = async_run(link.start_capture())
assert link._capture_vm is not None
async_run(link.read_pcap())
link._capture_vm["vm"].compute.streamFile.assert_called_with(project, "tmp/captures/" + link.capture_file_name())
async_run(link.read_pcap_from_source())
link._capture_vm["vm"].compute.streamFile.assert_called_with(project, "tmp/captures/" + link._capture_file_name)