# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.

""" Component tests for Base Image Updation functionality

    Feature Specifications: https://cwiki.apache.org/confluence/display/CLOUDSTACK/Base+Image+Update+facility

    Test Plan: https://cwiki.apache.org/confluence/display/CLOUDSTACK/Base+Image+Updation+facility+TestPlan

    Issue Link: https://issues.apache.org/jira/browse/CLOUDSTACK-2243
"""

#Import Local Modules
import marvin
from marvin.codes import (PASS,
                          RECURRING)
from nose.plugins.attrib import attr
from marvin.cloudstackTestCase import cloudstackTestCase, unittest

from marvin.integration.lib.base import (ServiceOffering,
                                         Account,
                                         VirtualMachine,
                                         Volume,
                                         Host,
                                         Snapshot,
                                         SnapshotPolicy,
                                         Template
                                         )

from marvin.integration.lib.common import (get_domain,
                                           get_zone,
                                           get_template,
                                           list_templates
                                           )

from marvin.integration.lib.utils import (validateList,
                                          cleanup_resources)

import time
from datetime import datetime, timedelta

class Services:
    """Test Base Image Updation
    """

    def __init__(self):
        self.services = {
            "account": {
                "email": "test@test.com",
                "firstname": "Test",
                "lastname": "User",
                "username": "test",
                # Random characters are appended for unique
                # username
                "password": "password",
            },
            "service_offering_with_reset": {
                "name": "Tiny Instance With Reset",
                "displaytext": "Tiny Instance With Reset",
                "cpunumber": 1,
                "cpuspeed": 100,
                "memory": 128,
                "isvolatile": True,
            },
            "service_offering_without_reset": {
                "name": "Tiny Instance Without Reset",
                "displaytext": "Tiny Instance Without Reset",
                "cpunumber": 1,
                "cpuspeed": 100,
                "memory": 128,
                "isvolatile": False,
            },
            "recurring_snapshot": {
                "intervaltype": 'HOURLY',
                # Frequency of snapshots
                "maxsnaps": 2,  # Should be min 2
                "schedule": 1,
                "timezone": 'America/New_York',
                # Timezone Formats - http://cloudstack.apache.org/docs/en-US/Apache_CloudStack/4.0.0-incubating/html-single/API_Developers_Guide/#time-zones
            },
            "templates": {
                # Configs for different Template formats
                # For Eg. raw image, zip etc
                "XenServer": {
                       "displaytext": "Public Template - Xen",
                        "name": "Public template - Xen",
                        "ostype": "CentOS 5.3 (64-bit)",
                        "url": "http://download.cloud.com/releases/2.0.0/UbuntuServer-10-04-64bit.vhd.bz2",
                        "hypervisor": "XenServer",
                        "format": "VHD",
                        "isfeatured": True,
                        "ispublic": True,
                        "isextractable": True,
                },
                "KVM": {
                        "displaytext": "Public Template - KVM",
                        "name": "Public template -KVM",
                        "ostype": "CentOS 5.3 (64-bit)",
                        "url": "http://download.cloud.com/releases/2.0.0/UbuntuServer-10-04-64bit.qcow2.bz2",
                        "hypervisor": "KVM",
                        "format": "qcow2",
                        "isfeatured": True,
                        "ispublic": True,
                        "isextractable": True,

                }
            },
            "template": {
                    "displaytext": "Cent OS Template 2",