Repository: red-hat-storage/cephci

Issue:
YAMLLoadWarning in utility/utils.py
I'm seeing the following warning in recent test runs:

```
utility/utils.py:636: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
```

Let's add unit tests for the methods in `utility.utils` that load YAML files, then change `yaml.load()` to `yaml.safe_load()`

Discussion:
rakeshgm: @psathyan can you squash the commits. 
and also please attach the successful logs of any test suite. 
please remove `WIP` if the changes are complete
psathyan: > @psathyan can you squash the commits.
> and also please attach the successful logs of any test suite.
> please remove `WIP` if the changes are complete

@rakeshgm I have intentionally put `WIP` in the title as I needed to add unit tests. I plan to remove it once my work is completed. Sorry for the confusion. Will also add the logs. 
ktdreyer: Please change your commit title from "Squash all commits" to "use yaml.safe_load()"

Patch:
diff --git a/ceph/utils.py b/ceph/utils.py
index 0c79118f..af326b0a 100644
--- a/ceph/utils.py
+++ b/ceph/utils.py
@@ -362,23 +362,6 @@ def open_firewall_port(ceph_node, port, protocol):
 
 
 def config_ntp(ceph_node):
-    distro_info = ceph_node.distro_info
-    distro_ver = distro_info['VERSION_ID']
-
-    if distro_ver.startswith('8'):
-        ceph_node.exec_command(
-            cmd="sudo sed -i '/pool*/d;/server*/d' /etc/chrony.conf",
-            long_running=True)
-        ceph_node.exec_command(
-            cmd="sudo sed -i '1i server clock.corp.redhat.com iburst'"
-                " /etc/chrony.conf",
-            long_running=True)
-        ceph_node.exec_command(
-            cmd="sudo systemctl stop chronyd.service; sudo chronyc makestep;"
-                " sudo systemctl start chronyd.service; sudo chronyc sources",
-            long_running=True,
-        )
-        return True
     ceph_node.exec_command(
         cmd="sudo sed -i '/server*/d' /etc/ntp.conf",
         long_running=True)
diff --git a/utility/utils.py b/utility/utils.py
index 7ebcfa12..62d1685f 100644
--- a/utility/utils.py
+++ b/utility/utils.py
@@ -509,7 +509,7 @@ def custom_ceph_config(suite_config, custom_config, custom_config_file):
     # retrieve custom config from file
     if custom_config_file:
         with open(custom_config_file) as f:
-            custom_config_dict = yaml.load(f)
+            custom_config_dict = yaml.safe_load(f)
             log.info("File contents: {}".format(custom_config_dict))
 
     # format cli configs into dict
@@ -633,7 +633,7 @@ def get_cephci_config():
     cfg_file = os.path.join(home_dir, ".cephci.yaml")
     try:
         with open(cfg_file, "r") as yml:
-            cfg = yaml.load(yml)
+            cfg = yaml.safe_load(yml)
     except IOError:
         log.error("Please create ~/.cephci.yaml from the cephci.yaml.template. "
                   "See README for more information.")


Test patch:
diff --git a/unittests/utility/fixtures/.cephci.yaml b/unittests/utility/fixtures/.cephci.yaml
new file mode 100644
index 00000000..862b121c
--- /dev/null
+++ b/unittests/utility/fixtures/.cephci.yaml
@@ -0,0 +1,23 @@
+report-portal:
+  endpoint: http://hostname:port
+  project: project_name
+  token: user_token
+
+polarion:
+  endpoint: some_url
+  username: some_user
+  password: some_password
+
+# Provide cdn_credentials before running suites which needs
+# access to GAed container image registry i.e 'registry.redhat.io'
+
+cdn_credentials:
+  username: some_user
+  password: some_password
+
+# Provide email address(es) in comma-seperated values
+# Example., address: email1, email2, ...... ,emailn
+# To completely disable all email, remove the "email:" and "address:" settings
+# entirely.
+email:
+  address: cephci@redhat.com
diff --git a/unittests/utility/test_utils.py b/unittests/utility/test_utils.py
index fa15bb67..81696fd0 100644
--- a/unittests/utility/test_utils.py
+++ b/unittests/utility/test_utils.py
@@ -1,7 +1,7 @@
 import os
 import pytest
 
-from utility.utils import custom_ceph_config
+from utility.utils import custom_ceph_config, get_cephci_config
 
 suite_config = {'global': {'osd_pool_default_pg_num': 64,
                            'osd_default_pool_size': 2,
@@ -98,3 +98,24 @@ def test_custom_ceph_config_all(config_file):
                         'mon_osd_nearfull_ratio': .70}}
     result = custom_ceph_config(suite_config, cli_config, config_file)
     assert result == expected
+
+
+def test_get_cephi_config(monkeypatch, fixtures_dir):
+    """Test loading of cephci configuration."""
+    def mock_return(x):
+        return fixtures_dir
+
+    monkeypatch.setattr(os.path, "expanduser", mock_return)
+    ceph_cfg = get_cephci_config()
+
+    assert ceph_cfg.get("email", {}).get("address") == "cephci@redhat.com"
+
+
+def test_get_cephci_config_raises(monkeypatch):
+    """Test exception thrown when invalid file is provided."""
+    def mock_return(x):
+        return "./"
+
+    monkeypatch.setattr(os.path, "expanduser", mock_return)
+    with pytest.raises(IOError):
+        get_cephci_config()