Added test to check ntpq peers state

The existing test 'test_ntp_sync' check the time is equal across
the nodes. Sometimes there can be some NTP issue, but the time
can be still correct. For example, Contrail can have "NTP state
unsynchronized" when noone from remote peers is chosen. So there
is some need to check "ntpq -pn" on the nodes and check the peers
state.
The new test gets ntpq peers state and check the system peer is
declared.

Change-Id: Icb8799b2323a446a3ec3dc6db54fd1d9de0356e5
diff --git a/test_set/cvp-sanity/tests/test_ntp_sync.py b/test_set/cvp-sanity/tests/test_ntp_sync.py
index 6e35215..23a98a3 100644
--- a/test_set/cvp-sanity/tests/test_ntp_sync.py
+++ b/test_set/cvp-sanity/tests/test_ntp_sync.py
@@ -1,6 +1,8 @@
-import utils
+import json
 import os
 
+import utils
+
 
 def test_ntp_sync(local_salt_client):
     testname = os.path.basename(__file__).split('.')[0]
@@ -26,3 +28,38 @@
     assert not fail, 'SaltMaster time: {}\n' \
                      'Nodes with time mismatch:\n {}'.format(saltmaster_time,
                                                              fail)
+
+
+def test_ntp_peers_state(local_salt_client):
+    """Test gets ntpq peers state and check the system peer is declared"""
+
+    active_nodes = utils.get_active_nodes(os.path.basename(__file__))
+    state = local_salt_client.cmd(
+        utils.list_to_target_string(active_nodes, 'or'),
+        'cmd.run',
+        ['ntpq -pn'],
+        expr_form='compound')
+    final_result = {}
+    for node in state:
+        sys_peer_declared = False
+        ntpq_output = state[node].split('\n')
+        # if output has no 'remote' in the head of ntpq output
+        # the 'ntqp -np' command failed and cannot check peers
+        if 'remote' not in ntpq_output[0]:
+            final_result[node] = ntpq_output
+            continue
+
+        # take 3rd+ line of output (the actual peers)
+        try:
+            peers = ntpq_output[2:]
+        except IndexError:
+            final_result[node] = ntpq_output
+            continue
+        for p in peers:
+            if p.split()[0].startswith("*"):
+                sys_peer_declared = True
+        if not sys_peer_declared:
+            final_result[node] = ntpq_output
+    assert not final_result,\
+        "NTP peers state is not expected on some nodes, could not find " \
+        "declared system peer:\n{}".format(json.dumps(final_result, indent=4))