aboutsummaryrefslogtreecommitdiffstats
path: root/ovp_testsuite/ovp-report.py
blob: 66745b12cbe1adb7907a6de4a4299125eb1d949c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# -*- coding: utf8 -*-
# ============LICENSE_START=======================================================
# org.onap.vvp/validation-scripts
# ===================================================================
# Copyright © 2020 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the "License");
# you may not use this software except in compliance with the License.
# You may obtain a copy of the License at
#
#             http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the "License");
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ============LICENSE_END============================================
import datetime
import hashlib
import json
import os
import argparse
import time
import uuid

from copy import deepcopy
from zipfile import ZipFile

OUTPUT_DATA = {
    "vnf_checksum": "",
    "build_tag": str(uuid.uuid4()),
    "version": "2020.r1",
    "test_date": "",
    "duration": "",
    "vnf_type": "heat",
    "testcases_list": [
        {
            "mandatory": "true",
            "name": "onap-vvp.validate.heat",
            "result": "NOT_STARTED",
            "objective": "onap heat template validation",
            "sub_testcase": [],
            "portal_key_file": "report.json",
        },
        {
            "mandatory": "true",
            "name": "onap-vvp.lifecycle_validate.heat",
            "result": "NOT_STARTED",
            "objective": "onap vnf lifecycle validation",
            "sub_testcase": [
                {"name": "model-and-distribute", "result": "NOT_STARTED"},
                {"name": "instantiation", "result": "NOT_STARTED"},
            ],
            "portal_key_file": "log.txt",
        },
        {
            "mandatory": "true",
            "name": "stack_validation",
            "result": "NOT_STARTED",
            "objective": "onap vnf openstack validation",
            "sub_testcase": [],
            "portal_key_file": "stack-validation.json",
        },
    ],
}


class OVPListener:
    def __init__(
        self,
        template_directory,
        start_time,
        test_date
    ):
        self.report = deepcopy(OUTPUT_DATA)
        self.test_date = test_date
        self.build_directory = template_directory
        self.output_directory = "/tmp"
        self.start_time = int(start_time)

        self.initialize()

    def initialize(self):
        self.template_directory = self.build_directory
        self.report["vnf_checksum"] = sha256(self.template_directory)
        self.report["test_date"] = self.test_date
        self.report["duration"] = (int(time.time()) - self.start_time)

    def update_validation_scripts(self, status):
        self.report["testcases_list"][0]["result"] = status

    def update_model_and_distribute(self, status):
        self.report["testcases_list"][1]["sub_testcase"][0]["result"] = status
        self.report["testcases_list"][1]["sub_testcase"][1]["result"] = status
        self.report["testcases_list"][1]["result"] = status

    def update_stack_validation(self, status):
        self.report["testcases_list"][2]["result"] = status

    def close(self):
        with open("{}/results.json".format(self.output_directory), "w") as f:
            json.dump(self.report, f, indent=4)


def sha256(template_directory):
    heat_sha = None
    zip_file = "{}/tmp.zip".format(template_directory)
    onlyfiles = [f for f in os.listdir(template_directory) if os.path.isfile(os.path.join(template_directory, f))]

    with ZipFile(zip_file, 'w') as zipObj:
        for filename in onlyfiles:
            zipObj.write(os.path.join(template_directory, filename), arcname=filename)

    with open(zip_file, "rb") as f:
        bytes = f.read()
        heat_sha = hashlib.sha256(bytes).hexdigest()

    if os.path.exists(zip_file):
        os.remove(zip_file)

    return heat_sha

def main(
        template_directory,
        start_time,
        test_date,
        validation_scripts="NOT_STARTED",
        stack_validation="NOT_STARTED",
        model_and_distribute="NOT_STARTED",
    ):
    t = OVPListener(template_directory, start_time, test_date)
    t.update_validation_scripts(validation_scripts)
    t.update_model_and_distribute(model_and_distribute)
    t.update_stack_validation(stack_validation)
    t.close()

if __name__ == "__main__":
  parser = argparse.ArgumentParser(description="Stack Validation Script")

  parser.add_argument(
      "--validation-scripts",
      required=False,
      choices=["SUCCESS", "FAIL", "NOT_STARTED"],
      default="NOT_STARTED",
      help="Validation Script Status.",
  )

  parser.add_argument(
      "--model-and-distribute",
      required=False,
      choices=["SUCCESS", "FAIL", "NOT_STARTED"],
      default="NOT_STARTED",
      help="Modeling Script Status.",
  )

  parser.add_argument(
      "--stack-validation",
      required=False,
      choices=["SUCCESS", "FAIL", "NOT_STARTED"],
      default="NOT_STARTED",
      help="Stack Validation Script Status.",
  )

  parser.add_argument(
      "--test-date",
      required=True,
      help="Test Date.",
  )

  parser.add_argument(
      "--template-directory",
      required=True,
      help="Heat Template Directory.",
  )

  parser.add_argument(
      "--start-time",
      required=True,
      help="Epoch start time.",
  )

  arguments = parser.parse_args()

  status = main(
    arguments.template_directory,
    arguments.start_time,
    arguments.test_date,
    validation_scripts=arguments.validation_scripts,
    stack_validation=arguments.stack_validation,
    model_and_distribute=arguments.model_and_distribute,
  )