Functional tests development 43/50943/3
authorThomas Duval <thomas.duval@orange.com>
Mon, 22 Jan 2018 14:51:03 +0000 (15:51 +0100)
committerThomas Duval <thomas.duval@orange.com>
Wed, 24 Jan 2018 08:55:31 +0000 (09:55 +0100)
Change-Id: I641522d813f171a4eed0e6b39b2f25cc470c76d1

moon_forming/Dockerfile
moon_forming/README.md
moon_forming/config_moon.sh [moved from moon_forming/run.sh with 90% similarity]
moon_forming/switch.sh [new file with mode: 0644]
moon_manager/tests/functional_pod/conftest.py [new file with mode: 0644]
moon_manager/tests/functional_pod/run_functional_tests.sh
moon_manager/tests/functional_pod/test_manager.py [new file with mode: 0644]
moon_manager/tests/functional_pod/test_models.py [new file with mode: 0644]
tests/functional/run_tests.sh
tests/functional/run_tests_for_component.sh [new file with mode: 0644]
tools/moon_kubernetes/templates/moon_forming_functest.yaml [new file with mode: 0644]

index ca0eba7..74616c8 100644 (file)
@@ -1,11 +1,11 @@
 FROM python:3
 
 WORKDIR /usr/src/app
-RUN pip install --no-cache-dir --upgrade requests pyyaml python_moonutilities python_moondb python_moonclient
+RUN pip install --no-cache-dir --upgrade requests pytest pyyaml python_moonutilities python_moondb python_moonclient
 
-ENV POPULATE_ARGS "-v"
+ENV COMMAND "config"
 
 ADD . /root
 WORKDIR /root
 
