Added helpers to pytest tests
authorEduardo Ramos Testillano (eramedu) <eduardo.ramos.testillano@ericsson.com>
Sun, 17 May 2020 18:29:04 +0000 (20:29 +0200)
committerEduardo Ramos Testillano (eramedu) <eduardo.ramos.testillano@ericsson.com>
Sun, 17 May 2020 18:29:04 +0000 (20:29 +0200)
New fixture 'saveB64Artifact' which decodes base64 for some responses
and write xml (if it is xml) to file and json equivalent representation.

18 files changed:
.gitignore
example/diameter/launcher/resources/rest_api/ct/conftest.py
example/diameter/launcher/resources/rest_api/ct/helpers/__init__.py [new file with mode: 0644]
example/diameter/launcher/resources/rest_api/ct/helpers/save-answermsg_test.py [new file with mode: 0644]
example/diameter/launcher/resources/rest_api/ct/helpers/save-decode_test.py [new file with mode: 0644]
example/diameter/launcher/resources/rest_api/ct/helpers/save-loadmsg_test.py [new file with mode: 0644]
example/diameter/launcher/resources/rest_api/ct/helpers/save-node_test.py [new file with mode: 0644]
example/diameter/launcher/resources/rest_api/ct/helpers/save-show-oam_test.py [new file with mode: 0644]
example/diameter/launcher/resources/rest_api/ct/helpers/save-show-stats_test.py [new file with mode: 0644]
example/diameter/launcher/resources/rest_api/ct/helpers/save-test-look_test.py [new file with mode: 0644]
example/diameter/launcher/resources/rest_api/ct/helpers/save-test-summary-counts_test.py [new file with mode: 0644]
example/diameter/launcher/resources/rest_api/ct/helpers/save-test-summary-states_test.py [new file with mode: 0644]
example/diameter/launcher/resources/rest_api/ct/helpers/save-test-summary_test.py [new file with mode: 0644]
example/diameter/launcher/resources/rest_api/ct/pytest.ini
example/diameter/launcher/resources/rest_api/ct/requirements.txt
example/diameter/launcher/resources/rest_api/ct/resources/aaa-diameterJson-request.json
example/diameter/launcher/resources/rest_api/ct/resources/aaa.hex
example/diameter/launcher/resources/rest_api/ct/resources/aaa.xml

index e7396cc..ab64cc3 100644 (file)
@@ -36,4 +36,7 @@ CMakeDoxygenDefaults.cmake
 docker-images/anna-adml-http/opt/
 
 # Component test (pytest)
