Exemplo n.º 1
0
    def add_docker_node(self, target, docker_network=None):
        target_img = target['img']
        target_type = target['type']
        target_cname = target['name'] + '-${uniq_id}'
        docker_network = self.cname if docker_network is None else docker_network

        # TODO: check for docker image and build if needed/can
        # TODO: move default command into Dockerfile
        # TODO: list of ports to forward, http proxy port for example and ssh
        self.sendline(
            'docker run --rm --privileged --name=%s -d -p 22 %s /usr/sbin/sshd -D'
            % (target_cname, target_img))
        self.expect(self.prompt)
        assert "Unable to find image" not in self.before, "Unable to find %s image!" % target_img
        self.expect(pexpect.TIMEOUT, timeout=1)
        self.sendline('docker network connect %s %s' %
                      (docker_network, target_cname))
        self.expect(self.prompt)
        assert 'Error response from daemon' not in self.before, "Failed to connect docker network"
        if self.created_docker_network == True:
            self.sendline('docker exec %s ip address flush dev eth1' %
                          target_cname)
            self.expect(self.prompt)
        self.sendline("docker port %s | grep '22/tcp' | sed 's/.*://g'" %
                      target_cname)
        self.expect_exact("docker port %s | grep '22/tcp' | sed 's/.*://g'" %
                          target_cname)
        self.expect(self.prompt)
        target['port'] = self.before.strip()
        int(self.before.strip())
        self.created_docker = True

        target['ipaddr'] = self.ipaddr
        from boardfarm.devices import get_device
        new_device = get_device(target_type, **target)
        self.extra_devices.append(new_device)

        self.target_cname.append(target_cname)
Exemplo n.º 2
0
    def runTest(self):
        wan = self.dev.wan

        if not wan:
            msg = 'No WAN Device defined, skipping test_create_session.'
            lib.common.test_msg(msg)
            self.skipTest(msg)

        from boardfarm import devices
        # this should fail, as "DebianBoxNonExistent" is not (yet) a device
        try:
            kwargs = {
                'name': "wan_test_calls_fail",
                'ipaddr': wan.ipaddr,
                'port': 22,
                'color': 'magenta'
            }
            self.session = devices.get_device("DebianBoxNonExistent", **kwargs)
        except:
            pass
        else:
            assert self.session is None, "Test Failed on wrong class name"
            print(
                "Failed to create session on wrong class name (expected) PASS")

        # this must fail, as "169.254.12.18" is not a valid ip
        try:
            kwargs = {
                'name': "wan_test_ip_fail",
                'ipaddr': "169.254.12.18",
                'port': 22,
                'color': 'cyan'
            }
            self.session = devices.get_device("DebianBox", **kwargs)
        except:
            pass
        else:
            assert self.session is None, "Test Failed on wrong IP"
            print("Failed to create session on wrong IP (expected) PASS")

        # this must fail, as 50 is not a valid port
        try:
            kwargs = {
                'name': "wan_test_port_fail",
                'ipaddr': wan.ipaddr,
                'port': 50,
                'color': 'red'
            }
            self.session = devices.get_device("DebianBox", **kwargs)
        except:
            pass
        else:
            assert self.session is None, "Test Failed on wrong port"
            print("Failed to create session on wrong port (expected) PASS")

        # this must fail, close but no cigar
        try:
            kwargs = {
                'name': "wan_test_type_fail",
                'ipaddr': wan.ipaddr,
                'port': 50,
                'color': 'red'
            }
            self.session = devices.get_device("debina", **kwargs)
        except:
            pass
        else:
            assert self.session is None, "Test Failed on misspelled class name"
            print(
                "Failed to create session on misspelled class name (expected) PASS"
            )

        # this should pass
        try:
            kwargs = {
                'name': "correct_wan_parms",
                'ipaddr': wan.ipaddr,
                'port': wan.port,
                'color': 'yellow'
            }
            self.session = devices.get_device("debian", **kwargs)
        except:
            assert 0, "Failed to create session, Test FAILED!"
        else:
            assert self.session is not None, "Test Failed on correct paramters!!"

        print("Session created successfully")

        # is the session really logged onto the wan?

        wan.sendline()
        wan.expect(wan.prompt)
        wan.sendline("ip a")
        wan.expect_exact("ip a")
        wan.expect(wan.prompt)
        w = wan.before

        self.session.sendline()
        self.session.expect(self.session.prompt)
        self.session.sendline("ip a")
        self.session.expect_exact("ip a")
        self.session.expect(self.session.prompt)
        s = self.session.before

        assert w == s, "Interfaces differ!!! Test Failed"

        self.session.sendline("exit")

        print("Test passed")
