<commit_before>'''
Copyright 2017 Dell Inc. or its subsidiaries. All Rights Reserved.

This script tests arbitrary payload of the RackHD API 2.0 OS bootstrap workflows.
The default case is running a minimum payload ESXi 6.0 OS install.
Other ESXi OS install cases can be specified by creating a payload file and specifiying it using the '-extra' argument.
This test takes 15-20 minutes to run.

Example payload file (installed in configuration dir):

{"bootstrap-payload":
    {"name": "Graph.InstallESXi",
        "options": {"defaults": {
        "version": "6",
        "repo": "http://172.31.128.1:8080/ESXi/6.0",
        "rootPassword": "<PASSWORD>",
        "hostname": "rackhdnode",
        "users": [{"name": "rackhduser",
                   "password": "<PASSWORD>!",
                   "uid": 1010}]}}}
}

Example command line using external payload file:

python run_tests.py -stack 4 -test tests/bootstrap/test_api20_esxi_bootstrap.py -extra base_esxi_55_install.json

'''

import fit_path  # NOQA: unused import
from nose.plugins.attrib import attr
import fit_common
import flogging
import random
import time
from nosedep import depends
log = flogging.get_loggers()

# sample default base payload
PAYLOAD = {"name": "Graph.InstallESXi",
           "options": {"defaults": {"version": "6",
                                    "repo": "http://172.31.128.1:8080/ESXi/6.0",
                                    "rootPassword": "<PASSWORD>",
                                    "hostname": "rackhdnode",
                                    "users": [{"name": "rackhduser",
                                               "password": "<PASSWORD>!",
                                               "uid": 1010}]}}}
# if an external payload file is specified, use that
config = fit_common.fitcfg().get('bootstrap-payload', None)
if config:
    PAYLOAD = config


# this routine polls a workflow task ID for completion
def wait_for_workflow_complete(instanceid, start_time, waittime=900, cycle=30):
    log.info_5(" Workflow started at time: " + str(start_time))
    while start_time - time.time() < waittime:  # limit test to 30 minutes
        result = fit_common.rackhdapi("/api/2.0/workflows/" + instanceid)
        if result['status'] != 200:
            log.error(" HTTP error: " + result['text'])
            return False
        if result['json']['status'] in ['running', 'pending']:
            log.info_5("{} workflow status: {}".format(result['json']['injectableName'], result['json']['status']))
            fit_common.time.sleep(cycle)
        elif result['json']['status'] == 'succeeded':
            log.info_5("{} workflow status: {}".format(result['json']['injectableName'], result['json']['status']))
            log.info_5(" Workflow completed at time: " + str(time.time()))
            return True
        else:
            log.error(" Workflow failed: status: %s text: %s", result['json']['status'], result['text'])
            return False
    log.error(" Workflow Timeout: " + result['text'])
    return False


# ------------------------ Tests -------------------------------------


@attr(all=False)
class api20_bootstrap_esxi(fit_common.unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # Get the list of nodes
        NODECATALOG = fit_common.node_select()<commit_msg>Fix start_time bug<commit_after>'''
Copyright 2017 Dell Inc. or its subsidiaries. All Rights Reserved.

This script tests arbitrary payload of the RackHD API 2.0 OS bootstrap workflows.
The default case is running a minimum payload ESXi 6.0 OS install.
Other ESXi OS install cases can be specified by creating a payload file and specifiying it using the '-extra' argument.
This test takes 15-20 minutes to run.

Example payload file (installed in configuration dir):

{"bootstrap-payload":
    {"name": "Graph.InstallESXi",
        "options": {"defaults": {
        "version": "6",
        "repo": "http://172.31.128.1:8080/ESXi/6.0",
        "rootPassword": "<PASSWORD>",
        "hostname": "rackhdnode",
        "users": [{"name": "rackhduser",
                   "password": "<PASSWORD>!",
                   "uid": 1010}]}}}
}

Example command line using external payload file:

python run_tests.py -stack 4 -test tests/bootstrap/test_api20_esxi_bootstrap.py -extra base_esxi_55_install.json

'''

import fit_path  # NOQA: unused import
from nose.plugins.attrib import attr
import fit_common
import flogging
import random
import time
from nosedep import depends
log = flogging.get_loggers()

# sample default base payload
PAYLOAD = {"name": "Graph.InstallESXi",
           "options": {"defaults": {"version": "6",
                                    "repo": "http://172.31.128.1:8080/ESXi/6.0",
                                    "rootPassword": "<PASSWORD>",
                                    "hostname": "rackhdnode",
                                    "users": [{"name": "rackhduser",
                                               "password": "<PASSWORD>!",
                                               "uid": 1010}]}}}
# if an external payload file is specified, use that
config = fit_common.fitcfg().get('bootstrap-payload', None)
if config:
    PAYLOAD = config


# this routine polls a workflow task ID for completion
def wait_for_workflow_complete(instanceid, start_time, waittime=1800, cycle=30):
    log.info_5(" Workflow started at time: " + str(start_time))
    while time.time() - start_time < waittime:  # limit test to waittime seconds
        result = fit_common.rackhdapi("/api/2.0/workflows/" + instanceid)
        if result['status'] != 200:
            log.error(" HTTP error: " + result['text'])
            return False
        if result['json']['status'] in ['running', 'pending']:
            log.info_5("{} workflow status: {}".format(result['json']['injectableName'], result['json']['status']))
            fit_common.time.sleep(cycle)
        elif result['json']['status'] == 'succeeded':
            log.info_5("{} workflow status: {}".format(result['json']['injectableName'], result['json']['status']))
            log.info_5(" Workflow completed at time: " + str(time.time()))
            return True
        else:
            log.error(" Workflow failed: status: %s text: %s", result['json']['status'], result['text'])
            return False
    log.error(" Workflow Timeout: " + result['text'])
    return False


# ------------------------ Tests -------------------------------------


@attr(all=False)
class api20_bootstrap_esxi(fit_common.unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # Get the list of nodes
        NODECATALOG = fit_common.node_select()
{'fraction_non_alphanumeric': 0.15081276339554486, 'fraction_numerical': 0.0224262492474413, 'mean_word_length': 2.409440738840431, 'pattern_counts': {'":': 46, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3625504', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1802, 'n_words': 537}