<reponame>Cray-HPE/bos<filename>ct-tests/lib/bos_functional_test/helpers.py
# Copyright 2020-2021 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# (MIT License)

"""
Test helper functions
"""

from common.helpers import debug, info, raise_test_error
import datetime
import json

DEFAULT_TEMPLATE_PATH = "/opt/cray/tests/dat/cms/bos_functional_test/bos_session_template.json"

def generate_session_template():
    """
    Loads the default template from the JSON file, sets the name and description, and returns it
    """
    info("Loading default template from %s" % DEFAULT_TEMPLATE_PATH)
    with open(DEFAULT_TEMPLATE_PATH, "rt") as f:
        template_object = json.load(f)
    now = datetime.datetime.now()
    debug("Setting description field")
    template_object["description"] = "Generated by BOS functional test %s" % str(now)
    debug("Setting name field")
    template_object["name"] = "test-%s" % now.strftime("%Y%m%d-%H%M%S.%f")
    return template_object

def verify_template_list(template_map, template_name, should_exist):
    """
    If should_exist is true, verifies that the specified template appears in the list.
    If should_exist is false, verifies that it does not.
    """
    for tname in template_map.keys():
        if tname != template_name:
            continue
        elif should_exist:
            info("Found %s in template list" % template_name)
            return
        else:
            raise_test_error("Found %s in template list, but it should not be present" % template_name)
    if should_exist:
        raise_test_error("Did not find %s in template list, but it should have been present" % template_name)
    info("Confirmed that %s is not present in template list" % template_name)

# Copyright 2020-2021 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# (MIT License)

"""
Test helper functions
"""

from common.helpers import debug, info, raise_test_error
import datetime
import json

DEFAULT_TEMPLATE_PATH = "/opt/cray/tests/dat/cms/bos_functional_test/bos_session_template.json"

def generate_session_template():
    """
    Loads the default template from the JSON file, sets the name and description, and returns it
    """
    info("Loading default template from %s" % DEFAULT_TEMPLATE_PATH)
    with open(DEFAULT_TEMPLATE_PATH, "rt") as f:
        template_object = json.load(f)
    now = datetime.datetime.now()
    debug("Setting description field")
    template_object["description"] = "Generated by BOS functional test %s" % str(now)
    debug("Setting name field")
    template_object["name"] = "test-%s" % now.strftime("%Y%m%d-%H%M%S.%f")
    return template_object

def verify_template_list(template_map, template_name, should_exist):
    """
    If should_exist is true, verifies that the specified template appears in the list.
    If should_exist is false, verifies that it does not.
    """
    for tname in template_map.keys():
        if tname != template_name:
            continue
        elif should_exist:
            info("Found %s in template list" % template_name)
            return
        else:
            raise_test_error("Found %s in template list, but it should not be present" % template_name)
    if should_exist:
        raise_test_error("Did not find %s in template list, but it should have been present" % template_name)
    info("Confirmed that %s is not present in template list" % template_name)
# Copyright 2020-2021 Hewlett Packard Enterprise Development LP # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. # # (MIT License) Test helper functions Loads the default template from the JSON file, sets the name and description, and returns it If should_exist is true, verifies that the specified template appears in the list.     If should_exist is false, verifies that it does not.