def test_unsubscribe_unregistered(self): connection.UEPConnection = StubUEP prod = StubProduct('stub_product') ent = StubEntitlementCertificate(prod) cmd = managercli.UnSubscribeCommand(ent_dir=StubEntitlementDirectory( [ent]), prod_dir=StubProductDirectory([])) managercli.ConsumerIdentity = StubConsumerIdentity StubConsumerIdentity.existsAndValid = classmethod(lambda cls: False) StubConsumerIdentity.exists = classmethod(lambda cls: False) cmd.main(['unsubscribe', '--all']) self.assertTrue(cmd.entitlement_dir.list_called) self.assertTrue(ent.is_deleted) prod = StubProduct('stub_product') ent1 = StubEntitlementCertificate(prod) ent2 = StubEntitlementCertificate(prod) cmd = managercli.UnSubscribeCommand(ent_dir=StubEntitlementDirectory( [ent1, ent2]), prod_dir=StubProductDirectory([])) managercli.ConsumerIdentity = StubConsumerIdentity StubConsumerIdentity.existsAndValid = classmethod(lambda cls: False) StubConsumerIdentity.exists = classmethod(lambda cls: False) cmd.main(['unsubscribe', '--serial=%s' % ent1.serial]) self.assertTrue(cmd.entitlement_dir.list_called) self.assertTrue(ent1.is_deleted) self.assertFalse(ent2.is_deleted)
def test_unsubscribe_unregistered(self): prod = StubProduct('stub_product') ent = StubEntitlementCertificate(prod) inj.provide(inj.ENT_DIR, StubEntitlementDirectory([ent])) inj.provide(inj.PROD_DIR, StubProductDirectory([])) cmd = managercli.RemoveCommand() self._inject_mock_invalid_consumer() cmd.main(['remove', '--all']) self.assertTrue(cmd.entitlement_dir.list_called) self.assertTrue(ent.is_deleted) prod = StubProduct('stub_product') ent1 = StubEntitlementCertificate(prod) ent2 = StubEntitlementCertificate(prod) ent3 = StubEntitlementCertificate(prod) inj.provide(inj.ENT_DIR, StubEntitlementDirectory([ent1, ent2, ent3])) inj.provide(inj.PROD_DIR, StubProductDirectory([])) cmd = managercli.RemoveCommand() cmd.main([ 'remove', '--serial=%s' % ent1.serial, '--serial=%s' % ent3.serial ]) self.assertTrue(cmd.entitlement_dir.list_called) self.assertTrue(ent1.is_deleted) self.assertFalse(ent2.is_deleted) self.assertTrue(ent3.is_deleted)
def setUp(self): # Setup mock product and entitlement certs: self.prod_dir = StubCertificateDirectory([ # Will be unentitled: StubProductCertificate(StubProduct(INST_PID_1)), # Will be entitled: StubProductCertificate(StubProduct(INST_PID_2)), # Will be entitled but expired: StubProductCertificate(StubProduct(INST_PID_3)), ]) self.ent_dir = StubEntitlementDirectory([ StubEntitlementCertificate(StubProduct(INST_PID_2)), StubEntitlementCertificate( StubProduct(INST_PID_3), start_date=datetime.now() - timedelta(days=365), end_date=datetime.now() - timedelta(days=2)), StubEntitlementCertificate( StubProduct(INST_PID_4), start_date=datetime.now() - timedelta(days=365), end_date=datetime.now() + timedelta(days=365)), StubEntitlementCertificate(StubProduct(INST_PID_5)), # entitled, but not installed StubEntitlementCertificate(StubProduct('not_installed_product')), ])
def setUp(self): super(TestPoolTypeCache, self).setUp() self.cp_provider = inj.require(inj.CP_PROVIDER) self.cp_provider.consumer_auth_cp = Mock() self.cp = self.cp_provider.consumer_auth_cp certs = [StubEntitlementCertificate(StubProduct('pid1'), pool=StubPool('someid'))] self.ent_dir = StubEntitlementDirectory(certificates=certs)
def test_scan_for_expired_or_future_products(self): prod_dir = StubProductDirectory(pids=["a", "b", "c", "d", "e"]) ent_dir = StubEntitlementDirectory([ StubEntitlementCertificate(StubProduct("a")), StubEntitlementCertificate(StubProduct("b")), StubEntitlementCertificate(StubProduct("c")), StubEntitlementCertificate( StubProduct("d"), start_date=datetime.now() - timedelta(days=365), end_date=datetime.now() - timedelta(days=2)), StubEntitlementCertificate( StubProduct("e"), start_date=datetime.now() + timedelta(days=365), end_date=datetime.now() + timedelta(days=730)), ]) inj.provide(inj.PROD_DIR, prod_dir) inj.provide(inj.ENT_DIR, ent_dir) sorter = StubCertSorter() sorter.valid_products = {"a": StubProduct("a")} sorter.partially_valid_products = {"b": StubProduct("b")} sorter._scan_entitlement_certs() self.assertEquals(["d"], sorter.expired_products.keys()) self.assertEquals(["e"], sorter.future_products.keys()) self.assertEquals(3, len(sorter.valid_entitlement_certs))
def test_expired_are_not_ignored_when_installing_certs(self, write_mock): valid_ent = StubEntitlementCertificate(StubProduct("PValid")) expired_ent = StubEntitlementCertificate( StubProduct("PExpired"), start_date=datetime.now() - timedelta(days=365), end_date=datetime.now() - timedelta(days=10)) cp_certificates = [valid_ent, expired_ent] mock_uep = Mock() mock_uep.getCertificates.return_value = cp_certificates # Passed into build(cert) update_action = TestingUpdateAction(mock_uep, StubEntitlementDirectory([])) report = UpdateReport() report.expected.append(valid_ent.serial) report.expected.append(expired_ent.serial) exceptions = update_action.install( [valid_ent.serial, expired_ent.serial], report) self.assertEqual(0, len(exceptions), "No exceptions should have been thrown") self.assertTrue(valid_ent in report.added) self.assertTrue(valid_ent.serial in report.expected) self.assertTrue(expired_ent.serial in report.expected)
def setUp(self, mock_update): SubManFixture.setUp(self) # Setup mock product and entitlement certs: self.prod_dir = StubProductDirectory( pids=[INST_PID_1, INST_PID_2, INST_PID_3, INST_PID_4]) self.ent_dir = StubEntitlementDirectory([ StubEntitlementCertificate(PROD_2, ent_id=ENT_ID_2), StubEntitlementCertificate(PROD_1, ent_id=ENT_ID_1), StubEntitlementCertificate(product=PROD_4, stacking_id=STACK_1, ent_id=ENT_ID_4), # entitled, but not installed StubEntitlementCertificate(StubProduct('not_installed_product', name="Some Product"), ent_id="SomeSubId"), ]) self.mock_uep = StubUEP() self.status_mgr = EntitlementStatusCache() self.status_mgr.load_status = Mock(return_value=SAMPLE_COMPLIANCE_JSON) self.status_mgr.write_cache = Mock() inj.provide(inj.ENTITLEMENT_STATUS_CACHE, self.status_mgr) inj.provide(inj.PROD_DIR, self.prod_dir) inj.provide(inj.ENT_DIR, self.ent_dir) self.sorter = CertSorter() self.sorter.is_registered = Mock(return_value=True)
def _stub_content(self, include_content_access=False): stub_prod = StubProduct('stub_product', provided_tags="stub-product") stub_content = StubContent("a_test_repo", required_tags="stub-product") stub_content2 = StubContent("test_repo_2", required_tags="stub-product") stub_ent_cert = StubEntitlementCertificate(stub_prod, content=[stub_content]) stub_prod_cert = StubProductCertificate(stub_prod) certs = [stub_ent_cert] if include_content_access: self.stub_content_access_cert = StubEntitlementCertificate( stub_prod, content=[stub_content, stub_content2], entitlement_type=CONTENT_ACCESS_CERT_TYPE) # content access cert is first and last, so naively wrong implementations will prioritize it. certs = [ self.stub_content_access_cert, stub_ent_cert, self.stub_content_access_cert ] stub_ent_dir = StubEntitlementDirectory(certs) stub_prod_dir = StubProductDirectory([stub_prod_cert]) inj.provide(inj.ENT_DIR, stub_ent_dir) inj.provide(inj.PROD_DIR, stub_prod_dir)
def test_unsubscribe_registered(self): prod = StubProduct('stub_product') ent1 = StubEntitlementCertificate(prod) ent2 = StubEntitlementCertificate(prod) ent3 = StubEntitlementCertificate(prod) inj.provide(inj.ENT_DIR, StubEntitlementDirectory([ent1, ent2, ent3])) inj.provide(inj.PROD_DIR, StubProductDirectory([])) cmd = managercli.UnSubscribeCommand() mock_identity = self._inject_mock_valid_consumer() managercli.EntCertActionInvoker = StubEntActionInvoker cmd.main(['unsubscribe', '--all']) self.assertEquals(cmd.cp.called_unbind_uuid, mock_identity.uuid) cmd.main(['unsubscribe', '--serial=%s' % ent1.serial]) self.assertEquals(cmd.cp.called_unbind_serial, ['%s' % ent1.serial]) code = cmd.main([ 'unsubscribe', '--serial=%s' % ent2.serial, '--serial=%s' % ent3.serial ]) self.assertEquals( cmd.cp.called_unbind_serial, ['%s' % ent2.serial, '%s' % ent3.serial]) self.assertEquals(code, 0) self.stub_cp_provider.get_consumer_auth_cp( ).unbindBySerial = mock.Mock(side_effect=connection.RestlibException( "Entitlement Certificate with serial number " "2300922701043065601 could not be found.")) code = cmd.main(['unsubscribe', '--serial=%s' % '2300922701043065601'])
def setUp(self): super(RepoUpdateActionTests, self).setUp() stub_prod = StubProduct("fauxprod", provided_tags="TAG1,TAG2") stub_prod2 = StubProduct("fauxprovidedprod", provided_tags="TAG4") stub_prod_cert = StubProductCertificate(stub_prod, provided_products=[stub_prod2]) stub_prod2 = StubProduct("fauxprod2", provided_tags="TAG5,TAG6") stub_prod2_cert = StubProductCertificate(stub_prod2) prod_dir = StubProductDirectory([stub_prod_cert, stub_prod2_cert]) inj.provide(inj.PROD_DIR, prod_dir) stub_content = [ StubContent("c1", required_tags="", gpg=None), # no required tags StubContent("c2", required_tags="TAG1", gpg=""), StubContent("c3", required_tags="TAG1,TAG2,TAG3"), # should get skipped StubContent("c4", required_tags="TAG1,TAG2,TAG4,TAG5,TAG6", gpg="/gpg.key", url="/$some/$path"), StubContent("c5", content_type="file", required_tags="", gpg=None), StubContent("c6", content_type="file", required_tags="", gpg=None), ] self.stub_ent_cert = StubEntitlementCertificate(stub_prod, content=stub_content) ent_dir = StubEntitlementDirectory([self.stub_ent_cert]) inj.provide(inj.ENT_DIR, ent_dir) repolib.ConsumerIdentity = StubConsumerIdentity
def test_print_consumed_no_ents(self): ent_dir = StubEntitlementDirectory([]) try: self.cc.print_consumed(ent_dir) self.fail("Should have exited.") except SystemExit: pass
def test_print_consumed_prints_nothing_with_no_service_level_match(self): ent_dir = StubEntitlementDirectory([self.cert_with_service_level]) try: self.cc.print_consumed(ent_dir, service_level="NotFound") self.fail("Should have exited since an entitlement with the " + \ "specified service level does not exist.") except SystemExit: pass
def setUp(self): self.fact_cache_dir = tempfile.mkdtemp() fact_cache = self.fact_cache_dir + "/facts.json" fd = open(fact_cache, "w") fd.write(facts_buf) fd.close() self.f = facts.Facts(ent_dir=StubEntitlementDirectory([]), prod_dir=StubProductDirectory([])) self.f.CACHE_FILE = fact_cache
def test_unsubscribe_unregistered(self): connection.UEPConnection = StubUEP prod = StubProduct('stub_product') ent = StubEntitlementCertificate(prod) inj.provide(inj.ENT_DIR, StubEntitlementDirectory([ent])) inj.provide(inj.PROD_DIR, StubProductDirectory([])) cmd = managercli.UnSubscribeCommand() managercli.ConsumerIdentity = StubConsumerIdentity StubConsumerIdentity.existsAndValid = classmethod(lambda cls: False) StubConsumerIdentity.exists = classmethod(lambda cls: False) cmd.main(['unsubscribe', '--all']) self.assertTrue(cmd.entitlement_dir.list_called) self.assertTrue(ent.is_deleted) prod = StubProduct('stub_product') ent1 = StubEntitlementCertificate(prod) ent2 = StubEntitlementCertificate(prod) ent3 = StubEntitlementCertificate(prod) inj.provide(inj.ENT_DIR, StubEntitlementDirectory([ent1, ent2, ent3])) inj.provide(inj.PROD_DIR, StubProductDirectory([])) cmd = managercli.UnSubscribeCommand() managercli.ConsumerIdentity = StubConsumerIdentity StubConsumerIdentity.existsAndValid = classmethod(lambda cls: False) StubConsumerIdentity.exists = classmethod(lambda cls: False) code = cmd.main([ 'unsubscribe', '--serial=%s' % ent1.serial, '--serial=%s' % ent3.serial ]) self.assertTrue(cmd.entitlement_dir.list_called) self.assertTrue(ent1.is_deleted) self.assertFalse(ent2.is_deleted) self.assertTrue(ent3.is_deleted) self.assertEquals(code, 0) code = cmd.main(['unsubscribe', '--serial=%s' % '33333333']) self.assertEquals(code, 1)
def setUp(self): self.cc = self.command_class(ent_dir=StubEntitlementDirectory([]), prod_dir=StubProductDirectory([])) # neuter the _do_command, since this is mostly # for testing arg parsing self.cc._do_command = self._do_command self.cc.assert_should_be_registered = self._asert_should_be_registered # stub out uep managercli.connection.UEPConnection = self._uep_connection sys.stdout = MockStdout() sys.stderr = MockStderr()
def _stub_content(self): stub_prod = StubProduct('stub_product', provided_tags="stub-product") stub_content = StubContent("a_test_repo", required_tags="stub-product") stub_ent_cert = StubEntitlementCertificate(stub_prod, content=[stub_content]) stub_prod_cert = StubProductCertificate(stub_prod) stub_ent_dir = StubEntitlementDirectory([stub_ent_cert]) stub_prod_dir = StubProductDirectory([stub_prod_cert]) inj.provide(inj.ENT_DIR, stub_ent_dir) inj.provide(inj.PROD_DIR, stub_prod_dir)
def test_delete(self): ent = StubEntitlementCertificate(StubProduct("Prod")) ent.delete = Mock(side_effect=OSError("Cert has already been deleted")) mock_uep = Mock() mock_uep.getCertificates = Mock(return_value=[]) mock_uep.getCertificateSerials = Mock(return_value=[]) update_action = TestingUpdateAction(mock_uep, StubEntitlementDirectory([ent])) try: updates, exceptions = update_action.perform() except OSError: self.fail("operation failed when certificate wasn't deleted") self.assertEquals(0, updates) self.assertEquals([], exceptions)
def test_unsubscribe_registered(self): connection.UEPConnection = StubUEP cmd = managercli.UnSubscribeCommand(ent_dir=StubEntitlementDirectory( []), prod_dir=StubProductDirectory([])) managercli.ConsumerIdentity = StubConsumerIdentity StubConsumerIdentity.existsAndValid = classmethod(lambda cls: True) StubConsumerIdentity.exists = classmethod(lambda cls: True) managercli.CertLib = StubCertLib cmd.main(['unsubscribe', '--all']) self.assertEquals(cmd.cp.called_unbind_uuid, StubConsumerIdentity.CONSUMER_ID) serial = '123456' cmd.main(['unsubscribe', '--serial=%s' % serial]) self.assertEquals(cmd.cp.called_unbind_serial, serial)
def test_installed_products_cache_written(self): connection.UEPConnection = StubUEP cmd = RegisterCommand(ent_dir=StubEntitlementDirectory([]), prod_dir=StubProductDirectory([])) cmd._persist_identity_cert = self.stub_persist ConsumerIdentity.exists = classmethod(lambda cls: False) # Mock out facts and installed products: cmd.facts.get_facts = Mock(return_value={ 'fact1': 'val1', 'fact2': 'val2' }) cmd.facts.write_cache = Mock() cmd.installed_mgr.write_cache = Mock() cmd.main(['register', '--username=testuser1', '--password=password']) self.assertEquals(1, cmd.installed_mgr.write_cache.call_count)
def test_register_persists_consumer_cert(self): connection.UEPConnection = StubUEP # When cmd = RegisterCommand(ent_dir=StubEntitlementDirectory([]), prod_dir=StubProductDirectory([])) ConsumerIdentity.exists = classmethod(lambda cls: False) cmd._persist_identity_cert = self.stub_persist cmd.facts.get_facts = Mock(return_value={ 'fact1': 'val1', 'fact2': 'val2' }) cmd.facts.write_cache = Mock() cmd.installed_mgr.write_cache = Mock() cmd.main(['register', '--username=testuser1', '--password=password']) # Then self.assertEqual('dummy-consumer-uuid', self.persisted_consumer["uuid"])
def test_unsubscribe_registered(self): connection.UEPConnection = StubUEP prod = StubProduct('stub_product') ent1 = StubEntitlementCertificate(prod) ent2 = StubEntitlementCertificate(prod) ent3 = StubEntitlementCertificate(prod) inj.provide(inj.ENT_DIR, StubEntitlementDirectory([ent1, ent2, ent3])) inj.provide(inj.PROD_DIR, StubProductDirectory([])) cmd = managercli.UnSubscribeCommand() managercli.ConsumerIdentity = StubConsumerIdentity StubConsumerIdentity.existsAndValid = classmethod(lambda cls: True) StubConsumerIdentity.exists = classmethod(lambda cls: True) managercli.CertLib = StubCertLib cmd.main(['unsubscribe', '--all']) self.assertEquals(cmd.cp.called_unbind_uuid, StubConsumerIdentity.CONSUMER_ID) cmd.main(['unsubscribe', '--serial=%s' % ent1.serial]) self.assertEquals(cmd.cp.called_unbind_serial, ['%s' % ent1.serial]) code = cmd.main([ 'unsubscribe', '--serial=%s' % ent2.serial, '--serial=%s' % ent3.serial ]) self.assertEquals( cmd.cp.called_unbind_serial, ['%s' % ent2.serial, '%s' % ent3.serial]) self.assertEquals(code, 0) connection.UEPConnection.unbindBySerial = mock.Mock( side_effect=connection.RestlibException( "Entitlement Certificate with serial number 2300922701043065601 could not be found." )) code = cmd.main(['unsubscribe', '--serial=%s' % '2300922701043065601']) self.assertEquals(code, 1)
def test_delete(self): ent = StubEntitlementCertificate(StubProduct("Prod")) ent.delete = Mock(side_effect=OSError("Cert has already been deleted")) mock_uep = Mock() mock_uep.getCertificates = Mock(return_value=[]) mock_uep.getCertificateSerials = Mock(return_value=[]) self.set_consumer_auth_cp(mock_uep) stub_ent_dir = StubEntitlementDirectory([ent]) inj.provide(inj.ENT_DIR, stub_ent_dir) # use the injected mock uep update_action = TestingUpdateAction() try: update_report = update_action.perform() except OSError: self.fail("operation failed when certificate wasn't deleted") self.assertEquals(0, update_report.updates()) exceptions = update_action.report.exceptions() self.assertEquals([], exceptions)
def test_expired_are_not_ignored_when_installing_certs(self, write_mock, build_cert_mock): valid_ent = StubEntitlementCertificate(StubProduct("PValid")) expired_ent = StubEntitlementCertificate(StubProduct("PExpired"), start_date=datetime.now() - timedelta(days=365), end_date=datetime.now() - timedelta(days=10)) cp_certificates = [valid_ent, expired_ent] # get certificates actually returns cert bundles cp_bundles = [{'key': Mock(), 'cert': x} for x in cp_certificates] # so we dont try to build actual x509 objects from stub certs def mock_build_cert(bundle): return (bundle['key'], bundle['cert']) build_cert_mock.side_effect = mock_build_cert mock_uep = Mock() mock_uep.getCertificateSerials.return_value = [x.serial for x in cp_certificates] mock_uep.getCertificates.return_value = cp_bundles # Passed into build_cert(bundle) self.set_consumer_auth_cp(mock_uep) stub_ent_dir = StubEntitlementDirectory([]) inj.provide(inj.ENT_DIR, stub_ent_dir) update_action = TestingUpdateAction() # we skip getting the expected serials, where this is normally # populated update_action.report.expected.append(valid_ent.serial) update_action.report.expected.append(expired_ent.serial) update_action.install([valid_ent.serial, expired_ent.serial]) update_report = update_action.report self.assertEqual(0, len(update_report.exceptions()), "No exceptions should have been thrown") self.assertTrue(valid_ent in update_report.added) self.assertTrue(valid_ent.serial in update_report.expected) self.assertTrue(expired_ent.serial in update_report.expected)
def create_cert_sorter(product_certs, entitlement_certs, machine_sockets=8): stub_facts = StubFacts(fact_dict={"cpu.cpu_socket(s)": machine_sockets}) return CertSorter(StubCertificateDirectory(product_certs), StubEntitlementDirectory(entitlement_certs), stub_facts.get_facts())
def test_print_consumed_one_ent_one_product(self): product = StubProduct("product1") ent_dir = StubEntitlementDirectory( [StubEntitlementCertificate(product)]) self.cc.print_consumed(ent_dir)
def test_print_consumed_one_ent_no_product(self): ent_dir = StubEntitlementDirectory( [StubEntitlementCertificate(product=None)]) self.cc.print_consumed(ent_dir)
def test_print_consumed_prints_enitlement_with_service_level_match(self): ent_dir = StubEntitlementDirectory([self.cert_with_service_level]) self.cc.print_consumed(ent_dir, service_level="Premium")