package org.zstack.test.storage.primary.local;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.zstack.compute.vm.VmGlobalConfig;
import org.zstack.core.cloudbus.CloudBus;
import org.zstack.core.componentloader.ComponentLoader;
import org.zstack.core.db.DatabaseFacade;
import org.zstack.core.db.SimpleQuery;
import org.zstack.core.db.SimpleQuery.Op;
import org.zstack.header.host.HostInventory;
import org.zstack.header.identity.SessionInventory;
import org.zstack.header.image.ImageDeletionPolicyManager.ImageDeletionPolicy;
import org.zstack.header.image.ImageInventory;
import org.zstack.header.storage.primary.ImageCacheVO;
import org.zstack.header.storage.primary.ImageCacheVO_;
import org.zstack.header.storage.primary.PrimaryStorageInventory;
import org.zstack.header.vm.VmInstanceDeletionPolicyManager.VmInstanceDeletionPolicy;
import org.zstack.header.vm.VmInstanceInventory;
import org.zstack.image.ImageGlobalConfig;
import org.zstack.storage.primary.local.LocalStorageKvmBackend.CacheInstallPath;
import org.zstack.storage.primary.local.LocalStorageKvmBackend.DeleteBitsCmd;
import org.zstack.storage.primary.local.LocalStorageSimulatorConfig;
import org.zstack.storage.primary.local.LocalStorageSimulatorConfig.Capacity;
import org.zstack.test.Api;
import org.zstack.test.ApiSenderException;
import org.zstack.test.DBUtil;
import org.zstack.test.WebBeanConstructor;
import org.zstack.test.deployer.Deployer;
import org.zstack.utils.data.SizeUnit;
import java.util.concurrent.TimeUnit;
/**
* 1. two local storage running two VMs with the same image
* 2. delete the image and two VMs
* 3. clean up image cache on the local storage 1
* <p>
* confirm the image cache of the local storage 1 get cleaned up
* confirm the image cache of the local storage 2 doesn't get cleaned up
*/
public class TestLocalStorage51 {
Deployer deployer;
Api api;
ComponentLoader loader;
CloudBus bus;
DatabaseFacade dbf;
SessionInventory session;
LocalStorageSimulatorConfig config;
long totalSize = SizeUnit.GIGABYTE.toByte(100);
@Before
public void setUp() throws Exception {
DBUtil.reDeployDB();
WebBeanConstructor con = new WebBeanConstructor();
deployer = new Deployer("deployerXml/localStorage/TestLocalStorage51.xml", con);
deployer.addSpringConfig("KVMRelated.xml");
deployer.addSpringConfig("localStorageSimulator.xml");
deployer.addSpringConfig("localStorage.xml");
deployer.load();
loader = deployer.getComponentLoader();
bus = loader.getComponent(CloudBus.class);
dbf = loader.getComponent(DatabaseFacade.class);
config = loader.getComponent(LocalStorageSimulatorConfig.class);
Capacity c = new Capacity();
c.total = totalSize;
c.avail = totalSize;
config.capacityMap.put("host1", c);
config.capacityMap.put("host2", c);
deployer.build();
api = deployer.getApi();
session = api.loginAsAdmin();
}
@Test
public void test() throws ApiSenderException, InterruptedException {
ImageGlobalConfig.DELETION_POLICY.updateValue(ImageDeletionPolicy.Direct.toString());
ImageInventory image1 = deployer.images.get("TestImage");
api.deleteImage(image1.getUuid());
VmGlobalConfig.VM_DELETION_POLICY.updateValue(VmInstanceDeletionPolicy.Direct.toString());
VmInstanceInventory vm1 = deployer.vms.get("TestVm");
api.destroyVmInstance(vm1.getUuid());
VmInstanceInventory vm2 = deployer.vms.get("TestVm1");
api.destroyVmInstance(vm2.getUuid());
HostInventory host1 = deployer.hosts.get("host1");
HostInventory host2 = deployer.hosts.get("host2");
SimpleQuery<ImageCacheVO> q = dbf.createQuery(ImageCacheVO.class);
q.add(ImageCacheVO_.imageUuid, Op.EQ, image1.getUuid());
q.add(ImageCacheVO_.installUrl, Op.LIKE, String.format("%%%s%%", host1.getUuid()));
ImageCacheVO c = q.find();
CacheInstallPath path = new CacheInstallPath();
path.fullPath = c.getInstallUrl();
path.disassemble();
PrimaryStorageInventory local = deployer.primaryStorages.get("local");
config.deleteDirCmds.clear();
api.cleanupImageCache(local.getUuid());
TimeUnit.SECONDS.sleep(3);
Assert.assertEquals(1, config.deleteDirCmds.size());
DeleteBitsCmd cmd = config.deleteDirCmds.get(0);
Assert.assertEquals(path.installPath.substring(0, path.installPath.lastIndexOf("/")), cmd.getPath());
c = q.find();
Assert.assertNull(c);
q = dbf.createQuery(ImageCacheVO.class);
q.add(ImageCacheVO_.imageUuid, Op.EQ, image1.getUuid());
q.add(ImageCacheVO_.installUrl, Op.LIKE, String.format("%%%s%%", host2.getUuid()));
c = q.find();
Assert.assertNotNull(c);
}
}