@Test public void hypervisorCheckInReportsFailureWhenGuestIdUpdateFails() throws Exception { Owner owner = new Owner("admin"); Map<String, List<GuestId>> hostGuestMap = new HashMap<String, List<GuestId>>(); String expectedHostVirtId = "test-host"; hostGuestMap.put(expectedHostVirtId, Arrays.asList(new GuestId("GUEST_B"))); Consumer existing = new Consumer(); existing.setUuid(expectedHostVirtId); existing.addGuestId(new GuestId("GUEST_A")); // Force update when(consumerCurator.findByUuid(eq(expectedHostVirtId))).thenReturn(existing); String expectedMessage = "Forced Exception."; RuntimeException exception = new RuntimeException(expectedMessage); // Simulate failure when checking the owner when(consumerCurator.getHost(any(String.class))).thenThrow(exception); HypervisorCheckInResult result = hypervisorResource.hypervisorCheckIn(hostGuestMap, principal, owner.getKey()); Set<String> failures = result.getFailedUpdate(); assertEquals(1, failures.size()); assertEquals(expectedHostVirtId + ": " + expectedMessage, failures.iterator().next()); }
@Test public void hypervisorCheckInUpdatesGuestIdsWhenHostConsumerExists() throws Exception { Owner owner = new Owner("admin"); Map<String, List<GuestId>> hostGuestMap = new HashMap<String, List<GuestId>>(); hostGuestMap.put("test-host", Arrays.asList(new GuestId("GUEST_B"))); Owner o = new Owner(); o.setId("owner-id"); Consumer existing = new Consumer(); existing.setUuid("test-host"); existing.setOwner(o); existing.addGuestId(new GuestId("GUEST_A")); when(consumerCurator.findByUuid(eq("test-host"))).thenReturn(existing); HypervisorCheckInResult result = hypervisorResource.hypervisorCheckIn(hostGuestMap, principal, owner.getKey()); Set<Consumer> updated = result.getUpdated(); assertEquals(1, updated.size()); Consumer c1 = updated.iterator().next(); assertEquals("test-host", c1.getUuid()); assertEquals(1, c1.getGuestIds().size()); assertEquals("GUEST_B", c1.getGuestIds().get(0).getGuestId()); }
@Test public void hypervisorCheckInCreatesNewConsumer() throws Exception { Owner owner = new Owner("admin"); Map<String, List<GuestId>> hostGuestMap = new HashMap<String, List<GuestId>>(); hostGuestMap.put("test-host", Arrays.asList(new GuestId("GUEST_A"), new GuestId("GUEST_B"))); when(consumerCurator.findByUuid(eq("test-host"))).thenReturn(null); when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner); when(principal.canAccess(eq(owner), eq(Access.ALL))).thenReturn(true); when(consumerTypeCurator.lookupByLabel(eq(ConsumerTypeEnum.HYPERVISOR.getLabel()))) .thenReturn(hypervisorType); when(idCertService.generateIdentityCert(any(Consumer.class))) .thenReturn(new IdentityCertificate()); HypervisorCheckInResult result = hypervisorResource.hypervisorCheckIn(hostGuestMap, principal, owner.getKey()); Set<Consumer> created = result.getCreated(); assertEquals(1, created.size()); Consumer c1 = created.iterator().next(); assertEquals("test-host", c1.getUuid()); assertEquals(2, c1.getGuestIds().size()); assertEquals("GUEST_A", c1.getGuestIds().get(0).getGuestId()); assertEquals("GUEST_B", c1.getGuestIds().get(1).getGuestId()); assertEquals("x86_64", c1.getFact("uname.machine")); assertEquals("hypervisor", c1.getType().getLabel()); }
@Test public void bindByProductsString() throws Exception { String[] pids = {"prod1", "prod2", "prod3"}; when(cc.findByUuid(eq("abcd1234"))).thenReturn(consumer); entitler.bindByProducts(pids, "abcd1234", null, null); AutobindData data = AutobindData.create(consumer).forProducts(pids); verify(pm).entitleByProducts(eq(data)); }
@Test(expected = BadRequestException.class) public void nullPool() throws EntitlementRefusedException { String poolid = "foo"; Consumer c = TestUtil.createConsumer(); // keeps me from casting null Map<String, Integer> pQs = new HashMap<String, Integer>(); pQs.put(poolid, 1); when(cc.findByUuid(eq(c.getUuid()))).thenReturn(c); when(pm.entitleByPools(eq(c), eq(pQs))).thenThrow(new IllegalArgumentException()); entitler.bindByPoolQuantities(c.getUuid(), pQs); }
@Test public void bindByPoolString() throws EntitlementRefusedException { String poolid = "pool10"; Entitlement ent = mock(Entitlement.class); List<Entitlement> eList = new ArrayList<Entitlement>(); eList.add(ent); when(cc.findByUuid(eq("abcd1234"))).thenReturn(consumer); Map<String, Integer> pQs = new HashMap<String, Integer>(); pQs.put(poolid, 1); when(pm.entitleByPools(eq(consumer), eq(pQs))).thenReturn(eList); List<Entitlement> ents = entitler.bindByPoolQuantities("abcd1234", pQs); assertNotNull(ents); assertEquals(ent, ents.get(0)); }
@Test public void testRevokesLapsedUnmappedGuestEntitlementsOnAutoHeal() throws Exception { Owner owner1 = new Owner("o1"); Product product = TestUtil.createProduct(); Pool p1 = TestUtil.createPool(owner1, product); p1.addAttribute(new PoolAttribute("unmapped_guests_only", "true")); Date thirtySixHoursAgo = new Date(new Date().getTime() - 36L * 60L * 60L * 1000L); Date twelveHoursAgo = new Date(new Date().getTime() - 12L * 60L * 60L * 1000L); Consumer c; c = TestUtil.createConsumer(owner1); c.setCreated(thirtySixHoursAgo); c.setFact("virt.uuid", "1"); Entitlement e1 = TestUtil.createEntitlement(owner1, c, p1, null); e1.setEndDateOverride(twelveHoursAgo); Set<Entitlement> entitlementSet1 = new HashSet<Entitlement>(); entitlementSet1.add(e1); p1.setEntitlements(entitlementSet1); CandlepinQuery cqmock = mock(CandlepinQuery.class); when(cqmock.iterator()).thenReturn(Arrays.asList(e1).iterator()); when(entitlementCurator.findByPoolAttribute(eq(c), eq("unmapped_guests_only"), eq("true"))) .thenReturn(cqmock); String[] pids = {product.getId(), "prod2"}; when(cc.findByUuid(eq("abcd1234"))).thenReturn(c); entitler.bindByProducts(pids, "abcd1234", null, null); AutobindData data = AutobindData.create(c).forProducts(pids); verify(pm).entitleByProducts(eq(data)); verify(pm).revokeEntitlement(e1); }
@Test public void hypervisorCheckInReportsFailuresOnCreateFailure() { Owner owner = new Owner("admin"); Map<String, List<GuestId>> hostGuestMap = new HashMap<String, List<GuestId>>(); String expectedHostVirtId = "test-host-id"; hostGuestMap.put( expectedHostVirtId, Arrays.asList(new GuestId("GUEST_A"), new GuestId("GUEST_B"))); // Force create. when(consumerCurator.findByUuid(eq(expectedHostVirtId))).thenReturn(null); String expectedMessage = "Forced Exception."; RuntimeException exception = new RuntimeException(expectedMessage); // Simulate failure when checking the owner when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenThrow(exception); HypervisorCheckInResult result = hypervisorResource.hypervisorCheckIn(hostGuestMap, principal, owner.getKey()); Set<String> failures = result.getFailedUpdate(); assertEquals(1, failures.size()); assertEquals(expectedHostVirtId + ": " + expectedMessage, failures.iterator().next()); }