import os
import yaml
import getpass
import tempfile
import unittest

from servicelab.stack import Context
from servicelab.utils import ccsdata_utils
from servicelab.utils import service_utils
from servicelab.utils import helper_utils
from servicelab.stack import Context


class TestCCsDataUtils(unittest.TestCase):
    """
    TestCCsDataUtils class is a unittest class for ccsdata_utils.
    It gets all the data avialable in the .stack directory. It
    uses ccsdata_utils to gets all the sites information in the
    data structure and verifies if the data associated with the
    given site svl-pod-1 is available or not. Also checks if the
    environment associated with the site exist or not.

    Attributes:
        ctx:  Context object of servicelab module.
        tempdir: Temporary directory generated by tempfile to use for simulated ccs-data

    """
    ctx = Context()
    tempdir = tempfile.mkdtemp()

    def setUp(self):
        """ setUp function of the TestCCsDataUtils class, it sets
        up the simulated ccs-data directories and host.yaml files.

        """
        self.site_env_path = os.path.join(self.tempdir, 'environments')
        self.site_to_check = "svl-svc-1"
        self.hostsd_path = os.path.join(self.site_env_path, 'test_env', 'hosts.d')
        if not os.path.exists(self.hostsd_path):
            os.makedirs(self.hostsd_path)
        self.flavors_list = []
        for i in range(1, 11):
            flavor = 'fake-flavor-' + str(i).zfill(3)
            self.flavors_list.append(flavor)
            hostname = 'fake-host-name-' + str(i).zfill(3) + '.yaml'
            host_file = os.path.join(self.hostsd_path, hostname)
            host_data = {
                'deploy_args': {
                    'flavor': flavor,
                }
            }
            with open(host_file, 'w') as output_file:
                output_file.write(yaml.dump(host_data, default_flow_style=False))
        self.flavors_list.sort()

    def test_ccs_site_exist(self):
        """ The test_ccs_site_exist is a test case to check if
        site_to_check is available amongs the sites which we get
        by using ccsdata_utilsr:.list_envs_or_sites function.

        If the assertion fails then test fails.
        """
        return_code, sites = ccsdata_utils.list_envs_or_sites(self.ctx.path)
        self.assertIsNotNone(sites[self.site_to_check])

    def test_ccs_site_env_exist(self):
        """ The test_ccs_site_env_exist is a test case to check if
        environment for site_to_check is available amongs the sites
        which we get by using ccsdata_utilsr:list_envs_or_sites
        function.

        If the assertion fails then test fails. The assertion can
        fail for number of reasons :
        1. The site_to_check site is not in the list returned by
           ccsdata_utilsr:list_envs_or_sites.
        2. If we do not have an environment for the the given
           site_to_check site.
        3. If the data exist but is not in the directory as
           supplied by (path, site, environemt).
        """
        return_code, sites = ccsdata_utils.list_envs_or_sites(self.ctx.path)
        self.assertIsNotNone(sites[self.site_to_check])

        envs = sites[self.site_to_check]
        self.assertIsNotNone(envs)

        # we expect atleast one environment to exist in svl-pod-1
        env = envs.keys()[0]
        self.assertIsNotNone(env)
        print env

        path = ccsdata_utils.get_env_for_site_path(self.ctx.path,
                                                   self.site_to_check,
                                                   env)
        self.assertTrue(os.path.isdir(path))

    def test_get_flavors_from_site(self):
        """ The test_get_flavors_from_site is a test case to check if
        deploy_args['flavors'] are properly extracted from ccs-data for
        a specified site.
        """
        site_flavors = ccsdata_utils.get_flavors_from_site(self.site_env_path)
        self.assertEqual(self.flavors_list, site_flavors)


if __name__ == '__main__':
    unittest.main()
