def test_fail_create_datadisk(self):
        # Disks are limited to a maximum of 1TB
        disk = [{'name': 'disk_fail_1',
                 'size': 5000,
                 'deletable': True,
                 'caching': 'None'
               }]

        test_name = 'test-fail-create-datadisk'
        ctx = self.mock_ctx(test_name, disk)
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN create VM test: {}".format(test_name)) 

        instance.create(ctx=ctx)
        
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, 'instance',timeout=900)

        current_ctx.set(ctx=ctx)
        self.assertRaises(utils.WindowsAzureError,
                          datadisks.create,
                          ctx=ctx
                          )

        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN delete VM test: {}".format(test_name)) 
        instance.delete(ctx=ctx)

        try:
            current_ctx.set(ctx=ctx)
            utils.wait_status(ctx, 'instance', 
                              constants.DELETING, timeout=900)
        except utils.WindowsAzureError:
            pass
    def test_conflict_instance(self):
        ctx = self.mock_ctx('testconflictinstance')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN conflict VM test")

        ctx.logger.info("create VM")
        instance.create(ctx=ctx)
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "instance",constants.SUCCEEDED, 600)

        ctx.logger.info("VM creation conflict")
        self.assertRaises(utils.WindowsAzureError,
                         instance.create,
                         ctx=ctx
                         )

        ctx.logger.info("delete VM")
        self.assertEqual(202, instance.delete(ctx=ctx))

        current_ctx.set(ctx=ctx)
        ctx.logger.info("check vm provisionning state in a deleted machine")
        self.assertRaises(
                          utils.WindowsAzureError,
                          instance.get_provisioning_state,
                          ctx=ctx
                          )

        ctx.logger.info("delete VM conflict")
        self.assertEqual(204, instance.delete(ctx=ctx))
        ctx.logger.info("END conflict VM test")
        time.sleep(TIME_DELAY)
    def test_create_too_much_datadisks(self):
        disks = [{'name': 'much_disks_1',
                  'size': 100,
                  'deletable': False,
                  'caching': 'None'
                },{'name': 'much_disks_2',
                   'size': 200,
                   'deletable': False,
                   'caching': 'ReadWrite'
                },{'name': 'much_disks_3',
                   'size': 200,
                   'deletable': False,
                   'caching': 'ReadOnly'
                }]

        test_name = 'test-create-too-much-datadisks'
        ctx = self.mock_ctx(test_name , disks)
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN create VM test: {}".format(test_name)) 

        instance.create(ctx=ctx)
        
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, 'instance', timeout=900)

        current_ctx.set(ctx=ctx)
        datadisks.create(ctx=ctx)

        current_ctx.set(ctx=ctx)
        json_VM = instance.get_json_from_azure(ctx=ctx)

        self.assertIsNotNone(json_VM['properties'][
                                        'storageProfile']['dataDisks'])

        disks_vm = json_VM['properties']['storageProfile']['dataDisks']

        ctx.logger.debug(disks_vm)

        self.assertNotEqual(len(disks), len(disks_vm))

        self.assertEqual(disks[0]['name'], disks_vm[0]['name'])
        self.assertEqual(disks[0]['size'], disks_vm[0]['diskSizeGB'])
        self.assertEqual(disks[0]['caching'], disks_vm[0]['caching'])

        self.assertEqual(disks[1]['name'], disks_vm[1]['name'])
        self.assertEqual(disks[1]['size'], disks_vm[1]['diskSizeGB'])
        self.assertEqual(disks[1]['caching'], disks_vm[1]['caching'])

        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN delete VM test: {}".format(test_name))
        instance.delete(ctx=ctx)

        try:
            current_ctx.set(ctx=ctx)
            utils.wait_status(ctx, 'instance', 
                              constants.DELETING, timeout=900)
        except utils.WindowsAzureError:
            pass
    def test_delete_datadisk(self):
        disk = [{'name': 'delete_disk',
                  'size': 100,
                  constants.DELETABLE_KEY: True,
                  'caching': 'None'
                },{'name': 'delete_disk_2',
                  'size': 100,
                  constants.DELETABLE_KEY: False,
                  'caching': 'None'
                }]

        test_name = 'test-delete-datadisk'
        ctx = self.mock_ctx(test_name, disk)

        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN create VM test: {}".format(test_name))
        instance.create(ctx=ctx)

        current_ctx.set(ctx=ctx)
        datadisks.create(ctx=ctx)
        
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, 'instance',timeout=900)

        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN delete VM test: {}".format(test_name))
        instance.delete(ctx=ctx)

        try:
            current_ctx.set(ctx=ctx)
            utils.wait_status(ctx, 'instance', 
                              constants.DELETING, timeout=900)
        except utils.WindowsAzureError:
            pass

        ctx.logger.info("BEGIN delete datadisk test: {}".format(
                                        disk[0]['name']
                                        )
                        )
        current_ctx.set(ctx=ctx)
        datadisks.delete(ctx=ctx)
        
        list  = datadisks._get_datadisks_from_storage(ctx)
        self.assertFalse(datadisks._is_datadisk_exists(
                                        list,
                                        disk[0]['name']
                                        )
                        )
        ctx.logger.info("Disk {} has been deleted.".format(disk[0]['name']))

        self.assertTrue(datadisks._is_datadisk_exists(
                                        list, 
                                        disk[1]['name']
                                        )
                        )
        ctx.logger.info("Datadisk {} still exists.".format(disk[1]['name']))
    def test_create_windows_instance(self):
        ctx = self.mock_ctx('testwin')
        ctx.node.properties[constants.IMAGE_KEY
                            ][constants.PUBLISHER_KEY] = \
            'MicrosoftWindowsServer'
        ctx.node.properties[constants.IMAGE_KEY
                            ][constants.OFFER_KEY] = 'WindowsServer'
        ctx.node.properties[constants.IMAGE_KEY
                            ][constants.SKU_KEY] = '2012-R2-Datacenter'
        ctx.node.properties[constants.WINDOWS_AUTOMATIC_UPDATES_KEY] = True

        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN create windows VM test")

        ctx.logger.info("Creating VM...")
        instance.create(ctx=ctx)

        current_ctx.set(ctx=ctx)
        jsonVM = instance.get_json_from_azure(ctx=ctx)

        self.assertIsNotNone(jsonVM['properties']['osProfile'][
                                    'windowsConfiguration'])
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "instance",constants.SUCCEEDED, 600)

        ctx.logger.info("delete windows VM")
        self.assertEqual(202, instance.delete(ctx=ctx))

        ctx.logger.info("END create windows VM test")
    def test_create_datadisk(self):
        disk = [{'name': 'disk_1',
                 'size': 100,
                 'deletable': False,
                 'caching': 'None'
               }]

        test_name = 'test-create-datadisk'
        ctx = self.mock_ctx(test_name, disk)
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN create VM test: {}".format(test_name)) 

        instance.create(ctx=ctx)
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, 'instance', constants.SUCCEEDED, timeout=900)

        current_ctx.set(ctx=ctx)
        datadisks.create(ctx=ctx)

        current_ctx.set(ctx=ctx)
        json_VM = instance.get_json_from_azure(ctx=ctx)

        ctx.logger.debug(json_VM)

        self.assertIsNotNone(json_VM['properties'][
                                        'storageProfile']['dataDisks'])
        
        disks_vm = json_VM['properties']['storageProfile']['dataDisks']

        ctx.logger.debug(disks_vm)

        self.assertEqual(disk[0]['name'], disks_vm[0]['name'])
        self.assertEqual(disk[0]['caching'], disks_vm[0]['caching'])
        self.assertEqual(disk[0]['size'], disks_vm[0]['diskSizeGB'])

        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN delete VM test: {}".format(test_name)) 
        instance.delete(ctx=ctx)

        try:
            current_ctx.set(ctx=ctx)
            utils.wait_status(ctx, 'instance', 
                              constants.DELETING, timeout=900)
        except utils.WindowsAzureError:
            pass
    def test_get_json_instance(self):
        ctx = self.mock_ctx('testgetjson')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN getjson VM test")

        ctx.logger.info("Creating VM...")
        instance.create(ctx=ctx)

        time.sleep(TIME_DELAY)

        ctx.logger.info("Getting json...")
        current_ctx.set(ctx=ctx)
        jsonVM = instance.get_json_from_azure(ctx=ctx)

        self.assertEqual(jsonVM['name'], ctx.node.properties[constants.COMPUTE_KEY])

        time.sleep(TIME_DELAY)

        ctx.logger.info("Deleting VM...")
        current_ctx.set(ctx=ctx)
        instance.delete(ctx=ctx)
    def test_create_2nic_instance(self):
        ctx = self.mock_ctx('testcreate2nicinstance')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN create 2 NIC VM test: {}".format(ctx.instance.id))

        subnet_name = 'instancesubnet_test_2_' + self.__random_id
        nic_name = 'instance_nic_test_2_' + self.__random_id

        ctx.logger.info("create new subnet")
        ctx.node.properties[constants.SUBNET_KEY] = subnet_name
        ctx.node.properties[constants.SUBNET_ADDRESS_KEY] =\
            "10.0.2.0/24"
        current_ctx.set(ctx=ctx)
        subnet.create(ctx=ctx)
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "subnet",constants.SUCCEEDED, 600)

        ctx.logger.info("create second NIC")
        ctx.node.properties[constants.NETWORK_INTERFACE_KEY] = nic_name
        ctx.node.properties[constants.NIC_PRIMARY_KEY] = True
        ctx.node.properties[constants.AZURE_CONFIG_KEY][constants.SUBNET_KEY] = subnet_name
        for relationship in ctx.instance.relationships:
            if relationship.type == constants.NIC_CONNECTED_TO_SUBNET:
                relationship.target.instance.runtime_properties[constants.SUBNET_KEY] = subnet_name
        current_ctx.set(ctx=ctx)
        nic.create(ctx=ctx)
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "nic",constants.SUCCEEDED, 600)

        ctx.logger.info("create VM")
        ctx.node.properties[constants.FLAVOR_KEY] = 'Standard_A3'
        ctx.instance.relationships.append(test_mockcontext.MockRelationshipContext(node_id='test',
            runtime_properties={
                constants.NETWORK_INTERFACE_KEY: nic_name,
                constants.NIC_PRIMARY_KEY: True
            },
            type=constants.INSTANCE_CONNECTED_TO_NIC)
        )
        current_ctx.set(ctx=ctx)
        instance.create(ctx=ctx)
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "instance",constants.SUCCEEDED, 600)

        ctx.logger.info("verify the NIC's number of the instance")
        json = instance.get_json_from_azure()
        self.assertEqual(len(json['properties']['networkProfile']['networkInterfaces']),2)

        ctx.logger.info("delete VM")
        self.assertEqual(202, instance.delete(ctx=ctx))

        ctx.logger.info("END create VM test")
    def test_delete_instance(self):    
        ctx = self.mock_ctx('testdeleteinstance')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN delete VM test")
    
        ctx.logger.info("create VM")    
        instance.create(ctx=ctx) 
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "instance",constants.SUCCEEDED, 600)
        
        ctx.logger.info("delete VM")
        self.assertEqual(202, instance.delete(ctx=ctx))

        ctx.logger.info("END delete VM test")
    def test_create_instance_from_vhd(self):
        '''To run this test, you need all the required resource to start a machine
        (resource group, storage account, nic). You then have to upload a valid
        bootable VHD on the storage account. Note the vhd's endpoint and replace 
        MY_URI_VHD by this value.
        Then, you can run the test.
        Note the resource group will not be deleted by the class teardown.
        '''
        ctx = self.mock_ctx('testinstancevhd')
        ctx.node.properties[constants.AZURE_CONFIG_KEY
                            ][constants.RESOURCE_GROUP_KEY] = MY_RESOURCE_GROUP
        ctx.node.properties[constants.AZURE_CONFIG_KEY
                            ][constants.STORAGE_ACCOUNT_KEY] = MY_STORAGE_ACCOUNT
        ctx.node.properties[constants.NETWORK_INTERFACE_KEY] = MY_NIC
        ctx.node.properties[constants.IMAGE_KEY] = {}
        ctx.node.properties[constants.IMAGE_KEY
                            ][constants.OS_URI_KEY] = MY_URI_VHD
        ctx.node.properties[constants.IMAGE_KEY
                            ][constants.OS_TYPE_KEY] = 'Linux'

        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN create VM test: {}".format(ctx.instance.id))
        ctx.logger.info("create VM") 

        instance.create(ctx=ctx) 
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "instance",constants.SUCCEEDED, 600)

        current_ctx.set(ctx=ctx)
        jsonVM = instance.get_json_from_azure(ctx=ctx)

        self.assertEqual(jsonVM['properties']['storageProfile'
                                              ]['osDisk']['osType'],  
                         ctx.node.properties[constants.IMAGE_KEY
                            ][constants.OS_TYPE_KEY]
                         )

        self.assertEqual(jsonVM['properties']['storageProfile'
                                              ]['osDisk']['image']['uri'],
                         ctx.node.properties[constants.IMAGE_KEY
                            ][constants.OS_URI_KEY]
                         )

        ctx.logger.info("delete VM")
        self.assertEqual(202, instance.delete(ctx=ctx))

        ctx.logger.info("END create VM test")
    def test_add_availability_set_instance(self):
        ctx = self.mock_ctx('testaddavailabilityinstance')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN add availability set VM test: {}".format(ctx.instance.id))

        ctx.logger.info("create availability_set")
        self.assertEqual(200, availability_set.create(ctx=ctx))
        ctx.logger.debug("availability_set_id = {}".format(
            ctx.instance.runtime_properties[constants.AVAILABILITY_ID_KEY]))

        ctx.instance.relationships.append(test_mockcontext.MockRelationshipContext(node_id='test',
            runtime_properties={
                constants.AVAILABILITY_ID_KEY:\
                     ctx.instance.runtime_properties[constants.AVAILABILITY_ID_KEY]
            },
            type=constants.INSTANCE_CONTAINED_IN_AVAILABILITY_SET)
        )

        ctx.logger.info("create VM")
        instance.create(ctx=ctx)

        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "instance",constants.SUCCEEDED, 600)

        ctx.logger.info("test instance is in availability_set")
        current_ctx.set(ctx=ctx)
        json = instance.get_json_from_azure(ctx=ctx)
        self.assertIsNotNone(json['properties']['availabilitySet'])
        self.assertEqual(str(json['properties']['availabilitySet']['id']).lower(),
            str(ctx.instance.runtime_properties[constants.AVAILABILITY_ID_KEY]).lower()
        )

        ctx.logger.info("delete VM")
        self.assertEqual(202, instance.delete(ctx=ctx))

        ctx.logger.info("delete availability_set")
        self.assertEqual(200, availability_set.delete(ctx=ctx))

        ctx.logger.info("END create VM test")
    def test_storage_relationship_instance(self):
        ctx = self.mock_ctx('teststorelainstance')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN storage relationship VM test: {}".format(ctx.instance.id))

        ctx.instance.relationships.append(test_mockcontext.MockRelationshipContext(node_id='test',
            runtime_properties={
                constants.STORAGE_ACCOUNT_KEY:\
                     ctx.node.properties[constants.STORAGE_ACCOUNT_KEY]
            },
            type=constants.INSTANCE_CONNECTED_TO_STORAGE_ACCOUNT)
        )

        ctx.logger.info("create VM")
        instance.create(ctx=ctx)

        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "instance",constants.SUCCEEDED, 600)

        ctx.logger.info("delete VM")
        self.assertEqual(202, instance.delete(ctx=ctx))

        ctx.logger.info("END create VM test")
 def delete_vm(mokctx, queue):
     queue.put(instance.delete(ctx=mokctx))
    def test_concurrent_create_instance(self):
        ctx1 = self.mock_ctx('testconcurrentcreate1', id=1)
        ctx2 = self.mock_ctx('testconcurrentcreate2', id=2)
        ctx1.logger.info("BEGIN concurrent create VM 1 test")
        ctx2.logger.info("BEGIN concurrent create VM 2 test")

        ctx1.logger.info("CREATE nic 1")
        current_ctx.set(ctx=ctx1)
        nic.create(ctx=ctx1)
        current_ctx.set(ctx=ctx1)
        utils.wait_status(ctx1, "nic",constants.SUCCEEDED, 600)

        ctx2.logger.info("CREATE nic 2")
        current_ctx.set(ctx=ctx2)
        nic.create(ctx=ctx2)
        current_ctx.set(ctx=ctx2)
        utils.wait_status(ctx2, "nic",constants.SUCCEEDED, 600)

        def create_vm(lctx):
            instance.create(ctx=lctx)
            return
        vm1 = threading.Thread(target=create_vm, args=(ctx1,))
        vm2 = threading.Thread(target=create_vm, args=(ctx2,))

        ctx1.logger.info("create VM 1")
        vm1.start()

        ctx2.logger.info("create VM 2")
        vm2.start()

        vm1.join()
        vm2.join()

        ctx1.logger.info("check VM 1 status")
        ctx2.logger.info("check VM 2 status")
        status_vm1 = constants.CREATING
        status_vm2 = constants.CREATING
        while bool(status_vm1 == constants.CREATING or status_vm2 == constants.CREATING) :
            current_ctx.set(ctx=ctx1)
            status_vm1 = instance.get_provisioning_state(ctx=ctx1)
            current_ctx.set(ctx=ctx2)
            status_vm2 = instance.get_provisioning_state(ctx=ctx2)
            time.sleep(TIME_DELAY)

        ctx1.logger.info("check VM 1 creation success")
        self.assertEqual(constants.SUCCEEDED, status_vm1)

        ctx2.logger.info("check VM 2 creation success")
        self.assertEqual(constants.SUCCEEDED, status_vm2)

        ctx1.logger.info("delete VM 1")
        self.assertEqual(202, instance.delete(ctx=ctx1))

        ctx2.logger.info("delete VM 2")
        self.assertEqual(202, instance.delete(ctx=ctx2))

        current_ctx.set(ctx=ctx1)
        ctx1.logger.info("DELETE nic 1")
        nic.delete(ctx=ctx1)

        current_ctx.set(ctx=ctx2)
        ctx2.logger.info("DELETE nic 2")
        nic.delete(ctx=ctx2)

        ctx1.logger.info("END concurrent create VM 1 test")
        ctx2.logger.info("END concurrent create VM 2 test")
    def test_datadisk_in_storage_account(self):
        disk = [{'name': 'attach_disk',
                  'size': 100,
                  'deletable': False,
                  'caching': 'None'
                }]

        test_name = 'test-datadisk-in-storage-account'
        ctx = self.mock_ctx(test_name, disk)

        current_ctx.set(ctx=ctx)
        ctx.logger.info("CREATE storage account")
        ctx.node.properties[constants.ACCOUNT_TYPE_KEY] = "Standard_LRS"
        ctx.node.properties[constants.STORAGE_ACCOUNT_KEY] = \
            "storageaccountdisk" + self.__random_id
        storage.create(ctx=ctx)
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "storage",constants.SUCCEEDED, timeout=600)

        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN create VM test: {}".format(test_name))

        instance.create(ctx=ctx)
        
        ctx.instance.relationships.append(
                    MockRelationshipContext(
                                'test',
                                {constants.STORAGE_ACCOUNT_KEY: \
                                       'storageaccountdisk' + self.__random_id
                                }, 
                                constants.DISK_CONTAINED_IN_STORAGE_ACCOUNT
                    )
        )
                                            
        current_ctx.set(ctx=ctx)
        datadisks.create(ctx=ctx)

        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, 'instance',timeout=900)

        jsonInstance = instance.get_json_from_azure(ctx=ctx)

        self.assertIn('storageaccountdisk' + self.__random_id,
                      jsonInstance['properties'
                                   ]['storageProfile'
                                     ]['dataDisks'][0]['vhd']['uri']
                      )

        ctx.logger.info('Disks are located in {}.'.format(
                                    'storageaccountdisk' + self.__random_id,
                                    )
                        )

        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN delete VM test: {}".format(test_name))
        instance.delete(ctx=ctx)

        try:
            current_ctx.set(ctx=ctx)
            utils.wait_status(ctx, 'instance', 
                              constants.DELETING, timeout=900)
        except utils.WindowsAzureError:
            pass

        current_ctx.set(ctx=ctx)
        ctx.logger.info("DELETE storage account")
        ctx.node.properties[constants.ACCOUNT_TYPE_KEY] = "Standard_LRS"
        ctx.node.properties[constants.STORAGE_ACCOUNT_KEY] = \
            "storageaccountdisk" + self.__random_id
        storage.delete(ctx=ctx)