#!/usr/bin/env python3
#                           PUBLIC DOMAIN NOTICE
#              National Center for Biotechnology Information
#  
# This software is a "United States Government Work" under the
# terms of the United States Copyright Act.  It was written as part of
# the authors' official duties as United States Government employees and
# thus cannot be copyrighted.  This software is freely available
# to the public for use.  The National Library of Medicine and the U.S.
# Government have not placed any restriction on its use or reproduction.
#   
# Although all reasonable efforts have been taken to ensure the accuracy
# and reliability of the software and data, the NLM and the U.S.
# Government do not and cannot warrant the performance or results that
# may be obtained by using this software or data.  The NLM and the U.S.
# Government disclaim all warranties, express or implied, including
# warranties of performance, merchantability or fitness for any particular
# purpose.
#   
# Please cite NCBI in any work or product based on this material.

"""
tests/resources/quotas/test_quotas.py - Tests for resource quotas module

Author: Christiam Camacho (camacho@ncbi.nlm.nih.gov)
"""
import os
import unittest
from unittest.mock import MagicMock, patch
import configparser
import pytest
from elb.config import _set_sections
from elb.constants import CSP
from elb.resources.quotas.quota_check import check_resource_quotas
from elb.base import InstanceProperties
from elb.elb_config import ElasticBlastConfig
from elb.constants import ElbCommand

TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), '..', '..', 'config', 'data')


class TestResourceQuotasAws(unittest.TestCase):

    def setUp(self):
        """ Initialize 2 configurations: one for GCP another for AWS """
        cfg_gcp = configparser.ConfigParser()
        cfg_aws = configparser.ConfigParser()
        _set_sections(cfg_gcp)
        _set_sections(cfg_aws)

        cfg_gcp.read(f"{TEST_DATA_DIR}/correct-cfg-file.ini")
        cfg_aws.read(f"{TEST_DATA_DIR}/elb-aws-blastn-pdbnt.ini")

        self.cfg_gcp = ElasticBlastConfig(cfg_gcp, task = ElbCommand.SUBMIT)
        with patch('elb.elb_config.aws_get_machine_properties', new=MagicMock(return_value=InstanceProperties(32, 120))):
            self.cfg_aws = ElasticBlastConfig(cfg_aws, task = ElbCommand.SUBMIT)

    @pytest.mark.skipif(os.getenv('TEAMCITY_VERSION') is not None, reason='AWS credentials not set in TC')
    def test_check_resource_quotas_aws(self):
        self.assertEqual(self.cfg_aws.aws.cloud, CSP.AWS)
        check_resource_quotas(self.cfg_aws)

    def test_check_resource_quotas_gcp(self):
        with self.assertRaises(NotImplementedError) as cm:
            self.assertEqual(self.cfg_gcp.gcp.cloud, CSP.GCP)
            check_resource_quotas(self.cfg_gcp)
            self.assertEqual('not implemented yet', str(cm.exception))


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