Ejemplo n.º 1
0
def test_device_address(info_dict, outcome):
    """ Ensures that a ValueError is reaised on invalid id """
    with mock.patch("two1.commands.util.zerotier.info") as info_mock:
        info_mock.return_value = info_dict

        if isinstance(outcome, str):
            assert zerotier.device_address() == outcome
        else:
            with pytest.raises(outcome):
                zerotier.device_address()
Ejemplo n.º 2
0
def test_device_address(info_dict, outcome):
    """ Ensures that a ValueError is reaised on invalid id """
    with mock.patch("two1.commands.util.zerotier.info") as info_mock:
        info_mock.return_value = info_dict

        if isinstance(outcome, str):
            assert zerotier.device_address() == outcome
        else:
            with pytest.raises(outcome):
                zerotier.device_address()
Ejemplo n.º 3
0
def _join(client, network):
    """Join the given network.

    Args:
        client (two1.server.rest_client.TwentyOneRestClient) an object
            for sending authenticated requests to the TwentyOne
            backend
        network (str): the name of the network being joined. Defaults
        to 21market

    Raises:
        ServerRequestError: if server returns an error code other than 401
    """
    try:
        # ensures the zerotier daemon is running
        zerotier.start_daemon()
        zt_device_address = zerotier.device_address()
        response = client.join(network, zt_device_address)
        if response.ok:
            network_id = response.json().get("networkid")
            zerotier.join_network(network_id)
            logger.info(uxstring.UxString.successful_join.format(click.style(network, fg="magenta")))
            return {'joined': True}
    except exceptions.ServerRequestError as e:
        if e.status_code == 400:
            logger.info(uxstring.UxString.invalid_network)
            return {'joined': False, 'reason': 'invalid network'}
        else:
            raise e
    except subprocess.CalledProcessError as e:
        logger.info(str(e))
        return {'joined': False, 'reason': str(e)}
Ejemplo n.º 4
0
def _join(client, network):
    """Join the given network.

    Args:
        client (two1.server.rest_client.TwentyOneRestClient) an object
            for sending authenticated requests to the TwentyOne
            backend
        network (str): the name of the network being joined. Defaults
        to 21market

    Raises:
        ServerRequestError: if server returns an error code other than 401
    """
    try:
        # ensures the zerotier daemon is running
        zerotier.start_daemon()
        zt_device_address = zerotier.device_address()
        response = client.join(network, zt_device_address)
        if response.ok:
            network_id = response.json().get("networkid")
            zerotier.join_network(network_id)
            logger.info(
                uxstring.UxString.successful_join.format(
                    click.style(network, fg="magenta")))
            return {'joined': True}
    except exceptions.ServerRequestError as e:
        if e.status_code == 400:
            logger.info(uxstring.UxString.invalid_network)
            return {'joined': False, 'reason': 'invalid network'}
        else:
            raise e
    except subprocess.CalledProcessError as e:
        logger.info(str(e))
        return {'joined': False, 'reason': str(e)}
Ejemplo n.º 5
0
 def connect_market(self, client, network):
     """ Connect to the 21market network.
     """
     try:
         zt_device_address = zerotier.device_address()
         response = client.join(network, zt_device_address)
         if response.ok:
             network_id = response.json().get("networkid")
             zerotier.join_network(network_id)
     except exceptions.ServerRequestError as e:
         if e.status_code == 400:
             logger.info(uxstring.UxString.invalid_network)
         else:
             raise e
     except subprocess.CalledProcessError as e:
         logger.info(str(e))
Ejemplo n.º 6
0
    def connect_market(self, client, network):
        """ Connect to the 21market network.

        Args:
            client: Client to join network
            network: Network to join
        """
        try:
            zt_device_address = zerotier.device_address()
            response = client.join(network, zt_device_address)
            if response.ok:
                network_id = response.json().get("networkid")
                zerotier.join_network(network_id)
        except exceptions.ServerRequestError as e:
            if e.status_code == 400:
                logger.info(uxstring.UxString.invalid_network)
            else:
                raise e
        except subprocess.CalledProcessError as e:
            logger.info(str(e))
Ejemplo n.º 7
0
    def connect_market(self, client, network):
        """ Connect to the 21market network.

        Args:
            client: Client to join network
            network: Network to join
        """
        try:
            zt_device_address = zerotier.device_address()
            response = client.join(network, zt_device_address)
            if response.ok:
                network_id = response.json().get("networkid")
                zerotier.join_network(network_id)
                if self.wait_for_zt_confirmation():
                    pass
        except exceptions.ServerRequestError as e:
            if e.status_code == 400:
                logger.info(uxstring.UxString.invalid_network)
            else:
                raise e
        except subprocess.CalledProcessError as e:
            logger.info(str(e))
        time.sleep(10)  # wait for interface to come up