#!/usr/bin/env python

# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

"""Refstack testcase implementation."""

import logging
import os
import shutil
import time

from refstack_client import list_parser
from xtesting.core import testcase
from xtesting.energy import energy

from functest.opnfv_tests.openstack.tempest import tempest
from functest.utils import config


class Refstack(tempest.TempestCommon):
    """Refstack testcase implementation class."""

    __logger = logging.getLogger(__name__)

    defcorelist = os.path.join(
        getattr(config.CONF, 'dir_refstack_data'), 'defcore.txt')

    def __init__(self, **kwargs):
        if "case_name" not in kwargs:
            kwargs["case_name"] = 'refstack'
        super(Refstack, self).__init__(**kwargs)
        self.res_dir = os.path.join(
            getattr(config.CONF, 'dir_results'), 'refstack')
        self.list = os.path.join(self.res_dir, 'tempest-list.txt')

    @energy.enable_recording
    def run(self, **kwargs):
        """Start Refstack testcase."""
        self.start_time = time.time()
        try:
            self.configure()
            parser = list_parser.TestListParser(
                getattr(config.CONF, 'dir_repo_tempest'))
            nfile = parser.get_normalized_test_list(Refstack.defcorelist)
            shutil.copyfile(nfile, self.list)
            self.run_verifier_tests()
            self.parse_verifier_result()
            self.generate_report()
            res = testcase.TestCase.EX_OK
        except Exception:  # pylint: disable=broad-except
            self.__logger.exception('Error with run')
            res = testcase.TestCase.EX_RUN_ERROR
        finally:
            self.resources.cleanup()
        self.stop_time = time.time()
        return res

@@ -406,20 +406,8 @@
 util
-%0Aimport time
 %0A%0Afr
@@ -447,78 +447,8 @@
 rser
-%0Afrom xtesting.core import testcase%0Afrom xtesting.energy import energy
 %0A%0Afr
@@ -1113,185 +1113,38 @@
     
-@energy.enable_recording%0A    def run(self, **kwargs):%0A        %22%22%22Start Refstack testcase.%22%22%22%0A        self.start_time = time.time()%0A        try:%0A            self.configure()%0A    
+def generate_test_list(self):%0A
     
@@ -1188,36 +1188,32 @@
 er(%0A            
-  
-  
 getattr(config.C
@@ -1238,20 +1238,16 @@
 pest'))%0A
-    
         
@@ -1316,20 +1316,16 @@
         
-   
- 
 shutil.c
@@ -1354,431 +1354,57 @@
 st)%0A
-            self.run_verifier_tests()%0A            self.parse_verifier_result()%0A            self.generate_report()%0A            res = testcase.TestCase.EX_OK%0A        except Exception:  # pylint: disable=broad-except%0A            self.__logger.exception('Error with run')%0A            res = testcase.TestCase.EX_RUN_ERROR%0A        finally:%0A            self.resources.cleanup()%0A        self.stop_time = time.time()%0A        return re
+%0A    def apply_tempest_blacklist(self):%0A        pas
 s%0A

Refactor refstack to limit instructions