예제 #1
0
 @Test
 public void testVirtUses1IfNoVcpu() {
   // Ensure that we start this test with no entitlements.
   consumer.getEntitlements().clear();
   consumer.setFact(IS_VIRT, "true");
   consumer.setFact(SOCKET_FACT, "4");
   consumer.setFact(CORES_FACT, "8");
   SuggestedQuantity suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date());
   assertEquals(new Long(1), suggested.getSuggested());
 }
예제 #2
0
 @Test
 public void testVirtIgnoresSockets() {
   // Ensure that we start this test with no entitlements.
   consumer.getEntitlements().clear();
   consumer.setFact(IS_VIRT, "true");
   consumer.setFact(SOCKET_FACT, "4");
   pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2");
   SuggestedQuantity suggested = quantityRules.getSuggestedQuantity(pool, consumer, new Date());
   assertEquals(new Long(1), suggested.getSuggested());
 }
예제 #3
0
  private void exportProducts(File baseDir, Consumer consumer) throws IOException {
    File productDir = new File(baseDir.getCanonicalPath(), "products");
    productDir.mkdir();

    Map<String, Product> products = new HashMap<String, Product>();
    for (Entitlement entitlement : consumer.getEntitlements()) {

      for (ProvidedProduct providedProduct : entitlement.getPool().getProvidedProducts()) {
        // Don't want to call the adapter if not needed, it can be expensive.
        if (!products.containsKey(providedProduct.getProductId())) {
          products.put(
              providedProduct.getProductId(),
              productAdapter.getProductById(providedProduct.getProductId()));
        }
      }

      // Don't forget the 'main' product!
      String productId = entitlement.getPool().getProductId();
      if (!products.containsKey(productId)) {
        products.put(productId, productAdapter.getProductById(productId));
      }
    }

    for (Product product : products.values()) {
      String path = productDir.getCanonicalPath();
      String productId = product.getId();
      File file = new File(path, productId + ".json");
      FileWriter writer = new FileWriter(file);
      productExporter.export(mapper, writer, product);
      writer.close();

      // Real products have a numeric id.
      if (StringUtils.isNumeric(product.getId())) {
        ProductCertificate cert = productAdapter.getProductCertificate(product);
        // XXX: not all product adapters implement getProductCertificate,
        // so just skip over this if we get null back
        // XXX: need to decide if the cert should always be in the export, or never.
        if (cert != null) {
          file = new File(productDir.getCanonicalPath(), product.getId() + ".pem");
          writer = new FileWriter(file);
          productCertExporter.export(writer, cert);
          writer.close();
        }
      }
    }
  }
예제 #4
0
  @Test
  public void createEntitlementShouldUpdateConsumer() throws Exception {
    Long numAvailEntitlements = 1L;

    Product newProduct = TestUtil.createProduct();
    productCurator.create(newProduct);

    Pool consumerPool =
        createPoolAndSub(
            owner,
            newProduct,
            numAvailEntitlements,
            TestUtil.createDate(2009, 11, 30),
            TestUtil.createDate(2050, 11, 30));
    poolCurator.create(consumerPool);

    assertEquals(0, consumer.getEntitlements().size());
    poolManager.entitleByProduct(consumer, newProduct.getId());

    assertEquals(1, consumerCurator.find(consumer.getId()).getEntitlements().size());
  }
예제 #5
0
  private void exportProducts(File baseDir, Consumer consumer) throws IOException {
    File productDir = new File(baseDir.getCanonicalPath(), "products");
    productDir.mkdir();

    Map<String, Product> products = new HashMap<String, Product>();
    for (Entitlement entitlement : consumer.getEntitlements()) {
      Pool pool = entitlement.getPool();

      for (Product providedProduct : pool.getProvidedProducts()) {
        products.put(providedProduct.getId(), providedProduct);
      }

      // Don't forget the 'main' product!
      Product product = pool.getProduct();
      products.put(product.getId(), product);

      // Also need to check for sub products
      Product derivedProduct = pool.getDerivedProduct();
      if (derivedProduct != null) {
        products.put(derivedProduct.getId(), derivedProduct);
      }

      for (Product derivedProvidedProduct : pool.getDerivedProvidedProducts()) {
        products.put(derivedProvidedProduct.getId(), derivedProvidedProduct);
      }
    }

    for (Product product : products.values()) {
      // Clear the owner and UUID so they can be re-generated/assigned on import
      // product.setUuid(null);
      // product.setOwner(null);

      String path = productDir.getCanonicalPath();
      String productId = product.getId();
      File file = new File(path, productId + ".json");
      FileWriter writer = null;
      try {
        writer = new FileWriter(file);
        productExporter.export(mapper, writer, product);
      } finally {
        if (writer != null) {
          writer.close();
        }
      }

      // Real products have a numeric id.
      if (StringUtils.isNumeric(product.getId())) {
        ProductCertificate cert =
            productAdapter.getProductCertificate(consumer.getOwner(), product.getId());

        // XXX: not all product adapters implement getProductCertificate,
        // so just skip over this if we get null back
        // XXX: need to decide if the cert should always be in the export, or never.
        if (cert != null) {
          file = new File(productDir.getCanonicalPath(), product.getId() + ".pem");
          writer = new FileWriter(file);
          productCertExporter.export(writer, cert);
          writer.close();
        }
      }
    }
  }
