#   Licensed under the Apache License, Version 2.0 (the "License"); you may
#   not use this file except in compliance with the License. You may obtain
#   a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.
# import datetime
import random
import uuid

import mock

from openstackclient.tests.unit import utils
from otcextensions.tests.unit.osclient import test_base

from otcextensions.sdk.dcs.v1 import backup
from otcextensions.sdk.dcs.v1 import config
from otcextensions.sdk.dcs.v1 import instance
from otcextensions.sdk.dcs.v1 import restore
from otcextensions.sdk.dcs.v1 import statistic


class TestDCS(utils.TestCommand):

    def setUp(self):
        super(TestDCS, self).setUp()

        self.app.client_manager.dcs = mock.Mock()
        self.client = self.app.client_manager.dcs

        self.client.get_instance = mock.Mock()
        self.client.find_instance = mock.Mock()
        self.client.instances = mock.Mock()
        self.client.delete_instance = mock.Mock()
        self.client.update_instance = mock.Mock()
        self.client.create_instance = mock.Mock()
        self.client.extend_instance = mock.Mock()


class FakeInstance(test_base.Fake):
    """Fake one or more Instance"""

    @classmethod
    def generate(cls):
        object_info = {
            'name': 'group-' + uuid.uuid4().hex,
            'id': 'id-' + uuid.uuid4().hex,
            'description': 'SOME description',
            'status': random.choice(['CREATING', 'CREATEFILED',
                                     'RUNNING', 'ERROR', 'STARTING',
                                     'RESTARTING', 'CLOSING', 'CLOSED',
                                     'EXTENDING']),
            'engine': uuid.uuid4().hex,
            'capacity': random.randint(1, 100),
            'ip': uuid.uuid4().hex,
            'port': random.randint(1, 65535),
            'resource_spec_code': random.choice(['dcs.single_node',
                                                 'dcs.master_standby',
                                                 'dcs.cluster'
                                                 ]),
            'engine_version': uuid.uuid4().hex,
            'internal_version': uuid.uuid4().hex,
            'charging_mode': random.randint(0, 10),
            'vpc_id': uuid.uuid4().hex,
            'vpc_name': uuid.uuid4().hex,
            'subnet_id': uuid.uuid4().hex,
            'subnet_name': uuid.uuid4().hex,
            'subnet_cidr': uuid.uuid4().hex,
            'security_group_id': uuid.uuid4().hex,
            'security_group_name': uuid.uuid4().hex,
            'created_at': uuid.uuid4().hex,
            'error_code': uuid.uuid4().hex,
            'product_id': random.choice(['OTC_DCS_SINGLE',
                                         'OTC_DCS_MS',
                                         'OTC_DCS_CL']),
            'available_zones': uuid.uuid4().hex,
            'max_memory': random.randint(0, 10),
            'used_memory': random.randint(0, 10),
            'user_id': uuid.uuid4().hex,
            'user_name': uuid.uuid4().hex,
            'order_id': uuid.uuid4().hex,
            'maintain_begin': uuid.uuid4().hex,
            'maintain_end': uuid.uuid4().hex,
        }
        obj = instance.Instance.existing(**object_info)
        return obj


class FakeStatistic(test_base.Fake):
    """Fake one or more Statistic"""

    @classmethod
    def generate(cls):
        object_info = {
            'instance_id': 'instance_id-' + uuid.uuid4().hex,
            'max_memory': random.randint(1, 65535),
            'used_memory': random.randint(1, 65535),
            'cmd_get_count': random.randint(1, 65535),
            'cmd_set_count': random.randint(1, 65535),
            'used_cpu': 'cpu-' + uuid.uuid4().hex,
            'input_kbps': 'input-' + uuid.uuid4().hex,
            'output_kbps': 'output-' + uuid.uuid4().hex,

        }
        obj = statistic.Statistic.existing(**object_info)
        return obj


class FakeBackup(test_base.Fake):
    """Fake one or more Backup"""

    @classmethod
    def generate(cls):
        object_info = {
            'instance_id': 'instance_id-' + uuid.uuid4().hex,
            'id': 'id-' + uuid.uuid4().hex,
            'size': random.randint(1, 65535),
            'period': uuid.uuid4().hex,
            'description': uuid.uuid4().hex,
            'progress': uuid.uuid4().hex,
            'created_at': uuid.uuid4().hex,
            'updated_at': uuid.uuid4().hex,
            'type': uuid.uuid4().hex,
            'name': uuid.uuid4().hex,
            'error_code': uuid.uuid4().hex,
            'is_restorable': True,

        }
        obj = backup.Backup.existing(**object_info)
        return obj


