#   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, 38, 33, 49), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((36, 35, 36, 46), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((37, 36, 37, 47), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((38, 32, 38, 43), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((39, 38, 39, 49), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((40, 38, 40, 49), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((41, 38, 41, 49), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((42, 38, 42, 49), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((90, 14, 90, 55), 'otcextensions.sdk.dcs.v1.instance.Instance.existing', 'instance.Instance.existing', ({}, {}), '(**object_info)', False, 'from otcextensions.sdk.dcs.v1 import instance\n'), ((110, 14, 110, 57), 'otcextensions.sdk.dcs.v1.statistic.Statistic.existing', 'statistic.Statistic.existing', ({}, {}), '(**object_info)', False, 'from otcextensions.sdk.dcs.v1 import statistic\n'), ((134, 14, 134, 51), 'otcextensions.sdk.dcs.v1.backup.Backup.existing', 'backup.Backup.existing', ({}, {}), '(**object_info)', False, 'from otcextensions.sdk.dcs.v1 import backup\n'), ((153, 14, 153, 53), 'otcextensions.sdk.dcs.v1.restore.Restore.existing', 'restore.Restore.existing', ({}, {}), '(**object_info)', False, 'from otcextensions.sdk.dcs.v1 import restore\n'), ((172, 14, 172, 51), 'otcextensions.sdk.dcs.v1.config.Config.existing', 'config.Config.existing', ({}, {}), '(**object_info)', False, 'from otcextensions.sdk.dcs.v1 import config\n'), ((54, 22, 57, 50), 'random.choice', 'random.choice', ({(54, 36, 57, 49): "['CREATING', 'CREATEFILED', 'RUNNING', 'ERROR', 'STARTING', 'RESTARTING',\n    'CLOSING', 'CLOSED', 'EXTENDING']"}, {}), "(['CREATING', 'CREATEFILED', 'RUNNING', 'ERROR', 'STARTING',\n    'RESTARTING', 'CLOSING', 'CLOSED', 'EXTENDING'])", False, 'import random\n'), ((59, 24, 59, 46), 'random.randint', 'random.randint', ({(59, 39, 59, 40): '(1)', (59, 42, 59, 45): '(100)'}, {}), '(1, 100)', False, 'import random\n'), ((61, 20, 61, 44), 'random.randint', 'random.randint', ({(61, 35, 61, 36): '(1)', (61, 38, 61, 43): '(65535)'}, {}), '(1, 65535)', False, 'import random\n'), ((62, 34, 65, 51), 'random.choice', 'random.choice', ({(62, 48, 65, 50): "['dcs.single_node', 'dcs.master_standby', 'dcs.cluster']"}, {}), "(['dcs.single_node', 'dcs.master_standby', 'dcs.cluster'])", False, 'import random\n'), ((68, 29, 68, 50), 'random.randint', 'random.randint', ({(68, 44, 68, 45): '(0)', (68, 47, 68, 49): '(10)'}, {}), '(0, 10)', False, 'import random\n'), ((78, 26, 80, 55), 'random.choice', 'random.choice', ({(78, 40, 80, 54): "['OTC_DCS_SINGLE', 'OTC_DCS_MS', 'OTC_DCS_CL']"}, {}), "(['OTC_DCS_SINGLE', 'OTC_DCS_MS', 'OTC_DCS_CL'])", False, 'import random\n'), ((82, 26, 82, 47), 'random.randint', 'random.randint', ({(82, 41, 82, 42): '(0)', (82, 44, 82, 46): '(10)'}, {}), '(0, 10)', False, 'import random\n'), ((83, 27, 83, 48), 'random.randint', 'random.randint', ({(83, 42, 83, 43): '(0)', (83, 45, 83, 47): '(10)'}, {}), '(0, 10)', False, 'import random\n'), ((101, 26, 101, 50), 'random.randint', 'random.randint', ({(101, 41, 101, 42): '(1)', (101, 44, 101, 49): '(65535)'}, {}), '(1, 65535)', False, 'import random\n'), ((102, 27, 102, 51), 'random.randint', 'random.randint', ({(102, 42, 102, 43): '(1)', (102, 45, 102, 50): '(65535)'}, {}), '(1, 65535)', False, 'import random\n'), ((103, 29, 103, 53), 'random.randint', 'random.randint', ({(103, 44, 103, 45): '(1)', (103, 47, 103, 52): '(65535)'}, {}), '(1, 65535)', False, 'import random\n'), ((104, 29, 104, 53), 'random.randint', 'random.randint', ({(104, 44, 104, 45): '(1)', (104, 47, 104, 52): '(65535)'}, {}), '(1, 65535)', False, 'import random\n'), ((122, 20, 122, 44), 'random.randint', 'random.randint', ({(122, 35, 122, 36): '(1)', (122, 38, 122, 43): '(65535)'}, {}), '(1, 65535)', False, 'import random\n'), ((145, 26, 145, 50), 'random.randint', 'random.randint', ({(145, 41, 145, 42): '(1)', (145, 44, 145, 49): '(65535)'}, {}), '(1, 65535)', False, 'import random\n'), ((146, 27, 146, 51), 'random.randint', 'random.randint', ({(146, 42, 146, 43): '(1)', (146, 45, 146, 50): '(65535)'}, {}), '(1, 65535)', False, 'import random\n'), ((147, 29, 147, 53), 'random.randint', 'random.randint', ({(147, 44, 147, 45): '(1)', (147, 47, 147, 52): '(65535)'}, {}), '(1, 65535)', False, 'import random\n'), ((148, 29, 148, 53), 'random.randint', 'random.randint', ({(148, 44, 148, 45): '(1)', (148, 47, 148, 52): '(65535)'}, {}), '(1, 65535)', False, 'import random\n'), ((58, 22, 58, 34), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((60, 18, 60, 30), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((66, 30, 66, 42), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((67, 32, 67, 44), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((69, 22, 69, 34), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((70, 24, 70, 36), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((71, 25, 71, 37), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((72, 27, 72, 39), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((73, 27, 73, 39), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((74, 33, 74, 45), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((75, 35, 75, 47), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((76, 26, 76, 38), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((77, 26, 77, 38), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((81, 31, 81, 43), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((84, 23, 84, 35), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((85, 25, 85, 37), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((86, 24, 86, 36), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((87, 30, 87, 42), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((88, 28, 88, 40), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((123, 22, 123, 34), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((124, 27, 124, 39), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((125, 24, 125, 36), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((126, 26, 126, 38), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((127, 26, 127, 38), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((128, 20, 128, 32), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((129, 20, 129, 32), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((130, 26, 130, 38), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((164, 18, 164, 30), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((165, 20, 165, 32), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((166, 21, 166, 33), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((167, 26, 167, 38), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((168, 27, 168, 39), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((169, 29, 169, 41), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((170, 27, 170, 39), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((51, 31, 51, 43), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((52, 26, 52, 38), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((100, 44, 100, 56), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((105, 33, 105, 45), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((106, 37, 106, 49), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((107, 39, 107, 51), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((120, 44, 120, 56), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((121, 26, 121, 38), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((144, 44, 144, 56), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((149, 33, 149, 45), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((150, 37, 150, 49), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((151, 39, 151, 51), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((163, 44, 163, 56), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n')]