Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions iotlabcli/parser/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import argparse
import contextlib
import errno
import os
import sys
from argparse import ArgumentParser
from collections import OrderedDict
Expand All @@ -46,6 +47,16 @@
DOMAIN_DNS = "iot-lab.info"


def domain_dns() -> str:
"""Domain of the sites and nodes: IOTLAB_DOMAIN environment variable
(another testbed, e.g. a local testbed), iot-lab.info by default

>>> domain_dns()
'iot-lab.info'
"""
return os.getenv("IOTLAB_DOMAIN") or DOMAIN_DNS


def base_parser(user_required: bool = False) -> ArgumentParser:
"""Base parser giving 'user' 'password' and 'version' arguments
:param user_required: set 'user' argument as required or not"""
Expand Down Expand Up @@ -238,10 +249,10 @@ def check_site_with_server(
raise argparse.ArgumentTypeError(f"Unknown site name {site_name!r}")


def site_with_domain_checked(site: str, domain: str = DOMAIN_DNS) -> str:
def site_with_domain_checked(site: str, domain: str | None = None) -> str:
"""Return site with domain and check site exists."""
check_site_with_server(site)
return f"{site}.{domain}"
return f"{site}.{domain or domain_dns()}"


def nodes_list_from_info(site: str, archi: str, nodes_str: str) -> list[str]:
Expand All @@ -264,7 +275,7 @@ def nodes_list_from_info(site: str, archi: str, nodes_str: str) -> list[str]:
"""

nodes_list = nodes_id_list(archi, nodes_str)
fmt = f"%s.{site}.{DOMAIN_DNS}"
fmt = f"%s.{site}.{domain_dns()}"
nodes_url_list = [fmt % node for node in nodes_list]
return nodes_url_list

Expand Down
13 changes: 13 additions & 0 deletions iotlabcli/tests/common_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# pylint: disable=too-many-public-methods

import argparse
import os
import sys
import unittest
from io import StringIO
Expand Down Expand Up @@ -268,6 +269,18 @@ def test_site_with_domain_checked(self):
ret = common.site_with_domain_checked("grenoble", domain="localhost")
self.assertEqual(ret, "grenoble.localhost")

@patch.dict(os.environ, {"IOTLAB_DOMAIN": "iotlab.internal"})
def test_domain_from_environment(self):
"""IOTLAB_DOMAIN replaces iot-lab.info in the sites and nodes"""
self.assertEqual(common.domain_dns(), "iotlab.internal")
self.assertEqual(
common.site_with_domain_checked("grenoble"), "grenoble.iotlab.internal"
)
self.assertEqual(
common.nodes_list_from_info("local", "custom", "1-2"),
["custom-1.local.iotlab.internal", "custom-2.local.iotlab.internal"],
)


class TestHelpAction(unittest.TestCase):
"""Test HelpAction class."""
Expand Down
Loading