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