def test_get_image_informations(project, manager, loop): response = { } with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest") loop.run_until_complete(asyncio.async(vm._get_image_informations())) mock.assert_called_with("GET", "images/ubuntu:latest/json")
def test_get_image_informations(project, manager, loop): response = {} with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest") loop.run_until_complete(asyncio. async (vm._get_image_informations())) mock.assert_called_with("GET", "images/ubuntu:latest/json")
def test_aux(project, manager, port_manager): aux = port_manager.get_free_tcp_port(project) port_manager.release_tcp_port(aux, project) vm = DockerVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager, "ubuntu", aux=aux) assert vm.aux == aux vm.aux = None assert vm.aux is None
def test_create_image_not_available(loop, project, manager): call = 0 @asyncio.coroutine def informations(): nonlocal call if call == 0: call += 1 raise DockerHttp404Error("missing") else: return {} response = { "Id": "e90e34656806", "Warnings": [] } vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest") vm._get_image_informations = MagicMock() vm._get_image_informations.side_effect = informations with asyncio_patch("gns3server.modules.docker.DockerVM.pull_image", return_value=True) as mock_pull: with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: loop.run_until_complete(asyncio.async(vm.create())) mock.assert_called_with("POST", "containers/create", data={ "Tty": True, "OpenStdin": True, "StdinOnce": False, "HostConfig": { "CapAdd": ["ALL"], "Binds": [ "{}:/gns3:ro".format(get_resource("modules/docker/resources")), "{}:/gns3volumes/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network")) ], "Privileged": True }, "Volumes": {}, "NetworkDisabled": True, "Name": "test", "Hostname": "test", "Image": "ubuntu:latest", "Env": [ "container=docker", "GNS3_MAX_ETHERNET=eth0", "GNS3_VOLUMES=/etc/network" ], "Entrypoint": ["/gns3/init.sh"], "Cmd": ["/bin/sh"] }) assert vm._cid == "e90e34656806" mock_pull.assert_called_with("ubuntu:latest")
def test_create_environment(loop, project, manager): response = {"Id": "e90e34656806", "Warnings": []} with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{ "image": "ubuntu:latest" }]) as mock_list_images: with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest") vm.environment = "YES=1\nNO=0" loop.run_until_complete(asyncio. async (vm.create())) mock.assert_called_with( "POST", "containers/create", data={ "Tty": True, "OpenStdin": True, "StdinOnce": False, "HostConfig": { "CapAdd": ["ALL"], "Binds": [ "{}:/gns3:ro".format( get_resource("modules/docker/resources")), "{}:/gns3volumes/etc/network:rw".format( os.path.join(vm.working_dir, "etc", "network")) ], "Privileged": True }, "Env": [ "container=docker", "GNS3_MAX_ETHERNET=eth0", "GNS3_VOLUMES=/etc/network", "YES=1", "NO=0" ], "Volumes": {}, "NetworkDisabled": True, "Name": "test", "Hostname": "test", "Image": "ubuntu:latest", "Entrypoint": ["/gns3/init.sh"], "Cmd": ["/bin/sh"] }) assert vm._cid == "e90e34656806"
def test_create_vnc(loop, project, manager): response = { "Id": "e90e34656806", "Warnings": [] } with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu:latest"}]) as mock_list_images: with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest", console_type="vnc", console=5900) vm._start_vnc = MagicMock() vm._display = 42 loop.run_until_complete(asyncio.async(vm.create())) mock.assert_called_with("POST", "containers/create", data={ "Tty": True, "OpenStdin": True, "StdinOnce": False, "HostConfig": { "CapAdd": ["ALL"], "Binds": [ "{}:/gns3:ro".format(get_resource("modules/docker/resources")), "{}:/gns3volumes/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network")), '/tmp/.X11-unix/:/tmp/.X11-unix/' ], "Privileged": True }, "Volumes": {}, "NetworkDisabled": True, "Name": "test", "Hostname": "test", "Image": "ubuntu:latest", "Env": [ "container=docker", "GNS3_MAX_ETHERNET=eth0", "GNS3_VOLUMES=/etc/network", "QT_GRAPHICSSYSTEM=native", "DISPLAY=:42" ], "Entrypoint": ["/gns3/init.sh"], "Cmd": ["/bin/sh"] }) assert vm._start_vnc.called assert vm._cid == "e90e34656806" assert vm._console_type == "vnc"
def test_allocate_aux(project, manager): vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager) assert vm.aux is None # Docker has an aux port by default vm = DockerVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager, "ubuntu") assert vm.aux is not None
def test_create_environment(loop, project, manager): response = { "Id": "e90e34656806", "Warnings": [] } with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu:latest"}]) as mock_list_images: with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest") vm.environment = "YES=1\nNO=0" loop.run_until_complete(asyncio.async(vm.create())) mock.assert_called_with("POST", "containers/create", data={ "Tty": True, "OpenStdin": True, "StdinOnce": False, "HostConfig": { "CapAdd": ["ALL"], "Binds": [ "{}:/gns3:ro".format(get_resource("modules/docker/resources")), "{}:/gns3volumes/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network")) ], "Privileged": True }, "Env": [ "container=docker", "GNS3_MAX_ETHERNET=eth0", "GNS3_VOLUMES=/etc/network", "YES=1", "NO=0" ], "Volumes": {}, "NetworkDisabled": True, "Name": "test", "Hostname": "test", "Image": "ubuntu:latest", "Entrypoint": ["/gns3/init.sh"], "Cmd": ["/bin/sh"] }) assert vm._cid == "e90e34656806"
def vm(project, manager): vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest") vm._cid = "e90e34656842" vm.allocate_aux = False return vm
def test_create_image_not_available(loop, project, manager): call = 0 @asyncio.coroutine def informations(): nonlocal call if call == 0: call += 1 raise DockerHttp404Error("missing") else: return {} response = {"Id": "e90e34656806", "Warnings": []} vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest") vm._get_image_informations = MagicMock() vm._get_image_informations.side_effect = informations with asyncio_patch("gns3server.modules.docker.DockerVM.pull_image", return_value=True) as mock_pull: with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: loop.run_until_complete(asyncio. async (vm.create())) mock.assert_called_with( "POST", "containers/create", data={ "Tty": True, "OpenStdin": True, "StdinOnce": False, "HostConfig": { "CapAdd": ["ALL"], "Binds": [ "{}:/gns3:ro".format( get_resource("modules/docker/resources")), "{}:/gns3volumes/etc/network:rw".format( os.path.join(vm.working_dir, "etc", "network")) ], "Privileged": True }, "Volumes": {}, "NetworkDisabled": True, "Name": "test", "Hostname": "test", "Image": "ubuntu:latest", "Env": [ "container=docker", "GNS3_MAX_ETHERNET=eth0", "GNS3_VOLUMES=/etc/network" ], "Entrypoint": ["/gns3/init.sh"], "Cmd": ["/bin/sh"] }) assert vm._cid == "e90e34656806" mock_pull.assert_called_with("ubuntu:latest")
def test_create_vnc(loop, project, manager): response = {"Id": "e90e34656806", "Warnings": []} with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{ "image": "ubuntu:latest" }]) as mock_list_images: with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest", console_type="vnc", console=5900) vm._start_vnc = MagicMock() vm._display = 42 loop.run_until_complete(asyncio. async (vm.create())) mock.assert_called_with( "POST", "containers/create", data={ "Tty": True, "OpenStdin": True, "StdinOnce": False, "HostConfig": { "CapAdd": ["ALL"], "Binds": [ "{}:/gns3:ro".format( get_resource("modules/docker/resources")), "{}:/gns3volumes/etc/network:rw".format( os.path.join(vm.working_dir, "etc", "network")), '/tmp/.X11-unix/:/tmp/.X11-unix/' ], "Privileged": True }, "Volumes": {}, "NetworkDisabled": True, "Name": "test", "Hostname": "test", "Image": "ubuntu:latest", "Env": [ "container=docker", "GNS3_MAX_ETHERNET=eth0", "GNS3_VOLUMES=/etc/network", "QT_GRAPHICSSYSTEM=native", "DISPLAY=:42" ], "Entrypoint": ["/gns3/init.sh"], "Cmd": ["/bin/sh"] }) assert vm._start_vnc.called assert vm._cid == "e90e34656806" assert vm._console_type == "vnc"