|
| 1 | +package org.zstack.test.mevoco.billing; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.zstack.billing.*; |
| 7 | +import org.zstack.core.cloudbus.CloudBus; |
| 8 | +import org.zstack.core.componentloader.ComponentLoader; |
| 9 | +import org.zstack.core.db.DatabaseFacade; |
| 10 | +import org.zstack.header.allocator.HostCapacityOverProvisioningManager; |
| 11 | +import org.zstack.header.identity.AccountConstant; |
| 12 | +import org.zstack.header.identity.AccountInventory; |
| 13 | +import org.zstack.header.identity.SessionInventory; |
| 14 | +import org.zstack.header.storage.primary.PrimaryStorageOverProvisioningManager; |
| 15 | +import org.zstack.header.vm.VmInstanceInventory; |
| 16 | +import org.zstack.network.service.flat.FlatNetworkServiceSimulatorConfig; |
| 17 | +import org.zstack.simulator.kvm.KVMSimulatorConfig; |
| 18 | +import org.zstack.storage.primary.local.LocalStorageSimulatorConfig; |
| 19 | +import org.zstack.storage.primary.local.LocalStorageSimulatorConfig.Capacity; |
| 20 | +import org.zstack.test.Api; |
| 21 | +import org.zstack.test.ApiSenderException; |
| 22 | +import org.zstack.test.DBUtil; |
| 23 | +import org.zstack.test.WebBeanConstructor; |
| 24 | +import org.zstack.test.deployer.Deployer; |
| 25 | +import org.zstack.test.identity.IdentityCreator; |
| 26 | +import org.zstack.utils.Utils; |
| 27 | +import org.zstack.utils.data.SizeUnit; |
| 28 | +import org.zstack.utils.logging.CLogger; |
| 29 | + |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | +/** |
| 33 | + * after changing owner, the billing of the old owner should not increase |
| 34 | + */ |
| 35 | +public class TestBilling14 { |
| 36 | + CLogger logger = Utils.getLogger(TestBilling14.class); |
| 37 | + Deployer deployer; |
| 38 | + Api api; |
| 39 | + ComponentLoader loader; |
| 40 | + CloudBus bus; |
| 41 | + DatabaseFacade dbf; |
| 42 | + SessionInventory session; |
| 43 | + LocalStorageSimulatorConfig config; |
| 44 | + FlatNetworkServiceSimulatorConfig fconfig; |
| 45 | + KVMSimulatorConfig kconfig; |
| 46 | + PrimaryStorageOverProvisioningManager psRatioMgr; |
| 47 | + HostCapacityOverProvisioningManager hostRatioMgr; |
| 48 | + long totalSize = SizeUnit.GIGABYTE.toByte(100); |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setUp() throws Exception { |
| 52 | + DBUtil.reDeployDB(); |
| 53 | + WebBeanConstructor con = new WebBeanConstructor(); |
| 54 | + deployer = new Deployer("deployerXml/mevoco/TestMevoco.xml", con); |
| 55 | + deployer.addSpringConfig("mevocoRelated.xml"); |
| 56 | + deployer.addSpringConfig("billing.xml"); |
| 57 | + deployer.load(); |
| 58 | + |
| 59 | + loader = deployer.getComponentLoader(); |
| 60 | + bus = loader.getComponent(CloudBus.class); |
| 61 | + dbf = loader.getComponent(DatabaseFacade.class); |
| 62 | + config = loader.getComponent(LocalStorageSimulatorConfig.class); |
| 63 | + fconfig = loader.getComponent(FlatNetworkServiceSimulatorConfig.class); |
| 64 | + kconfig = loader.getComponent(KVMSimulatorConfig.class); |
| 65 | + psRatioMgr = loader.getComponent(PrimaryStorageOverProvisioningManager.class); |
| 66 | + hostRatioMgr = loader.getComponent(HostCapacityOverProvisioningManager.class); |
| 67 | + |
| 68 | + Capacity c = new Capacity(); |
| 69 | + c.total = totalSize; |
| 70 | + c.avail = totalSize; |
| 71 | + |
| 72 | + config.capacityMap.put("host1", c); |
| 73 | + |
| 74 | + deployer.build(); |
| 75 | + api = deployer.getApi(); |
| 76 | + session = api.loginAsAdmin(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void test() throws ApiSenderException, InterruptedException { |
| 81 | + BillingGlobalConfig.SAMPLING_INTERVAL.updateValue(1); |
| 82 | + |
| 83 | + VmInstanceInventory vm = deployer.vms.get("TestVm"); |
| 84 | + api.stopVmInstance(vm.getUuid()); |
| 85 | + |
| 86 | + TimeUnit.SECONDS.sleep(1); |
| 87 | + |
| 88 | + APICreateResourcePriceMsg msg = new APICreateResourcePriceMsg(); |
| 89 | + msg.setTimeUnit("s"); |
| 90 | + msg.setPrice(100f); |
| 91 | + msg.setResourceName(BillingConstants.SPENDING_CPU); |
| 92 | + PriceInventory priceinv = api.createPrice(msg); |
| 93 | + Assert.assertTrue(dbf.isExist(priceinv.getUuid(), PriceVO.class)); |
| 94 | + |
| 95 | + msg = new APICreateResourcePriceMsg(); |
| 96 | + msg.setTimeUnit("s"); |
| 97 | + msg.setPrice(10f); |
| 98 | + msg.setResourceName(BillingConstants.SPENDING_MEMORY); |
| 99 | + msg.setResourceUnit("m"); |
| 100 | + api.createPrice(msg); |
| 101 | + |
| 102 | + msg = new APICreateResourcePriceMsg(); |
| 103 | + msg.setTimeUnit("s"); |
| 104 | + msg.setPrice(9f); |
| 105 | + msg.setResourceName(BillingConstants.SPENDING_ROOT_VOLUME); |
| 106 | + msg.setResourceUnit("m"); |
| 107 | + api.createPrice(msg); |
| 108 | + |
| 109 | + long startTime = System.currentTimeMillis(); |
| 110 | + long during = 5; |
| 111 | + api.startVmInstance(vm.getUuid()); |
| 112 | + TimeUnit.SECONDS.sleep(during); |
| 113 | + |
| 114 | + IdentityCreator identityCreator = new IdentityCreator(api); |
| 115 | + AccountInventory account = identityCreator.createAccount("root", "password"); |
| 116 | + api.changeResourceOwner(vm.getUuid(), account.getUuid()); |
| 117 | + |
| 118 | + TimeUnit.SECONDS.sleep(2); |
| 119 | + //APICalculateAccountSpendingReply reply1 = api.calculateSpending(AccountConstant.INITIAL_SYSTEM_ADMIN_UUID, startTime, System.currentTimeMillis(), null); |
| 120 | + APICalculateAccountSpendingReply reply1 = api.calculateSpending(AccountConstant.INITIAL_SYSTEM_ADMIN_UUID, startTime, null, null); |
| 121 | + TimeUnit.SECONDS.sleep(3); |
| 122 | + APICalculateAccountSpendingReply reply2 = api.calculateSpending(AccountConstant.INITIAL_SYSTEM_ADMIN_UUID, startTime, System.currentTimeMillis(), null); |
| 123 | + Assert.assertEquals(reply1.getTotal(), reply2.getTotal(), 0.2); |
| 124 | + } |
| 125 | +} |
0 commit comments