/** * Copyright (c) 2009 - 2012 Red Hat, Inc. * * This software is licensed to you under the GNU General Public License, * version 2 (GPLv2). There is NO WARRANTY for this software, express or * implied, including the implied warranties of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 * along with this software; if not, see * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. * * Red Hat trademarks are not licensed under GPLv2. No permission is * granted to use or replicate Red Hat trademarks that are incorporated * in this software or its documentation. */ package org.candlepin.resource; import static org.junit.Assert.*; import org.candlepin.common.config.Configuration; import org.candlepin.config.CandlepinCommonTestConfig; import org.candlepin.config.ConfigProperties; import org.candlepin.controller.PoolManager; import org.candlepin.model.Consumer; import org.candlepin.model.ConsumerType; import org.candlepin.model.Owner; import org.candlepin.model.Pool; import org.candlepin.model.Product; import org.candlepin.model.dto.Subscription; import org.candlepin.policy.js.entitlement.Enforcer; import org.candlepin.policy.js.entitlement.EntitlementRules; import org.candlepin.service.SubscriptionServiceAdapter; import org.candlepin.service.impl.ImportSubscriptionServiceAdapter; import org.candlepin.test.DatabaseTestFixture; import org.candlepin.test.TestUtil; import org.candlepin.util.Util; import com.google.inject.AbstractModule; import com.google.inject.Module; import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import javax.inject.Inject; /** * ConsumerResourceVirtEntitlementTest */ public class ConsumerResourceVirtEntitlementTest extends DatabaseTestFixture { @Inject private ConsumerResource consumerResource; @Inject private PoolManager poolManager; @Inject private SubscriptionServiceAdapter subAdapter; private ConsumerType manifestType; private ConsumerType systemType; private Consumer manifestConsumer; private Consumer systemConsumer; private Product productLimit; private Product productUnlimit; private List<Pool> limitPools; private List<Pool> unlimitPools; private Owner owner; @Override protected Module getGuiceOverrideModule() { return new ConsumerResourceVirtEntitlementModule(); } @Before public void setUp() { List<Subscription> subscriptions = new ArrayList<Subscription>(); subAdapter = new ImportSubscriptionServiceAdapter(subscriptions); manifestType = consumerTypeCurator.create( new ConsumerType(ConsumerType.ConsumerTypeEnum.CANDLEPIN)); systemType = consumerTypeCurator.create( new ConsumerType(ConsumerType.ConsumerTypeEnum.SYSTEM)); owner = ownerCurator.create(new Owner("test-owner")); ownerCurator.create(owner); manifestConsumer = TestUtil.createConsumer(manifestType, owner); consumerCurator.create(manifestConsumer); systemConsumer = TestUtil.createConsumer(systemType, owner); consumerCurator.create(systemConsumer); // create a physical pool with numeric virt_limit productLimit = TestUtil.createProduct(); productLimit.setAttribute(Product.Attributes.VIRT_LIMIT, "10"); productLimit.setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes"); productLimit = this.createProduct(productLimit, owner); Subscription limitSub = TestUtil.createSubscription(owner, productLimit, new HashSet<Product>()); limitSub.setId(Util.generateDbUUID()); limitSub.setQuantity(10L); limitSub.setStartDate(TestUtil.createDate(2010, 1, 1)); limitSub.setEndDate(TestUtil.createDate(2020, 1, 1)); limitSub.setModified(TestUtil.createDate(2000, 1, 1)); subscriptions.add(limitSub); limitPools = poolManager.createAndEnrichPools(limitSub); // create a physical pool with unlimited virt_limit productUnlimit = TestUtil.createProduct(); productUnlimit.setAttribute(Product.Attributes.VIRT_LIMIT, "unlimited"); productUnlimit.setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes"); productUnlimit = this.createProduct(productUnlimit, owner); Subscription unlimitSub = TestUtil.createSubscription(owner, productUnlimit, new HashSet<Product>()); unlimitSub.setId(Util.generateDbUUID()); unlimitSub.setQuantity(10L); unlimitSub.setStartDate(TestUtil.createDate(2010, 1, 1)); unlimitSub.setEndDate(TestUtil.createDate(2020, 1, 1)); unlimitSub.setModified(TestUtil.createDate(2000, 1, 1)); subscriptions.add(unlimitSub); unlimitPools = poolManager.createAndEnrichPools(unlimitSub); } /** * Checking behavior when the physical pool has a numeric virt_limit */ @Test public void testLimitPool() { List<Pool> subscribedTo = new ArrayList<Pool>(); Consumer guestConsumer = TestUtil.createConsumer(systemType, owner); guestConsumer.setFact("virt.is_guest", "true"); consumerCurator.create(guestConsumer); Pool parentPool = null; assertTrue(limitPools != null && limitPools.size() == 2); for (Pool p : limitPools) { // bonus pools have the attribute pool_derived if (null != p.getAttributeValue(Pool.Attributes.DERIVED_POOL)) { // consume 2 times so one can get revoked later. consumerResource.bind(guestConsumer.getUuid(), p.getId(), null, 10, null, null, false, null, null, null, null); consumerResource.bind(guestConsumer.getUuid(), p.getId(), null, 10, null, null, false, null, null, null, null); p = poolManager.find(p.getId()); // ensure the correct # consumed from the bonus pool assertTrue(p.getConsumed() == 20); assertTrue(p.getQuantity() == 100); // keep this list so we don't need to search again subscribedTo.add(p); } else { parentPool = p; } } // manifest consume from the physical pool and then check bonus pool quantities consumerResource.bind(manifestConsumer.getUuid(), parentPool.getId(), null, 7, null, null, false, null, null, null, null); for (Pool p : subscribedTo) { p = poolManager.find(p.getId()); assertTrue(p.getConsumed() == 20); assertTrue(p.getQuantity() == 30); } // manifest consume from the physical pool and then check bonus pool quantities. // Should result in a revocation of one of the 10 count entitlements. consumerResource.bind(manifestConsumer.getUuid(), parentPool.getId(), null, 2, null, null, false, null, null, null, null); for (Pool p : subscribedTo) { p = poolManager.find(p.getId()); assertTrue(p.getConsumed() == 10); assertTrue(p.getQuantity() == 10); } // system consume from the physical pool and then check bonus pool quantities. // Should result in no change in the entitlements for the guest. consumerResource.bind(systemConsumer.getUuid(), parentPool.getId(), null, 1, null, null, false, null, null, null, null); for (Pool p : subscribedTo) { p = poolManager.find(p.getId()); assertTrue(p.getConsumed() == 10); assertTrue(p.getQuantity() == 10); } } @Test public void testUnlimitPool() { List<Pool> subscribedTo = new ArrayList<Pool>(); Consumer guestConsumer = TestUtil.createConsumer(systemType, owner); guestConsumer.setFact("virt.is_guest", "true"); consumerCurator.create(guestConsumer); Pool parentPool = null; assertTrue(unlimitPools != null && unlimitPools.size() == 2); for (Pool p : unlimitPools) { // bonus pools have the attribute pool_derived if (null != p.getAttributeValue(Pool.Attributes.DERIVED_POOL)) { // consume 2 times so they can get revoked separately. consumerResource.bind(guestConsumer.getUuid(), p.getId(), null, 10, null, null, false, null, null, null, null); consumerResource.bind(guestConsumer.getUuid(), p.getId(), null, 10, null, null, false, null, null, null, null); p = poolManager.find(p.getId()); assertTrue(p.getConsumed() == 20); assertTrue(p.getQuantity() == -1); poolManager.getRefresher(subAdapter).add(owner).run(); // double check after pools refresh assertTrue(p.getConsumed() == 20); assertTrue(p.getQuantity() == -1); subscribedTo.add(p); } else { parentPool = p; } } // Incomplete consumption of physical pool leaves unlimited pool unchanged. consumerResource.bind(manifestConsumer.getUuid(), parentPool.getId(), null, 7, null, null, false, null, null, null, null); for (Pool p : subscribedTo) { assertTrue(p.getConsumed() == 20); assertTrue(p.getQuantity() == -1); } // Full consumption of physical pool causes revocation of bonus pool entitlements // and quantity change to 0 consumerResource.bind(manifestConsumer.getUuid(), parentPool.getId(), null, 3, null, null, false, null, null, null, null); for (Pool p : subscribedTo) { p = poolManager.find(p.getId()); assertEquals(new Long(0), p.getConsumed()); assertTrue(p.getQuantity() == 0); } } private static class ConsumerResourceVirtEntitlementModule extends AbstractModule { @Override protected void configure() { Configuration config = new CandlepinCommonTestConfig(); config.setProperty(ConfigProperties.STANDALONE, "false"); config.setProperty(ConfigProperties.CONSUMER_FACTS_MATCHER, "^virt.*"); config.setProperty(ConfigProperties.CONSUMER_SYSTEM_NAME_PATTERN, "[\\#\\?\\'\\`\\!@{}()\\[\\]\\?&\\w-\\.]+"); config.setProperty(ConfigProperties.CONSUMER_PERSON_NAME_PATTERN, "[\\#\\?\\'\\`\\!@{}()\\[\\]\\?&\\w-\\.]+"); bind(Configuration.class).toInstance(config); bind(Enforcer.class).to(EntitlementRules.class); } } }