def assertCalledTar(self, mock, call=0):
     path = dbupgrade_command._path_to_django16_south_maas19()
     call = mock.call_args_list[call][0][0]
     self.assertThat(
         call,
         MatchesListwise([
             Equals('tar'),
             Equals('zxf'),
             Equals(path),
             Equals('-C'),
             StartsWith('/tmp/maas-upgrade-'),
         ]))
    def test_file_is_sent_in_single_packet(self):
        with temp_file_contents(b"Hello") as f:
            result = get_result_for(
                [self.option, self.test_id, '--attach-file', f.name])

            self.assertThat(
                result._events,
                MatchesListwise([
                    MatchesStatusCall(call='startTestRun'),
                    MatchesStatusCall(file_bytes=b'Hello', eof=True),
                    MatchesStatusCall(call='stopTestRun'),
                ]))
Beispiel #3
0
    def test_errors(self):
        """
        If a cert renewal fails within the panic interval, the panic callback
        is invoked; otherwise the error is logged normally.
        """
        now = datetime(2000, 1, 1, 0, 0, 0)
        certs = {
            u'example.com': _generate_cert(
                u'example.com',
                not_valid_before=now - timedelta(seconds=1),
                not_valid_after=now + timedelta(days=31)),
            }
        panics = []
        with AcmeFixture(now=now, certs=certs,
                         panic=lambda *a: panics.append(a)) as fixture:
            fixture.service.startService()
            self.assertThat(
                fixture.service.when_certs_valid(),
                succeeded(Is(None)))
            self.assertThat(fixture.responder.challenges, HasLength(0))

            fixture.controller.pause()
            fixture.clock.advance(36 * 60 * 60)
            # Resume the client.request_issuance deferred with an exception.
            fixture.controller.resume(Failure(Exception()))
            self.assertThat(flush_logged_errors(), HasLength(1))
            self.assertThat(panics, Equals([]))
            self.assertThat(fixture.responder.challenges, HasLength(0))

            fixture.controller.pause()
            fixture.clock.advance(15 * 24 * 60 * 60)
            # Resume the client.request_issuance deferred with an exception.
            fixture.controller.resume(Failure(Exception()))
            self.assertThat(
                panics,
                MatchesListwise([
                    MatchesListwise([IsInstance(Failure),
                                     Equals(u'example.com')]),
                    ]))
            self.assertThat(fixture.responder.challenges, HasLength(0))
Beispiel #4
0
    def test_sync_multiple_apps(self):
        """
        When a sync is run and there are multiple apps, certificates are
        fetched for the domains of all the apps.
        """
        self.fake_marathon.add_app({
            'id':
            '/my-app_1',
            'labels': {
                'HAPROXY_GROUP': 'external',
                'MARATHON_ACME_0_DOMAIN': 'example.com',
            },
            'portDefinitions': [{
                'port': 9000,
                'protocol': 'tcp',
                'labels': {}
            }]
        })
        self.fake_marathon.add_app({
            'id':
            '/my-app_2',
            'labels': {
                'HAPROXY_GROUP': 'external',
                'MARATHON_ACME_0_DOMAIN': 'example2.com',
            },
            'portDefinitions': [
                {
                    'port': 8000,
                    'protocol': 'tcp',
                    'labels': {}
                },
            ]
        })

        d = self.marathon_acme.sync()
        assert_that(
            d,
            succeeded(
                MatchesListwise([  # Per domain
                    is_marathon_lb_sigusr_response,
                    is_marathon_lb_sigusr_response
                ])))

        assert_that(
            self.cert_store.as_dict(),
            succeeded(
                MatchesDict({
                    'example.com': Not(Is(None)),
                    'example2.com': Not(Is(None))
                })))

        assert_that(self.fake_marathon_lb.check_signalled_usr1(), Equals(True))
Beispiel #5
0
 def test_machine_asdict(self):
     hostname = factory.make_name('hostname')
     cores = random.randint(1, 8)
     cpu_speed = random.randint(1000, 2000)
     memory = random.randint(4096, 8192)
     interfaces = [
         RequestedMachineInterface()
         for _ in range(3)
     ]
     block_devices = [
         RequestedMachineBlockDevice(
             size=random.randint(512, 1024), tags=[
                 factory.make_name('tag')
                 for _ in range(3)
             ])
         for _ in range(3)
     ]
     machine = RequestedMachine(
         hostname=hostname, architecture='amd64/generic',
         cores=cores, cpu_speed=cpu_speed, memory=memory,
         interfaces=interfaces, block_devices=block_devices)
     self.assertThat(machine.asdict(), MatchesDict({
         "hostname": Equals(hostname),
         "architecture": Equals("amd64/generic"),
         "cores": Equals(cores),
         "cpu_speed": Equals(cpu_speed),
         "memory": Equals(memory),
         "interfaces": MatchesListwise([
             MatchesDict({})
             for interface in interfaces
         ]),
         "block_devices": MatchesListwise([
             MatchesDict({
                 "size": Equals(block_device.size),
                 "tags": Equals(block_device.tags),
             })
             for block_device in block_devices
         ]),
     }))
