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