def test__renders_with_hosts(self): params = make_sample_params(self, ipv6=self.ipv6) self.assertThat( params["hosts"], AfterPreprocessing( len, GreaterThanOrEqual(1))) config_output = config.get_config(self.template, **params) validate_dhcpd_configuration(self, config_output, self.ipv6) self.assertThat( config_output, ContainsAll([ host['host'] for host in params["hosts"] ])) self.assertThat( config_output, ContainsAll([ host['mac'] for host in params["hosts"] ])) self.assertThat( config_output, ContainsAll([ host['ip'] for host in params["hosts"] ]))
def test_oauth_headers(self): now = time.time() is_about_now = MatchesAll( GreaterThanOrEqual(int(now)), LessThanOrEqual(int(now) + 3), ) url = factory.make_name("url") consumer_key = factory.make_name("consumer_key") token_key = factory.make_name("token_key") token_secret = factory.make_name("token_secret") consumer_secret = factory.make_name("consumer_secret") headers = maas_api_helper.oauth_headers( url, consumer_key, token_key, token_secret, consumer_secret) authorization = headers['Authorization'] self.assertRegex(authorization, '^OAuth .*') authorization = authorization.replace('OAuth ', '') oauth_arguments = {} for argument in authorization.split(', '): key, value = argument.split('=') oauth_arguments[key] = value.replace('"', '') self.assertIn('oauth_nonce', oauth_arguments) oauth_arguments.pop('oauth_nonce', None) self.assertThat(oauth_arguments, MatchesDict({ 'oauth_timestamp': AfterPreprocessing(int, is_about_now), 'oauth_version': Equals('1.0'), 'oauth_signature_method': Equals('PLAINTEXT'), 'oauth_consumer_key': Equals(consumer_key), 'oauth_token': Equals(token_key), 'oauth_signature': Equals( "%s%%26%s" % (consumer_secret, token_secret)), }))
def test_full_jitter(self, values): jittered = list(full_jitter(values)) self.assertThat(jittered, AllMatch(IsInstance(float))) self.assertThat( jittered, AllMatch(MatchesAll(GreaterThanOrEqual(0.0), LessThan(10000.0))), )
def test_oauth_headers(self): now = time.time() is_about_now = MatchesAll( GreaterThanOrEqual(int(now)), LessThanOrEqual(int(now) + 3) ) url = factory.make_name("url") consumer_key = factory.make_name("consumer_key") token_key = factory.make_name("token_key") token_secret = factory.make_name("token_secret") consumer_secret = factory.make_name("consumer_secret") credentials = maas_api_helper.Credentials() credentials.update( { "consumer_key": consumer_key, "token_key": token_key, "token_secret": token_secret, "consumer_secret": consumer_secret, } ) headers = credentials.oauth_headers(url) authorization = headers["Authorization"] self.assertRegex(authorization, "^OAuth .*") authorization = authorization.replace("OAuth ", "") oauth_arguments = {} for argument in authorization.split(", "): key, value = argument.split("=") oauth_arguments[key] = value.replace('"', "") self.assertIn("oauth_nonce", oauth_arguments) oauth_arguments.pop("oauth_nonce", None) self.assertThat( oauth_arguments, MatchesDict( { "oauth_timestamp": AfterPreprocessing(int, is_about_now), "oauth_version": Equals("1.0"), "oauth_signature_method": Equals("PLAINTEXT"), "oauth_consumer_key": Equals(consumer_key), "oauth_token": Equals(token_key), "oauth_signature": Equals( "%s%%26%s" % (consumer_secret, token_secret) ), } ), )
def test__does_not_match_less_than(self): self.assertMismatch(GreaterThanOrEqual(6).match(5), "Differences:") self.assertMismatch( GreaterThanOrEqual("ccc").match("bbb"), "Differences:")
def test__matches_equal_to(self): self.assertThat(5, GreaterThanOrEqual(5)) self.assertThat("bbb", GreaterThanOrEqual("bbb"))
def test__matches_greater_than(self): self.assertThat(5, GreaterThanOrEqual(4)) self.assertThat("bbb", GreaterThanOrEqual("aaa"))
dbname = database["NAME"] assert dbname.endswith("_test") database["NAME"] = dbname[:-5] def close_all_connections(): from django.db import connections for conn in connections.all(): conn.close() # These are the expected row counts for tables in MAAS; those not mentioned # are assumed to have zero rows. expected_table_counts = { 'auth_permission': GreaterThan(0), 'auth_user': GreaterThanOrEqual(0), 'django_content_type': GreaterThan(0), 'django_migrations': GreaterThan(0), # Mystery Django stuff here. No idea what django_site does; it seems to # only ever contain a single row referring to "example.com". 'django_site': Equals(1), # Tests do not use migrations, but were they to do so the following # maasserver_* tables would have one row or more each. 'maasserver_dnspublication': Equals(1), 'maasserver_domain': Equals(1), 'maasserver_fabric': Equals(1), 'maasserver_packagerepository': Equals(2), 'maasserver_vlan': Equals(1), 'maasserver_zone': Equals(1), }
GreaterThanOrEqual, MockCalledOnceWith, MockNotCalled, ) from maastesting.runtest import MAASCrochetRunTest from maastesting.testcase import MAASTestCase from maastesting.twisted import TwistedLoggerFixture from provisioningserver.utils.twisted import pause from pytz import UTC from testtools.matchers import LessThan, MatchesAll from twisted.internet.defer import fail, inlineCallbacks from twisted.internet.task import Clock IsExpectedInterval = MatchesAll( GreaterThanOrEqual(3 * 60 * 60), LessThan(6 * 60 * 60), first_only=True ) def patch_utcnow(test): utcnow = test.patch(publication, "datetime").utcnow ref = utcnow.return_value = datetime.utcnow() return ref class TestDNSPublicationGarbageService(MAASTestCase): """Tests for `DNSPublicationGarbageService`.""" run_tests_with = MAASCrochetRunTest def test_starting_and_stopping(self):
# Copyright 2016 Canonical Ltd. This software is licensed under the # GNU Affero General Public License version 3 (see the file LICENSE). """Utilities related to table row counts.""" from django.db import connection from testtools.matchers import Annotate, Equals, GreaterThan, MatchesDict from maastesting.matchers import GreaterThanOrEqual # These are the expected row counts for tables in MAAS; those not mentioned # are assumed to have zero rows. expected_table_counts = { "auth_permission": GreaterThan(0), "auth_user": GreaterThanOrEqual(0), # Tests do not use migrations, but were they to do so the # django_content_type table would contain 10s of rows. "django_content_type": GreaterThan(0), # Tests do not use migrations; they're way too slow. "django_migrations": Equals(0), # Mystery Django stuff here. No idea what django_site does; it seems to # only ever contain a single row referring to "example.com". "django_site": Equals(1), # Tests do not use migrations, but were they to do so the following # maasserver_* tables would have one row or more each. "maasserver_dnspublication": Equals(0), "maasserver_domain": Equals(0), "maasserver_fabric": Equals(0), "maasserver_vlan": Equals(0), "maasserver_zone": Equals(0), }
from maastesting.runtest import MAASCrochetRunTest from maastesting.testcase import MAASTestCase from maastesting.twisted import TwistedLoggerFixture from provisioningserver.utils.twisted import pause from pytz import UTC from testtools.matchers import ( LessThan, MatchesAll, ) from twisted.internet.defer import ( fail, inlineCallbacks, ) from twisted.internet.task import Clock IsExpectedInterval = MatchesAll(GreaterThanOrEqual(3 * 60 * 60), LessThan(6 * 60 * 60), first_only=True) def patch_utcnow(test): utcnow = test.patch(publication, "datetime").utcnow ref = utcnow.return_value = datetime.utcnow() return ref class TestDNSPublicationGarbageService(MAASTestCase): """Tests for `DNSPublicationGarbageService`.""" run_tests_with = MAASCrochetRunTest