Beispiel #6
0
 def test__get_requested_machine_uses_all_initial_values(self):
     request = MagicMock()
     pod = make_pod_with_hints()
     form = ComposeMachineForm(data={}, request=request, pod=pod)
     self.assertTrue(form.is_valid())
     request_machine = form.get_requested_machine()
     self.assertThat(
         request_machine,
         MatchesAll(
             IsInstance(RequestedMachine),
             MatchesStructure(
                 architecture=Equals(pod.architectures[0]),
                 cores=Equals(1),
                 memory=Equals(1024),
                 cpu_speed=Is(None),
                 block_devices=MatchesListwise([
                     MatchesAll(
                         IsInstance(RequestedMachineBlockDevice),
                         MatchesStructure(size=Equals(8 * (1000**3))))
                 ]),
                 interfaces=MatchesListwise(
                     [IsInstance(RequestedMachineInterface)]))))
Beispiel #7
0
def IsMultiResultOf(*results):
    """Match a `MultiTestResult` wrapping the given results."""
    return MatchesAll(
        IsInstance(MultiTestResult),
        MatchesStructure(_results=MatchesListwise([
            MatchesAll(
                IsInstance(ExtendedToOriginalDecorator),
                MatchesStructure(decorated=matcher),
                first_only=True,
            ) for matcher in results
        ])),
        first_only=True,
    )
Beispiel #8
0
 def test_describe_returns_json(self):
     response = self.client.get(reverse('describe'))
     self.assertThat(
         (response.status_code,
          response['Content-Type'],
          response.content,
          response.content),
         MatchesListwise(
             (Equals(http.client.OK),
              Equals("application/json"),
              StartsWith(b'{'),
              Contains(b'name'))),
         response)
Beispiel #9
0
 def test_issueMacaroon_good(self):
     build = self.factory.makeSnapBuild(snap=self.factory.makeSnap(
         private=True))
     issuer = getUtility(IMacaroonIssuer, "snap-build")
     macaroon = removeSecurityProxy(issuer).issueMacaroon(build)
     self.assertThat(
         macaroon,
         MatchesStructure(location=Equals("launchpad.dev"),
                          identifier=Equals("snap-build"),
                          caveats=MatchesListwise([
                              MatchesStructure.byEquality(
                                  caveat_id="lp.snap-build %s" % build.id),
                          ])))
Beispiel #10
0
 def test_start_date(self):
     commits = self.ref.getCommits(self.sha1_tip,
                                   start_date=(self.dates[1] -
                                               timedelta(seconds=1)))
     path = self.ref.repository.getInternalPath()
     self.assertThat(
         commits,
         MatchesListwise([
             ContainsDict({"sha1": Equals(self.sha1_tip)}),
         ]))
     key = "git.launchpad.dev:git-log:%s:%s" % (path, self.sha1_tip)
     self.assertEqual(json.dumps(self.log),
                      getUtility(IMemcacheClient).get(key.encode("UTF-8")))
Beispiel #11
0
 def test_error(self):
     """
     An Effect with callbacks
     - performs the wrapped effect,
     - in the case of an exception, invokes the errback with exc_info,
     - returns the result of the errback.
     """
     self.assertThat(
         sync_perform(
             Effect(ErrorIntent()).on_error(lambda x: ("handled", x))),
         MatchesListwise(
             [Equals('handled'),
              MatchesException(ValueError('oh dear'))]))
    def test_can_specify_tags_without_test_status(self):
        result = get_result_for([
            '--tag',
            'foo',
        ])

        self.assertThat(
            result._events,
            MatchesListwise([
                MatchesStatusCall(call='startTestRun'),
                MatchesStatusCall(test_tags=set(['foo'])),
                MatchesStatusCall(call='stopTestRun'),
            ]))
    def test_file_name_is_used_by_default(self):
        with temp_file_contents(b"Hello") as f:
            result = get_result_for(['--attach-file', f.name])

            self.assertThat(
                result._events,
                MatchesListwise([
                    MatchesStatusCall(call='startTestRun'),
                    MatchesStatusCall(file_name=f.name,
                                      file_bytes=b'Hello',
                                      eof=True),
                    MatchesStatusCall(call='stopTestRun'),
                ]))
    def test_can_attach_file_without_test_id(self):
        with temp_file_contents(b"Hello") as f:
            result = get_result_for(['--attach-file', f.name])

            self.assertThat(
                result._events,
                MatchesListwise([
                    MatchesStatusCall(call='startTestRun'),
                    MatchesStatusCall(test_id=None,
                                      file_bytes=b'Hello',
                                      eof=True),
                    MatchesStatusCall(call='stopTestRun'),
                ]))
    def test_can_read_binary_files(self):
        with temp_file_contents(b"\xDE\xAD\xBE\xEF") as f:
            result = get_result_for(
                [self.option, self.test_id, '--attach-file', f.name])

            self.assertThat(
                result._events,
                MatchesListwise([
                    MatchesStatusCall(call='startTestRun'),
                    MatchesStatusCall(file_bytes=b"\xDE\xAD\xBE\xEF",
                                      eof=True),
                    MatchesStatusCall(call='stopTestRun'),
                ]))
