test_nilrt_ip.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import io
  2. import pytest
  3. import salt.modules.nilrt_ip as nilrt_ip
  4. from tests.support.mock import patch
  5. @pytest.fixture(autouse=True)
  6. def setup_loader():
  7. setup_loader_modules = {nilrt_ip: {}}
  8. with pytest.helpers.loader_mock(setup_loader_modules) as loader_mock:
  9. yield loader_mock
  10. @pytest.fixture
  11. def patched_config_file():
  12. config_file = io.StringIO(
  13. """
  14. [some_section]
  15. name = thing
  16. fnord = bar
  17. icanhazquotes = "this string is quoted"
  18. icannothazquotes = this string is unquoted
  19. number_value = 42
  20. """
  21. )
  22. with patch("salt.utils.files.fopen", return_value=config_file):
  23. yield
  24. def test_when_config_has_quotes_around_string_they_should_be_removed(
  25. patched_config_file,
  26. ):
  27. expected_value = "this string is quoted"
  28. option = "icanhazquotes"
  29. actual_value = nilrt_ip._load_config("some_section", [option])[option]
  30. assert actual_value == expected_value
  31. def test_when_config_has_no_quotes_around_string_it_should_be_returned_as_is(
  32. patched_config_file,
  33. ):
  34. expected_value = "this string is unquoted"
  35. option = "icannothazquotes"
  36. actual_value = nilrt_ip._load_config("some_section", [option])[option]
  37. assert actual_value == expected_value
  38. @pytest.mark.parametrize(
  39. "default_value",
  40. [
  41. 42,
  42. -99.9,
  43. ('"', "some value", 42, '"'),
  44. ['"', "a weird list of values", '"'],
  45. {"this": "dictionary", "has": "multiple values", 0: '"', -1: '"'},
  46. ],
  47. )
  48. def test_when_default_value_is_not_a_string_and_option_is_missing_the_default_value_should_be_returned(
  49. patched_config_file, default_value
  50. ):
  51. option = "non existent option"
  52. actual_value = nilrt_ip._load_config(
  53. "some_section", options=[option], default_value=default_value
  54. )[option]
  55. assert actual_value == default_value