class FakeRestore(test_base.Fake):
    """Fake one or more Restore"""

    @classmethod
    def generate(cls):
        object_info = {
            'instance_id': 'instance_id-' + uuid.uuid4().hex,
            'max_memory': random.randint(1, 65535),
            'used_memory': random.randint(1, 65535),
            'cmd_get_count': random.randint(1, 65535),
            'cmd_set_count': random.randint(1, 65535),
            'used_cpu': 'cpu-' + uuid.uuid4().hex,
            'input_kbps': 'input-' + uuid.uuid4().hex,
            'output_kbps': 'output-' + uuid.uuid4().hex
        }
        obj = restore.Restore.existing(**object_info)
        return obj


class FakeConfig(test_base.Fake):
    """Fake one or more Config"""

    @classmethod
    def generate(cls):
        object_info = {
            'instance_id': 'instance_id-' + uuid.uuid4().hex,
            'id': uuid.uuid4().hex,
            'name': uuid.uuid4().hex,
            'value': uuid.uuid4().hex,
            'value_type': uuid.uuid4().hex,
            'value_range': uuid.uuid4().hex,
            'default_value': uuid.uuid4().hex,
            'description': uuid.uuid4().hex
        }
        obj = config.Config.existing(**object_info)
        return obj

[(33, 'mock.Mock', 'mock.Mock', False, 'import mock\n'), (36, 'mock.Mock', 'mock.Mock', False, 'import mock\n'), (37, 'mock.Mock', 'mock.Mock', False, 'import mock\n'), (38, 'mock.Mock', 'mock.Mock', False, 'import mock\n'), (39, 'mock.Mock', 'mock.Mock', False, 'import mock\n'), (40, 'mock.Mock', 'mock.Mock', False, 'import mock\n'), (41, 'mock.Mock', 'mock.Mock', False, 'import mock\n'), (42, 'mock.Mock', 'mock.Mock', False, 'import mock\n'), (90, 'otcextensions.sdk.dcs.v1.instance.Instance.existing', 'instance.Instance.existing', False, 'from otcextensions.sdk.dcs.v1 import instance\n'), (110, 'otcextensions.sdk.dcs.v1.statistic.Statistic.existing', 'statistic.Statistic.existing', False, 'from otcextensions.sdk.dcs.v1 import statistic\n'), (134, 'otcextensions.sdk.dcs.v1.backup.Backup.existing', 'backup.Backup.existing', False, 'from otcextensions.sdk.dcs.v1 import backup\n'), (153, 'otcextensions.sdk.dcs.v1.restore.Restore.existing', 'restore.Restore.existing', False, 'from otcextensions.sdk.dcs.v1 import restore\n'), (172, 'otcextensions.sdk.dcs.v1.config.Config.existing', 'config.Config.existing', False, 'from otcextensions.sdk.dcs.v1 import config\n'), (54, 'random.choice', 'random.choice', False, 'import random\n'), (59, 'random.randint', 'random.randint', False, 'import random\n'), (61, 'random.randint', 'random.randint', False, 'import random\n'), (62, 'random.choice', 'random.choice', False, 'import random\n'), (68, 'random.randint', 'random.randint', False, 'import random\n'), (78, 'random.choice', 'random.choice', False, 'import random\n'), (82, 'random.randint', 'random.randint', False, 'import random\n'), (83, 'random.randint', 'random.randint', False, 'import random\n'), (101, 'random.randint', 'random.randint', False, 'import random\n'), (102, 'random.randint', 'random.randint', False, 'import random\n'), (103, 'random.randint', 'random.randint', False, 'import random\n'), (104, 'random.randint', 'random.randint', False, 'import random\n'), (122, 'random.randint', 'random.randint', False, 'import random\n'), (145, 'random.randint', 'random.randint', False, 'import random\n'), (146, 'random.randint', 'random.randint', False, 'import random\n'), (147, 'random.randint', 'random.randint', False, 'import random\n'), (148, 'random.randint', 'random.randint', False, 'import random\n'), (58, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (60, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (66, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (67, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (69, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (70, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (71, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (72, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (73, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (74, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (75, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (76, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (77, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (81, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (84, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (85, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (86, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (87, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (88, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (123, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (124, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (125, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (126, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (127, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (128, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (129, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (130, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (164, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (165, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (166, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (167, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (168, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (169, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (170, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (51, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (52, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (100, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (105, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (106, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (107, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (120, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (121, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (144, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (149, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (150, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (151, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (163, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n')]