Beispiel #16
0
 def test__captures_stdout_after_stderr_closes(self):
     # Write to stderr, close stderr, then write to stdout.
     proc = Popen(
         'echo -n bar >&2 && exec 2>&- && echo -n foo >&1',
         stdout=PIPE, stderr=PIPE, shell=True)
     # Capturing gets the foo even after stderr is closed.
     self.assertThat(
         self.capture(proc), MatchesListwise((
             Equals(0), Equals("foo"), Equals("bar"),
             # The writes to stdout and stderr occur so close in time that
             # they may be received in any order.
             MatchesAny(Equals("foobar"), Equals("barfoo")),
         )))
 def _make_matcher(self, obj):
     # This isn't very safe for general use, but is good enough to make
     # some tests in this module more readable.
     if hasattr(obj, 'match'):
         return obj
     elif isinstance(obj, tuple) or isinstance(obj, list):
         return MatchesListwise([self._make_matcher(item) for item in obj])
     elif isinstance(obj, dict):
         return MatchesDict(
             dict((key, self._make_matcher(value))
                  for key, value in obj.items()))
     else:
         return Equals(obj)
Beispiel #18
0
 def test_invalid_oauth_request(self):
     # An OAuth-signed request that does not validate is an error.
     user = factory.make_User()
     client = MAASSensibleOAuthClient(user)
     # Delete the user's API keys.
     get_auth_tokens(user).delete()
     response = client.post(reverse('nodes_handler'), {'op': 'start'})
     observed = response.status_code, response.content
     expected = (
         Equals(http.client.UNAUTHORIZED),
         Contains(b"Invalid access token:"),
     )
     self.assertThat(observed, MatchesListwise(expected))
Beispiel #19
0
 def test_no_performer(self):
     """
     When a dispatcher returns None, :class:`NoPerformerFoundError` is
     provided as an error to the Effect's callbacks.
     """
     dispatcher = lambda i: None
     calls = []
     intent = object()
     eff = Effect(intent).on(error=calls.append)
     perform(dispatcher, eff)
     self.assertThat(
         calls,
         MatchesListwise([MatchesException(NoPerformerFoundError(intent))]))
Beispiel #20
0
    def assertRetry(self, clock, observed, expected_elapsed,
                    expected_remaining, expected_wait):
        """Assert that the retry tuple matches the given expectations.

        Retry tuples are those returned by `retries`.
        """
        self.assertThat(
            observed,
            MatchesListwise([
                Equals(expected_elapsed),  # elapsed
                Equals(expected_remaining),  # remaining
                Equals(expected_wait),  # wait
            ]))
Beispiel #21
0
 def test__interprets_carriage_return(self):
     proc = Popen(
         'bash -c "echo -en foo\rmaas"',
         stdout=PIPE,
         stderr=PIPE,
         shell=True,
     )
     self.assertThat(
         self.capture(proc),
         MatchesListwise(
             (Equals(0), Equals("maas"), Equals(""), Equals("maas"))
         ),
     )
Beispiel #22
0
 def test__interprets_backslash(self):
     proc = Popen(
         'bash -c "echo -en \bmas\bas"',
         stdout=PIPE,
         stderr=PIPE,
         shell=True,
     )
     self.assertThat(
         self.capture(proc),
         MatchesListwise(
             (Equals(0), Equals("maas"), Equals(""), Equals("maas"))
         ),
     )
