def test_different_succeeded_result_fails(self): # A Deferred that has fired successfully matches against the value it # was fired with. result = object() deferred = defer.succeed(result) matcher = Is(None) # Something that doesn't match `result`. mismatch = matcher.match(result) self.assertThat( self.match(matcher, deferred), mismatches(Equals(mismatch.describe()), Equals(mismatch.get_details())))
def test_different_failure_fails(self): # A Deferred that has fired with a failure matches against the value # it was fired with. fail = make_failure(RuntimeError('arbitrary failure')) deferred = defer.fail(fail) matcher = Is(None) # Something that doesn't match `fail`. mismatch = matcher.match(fail) self.assertThat( self.match(matcher, deferred), mismatches(Equals(mismatch.describe()), Equals(mismatch.get_details())))
def test_catches_generator_tests(self): class BrokenTests(TestCase): run_tests_with = self.executor def test(self): yield None test = BrokenTests("test") result = test.run() self.assertThat(result.errors, HasLength(1)) self.assertThat( result.errors[0], MatchesListwise(( Is(test), DocTestMatches("""\ ...InvalidTest: Test returned a generator. Should it be decorated with inlineCallbacks? """), )), )
def test_cannot_mount_two_filesystems_at_same_point(self): substrate = self.make_substrate() filesystem1 = factory.make_Filesystem(fstype=self.fstype, **substrate) filesystem2 = factory.make_Filesystem(node=filesystem1.get_node()) mount_point = factory.make_absolute_path() filesystem1.mount_point = mount_point filesystem1.save() filesystem2.mount_point = mount_point # Filesystems that can be mounted at a directory complain when mounted # at the same directory as another filesystem. if filesystem1.uses_mount_point: error = self.assertRaises(ValidationError, filesystem2.save) self.assertThat( error.messages, Equals( [ "Another filesystem is already mounted at %s." % mount_point ] ), ) else: self.assertThat(filesystem2.save(), Is(None))
def test_starting_and_stopping_the_service(self): service = RegionService(sentinel.ipcWorker) self.assertThat(service.starting, Is(None)) service.startService() self.assertThat(service.starting, IsInstance(Deferred)) def check_started(_): # Ports are saved as private instance vars. self.assertThat(service.ports, HasLength(1)) [port] = service.ports self.assertThat(port, IsInstance(tcp.Port)) self.assertThat(port.factory, IsInstance(Factory)) self.assertThat(port.factory.protocol, Equals(RegionServer)) return service.stopService() service.starting.addCallback(check_started) def check_stopped(ignore, service=service): self.assertThat(service.ports, Equals([])) service.starting.addCallback(check_stopped) return service.starting
def test_calling_importer_issues_rpc_calls_to_clusters(self): # Some clusters that we'll ask to import resources. rack_1 = factory.make_RackController() rack_2 = factory.make_RackController() # Connect only cluster #1. rack_1_conn = self.rpc.makeCluster(rack_1, ImportBootImages) rack_1_conn.ImportBootImages.return_value = succeed({}) # Do the import. importer = RackControllersImporter.new( [rack_1.system_id, rack_2.system_id] ) results = importer(lock=DeferredLock()).wait(5) # The results are a list (it's from a DeferredList). self.assertThat( results, MatchesListwise( ( # Success when calling rack_1. Equals((True, {})), # Failure when calling rack_1: no connection. MatchesListwise( ( Is(False), MatchesAll( IsInstance(Failure), MatchesStructure( value=IsInstance(NoConnectionsAvailable) ), ), ) ), ) ), )
def test_discover_and_sync_existing_pod(self): discovered_pod, discovered_racks, failed_racks = ( self.fake_pod_discovery()) zone = factory.make_Zone() pod_info = self.make_pod_info() orig_pod = factory.make_Pod(zone=zone, pod_type=pod_info['type']) request = MagicMock() request.user = factory.make_User() form = PodForm(data=pod_info, request=request, instance=orig_pod) pod = form.discover_and_sync_pod() self.assertThat( pod, MatchesStructure( id=Equals(orig_pod.id), bmc_type=Equals(BMC_TYPE.POD), architectures=Equals(['amd64/generic']), name=Equals(orig_pod.name), cores=Equals(discovered_pod.cores), memory=Equals(discovered_pod.memory), cpu_speed=Equals(discovered_pod.cpu_speed), zone=Equals(zone), power_type=Equals(pod_info['type']), power_parameters=Equals({}), ip_address=Is(None), )) routable_racks = [ relation.rack_controller for relation in pod.routable_rack_relationships.all() if relation.routable ] not_routable_racks = [ relation.rack_controller for relation in pod.routable_rack_relationships.all() if not relation.routable ] self.assertItemsEqual(routable_racks, discovered_racks) self.assertItemsEqual(not_routable_racks, failed_racks)
def test_run(self): # The job requests builds and records the result. distroseries, processors = self.makeSeriesAndProcessors( ["avr2001", "sparc64", "x32"]) [git_ref] = self.factory.makeGitRefs() snap = self.factory.makeSnap( git_ref=git_ref, distroseries=distroseries, processors=processors) expected_date_created = get_transaction_timestamp(IStore(snap)) job = SnapRequestBuildsJob.create( snap, snap.registrant, distroseries.main_archive, PackagePublishingPocket.RELEASE, {"core": "stable"}) snapcraft_yaml = dedent("""\ architectures: - build-on: avr2001 - build-on: x32 """) self.useFixture(GitHostingFixture(blob=snapcraft_yaml)) with dbuser(config.ISnapRequestBuildsJobSource.dbuser): JobRunner([job]).runAll() now = get_transaction_timestamp(IStore(snap)) self.assertEmailQueueLength(0) self.assertThat(job, MatchesStructure( job=MatchesStructure.byEquality(status=JobStatus.COMPLETED), date_created=Equals(expected_date_created), date_finished=MatchesAll( GreaterThan(expected_date_created), LessThan(now)), error_message=Is(None), builds=AfterPreprocessing(set, MatchesSetwise(*[ MatchesStructure( build_request=MatchesStructure.byEquality(id=job.job.id), requester=Equals(snap.registrant), snap=Equals(snap), archive=Equals(distroseries.main_archive), distro_arch_series=Equals(distroseries[arch]), pocket=Equals(PackagePublishingPocket.RELEASE), channels=Equals({"core": "stable"})) for arch in ("avr2001", "x32")]))))
def test__schedule_arranges_for_later_run(self): # Avoid deferring to the database. self.patch(boot_images_module, "deferToDatabase", maybeDeferred) # Avoid actually initiating a run. self.patch_autospec(RackControllersImporter, "run") system_ids = [factory.make_name("system_id") for _ in range(3)] sources = [sentinel.source] proxy = factory.make_simple_http_url() conc = random.randint(1, 9) delay = random.randint(1, 9) clock = Clock() delayed_call = RackControllersImporter.schedule(system_ids=system_ids, sources=sources, proxy=proxy, delay=delay, concurrency=conc, clock=clock) # The call is scheduled for `delay` seconds from now. self.assertThat(delayed_call, MatchesStructure(time=Equals(delay))) self.assertThat(RackControllersImporter.run, MockNotCalled()) clock.advance(delay) self.assertThat(RackControllersImporter.run, MockCalledOnceWith(ANY, conc)) # The system_ids, sources, and proxy were all passed through. [importer, _] = RackControllersImporter.run.call_args[0] self.assertThat( importer, MatchesStructure( system_ids=Equals(tuple(system_ids)), sources=Is(sources), proxy=Equals(urlparse(proxy)), ))
def test__send_multicast_beacon_sets_ipv6_source(self): # Due to issues beyond my control, this test doesn't do what I expected # it to do. But it's still useful for code coverage (to make sure no # blatant exceptions occur in the IPv6 path). # self.skipTest( # "IPv6 loopback multicast isn't working, for whatever reason.") # Since we can't test IPv6 multicast on the loopback interface, another # method can be used to verify that it's working: # (1) sudo tcpdump -i <physical-interface> 'udp and port == 5240' # (2) bin/maas-rack send-beacons -p 5240 # Verifying IPv6 (and IPv4) multicast group join behavior can be # validated by doing something like: # (1) bin/maas-rack send-beacons -t 600 # (the high timeout will cause it to wait for 10 minutes) # (2) ip maddr show | egrep 'ff02::15a|224.0.0.118|$' # The expected result from command (2) will be that 'egrep' will # highlight the MAAS multicast groups in red text. Any Ethernet # interface with an assigned IPv4 address should have joined the # 224.0.0.118 group. All Ethernet interfaces should have joined the # 'ff02::15a' group. # Note: Always use a random port for testing. (port=0) protocol = BeaconingSocketProtocol(reactor, port=0, process_incoming=True, loopback=True, interface="::", debug=False) self.assertThat(protocol.listen_port, Not(Is(None))) listen_port = protocol.listen_port._realPortNumber self.write_secret() beacon = create_beacon_payload("advertisement", {}) # The loopback interface ifindex should always be 1; this is saying # to send an IPv6 multicast on ifIndex == 1. protocol.send_multicast_beacon(1, beacon, port=listen_port) # Instead of skipping the test, just don't expect to receive anything. # yield wait_for_rx_packets(protocol, 1) yield protocol.stopProtocol()
def test_refresh_with_multiple_pk(self): object_type = type("PKObject", (Object, ), { "pk1": ObjectField("pk_1", pk=0), "pk2": ObjectField("pk_2", pk=1), }) object_pk_1 = randint(0, 20) object_pk_2 = randint(0, 20) new_data = { 'pk_1': object_pk_1, 'pk_2': object_pk_2, 'other': randint(0, 20), } mock_read = AsyncCallableMock(return_value=object_type(new_data)) self.patch(object_type, 'read', mock_read) object_a = object_type([object_pk_1, object_pk_2]) self.assertFalse(object_a.loaded) object_a.refresh() self.assertTrue(object_a.loaded) self.assertThat(object_a._data, Equals(new_data)) self.assertThat(object_a._orig_data, Equals(new_data)) self.assertThat(object_a._orig_data, Not(Is(new_data))) self.assertThat(object_a._changed_data, Equals({})) self.assertThat(mock_read.call_args_list, Equals([call(object_pk_1, object_pk_2)]))
def test_useprefix(self): """ ``PClasses`` can be used with ``UsePrefix`` to get access to the ``PClass``\ s representing Swagger definitions without repetition of the definition version prefix commonly used. """ template = freeze({ u"type": u"object", u"properties": {}, }) spec = Swagger.from_document({ u"definitions": { u"a.X": template, u"b.X": template, }, }) pclasses = PClasses( specification=spec, name_translator=UsePrefix(prefix=u"a."), ) self.assertThat( pclasses[u"X"], Is(spec.pclass_for_definition(u"a.X")), )
def test_yield_unfired_deferred(self, logger): waiting = Deferred() @inline_callbacks def g(): Message.log(message_type=u"a") yield waiting Message.log(message_type=u"b") with start_action(action_type=u"the-action"): d = g() self.assertThat(waiting, has_no_result()) waiting.callback(None) self.assertThat(d, succeeded(Is(None))) assert_expected_action_tree( self, logger, u"the-action", [ u"a", u"yielded", u"b", ], )
def test_when_certs_valid_certs_expired(self, fixture): """ The deferred returned by ``when_certs_valid`` only fires once all panicing and expired certs have been renewed. """ with fixture: service = fixture.service d = service.when_certs_valid() self.assertThat(d, has_no_result()) service.startService() self.assertThat(d, succeeded(Is(None))) max_expiry = fixture.now + service.panic_interval self.assertThat( fixture.cert_store.as_dict(), succeeded( AfterPreprocessing( methodcaller('values'), AllMatch( AllMatch( _match_certificate( MatchesStructure( not_valid_after=GreaterThan( max_expiry)))))))) self.assertThat(fixture.responder.challenges, HasLength(0))
def test_client_v2_signed(self): introducer = IntroducerService() tub = introducer_furl = None client_v2 = IntroducerClient(tub, introducer_furl, u"nick-v2", "my_version", "oldest", fakeseq, FilePath(self.mktemp())) furl1 = b"pb://[email protected]:0/swissnum" private_key, public_key = ed25519.create_signing_keypair() public_key_str = remove_prefix( ed25519.string_from_verifying_key(public_key), b"pub-") ann_t0 = make_ann_t(client_v2, furl1, private_key, 10) canary0 = Referenceable() introducer.remote_publish_v2(ann_t0, canary0) a = introducer.get_announcements() self.failUnlessEqual(len(a), 1) self.assertThat(a[0].canary, Is(canary0)) self.failUnlessEqual(a[0].index, ("storage", public_key_str)) self.failUnlessEqual(a[0].nickname, u"nick-v2") self.failUnlessEqual(a[0].service_name, "storage") self.failUnlessEqual(a[0].version, "my_version") self.failUnlessEqual( ensure_binary(a[0].announcement["anonymous-storage-FURL"]), furl1)
def test_local_snapshot_service_child(self): """ ``MagicFolder`` adds the service given as ``LocalSnapshotService`` to itself as a child. """ local_snapshot_service = Service() tahoe_client = object() reactor = object() name = u"local-snapshot-service-test" config = object() participants = object() magic_folder = MagicFolder( client=tahoe_client, config=config, name=name, local_snapshot_service=local_snapshot_service, uploader_service=Service(), initial_participants=participants, clock=reactor, ) self.assertThat( local_snapshot_service.parent, Is(magic_folder), )
def test_client_v2_signed(self): introducer = IntroducerService() tub = introducer_furl = None app_versions = {"whizzy": "fizzy"} client_v2 = IntroducerClient(tub, introducer_furl, u"nick-v2", "my_version", "oldest", app_versions, fakeseq, FilePath(self.mktemp())) furl1 = "pb://[email protected]:0/swissnum" sk_s, vk_s = keyutil.make_keypair() sk, _ignored = keyutil.parse_privkey(sk_s) pks = keyutil.remove_prefix(vk_s, "pub-") ann_t0 = make_ann_t(client_v2, furl1, sk, 10) canary0 = Referenceable() introducer.remote_publish_v2(ann_t0, canary0) a = introducer.get_announcements() self.failUnlessEqual(len(a), 1) self.assertThat(a[0].canary, Is(canary0)) self.failUnlessEqual(a[0].index, ("storage", pks)) self.failUnlessEqual(a[0].announcement["app-versions"], app_versions) self.failUnlessEqual(a[0].nickname, u"nick-v2") self.failUnlessEqual(a[0].service_name, "storage") self.failUnlessEqual(a[0].version, "my_version") self.failUnlessEqual(a[0].announcement["anonymous-storage-FURL"], furl1)
def test_sets_syslog_rack_service_to_any_when_is_region(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, "debug") self.useFixture(MAASRootFixture()) rpc_service, _ = yield prepareRegion(self, is_region=True) service, syslog = self.make_RackSyslog_ExternalService( rpc_service, reactor ) self.patch_autospec(syslog, "_configure") # No-op configuration. # There is no most recently applied configuration. self.assertThat(syslog._configuration, Is(None)) with TwistedLoggerFixture() as logger: yield service.startService() self.addCleanup((yield service.stopService)) yield service._orig_tryUpdate() # Ensure that the service was set to any. service = service_monitor.getServiceByName("syslog_rack") self.assertEqual( (SERVICE_STATE.ANY, "managed by the region"), service.getExpectedState(), ) # The most recently applied configuration is set, though it was not # actually "applied" because this host was configured as a region+rack # controller, and the rack should not attempt to manage the DNS server # on a region+rack. self.assertThat( syslog._configuration, IsInstance(external._SyslogConfiguration) ) # The configuration was not applied. self.assertThat(syslog._configure, MockNotCalled()) # Nothing was logged; there's no need for lots of chatter. self.assertThat(logger.output, Equals(""))
def test__marks_failed_if_save_raises(self): mock_pod_form = Mock() self.mock_PodForm.return_value = mock_pod_form mock_pod_form.errors = {} mock_pod_form.is_valid = Mock() mock_pod_form.is_valid.return_value = True mock_pod_form.save = Mock() mock_pod_form.save.side_effect = ValueError node = factory.make_Node_with_Interface_on_Subnet( status=NODE_STATUS.DEPLOYING, agent_name="maas-kvm-pod", install_kvm=True, ) factory.make_StaticIPAddress(interface=node.boot_interface) meta = NodeMetadata.objects.create( node=node, key="virsh_password", value="xyz123" ) _create_pod_for_deployment(node) meta = reload_object(meta) self.assertThat(meta, Is(None)) self.assertThat(node.status, Equals(NODE_STATUS.FAILED_DEPLOYMENT)) self.assertThat( node.error_description, DocTestMatches(POD_CREATION_ERROR) )
def test_create_creates_device_with_dynamic_ip_assignment(self): user = factory.make_User() request = HttpRequest() request.user = user handler = DeviceHandler(user, {}, request) mac = factory.make_mac_address() hostname = factory.make_name("hostname") created_device = handler.create({ "hostname": hostname, "primary_mac": mac, "interfaces": [{ "mac": mac, "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.DYNAMIC, }], }) self.expectThat(created_device["hostname"], Equals(hostname)) self.expectThat(created_device["primary_mac"], Equals(mac)) self.expectThat(created_device["extra_macs"], Equals([])) self.expectThat(created_device["ip_assignment"], Equals(DEVICE_IP_ASSIGNMENT_TYPE.DYNAMIC)) self.expectThat(created_device["ip_address"], Is(None)) self.expectThat(created_device["owner"], Equals(user.username))
def test_is_silent_does_nothing_but_saves_config_when_is_region(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, 'debug') self.useFixture(MAASRootFixture()) rpc_service, _ = yield prepareRegion(self, is_region=True) service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor) self.patch_autospec(ntp, "configure_rack") # No-op configuration. # There is no most recently applied configuration. self.assertThat(service._configuration, Is(None)) with TwistedLoggerFixture() as logger: yield service._tryUpdate() # The most recently applied configuration is set, though it was not # actually "applied" because this host was configured as a region+rack # controller, and the rack should not attempt to manage the NTP server # on a region+rack. self.assertThat(service._configuration, IsInstance(ntp._Configuration)) # The configuration was not applied. self.assertThat(ntp.configure_rack, MockNotCalled()) # Nothing was logged; there's no need for lots of chatter. self.assertThat(logger.output, Equals(""))
def test_add_app_triggers_api_post_event(self): """ When an app is added to the underlying fake Marathon, an ``api_post_event`` should be received by any event listeners. """ response = self.client.get('http://localhost/v2/events', headers={'Accept': 'text/event-stream'}) assert_that(response, succeeded(IsSseResponse())) app = { 'id': '/my-app_1', 'labels': { 'HAPROXY_GROUP': 'external', 'MARATHON_ACME_0_DOMAIN': 'example.com' }, 'portDefinitions': [{ 'port': 9000, 'protocol': 'tcp', 'labels': {} }] } self.marathon.add_app(app) assert_that( response, succeeded( After( partial(collect_events, 'api_post_event'), MatchesListwise([ After( json.loads, IsMarathonEvent('api_post_event', clientIp=Is(None), uri=Equals('/v2/apps/my-app_1'), appDefinition=Equals(app))) ]))))
def test_is_root_enabled(self): when(dbaas.MySqlAdmin).is_root_enabled().thenReturn(True) is_enabled = self.manager.is_root_enabled(self.context) self.assertThat(is_enabled, Is(True)) verify(dbaas.MySqlAdmin).is_root_enabled()
def test_init(self): conn = DummyConnection() client = common.Client(conn) self.assertThat(client._conn, Is(conn))
def test_enable_root(self): when(dbaas.MySqlAdmin).enable_root().thenReturn('user_id_stuff') user_id = self.manager.enable_root(self.context) self.assertThat(user_id, Is('user_id_stuff')) verify(dbaas.MySqlAdmin).enable_root()
def test_authenticated_is_True_when_authentication_is_provided(self): resource = OperationsResource( StubHandler, authentication=sentinel.authentication ) self.assertThat(resource.is_authentication_attempted, Is(True))
def test_getPeerCertificate(self): conn, client = self.make_connection_and_client() conn.peerCertificate = sentinel.peerCertificate self.assertThat(client.getPeerCertificate(), Is(sentinel.peerCertificate))
def test_authenticated_is_False_when_authentication_is_NoAuthn(self): resource = OperationsResource( StubHandler, authentication=NoAuthentication() ) self.assertThat(resource.is_authentication_attempted, Is(False))
def test_authenticated_is_False_when_no_authentication_provided(self): resource = OperationsResource(StubHandler) self.assertThat(resource.is_authentication_attempted, Is(False))
def test_authentication_is_okay(self): resource = self.resource_type( StubHandler, authentication=sentinel.authentication ) self.assertThat(resource.is_authentication_attempted, Is(True))
def test_get_returns_None_if_maas_id_file_is_empty(self): with open(self.maas_id_path, "w"): pass # Write nothing. self.assertThat(env.get_maas_id(), Is(None))
def test_get_returns_None_if_maas_id_file_does_not_exist(self): self.assertThat(self.maas_id_path, Not(FileExists())) self.assertThat(env.get_maas_id(), Is(None))