bug fix case name for VNFonboarding
[functest.git] / functest / opnfv_tests / vnf / ims / opera_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='opera_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         # deploy open-O from Functest docker located on the Jumphost
39         # you have admin rights on OpenStack SUT
40         # you can cretae a VM, spawn docker on the jumphost
41         # spawn docker on a VM in the SUT, ..up to you
42         #
43         # note: this step can be ignored
44         # if Open-O is part of the installer
45         self.logger.info("Deploy orchestrator: OK")
46
47     def deploy_vnf(self):
48         # TODO
49         self.logger.info("Deploy VNF: OK")
50
51     def test_vnf(self):
52         # Adaptations probably needed
53         # code used for cloudify_ims
54         # ruby client on jumphost calling the vIMS on the SUT
55         script = "source {0}venv_cloudify/bin/activate; "
56         script += "cd {0}; "
57         script += "cfy status | grep -Eo \"([0-9]{{1,3}}\.){{3}}[0-9]{{1,3}}\""
58         cmd = "/bin/bash -c '" + script.format(self.data_dir) + "'"
59
60         try:
61             self.logger.debug("Trying to get clearwater manager IP ... ")
62             mgr_ip = os.popen(cmd).read()
63             mgr_ip = mgr_ip.splitlines()[0]
64         except:
65             self.step_failure("Unable to retrieve the IP of the "
66                               "cloudify manager server !")
67
68         api_url = "http://" + mgr_ip + "/api/v2"
69         dep_outputs = requests.get(api_url + "/deployments/" +
70                                    self.vnf.deployment_name + "/outputs")
71         dns_ip = dep_outputs.json()['outputs']['dns_ip']
72         ellis_ip = dep_outputs.json()['outputs']['ellis_ip']
73
74         ellis_url = "http://" + ellis_ip + "/"
75         url = ellis_url + "accounts"
76
77         params = {"password": "functest",
78                   "full_name": "opnfv functest user",
79                   "email": "functest@opnfv.fr",
80                   "signup_code": "secret"}
81
82         rq = requests.post(url, data=params)
83         i = 20
84         while rq.status_code != 201 and i > 0:
85             rq = requests.post(url, data=params)
86             i = i - 1
87             time.sleep(10)
88
89         if rq.status_code == 201:
90             url = ellis_url + "session"
91             rq = requests.post(url, data=params)
92             cookies = rq.cookies
93
94         url = ellis_url + "accounts/" + params['email'] + "/numbers"
95         if cookies != "":
96             rq = requests.post(url, cookies=cookies)
97             i = 24
98             while rq.status_code != 200 and i > 0:
99                 rq = requests.post(url, cookies=cookies)
100                 i = i - 1
101                 time.sleep(25)
102
103         if rq.status_code != 200:
104             self.step_failure("Unable to create a number: %s"
105                               % rq.json()['reason'])
106
107         nameservers = ft_utils.get_resolvconf_ns()
108         resolvconf = ""
109         for ns in nameservers:
110             resolvconf += "\nnameserver " + ns
111
112         if dns_ip != "":
113             script = ('echo -e "nameserver ' + dns_ip + resolvconf +
114                       '" > /etc/resolv.conf; ')
115             script += 'source /etc/profile.d/rvm.sh; '
116             script += 'cd {0}; '
117             script += ('rake test[{1}] SIGNUP_CODE="secret"')
118
119             cmd = ("/bin/bash -c '" +
120                    script.format(self.data_dir, self.inputs["public_domain"]) +
121                    "'")
122             output_file = "output.txt"
123             f = open(output_file, 'w+')
124             subprocess.call(cmd, shell=True, stdout=f,
125                             stderr=subprocess.STDOUT)
126             f.close()
127
128             f = open(output_file, 'r')
129             result = f.read()
130             if result != "":
131                 self.logger.debug(result)
132
133             vims_test_result = ""
134             tempFile = os.path.join(self.test_dir, "temp.json")
135             try:
136                 self.logger.debug("Trying to load test results")
137                 with open(tempFile) as f:
138                     vims_test_result = json.load(f)
139                 f.close()
140             except:
141                 self.logger.error("Unable to retrieve test results")
142
143             try:
144                 os.remove(tempFile)
145             except:
146                 self.logger.error("Deleting file failed")
147
148             if vims_test_result != '':
149                 return {'status': 'PASS', 'result': vims_test_result}
150             else:
151                 return {'status': 'FAIL', 'result': ''}
152
153     def clean(self):
154         # TODO
155         super(ImsVnf, self).clean()