예제 #6
0
  @SuppressWarnings("unchecked")
  @Test
  public void exportProducts() throws Exception {
    config.setProperty(ConfigProperties.SYNC_WORK_DIR, "/tmp/");
    Consumer consumer = mock(Consumer.class);
    Entitlement ent = mock(Entitlement.class);
    ProvidedProduct pp = mock(ProvidedProduct.class);
    Pool pool = mock(Pool.class);
    Rules mrules = mock(Rules.class);
    Principal principal = mock(Principal.class);
    IdentityCertificate idcert = new IdentityCertificate();

    Set<ProvidedProduct> ppset = new HashSet<ProvidedProduct>();
    ppset.add(pp);

    Set<Entitlement> entitlements = new HashSet<Entitlement>();
    entitlements.add(ent);

    Product prod = new Product("12345", "RHEL Product");
    prod.setMultiplier(1L);
    prod.setCreated(new Date());
    prod.setUpdated(new Date());
    prod.setHref("http://localhost");
    prod.setAttributes(Collections.EMPTY_SET);

    Product prod1 = new Product("MKT-prod", "RHEL Product");
    prod1.setMultiplier(1L);
    prod1.setCreated(new Date());
    prod1.setUpdated(new Date());
    prod1.setHref("http://localhost");
    prod1.setAttributes(Collections.EMPTY_SET);

    ProductCertificate pcert = new ProductCertificate();
    pcert.setKey("euh0876puhapodifbvj094");
    pcert.setCert("hpj-08ha-w4gpoknpon*)&^%#");
    pcert.setCreated(new Date());
    pcert.setUpdated(new Date());

    when(pp.getProductId()).thenReturn("12345");
    when(pool.getProvidedProducts()).thenReturn(ppset);
    when(pool.getProductId()).thenReturn("MKT-prod");
    when(ent.getPool()).thenReturn(pool);
    when(mrules.getRules()).thenReturn("foobar");
    when(pki.getSHA256WithRSAHash(any(InputStream.class))).thenReturn("signature".getBytes());
    when(rc.getRules()).thenReturn(mrules);
    when(consumer.getEntitlements()).thenReturn(entitlements);
    when(psa.getProductById("12345")).thenReturn(prod);
    when(psa.getProductById("MKT-prod")).thenReturn(prod1);
    when(psa.getProductCertificate(any(Product.class))).thenReturn(pcert);
    when(pprov.get()).thenReturn(principal);
    when(principal.getUsername()).thenReturn("testUser");
    idcert.setSerial(new CertificateSerial(10L, new Date()));
    idcert.setKey("euh0876puhapodifbvj094");
    idcert.setCert("hpj-08ha-w4gpoknpon*)&^%#");
    idcert.setCreated(new Date());
    idcert.setUpdated(new Date());
    when(consumer.getIdCert()).thenReturn(idcert);

    KeyPair keyPair = createKeyPair();
    when(consumer.getKeyPair()).thenReturn(keyPair);
    when(pki.getPemEncoded(keyPair.getPrivateKey())).thenReturn("privateKey".getBytes());
    when(pki.getPemEncoded(keyPair.getPublicKey())).thenReturn("publicKey".getBytes());

    // FINALLY test this badboy
    Exporter e =
        new Exporter(
            ctc, me, ce, cte, re, ece, ecsa, pe, psa, pce, ec, ee, pki, config, exportRules, pprov);

    File export = e.getFullExport(consumer);

    // VERIFY
    assertNotNull(export);
    verifyContent(export, "export/products/12345.pem", new VerifyProductCert("12345.pem"));
    assertFalse(verifyHasEntry(export, "export/products/MKT-prod.pem"));
    FileUtils.deleteDirectory(export.getParentFile());
    assertTrue(new File("/tmp/consumer_export.zip").delete());
    assertTrue(new File("/tmp/12345.pem").delete());
  }