Exemplo n.º 3
0
    def add_target(self, target, docker_network=None):
        """Run a docker container from DockerFactory.

        ``add_target`` performs the following actions:
            - Validate / Build target image for container
            - Spawn the requested docker container
            - Connect the docker network to container
            - Register the target to ``boardfarm.lib.DeviceManager``

        :param docker_network: docker network to connect to target container
        :type docker_network: string
        :param target: mandatory keys **img, type, name**
        :type target: dict

        .. note::

            1. If wan containers are being spawned, ensure adding **static-routes** to ``target['options']``
            2. LAN and WAN containers both have a mgmt iface ``eth0`` and a data iface ``eth1``
            3. JSON validation for target is not handled by add_target
        """
        target_img = target["img"]
        target_type = target["type"]
        name = target["name"]

        counter = self.device_counter
        counter[name] += 1

        target_cname = target["name"] + "-${uniq_id}" + str(counter[name])

        ip = getattr(self, "docker_engine", None)
        if not ip:
            ip = self.ipaddr
        target["ipaddr"] = ip.split("@")[-1]
        target["device_mgr"] = self.dev

        options = target.get("options", [])
        if options:
            options = options.split(",")
        docker_network = self.docker_network if not docker_network else docker_network

        self.configure_docker_image(target_img)

        # TODO: check for docker image and build if needed/can
        # TODO: move default command into Dockerfile
        # TODO: list of ports to forward, http proxy port for example and ssh
        self.sendline(
            "docker run --rm --privileged --name=%s -d -p 22 %s /usr/sbin/sshd -D"
            % (target_cname, target_img))
        self.expect(self.prompt)
        self.expect(pexpect.TIMEOUT, timeout=5)
        self.created_docker = True
        self.target_cname.append(target_cname)

        self.isolate_traffic(target_cname)

        self.sendline("docker network connect %s %s" %
                      (docker_network, target_cname))
        self.expect(self.prompt)
        assert ("Error response from daemon"
                not in self.before), "Failed to connect docker network"

        if self.network_options:
            ip = json.loads(
                self.check_output(
                    "docker inspect -f '{{json .NetworkSettings.Networks.%s}}' %s"
                    % (docker_network, target_cname)))

            assert ip["IPAddress"], (
                "Failed to set a static IPv4 Address for container : %s" %
                target_cname)

            options.append("wan-static-ip:%s/%s " %
                           (ip["IPAddress"], ip["IPPrefixLen"]))

            # IPv6 is optional
            if ip["GlobalIPv6Address"]:
                options.append(
                    "wan-static-ipv6:%s/%s" %
                    (ip["GlobalIPv6Address"], ip["GlobalIPv6PrefixLen"]))

            if ip["Gateway"]:
                options.append("static-route:0.0.0.0/0-%s" % ip["Gateway"])
        else:
            # if the IP address for interface is suppose to be assigned using DHCP
            self.sendline("docker exec %s ip address flush dev eth1" %
                          target_cname)
            self.expect(self.prompt)

        self.sendline("docker port %s | grep '22/tcp' | sed 's/.*://g'" %
                      target_cname)
        self.expect_exact("docker port %s | grep '22/tcp' | sed 's/.*://g'" %
                          target_cname)
        self.expect(self.prompt)
        target["port"] = self.before.strip()
        target["options"] = ",".join(options)
        int(self.before.strip())

        new_device = get_device(target_type, **target)
        self.extra_devices.append(new_device)
Exemplo n.º 4
0
    def test_main(self):
        """Tests the create_session function in devices/__init__.py."""
        wan = self.dev.wan

        if not wan:
            msg = "No WAN Device defined, skipping test_create_session."
            lib.common.test_msg(msg)
            self.skipTest(msg)

        from boardfarm import devices

        # this should fail, as "DebianBoxNonExistent" is not (yet) a device
        try:
            kwargs = {
                "name": "wan_test_calls_fail",
                "ipaddr": wan.ipaddr,
                "port": 22,
                "color": "magenta",
            }
            self.session = devices.get_device("DebianBoxNonExistent",
                                              device_mgr=self.dev,
                                              **kwargs)
        except Exception as e:
            print(e)
        else:
            assert self.session is None, "Test Failed on wrong class name"
            print(
                "Failed to create session on wrong class name (expected) PASS")

        # this must fail, as "169.254.12.18" is not a valid ip
        try:
            kwargs = {
                "name": "wan_test_ip_fail",
                "ipaddr": "169.254.12.18",
                "port": 22,
                "color": "cyan",
            }
            self.session = devices.get_device("DebianBox",
                                              device_mgr=self.dev,
                                              **kwargs)
        except Exception as e:
            print(e)
        else:
            assert self.session is None, "Test Failed on wrong IP"
            print("Failed to create session on wrong IP (expected) PASS")

        # this must fail, as 50 is not a valid port
        try:
            kwargs = {
                "name": "wan_test_port_fail",
                "ipaddr": wan.ipaddr,
                "port": 50,
                "color": "red",
            }
            self.session = devices.get_device("DebianBox",
                                              device_mgr=self.dev,
                                              **kwargs)
        except Exception as e:
            print(e)
        else:
            assert self.session is None, "Test Failed on wrong port"
            print("Failed to create session on wrong port (expected) PASS")

        # this must fail, close but no cigar
        try:
            kwargs = {
                "name": "wan_test_type_fail",
                "ipaddr": wan.ipaddr,
                "port": 50,
                "color": "red",
            }
            self.session = devices.get_device("debina",
                                              device_mgr=self.dev,
                                              **kwargs)
        except Exception as e:
            print(e)
        else:
            assert self.session is None, "Test Failed on misspelled class name"
            print(
                "Failed to create session on misspelled class name (expected) PASS"
            )

        # this should pass
        try:
            kwargs = {
                "name": "correct_wan_parms",
                "ipaddr": wan.ipaddr,
                "port": wan.port,
                "color": "yellow",
            }
            self.session = devices.get_device("debian",
                                              device_mgr=self.dev,
                                              override=True,
                                              **kwargs)
        except Exception:
            assert 0, "Failed to create session, Test FAILED!"
        else:
            assert self.session is not None, "Test Failed on correct parameters!!"

        print("Session created successfully")

        # is the session really logged onto the wan?

        wan.sendline()
        wan.expect(wan.prompt)
        wan.sendline("ip a")
        wan.expect_exact("ip a")
        wan.expect(wan.prompt)
        w = wan.before

        self.session.sendline()
        self.session.expect(self.session.prompt)
        self.session.sendline("ip a")
        self.session.expect_exact("ip a")
        self.session.expect(self.session.prompt)
        s = self.session.before

        assert w == s, "Interfaces differ!!! Test Failed"

        self.session.sendline("exit")

        print("Test passed")