CI for the TripleO project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_write_bash_script.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from emit_releases_file import write_releases_dictionary_to_bash
  2. import os
  3. import mock
  4. from mock import mock_open
  5. from six import PY2
  6. import pytest
  7. if PY2:
  8. BUILTINS_OPEN = "__builtin__.open"
  9. else:
  10. BUILTINS_OPEN = "builtins.open"
  11. def test_empty_relases_dictionary_fails():
  12. assert (not write_releases_dictionary_to_bash({}, ""))
  13. @pytest.fixture
  14. def releases_dictionary():
  15. return {
  16. 'undercloud_install_release': 'queens',
  17. 'undercloud_install_hash': 'current-tripleo',
  18. 'undercloud_target_release': 'master',
  19. 'undercloud_target_hash': 'current-tripleo',
  20. 'overcloud_deploy_release': 'master',
  21. 'overcloud_deploy_hash': 'current-tripleo',
  22. 'overcloud_target_release': 'master',
  23. 'overcloud_target_hash': 'current-tripleo',
  24. }
  25. @pytest.mark.parametrize('deleted_key', [
  26. 'undercloud_install_release',
  27. 'undercloud_install_hash',
  28. 'undercloud_target_release',
  29. 'undercloud_target_hash',
  30. 'overcloud_deploy_release',
  31. 'overcloud_deploy_hash',
  32. 'overcloud_target_release',
  33. 'overcloud_target_hash',
  34. ])
  35. def test_missing_key_fails(releases_dictionary, deleted_key):
  36. wrong_releases_dictionary = releases_dictionary.pop(deleted_key)
  37. assert (not write_releases_dictionary_to_bash(wrong_releases_dictionary,
  38. ""))
  39. @mock.patch(BUILTINS_OPEN, new_callable=mock_open)
  40. def test_open_exception_fails(mock, releases_dictionary):
  41. bash_script = '/foo/bar.sh'
  42. mock.side_effect = IOError
  43. assert (not write_releases_dictionary_to_bash(releases_dictionary,
  44. bash_script))
  45. @mock.patch(BUILTINS_OPEN, new_callable=mock_open)
  46. def test_output_is_sourceable(mock, releases_dictionary):
  47. bash_script = '/foo/bar.sh'
  48. assert (write_releases_dictionary_to_bash(releases_dictionary,
  49. bash_script))
  50. mock.assert_called_once_with(bash_script, 'w')
  51. handle = mock()
  52. args, _ = handle.write.call_args
  53. written_content = args[0]
  54. # TODO: check environment variables
  55. assert (0 == os.system(written_content))