Merge "Reverted the file permission"
[functest.git] / functest / opnfv_tests / vnf / ims / orchestra_ims.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 import json
11 import os
12 import requests
13 import subprocess
14 import time
15
16 import functest.core.vnf_base as vnf_base
17 import functest.utils.functest_logger as ft_logger
18 import functest.utils.functest_utils as ft_utils
19 from functest.utils.constants import CONST
20
21
22 class ImsVnf(vnf_base.VnfOnBoardingBase):
23
24     def __init__(self, project='functest', case='orchestra_ims',
25                  repo='', cmd=''):
26         super(ImsVnf, self).__init__(project, case, repo, cmd)
27         self.logger = ft_logger.Logger("vIMS").getLogger()
28         self.case_dir = os.path.join(CONST.functest_test, 'vnf/ims/')
29         self.data_dir = CONST.dir_vIMS_data
30         self.test_dir = CONST.dir_repo_vims_test
31
32         # vIMS Data directory creation
33         if not os.path.exists(self.data_dir):
34             os.makedirs(self.data_dir)
35
36     def deploy_orchestrator(self, **kwargs):
37         # TODO
38         # put your code here to deploy openbaton
39         # from the functest docker located on the jumphost
40         # you have admin rights on OpenStack SUT
41         # you can cretae a VM, spawn docker on the jumphost
42         # spawn docker on a VM in the SUT, ..up to you
43         #
44         # note: this step can be ignored
45         # if OpenBaton is part of the installer
46         self.logger.info("Deploy orchestrator: OK")
47
48     def deploy_vnf(self):
49         # deploy the VNF
50         # call openbaton to deploy the vIMS
51         self.logger.info("Deploy VNF: OK")
52
53     def test_vnf(self):
54         # Adaptations probably needed
55         # code used for cloudify_ims
56         # ruby client on jumphost calling the vIMS on the SUT
57         script = "source {0}venv_cloudify/bin/activate; "
58         script += "cd {0}; "
59         script += "cfy status | grep -Eo \"([0-9]{{1,3}}\.){{3}}[0-9]{{1,3}}\""
60         cmd = "/bin/bash -c '" + script.format(self.data_dir) + "'"
61
62         try:
63             self.logger.debug("Trying to get clearwater manager IP ... ")
64             mgr_ip = os.popen(cmd).read()
65             mgr_ip = mgr_ip.splitlines()[0]
66         except:
67             self.step_failure("Unable to retrieve the IP of the "
68                               "cloudify manager server !")
69
70         api_url = "http://" + mgr_ip + "/api/v2"
71         dep_outputs = requests.get(api_url + "/deployments/" +
72                                    self.vnf.deployment_name + "/outputs")
73         dns_ip = dep_outputs.json()['outputs']['dns_ip']
74         ellis_ip = dep_outputs.json()['outputs']['ellis_ip']
75
76         ellis_url = "http://" + ellis_ip + "/"
77         url = ellis_url + "accounts"
78
79         params = {"password": "functest",
80                   "full_name": "opnfv functest user",
81                   "email": "functest@opnfv.fr",
82                   "signup_code": "secret"}
83
84         rq = requests.post(url, data=params)
85         i = 20
86         while rq.status_code != 201 and i > 0:
87             rq = requests.post(url, data=params)
88             i = i - 1
89             time.sleep(10)
90
91         if rq.status_code == 201:
92             url = ellis_url + "session"
93             rq = requests.post(url, data=params)
94             cookies = rq.cookies
95
96         url = ellis_url + "accounts/" + params['email'] + "/numbers"
97         if cookies != "":
98             rq = requests.post(url, cookies=cookies)
99             i = 24
100             while rq.status_code != 200 and i > 0:
101                 rq = requests.post(url, cookies=cookies)
102                 i = i - 1
103                 time.sleep(25)
104
105         if rq.status_code != 200:
106             self.step_failure("Unable to create a number: %s"
107                               % rq.json()['reason'])
108
109         nameservers = ft_utils.get_resolvconf_ns()
110         resolvconf = ""
111         for ns in nameservers:
112             resolvconf += "\nnameserver " + ns
113
114         if dns_ip != "":
115             script = ('echo -e "nameserver ' + dns_ip + resolvconf +
116                       '" > /etc/resolv.conf; ')
117             script += 'source /etc/profile.d/rvm.sh; '
118             script += 'cd {0}; '
119             script += ('rake test[{1}] SIGNUP_CODE="secret"')
120
121             cmd = ("/bin/bash -c '" +
122                    script.format(self.data_dir, self.inputs["public_domain"]) +
123                    "'")
124             output_file = "output.txt"
125             f = open(output_file, 'w+')
126             subprocess.call(cmd, shell=True, stdout=f,
127                             stderr=subprocess.STDOUT)
128             f.close()
129
130             f = open(output_file, 'r')
131             result = f.read()
132             if result != "":
133                 self.logger.debug(result)
134
135             vims_test_result = ""
136             tempFile = os.path.join(self.test_dir, "temp.json")
137             try:
138                 self.logger.debug("Trying to load test results")
139                 with open(tempFile) as f:
140                     vims_test_result = json.load(f)
141                 f.close()
142             except:
143                 self.logger.error("Unable to retrieve test results")
144
145             try:
146                 os.remove(tempFile)
147             except:
148                 self.logger.error("Deleting file failed")
149
150             if vims_test_result != '':
151                 return {'status': 'PASS', 'result': vims_test_result}
152             else:
153                 return {'status': 'FAIL', 'result': ''}
154
155     def clean(self):
156         # TODO
157         super(ImsVnf, self).clean()