-CMD sh /root/run.sh ${POPULATE_ARGS}
\ No newline at end of file
+CMD /bin/bash /root/switch.sh ${COMMAND}
index cc08f67..9b755d9 100644 (file)
@@ -39,6 +39,9 @@ kubectl delete -f $MOON_HOME/tools/moon_kubernetes/templates/moon_forming.yaml
 kubectl create -f $MOON_HOME/tools/moon_kubernetes/templates/moon_forming.yaml
 ```
 
+## Functional tests
 
-
-
+```bash
+cd $MOON_HOME/moon_manager
+bash ../tests/functional/run_tests_for_component.sh
+```
similarity index 90%
rename from moon_forming/run.sh
rename to moon_forming/config_moon.sh
index d731cb1..0a55898 100644 (file)
@@ -37,7 +37,3 @@ while ! python -c "import requests; req = requests.get('http://manager:8082')" 2
 done
 echo "."
 echo "Manager (http://manager:8082) is up."
-
-#for i in /data/*.py ; do
-#    moon_populate_values $populate_args --consul-host=consul --consul-port=8500 $i
-#done
diff --git a/moon_forming/switch.sh b/moon_forming/switch.sh
new file mode 100644 (file)
index 0000000..adb1ebe
--- /dev/null
@@ -0,0 +1,19 @@
+#!/usr/bin/env bash
+
+CMD=$1
+
+echo "COMMAND IS ${CMD}"
+
+if [ "${CMD}" = "functest" ]; then
+    echo "FUNCTIONAL TESTS"
+    ls -l /data
+    ls -l /data/tests
+    sh /data/tests/functional_pod/run_functional_tests.sh
+#elif [ $CMD == "unittest" ]; then
+#    sh /data/tests/functional_pod/run_functional_tests.sh
+else
+    echo "CONFIGURATION"
+    bash config_moon.sh
+fi
+
+echo "<END OF JOB>"
\ No newline at end of file
diff --git a/moon_manager/tests/functional_pod/conftest.py b/moon_manager/tests/functional_pod/conftest.py
new file mode 100644 (file)
index 0000000..b581175
--- /dev/null
@@ -0,0 +1,12 @@
+import pytest
+
+print("ANALYSING CONFTEST")
+
+
+@pytest.fixture
+def context():
+    print("CREATING CONTEXT")
+    yield {
+        "hostname": "manager",
+        "port": 8082,
+    }
index c80bf15..7a95a49 100644 (file)
@@ -1,11 +1,4 @@
 #!/usr/bin/env bash
 
-set -x
-
-kubectl create -n moon -f tools/moon_kubernetes/templates/moon_forming.yaml
-
-echo Waiting for jobs forming
-sleep 5
-kubectl get jobs -n moon
-kubectl logs -n moon jobs/forming
-
+cd /data/tests/functional_pod
+pytest .
diff --git a/moon_manager/tests/functional_pod/test_manager.py b/moon_manager/tests/functional_pod/test_manager.py
new file mode 100644 (file)
index 0000000..aab5fba
--- /dev/null
@@ -0,0 +1,77 @@
+import json
+import requests
+
+
+def get_json(data):
+    return json.loads(data.decode("utf-8"))
+
+
+def get_pdp(context):
+    req = requests.get("http://{}:{}/pdp".format(
+        context.get("hostname"),
+        context.get("port")),
+        timeout=3)
+    pdp = req.json()
+    return req, pdp
+
+
+def add_pdp(context, data):
+    req = requests.post("http://{}:{}/pdp".format(
+        context.get("hostname"),
+        context.get("port")),
+        data=json.dumps(data),
+        headers={'Content-Type': 'application/json'},
+        timeout=3)
+    pdp = req.json()
+    return req, pdp
+
+
+def delete_pdp(context, key):
+    req = requests.delete("http://{}:{}/pdp/{}".format(
+        context.get("hostname"),
+        context.get("port"), key),
+        timeout=3)
+    return req
+
+
+def delete_pdp_without_id(context):
+    req = requests.delete("http://{}:{}/pdp/{}".format(
+        context.get("hostname"),
+        context.get("port"), ""),
+        timeout=3)
+    return req
+
+
+def test_get_pdp(context):
+    req, pdp = get_pdp(context)
+    assert req.status_code == 200
+    assert isinstance(pdp, dict)
+    assert "pdps" in pdp
+
+
+def test_add_pdp(context):
+    data = {
+        "name": "testuser",
+        "security_pipeline": ["policy_id_1", "policy_id_2"],
+        "keystone_project_id": "keystone_project_id",
+        "description": "description of testuser"
+    }
+    req, pdp = add_pdp(context, data)
+    assert req.status_code == 200
+    assert isinstance(pdp, dict)
+    value = list(pdp["pdps"].values())[0]
+    assert "pdps" in pdp
+    assert value['name'] == "testuser"
+    assert value["description"] == "description of {}".format("testuser")
+    assert value["keystone_project_id"] == "keystone_project_id"
+
+
+def test_delete_pdp(context):
+    request, pdp = get_pdp(context)
+    success_req = None
+    for key, value in pdp['pdps'].items():
+        if value['name'] == "testuser":
+            success_req = delete_pdp(context, key)
+            break
+    assert success_req
+    assert success_req.status_code == 200
diff --git a/moon_manager/tests/functional_pod/test_models.py b/moon_manager/tests/functional_pod/test_models.py
new file mode 100644 (file)
index 0000000..dcda9f3
--- /dev/null
@@ -0,0 +1,78 @@
+import json
+import requests
+
+
+def get_models(context):
+    req = requests.get("http://{}:{}/models".format(
+        context.get("hostname"),
+        context.get("port")),
+        timeout=3)
+    models = req.json()
+    return req, models
+
+
+def add_models(context, name):
+    data = {
+        "name": name,
+        "description": "description of {}".format(name),
+        "meta_rules": ["meta_rule_id1", "meta_rule_id2"]
+    }
+    req = requests.post("http://{}:{}/models".format(
+        context.get("hostname"),
+        context.get("port")),
+        data=json.dumps(data),
+        headers={'Content-Type': 'application/json'},
+        timeout=3)
+    models = req.json()
+    return req, models
+
+
+def delete_models(context, name):
+    _, models = get_models(context)
+    request = None
+    for key, value in models['models'].items():
+        if value['name'] == name:
+            request = requests.delete("http://{}:{}/models/{}".format(key,
+                                      context.get("hostname"),
+                                      context.get("port")),
+                                      timeout=3)
+            break
+    return request
+
+
+def delete_models_without_id(context):
+    req = requests.delete("http://{}:{}/models/{}".format(
+                          context.get("hostname"),
+                          context.get("port"),
+                          ""),
+                          timeout=3)
+    return req
+
+
+def test_get_models(context):
+    req, models = get_models(context)
+    assert req.status_code == 200
+    assert isinstance(models, dict)
+    assert "models" in models
+
+
+def test_add_models(context):
+    req, models = add_models(context, "testuser")
+    assert req.status_code == 200
+    assert isinstance(models, dict)
+    value = list(models["models"].values())[0]
+    assert "models" in models
+    assert value['name'] == "testuser"
+    assert value["description"] == "description of {}".format("testuser")
+    assert value["meta_rules"][0] == "meta_rule_id1"
+
+
+def test_delete_models(context):
+    req = delete_models(context, "testuser")
+    assert req.status_code == 200
+
+
+def test_delete_models_without_id(context):
+    req = delete_models_without_id(context)
+    assert req.status_code == 500
+
index ced0f9f..c5cbabb 100644 (file)
@@ -1,3 +1,13 @@
 #!/usr/bin/env bash
 
 echo "starting Moon Functional Tests"
+
+COMPONENTS="moon_authz, moon_interface, moon_manager, moon_orchestrator, moon_wrapper"
+
+for dir in ${COMPONENTS}; do
+    echo "Testing component ${dir}"
+    cd ${MOON_HOME}/${dir}
+    docker run --rm --volume $(pwd):/data wukongsun/moon_forming:latest /bin/bash /root/switch.sh functest
+done
+
+# TODO: download tests results
diff --git a/tests/functional/run_tests_for_component.sh b/tests/functional/run_tests_for_component.sh
new file mode 100644 (file)
index 0000000..fd9ab7f
--- /dev/null
@@ -0,0 +1,27 @@
+#!/usr/bin/env bash
+
+CUR_PWD=$(pwd)
+INPUT_FILE=../tools/moon_kubernetes/templates/moon_forming_functest.yaml
+OUTPUT_FILE=tests/functional_pod/moon_forming_functest.yaml
+
+echo current working directory: ${CUR_PWD}
+
+cat ${INPUT_FILE} | sed "s|{{PATH}}|${CUR_PWD}|" > ${OUTPUT_FILE}
+
+kubectl create -f ${OUTPUT_FILE}
+
+sleep 5
+kubectl get -n moon jobs
+echo OUTPUT is $?
+if [ "$?" -ne 0 ]
+then
+    sleep 5
+    kubectl get -n moon jobs
+fi
+
+echo "waiting for FuncTests (it may takes time)..."
+echo -e "\033[35m"
+sed '/<END OF JOB>/q' <(kubectl logs -n moon jobs/functest -f)
+echo -e "\033[m"
+
+kubectl delete -f ${OUTPUT_FILE}
diff --git a/tools/moon_kubernetes/templates/moon_forming_functest.yaml b/tools/moon_kubernetes/templates/moon_forming_functest.yaml
new file mode 100644 (file)
index 0000000..4cb2c3a
--- /dev/null
@@ -0,0 +1,30 @@
+apiVersion: batch/v1
+kind: Job
+metadata:
+  name: functest
+  namespace: moon
+spec:
+  template:
+    metadata:
+      name: functest
+    spec:
+      containers:
+      - name: functest
+        image: wukongsun/moon_forming:dev
+        env:
+        - name: COMMAND
+          value: "functest"
+        volumeMounts:
+        - name: config-volume
+          mountPath: /etc/moon
+        - name: tests-volume
+          mountPath: /data
+      volumes:
+      - name: config-volume
+        configMap:
+          name: moon-config
+      - name: tests-volume
+        hostPath:
+          path: "{{PATH}}"
+      restartPolicy: Never
+      #backoffLimit: 4
\ No newline at end of file