-__pycache__
+example/diameter/launcher/resources/rest_api/ct/**/__pycache__
+example/diameter/launcher/resources/rest_api/ct/helpers/*.xml
+example/diameter/launcher/resources/rest_api/ct/helpers/*.json
+example/diameter/launcher/resources/rest_api/ct/helpers/*.txt
index fd4f339..81f703b 100644 (file)
@@ -9,6 +9,7 @@ import logging
 import os
 import pytest
 import re
+import xmltodict
 
 #############
 # CONSTANTS #
@@ -128,6 +129,39 @@ def b64_decode():
     return message_bytes.decode('ascii')
   return decode
 
+@pytest.fixture(scope='session')
+def saveB64Artifact(b64_decode):
+  """
+  Decode base64 string provided to disk
+
+  base64_message: base64 encoded string
+  file_basename: file basename where to write
+  isXML: decoded data corresponds to xml string. In this case, also json equivalente representation is written to disk
+  """
+  def save_b64_artifact(base64_message, file_basename, isXML = True):
+
+    targetFile = file_basename + ".txt"
+    data = b64_decode(base64_message)
+
+    if isXML:
+      targetFile = file_basename + ".xml"
+
+    _file = open(targetFile, "w")
+    n = _file.write(data)
+    _file.close()
+
+    if isXML:
+      targetFile = file_basename + ".json"
+      xml_dict = xmltodict.parse(data)
+      data = json.dumps(xml_dict, indent = 4, sort_keys=True)
+
+      _file = open(targetFile, "w")
+      n = _file.write(data)
+      _file.close()
+
+  return save_b64_artifact
+
+
 # HTTP communication:
 class RestClient(object):
     """A client helper to perform rest operations: GET, POST.
diff --git a/example/diameter/launcher/resources/rest_api/ct/helpers/__init__.py b/example/diameter/launcher/resources/rest_api/ct/helpers/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/example/diameter/launcher/resources/rest_api/ct/helpers/save-answermsg_test.py b/example/diameter/launcher/resources/rest_api/ct/helpers/save-answermsg_test.py
new file mode 100644 (file)
index 0000000..c0976a6
--- /dev/null
@@ -0,0 +1,46 @@
+import pytest
+
+
+@pytest.mark.helpers
+def test_001_save_answermsg2e(saveB64Artifact, admlc):
+
+  requestBody = { "name":"afHost.afRealm.com" }
+  responseBodyRef = { "success":"true", "response":"Forced node is now 'afHost.afRealm.com'" }
+
+  # Send POST
+  response = admlc.postDict("/node", requestBody)
+
+  # Verify response
+  admlc.assert_response__status_body_headers(response, 200, responseBodyRef)
+
+  # Send POST
+  response = admlc.postDict("/answermsg2e", { "action":"" })
+
+  # Verify response
+  assert response["status"] == 200
+  assert response["body"]["success"] == "true"
+
+  # Save artifact from base64 encoded string received
+  saveB64Artifact(response["body"]["response"], "helpers/answermsg2e", False) # it is direct text content
+
+@pytest.mark.helpers
+def test_002_save_answermsg2c(saveB64Artifact, admlc):
+
+  requestBody = { "name":"ownHostId.operatorRealm.com" }
+  responseBodyRef = { "success":"true", "response":"Forced node is now 'ownHostId.operatorRealm.com'" }
+
+  # Send POST
+  response = admlc.postDict("/node", requestBody)
+
+  # Verify response
+  admlc.assert_response__status_body_headers(response, 200, responseBodyRef)
+
+  # Send POST
+  response = admlc.postDict("/answermsg2c", { "action":"" })
+
+  # Verify response
+  assert response["status"] == 200
+  assert response["body"]["success"] == "true"
+
+  # Save artifact from base64 encoded string received
+  saveB64Artifact(response["body"]["response"], "helpers/answermsg2c", False) # it is direct text content
diff --git a/example/diameter/launcher/resources/rest_api/ct/helpers/save-decode_test.py b/example/diameter/launcher/resources/rest_api/ct/helpers/save-decode_test.py
new file mode 100644 (file)
index 0000000..c4d7eab
--- /dev/null
@@ -0,0 +1,22 @@
+import pytest
+
+
+@pytest.mark.helpers
+def test_001_save_decoded(saveB64Artifact, b64_encode, resources, admlc):
+
+  diameterHex = resources("aaa.hex")
+  #requestBody = resources("aaa-diameterHex.json.in", diameterHex=diameterHex.rstrip())
+  requestBodyDict = { "diameterHex":"{}".format(diameterHex.rstrip()) }
+
+  # reponse field in response body, should carry the base64 encoding for xml message decoded by ADML:
+  xmlExpected = resources("aaa.xml")
+  responseBodyRef = { "success":"true", "response":"{}".format(b64_encode(xmlExpected)) }
+
+  # Send POST
+  response = admlc.postDict("/decode", requestBodyDict)
+
+  # Verify response
+  admlc.assert_response__status_body_headers(response, 200, responseBodyRef)
+
+  # Save artifact from base64 encoded string received
+  saveB64Artifact(response["body"]["response"], "helpers/decode")
diff --git a/example/diameter/launcher/resources/rest_api/ct/helpers/save-loadmsg_test.py b/example/diameter/launcher/resources/rest_api/ct/helpers/save-loadmsg_test.py
new file mode 100644 (file)
index 0000000..3a89500
--- /dev/null
@@ -0,0 +1,20 @@
+import pytest
+
+
+@pytest.mark.helpers
+def test_001_save_msg_loaded(saveB64Artifact, b64_encode, resources, admlc):
+
+  requestBody = resources("aar-diameterJson-request.json")
+
+  # reponse field in response body, should carry the base64 encoding for xml message loaded by ADML:
+  xmlExpected = resources("aar.xml")
+  responseBodyRef = { "success":"true", "response":"{}".format(b64_encode(xmlExpected)) }
+
+  # Send POST
+  response = admlc.post("/loadmsg", requestBody)
+
+  ### Verify response
+  admlc.assert_response__status_body_headers(response, 200, responseBodyRef)
+
+  # Save artifact from base64 encoded string received
+  saveB64Artifact(response["body"]["response"], "helpers/loadmsg")
diff --git a/example/diameter/launcher/resources/rest_api/ct/helpers/save-node_test.py b/example/diameter/launcher/resources/rest_api/ct/helpers/save-node_test.py
new file mode 100644 (file)
index 0000000..56034cf
--- /dev/null
@@ -0,0 +1,26 @@
+import pytest
+
+@pytest.mark.helpers
+def test_001_save_node(saveB64Artifact, admlc):
+
+  requestBody = { "name":"afHost.afRealm.com" }
+  responseBodyRef = { "success":"true", "response":"Forced node is now 'afHost.afRealm.com'" }
+
+  # Send POST
+  response = admlc.postDict("/node", requestBody)
+
+  # Verify response
+  admlc.assert_response__status_body_headers(response, 200, responseBodyRef)
+
+  requestBody = { "name":"" }
+
+  # Send POST
+  response = admlc.postDict("/node", requestBody)
+
+  # Verify response is ok (omit response content because it is informative and have dynamic data (timing)):
+  assert response["status"] == 200
+  assert response["body"]["success"] == "true"
+
+  # Save artifact from base64 encoded string received
+  saveB64Artifact(response["body"]["response"], "helpers/node")
+
diff --git a/example/diameter/launcher/resources/rest_api/ct/helpers/save-show-oam_test.py b/example/diameter/launcher/resources/rest_api/ct/helpers/save-show-oam_test.py
new file mode 100644 (file)
index 0000000..47bdf82
--- /dev/null
@@ -0,0 +1,16 @@
+import pytest
+
+
+@pytest.mark.helpers
+def test_001_save_oam(saveB64Artifact, admlc):
+
+  # Send GET
+  response = admlc.get("/show-oam")
+
+  # Verify response is ok (omit response content because it is informative and have dynamic data (timing)):
+  assert response["status"] == 200
+  assert response["body"]["success"] == "true"
+
+  # Save artifact from base64 encoded string received
+  saveB64Artifact(response["body"]["response"], "helpers/show-oam")
+
diff --git a/example/diameter/launcher/resources/rest_api/ct/helpers/save-show-stats_test.py b/example/diameter/launcher/resources/rest_api/ct/helpers/save-show-stats_test.py
new file mode 100644 (file)
index 0000000..cb63254
--- /dev/null
@@ -0,0 +1,16 @@
+import pytest
+
+
+@pytest.mark.helpers
+def test_001_save_statistics(saveB64Artifact, admlc):
+
+  # Send GET
+  response = admlc.get("/show-stats")
+
+  # Verify response is ok (omit response content because it is informative and have dynamic data (timing)):
+  assert response["status"] == 200
+  assert response["body"]["success"] == "true"
+
+  # Save artifact from base64 encoded string received
+  saveB64Artifact(response["body"]["response"], "helpers/show-stats")
+
diff --git a/example/diameter/launcher/resources/rest_api/ct/helpers/save-test-look_test.py b/example/diameter/launcher/resources/rest_api/ct/helpers/save-test-look_test.py
new file mode 100644 (file)
index 0000000..a7376b2
--- /dev/null
@@ -0,0 +1,17 @@
+import pytest
+
+
+@pytest.mark.dynamic_helpers
+@pytest.mark.xfail(reason="To be used after dynamic mark execution")
+def test_001_save_test_look(saveB64Artifact, admlc):
+
+  # Send POST
+  response = admlc.postDict("/test-look", { "id":5000 })
+
+  # Verify response is ok (omit response content because it is informative and have dynamic data (timing)):
+  assert response["status"] == 200
+  assert response["body"]["success"] == "true"
+
+  # Save artifact from base64 encoded string received
+  saveB64Artifact(response["body"]["response"], "helpers/test-look")
+
diff --git a/example/diameter/launcher/resources/rest_api/ct/helpers/save-test-summary-counts_test.py b/example/diameter/launcher/resources/rest_api/ct/helpers/save-test-summary-counts_test.py
new file mode 100644 (file)
index 0000000..98ccd18
--- /dev/null
@@ -0,0 +1,16 @@
+import pytest
+
+
+@pytest.mark.helpers
+def test_001_save_test_summary_counts(saveB64Artifact, admlc):
+
+  # Send GET
+  response = admlc.post("/test-summary-counts")
+
+  # Verify response is ok (omit response content because it is informative and have dynamic data (timing)):
+  assert response["status"] == 200
+  assert response["body"]["success"] == "true"
+
+  # Save artifact from base64 encoded string received
+  saveB64Artifact(response["body"]["response"], "helpers/test-summary-counts", False) # it is direct text content
+
diff --git a/example/diameter/launcher/resources/rest_api/ct/helpers/save-test-summary-states_test.py b/example/diameter/launcher/resources/rest_api/ct/helpers/save-test-summary-states_test.py
new file mode 100644 (file)
index 0000000..ad538b0
--- /dev/null
@@ -0,0 +1,16 @@
+import pytest
+
+
+@pytest.mark.helpers
+def test_001_save_test_summary_states(saveB64Artifact, admlc):
+
+  # Send GET
+  response = admlc.post("/test-summary-states")
+
+  # Verify response is ok (omit response content because it is informative and have dynamic data (timing)):
+  assert response["status"] == 200
+  assert response["body"]["success"] == "true"
+
+  # Save artifact from base64 encoded string received
+  saveB64Artifact(response["body"]["response"], "helpers/test-summary-states", False) # it is direct text content
+
diff --git a/example/diameter/launcher/resources/rest_api/ct/helpers/save-test-summary_test.py b/example/diameter/launcher/resources/rest_api/ct/helpers/save-test-summary_test.py
new file mode 100644 (file)
index 0000000..6488d03
--- /dev/null
@@ -0,0 +1,16 @@
+import pytest
+
+
+@pytest.mark.helpers
+def test_001_save_test_summary(saveB64Artifact, admlc):
+
+  # Send GET
+  response = admlc.post("/test-summary")
+
+  # Verify response is ok (omit response content because it is informative and have dynamic data (timing)):
+  assert response["status"] == 200
+  assert response["body"]["success"] == "true"
+
+  # Save artifact from base64 encoded string received
+  saveB64Artifact(response["body"]["response"], "helpers/test-summary")
+
index cd7b596..ccea138 100644 (file)
@@ -1,5 +1,5 @@
 [pytest]
-addopts = -v --junitxml=/tmp/junit.xml -n 0 -m "not dynamic"
+addopts = -v --junitxml=/tmp/junit.xml -n 0 -m "not dynamic and not helpers"
 
 log_format=%(asctime)s|%(name)s|%(filename)s:%(lineno)d|%(levelname)s|%(message)s
 log_date_format = %Y-%m-%d %H:%M:%S
@@ -21,3 +21,5 @@ junit_family = xunit1
 # Markers
 markers =
  dynamic: this test requires ADML dynamic test library enabled (/opt/adml/dynlibs)
+ helpers: auxiliary helpers to check ADML state and reports
+ dynamic_helpers: auxiliary helpers for dynamic context
index e087719..40ef15f 100644 (file)
@@ -8,7 +8,7 @@
         "@version": "1",
         "avp": [
            {
-              "@data": "tc_01_FullAVPs;afNodeHostname.afNodeHostRealm.com;1;859608",
+              "@data": "FullAVPs;afNodeHostname.afNodeHostRealm.com;1;859608",
               "@name": "Session-Id"
            },
            {
@@ -16,7 +16,7 @@
               "@name": "Auth-Application-Id"
            },
            {
-              "@data": "sapcOwnHostId.operatorRealm.com",
+              "@data": "ownHostId.operatorRealm.com",
               "@name": "Origin-Host"
            },
            {
index 08931a3..118d435 100644 (file)
@@ -1 +1 @@
-010000f80000010901000014000c73c30004cee4000001024000000c01000014000001074000004274635f30315f46756c6c415650733b61664e6f6465486f73746e616d652e61664e6f6465486f73745265616c6d2e636f6d3b313b38353936303800000000010840000027736170634f776e486f737449642e6f70657261746f725265616c6d2e636f6d000000010c4000000c000007d1000001164000000c551192a700000128400000196f70657261746f725265616c6d2e636f6d0000000000027480000038000028af0000010a4000000c000028af0000027580000010000028af000000010000027680000010000028af00000013
+010000ec0000010901000014000c73c30004cee4000001074000003c46756c6c415650733b61664e6f6465486f73746e616d652e61664e6f6465486f73745265616c6d2e636f6d3b313b383539363038000001024000000c0100001400000108400000236f776e486f737449642e6f70657261746f725265616c6d2e636f6d0000000128400000196f70657261746f725265616c6d2e636f6d0000000000010c4000000c000007d100000274c0000038000028af0000010a4000000c000028af0000027580000010000028af000000010000027680000010000028af00000013000001164000000c551192a7
\ No newline at end of file
index 8338a6a..8ffcd96 100644 (file)
@@ -1,7 +1,7 @@
 <message version="1" name="AA-Answer" application-id="16777236" hop-by-hop-id="816067" end-to-end-id="315108">
-   <avp name="Session-Id" data="tc_01_FullAVPs;afNodeHostname.afNodeHostRealm.com;1;859608"/>
+   <avp name="Session-Id" data="FullAVPs;afNodeHostname.afNodeHostRealm.com;1;859608"/>
    <avp name="Auth-Application-Id" data="16777236"/>
-   <avp name="Origin-Host" data="sapcOwnHostId.operatorRealm.com"/>
+   <avp name="Origin-Host" data="ownHostId.operatorRealm.com"/>
    <avp name="Origin-Realm" data="operatorRealm.com"/>
    <avp name="Result-Code" data="2001" alias="DIAMETER_SUCCESS"/>
    <avp name="Supported-Features">