Beispiel #23
0
 def test_setGrants(self):
     [ref] = self.factory.makeGitRefs()
     owner = ref.owner
     grantee = self.factory.makePerson()
     with person_logged_in(owner):
         ref_url = api_url(ref)
         grantee_url = api_url(grantee)
     webservice = webservice_for_person(
         owner, permission=OAuthPermission.WRITE_PUBLIC)
     webservice.default_api_version = "devel"
     response = webservice.named_post(ref_url,
                                      "setGrants",
                                      grants=[
                                          {
                                              "grantee_type":
                                              "Repository owner",
                                              "can_create": True,
                                              "can_force_push": True,
                                          },
                                          {
                                              "grantee_type": "Person",
                                              "grantee_link": grantee_url,
                                              "can_push": True,
                                          },
                                      ])
     self.assertEqual(200, response.status)
     with person_logged_in(owner):
         self.assertThat(
             list(ref.repository.rules),
             MatchesListwise([
                 MatchesStructure(
                     repository=Equals(ref.repository),
                     ref_pattern=Equals(ref.path),
                     creator=Equals(owner),
                     grants=MatchesSetwise(
                         MatchesStructure(
                             grantor=Equals(owner),
                             grantee_type=Equals(
                                 GitGranteeType.REPOSITORY_OWNER),
                             grantee=Is(None),
                             can_create=Is(True),
                             can_push=Is(False),
                             can_force_push=Is(True)),
                         MatchesStructure(grantor=Equals(owner),
                                          grantee_type=Equals(
                                              GitGranteeType.PERSON),
                                          grantee=Equals(grantee),
                                          can_create=Is(False),
                                          can_push=Is(True),
                                          can_force_push=Is(False)))),
             ]))
 def test_composeBuildRequest(self):
     job = self.makeJob()
     lfa = self.factory.makeLibraryFileAlias(db_only=True)
     job.build.distro_arch_series.addOrUpdateChroot(lfa)
     build_request = yield job.composeBuildRequest(None)
     self.assertThat(
         build_request,
         MatchesListwise([
             Equals('snap'),
             Equals(job.build.distro_arch_series),
             Equals(job.build.pocket),
             Equals({}),
             IsInstance(dict),
         ]))
Beispiel #25
0
    def test_bare_callable(self):
        """
        Bare callables are wrapped in a `handler` interceptor and placed into a
        pvector. The function name becomes the route name.
        """
        def func(_):
            return 42

        self.assertThat(
            route.route(u'/foo', route.GET, func),
            MatchesStructure(
                name=Equals('func'),
                interceptors=MatchesListwise(
                    [EnterStage(ContainsDict({RESPONSE: Equals(42)}))])))
Beispiel #26
0
 def test_nested_effect_exception_passes_to_outer_error_handler(self):
     """
     If an inner effect raises an exception, it bubbles up and the
     exc_info is passed to the outer effect's error handlers.
     """
     calls = []
     intent = lambda box: box.fail(
         (ValueError, ValueError('oh dear'), None))
     perform(
         func_dispatcher,
         Effect(lambda box: box.succeed(Effect(intent))).on(
             error=calls.append))
     self.assertThat(
         calls, MatchesListwise([MatchesException(ValueError('oh dear'))]))
Beispiel #27
0
    def test_callback_sucecss_exception(self):
        """
        If a success callback raises an error, the exception is passed to the
        error callback.
        """
        calls = []

        perform(
            func_dispatcher,
            Effect(lambda box: box.succeed("foo")).on(
                success=lambda _: raise_(ValueError("oh dear"))).on(
                    error=calls.append))
        self.assertThat(
            calls, MatchesListwise([MatchesException(ValueError('oh dear'))]))
Beispiel #28
0
 def test__does_not_wait_for_forked_process(self):
     start_time = time.time()
     proc = Popen('sleep 6 &', stdout=PIPE, stderr=PIPE, shell=True)
     self.assertThat(
         self.capture(proc),
         MatchesListwise((
             Equals(0),
             Equals(""),
             Equals(""),
             Equals(""),
         )))
     # A forked process should continue running after capture_script_output
     # returns. capture_script_output should not block on the forked call.
     self.assertLess(time.time() - start_time, 3)
 def test_mantis_makeRequest_can_handle_cookies(self):
     # Ensure that the makeRequest method of the Mantis bug tracker
     # handles cookies.
     responses.add('GET',
                   'http://mantis.example.com/',
                   status=200,
                   headers={'Set-Cookie': 'foo=bar'})
     tracker = Mantis('http://mantis.example.com')
     tracker.makeRequest('GET', 'http://mantis.example.com/')
     self.assertThat(
         tracker._cookie_jar,
         MatchesListwise([
             MatchesStructure.byEquality(name='foo', value='bar'),
         ]))
Beispiel #30
0
 def test_resolve_effect_cb_exception(self):
     """
     When a callback raises an exception, the next error handler is called
     with the exception info.
     """
     self.assertThat(
         resolve_effect(
             Effect("orig")
                 .on(success=lambda r: 1 / 0)
                 .on(error=lambda exc: ('handled', exc)),
             'result'),
         MatchesListwise([
             Equals('handled'),
             MatchesException(ZeroDivisionError)]))