Update INFO.yaml with new PTL
[testsuite/python-testing-utils.git] / robotframework-onap / ONAPLibrary / VVPValidation.py
1 # Copyright 2019 AT&T Intellectual Property. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from robot.api.deco import keyword
15 import os
16 import subprocess
17
18 VVP_BRANCH = "master"
19 VVP_URL = "https://gerrit.onap.org/r/vvp/validation-scripts"
20
21
22 # use this to import and run validation from robot
23 class HeatValidationScripts:
24     def __init__(self):
25         pass
26
27     @keyword
28     def validate(self, build_dir, template_directory, output_directory):
29         """
30         keyword invoked by robot to execute VVP validation scripts
31
32         :build_dir:                 directory to install virtualenv
33                                     and clone validation scripts
34         :template_directory:        directory with heat templates
35         :output_directory:          directory to store output files
36         """
37         t = VVP(build_dir, template_directory, output_directory)
38         t.install_requirements()
39         status = t.run_vvp()
40
41         return status
42
43
44 class VVP:
45     def __init__(self, build_dir, template_directory, output_directory):
46         self._build_dir = build_dir
47         self.initialize()
48
49         self.virtualenv = "{}/test_env".format(build_dir)
50         self.vvp = "{}/validation_scripts".format(build_dir)
51         self.template_directory = template_directory
52         self.output_directory = output_directory
53
54     def initialize(self):
55         self.create_venv(self._build_dir)
56         self.clone_vvp(self._build_dir)
57
58     def create_venv(self, build_dir):
59         try:
60             subprocess.call(
61                 ["python3.7", "-m", "virtualenv", "--clear", "{}/test_env".format(build_dir)]
62             )
63         except OSError as e:
64             print("error creating virtual environment for vvp {}".format(e))
65             raise
66
67     def clone_vvp(self, build_dir):
68         if not os.path.exists("{}/validation_scripts".format(build_dir)):
69             try:
70                 subprocess.call(
71                     [
72                         "git",
73                         "clone",
74                         "-b",
75                         VVP_BRANCH,
76                         VVP_URL,
77                         "{}/validation_scripts".format(build_dir),
78                     ]
79                 )
80             except OSError as e:
81                 print("error cloning vvp validation scripts {}".format(e))
82                 raise
83
84     def install_requirements(self):
85         try:
86             subprocess.call(
87                 [
88                     "{}/bin/python".format(self.virtualenv),
89                     "-m",
90                     "pip",
91                     "install",
92                     "--upgrade",
93                     "pip",
94                     "wheel",
95                 ]
96             )
97             subprocess.call(
98                 [
99                     "{}/bin/python".format(self.virtualenv),
100                     "-m",
101                     "pip",
102                     "install",
103                     "wheel",
104                     "-r",
105                     "{}/requirements.txt".format(self.vvp),
106                 ]
107             )
108         except OSError as e:
109             print("error installing vvp requirements {}".format(e))
110             raise
111
112     def run_vvp(self):
113         try:
114             ret = subprocess.call(
115                 [
116                     "{}/bin/python".format(self.virtualenv),
117                     "-m",
118                     "pytest",
119                     "--rootdir={}/ice_validator/".format(self.vvp),
120                     "--template-directory={}".format(self.template_directory),
121                     "--output-directory={}".format(self.output_directory),
122                     "{}/ice_validator/tests/".format(self.vvp),
123                 ]
124             )
125         except OSError as e:
126             print("error running vvp validation scripts {}".format(e))
127             raise
128
129         if ret != 0:
130             raise ValueError("Validation Script error detected")