package org.zstack.test.kvm;

import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.zstack.core.cloudbus.CloudBus;
import org.zstack.core.componentloader.ComponentLoader;
import org.zstack.core.db.DatabaseFacade;
import org.zstack.header.configuration.DiskOfferingInventory;
import org.zstack.header.configuration.InstanceOfferingInventory;
import org.zstack.header.identity.SessionInventory;
import org.zstack.header.image.ImageInventory;
import org.zstack.header.network.l3.L3NetworkInventory;
import org.zstack.header.vm.VmInstanceInventory;
import org.zstack.header.volume.VolumeInventory;
import org.zstack.simulator.kvm.KVMSimulatorConfig;
import org.zstack.simulator.storage.backup.sftp.SftpBackupStorageSimulatorConfig;
import org.zstack.test.*;
import org.zstack.test.deployer.Deployer;
import org.zstack.test.storage.backup.sftp.TestSftpBackupStorageDeleteImage2;
import org.zstack.utils.SizeUtils;
import org.zstack.utils.Utils;
import org.zstack.utils.data.SizeUnit;
import org.zstack.utils.logging.CLogger;

/**
 * Created by miao on 16-7-26.
 */


/**
 * 1 create a vm from a data volume template
 * <p>
 * confirm the vm failed to create
 */
public class TestCreateVmOnKvm1FailureAtEnd {
    CLogger logger = Utils.getLogger(TestSftpBackupStorageDeleteImage2.class);
    Deployer deployer;
    Api api;
    ComponentLoader loader;
    CloudBus bus;
    DatabaseFacade dbf;
    SessionInventory session;
    SftpBackupStorageSimulatorConfig config;
    KVMSimulatorConfig kconfig;

    @Before
    public void setUp() throws Exception {
        DBUtil.reDeployDB();
        WebBeanConstructor con = new WebBeanConstructor();
        deployer = new Deployer("deployerXml/kvm/TestCreateVmOnKvmFailureAtEnd.xml", con);
        deployer.addSpringConfig("KVMRelated.xml");
        deployer.build();
        api = deployer.getApi();
        loader = deployer.getComponentLoader();
        bus = loader.getComponent(CloudBus.class);
        dbf = loader.getComponent(DatabaseFacade.class);
        config = loader.getComponent(SftpBackupStorageSimulatorConfig.class);
        kconfig = loader.getComponent(KVMSimulatorConfig.class);
        session = api.loginAsAdmin();
    }

    @Test
    public void test() throws ApiSenderException {
        ImageInventory img = deployer.images.get("TestImage");
        InstanceOfferingInventory ioinv = deployer.instanceOfferings.get("TestInstanceOffering");
        L3NetworkInventory l3 = deployer.l3Networks.get("TestL3Network1");

        kconfig.startVmSuccess = false;
        //kconfig.totalMemory = SizeUnit.GIGABYTE.toByte(7);
        VmCreator creator = new VmCreator(api);
        creator.addL3Network(l3.getUuid());
        creator.name = "vm";
        creator.imageUuid = img.getUuid();
        creator.instanceOfferingUuid = ioinv.getUuid();
        boolean s = false;
        try {
            creator.create();
        } catch (ApiSenderException e) {
            s = true;
        }

        Assert.assertTrue(s);
    }

}

@@ -102,16 +102,223 @@
 t.Test;%0A
+import org.springframework.transaction.annotation.Transactional;%0Aimport org.zstack.appliancevm.ApplianceVmConstant;%0Aimport org.zstack.appliancevm.ApplianceVmVO;%0Aimport org.zstack.appliancevm.ApplianceVmVO_;%0A
 import o
@@ -449,16 +449,55 @@
 Facade;%0A
+import org.zstack.core.db.SimpleQuery;%0A
 import o
@@ -608,32 +608,145 @@
 eringInventory;%0A
+import org.zstack.header.identity.AccountResourceRefVO;%0Aimport org.zstack.header.identity.AccountResourceRefVO_;%0A
 import org.zstac
@@ -963,29 +963,236 @@
 er.v
-olume.VolumeInventory
+m.VmInstanceState;%0Aimport org.zstack.header.volume.VolumeConstant;%0Aimport org.zstack.header.volume.VolumeInventory;%0Aimport org.zstack.header.volume.VolumeVO_;%0Aimport org.zstack.network.service.virtualrouter.VirtualRouterConstant
 ;%0Aim
@@ -1616,16 +1616,86 @@
 ogger;%0A%0A
+import javax.persistence.Tuple;%0Aimport javax.persistence.TypedQuery;%0A%0A
 /**%0A * C
@@ -3609,25 +3609,24 @@
 ;%0A        %7D%0A
-%0A
         Asse
@@ -3642,18 +3642,308 @@
 True(s);
+%0A%0A        SimpleQuery%3CAccountResourceRefVO%3E q = dbf.createQuery(AccountResourceRefVO.class);%0A        q.add(AccountResourceRefVO_.resourceType, SimpleQuery.Op.EQ, %22VolumeVO%22);%0A        long count = q.count();%0A        logger.debug(String.valueOf(count));%0A        Assert.assertEquals(0, count);
 %0A    %7D%0A%0A
 %7D%0A
@@ -3940,10 +3940,11 @@
 %0A    %7D%0A%0A
+%0A
 %7D%0A

improve unit test for detecting Database Cascade Deletion Problem #616