~veebers/juju-ci-tools/poc-juju-ci-timing

« back to all changes in this revision

Viewing changes to jujupy/fake.py

  • Committer: Aaron Bentley
  • Date: 2017-03-16 20:32:33 UTC
  • mfrom: (1937.1.1 coroutine-expect-child)
  • Revision ID: aaron.bentley@canonical.com-20170316203233-xqk8s0yec003osig
Support generator coroutines in PromptingExpectChild.

Show diffs side-by-side

added added

removed removed

Lines of Context:
378
378
 
379
379
 
380
380
class PromptingExpectChild(FakeExpectChild):
 
381
    """A fake ExpectChild based on prompt/response.
 
382
 
 
383
    It accepts an iterator of prompts.  If that iterator supports send(),
 
384
    the last input to sendline will be sent.
 
385
 
 
386
    This allows fairly natural generators, e.g.:
 
387
 
 
388
        foo = yield "Please give me foo".
 
389
 
 
390
    You can also just iterate through prompts and retrieve the corresponding
 
391
    values from self.values at the end.
 
392
    """
381
393
 
382
394
    def __init__(self, backend, juju_home, extra_env, prompts):
383
395
        super(PromptingExpectChild, self).__init__(backend, juju_home,
384
396
                                                   extra_env)
385
 
        self.prompts = iter(prompts)
 
397
        self._prompts = iter(prompts)
386
398
        self.values = {}
387
399
        self.lines = []
 
400
        # If not a generator, invoke next() instead of send.
 
401
        self._send = getattr(self._prompts, 'send',
 
402
                             lambda x: next(self._prompts))
 
403
        self._send_line = None
 
404
 
 
405
    @property
 
406
    def prompts(self):
 
407
        return self._prompts
388
408
 
389
409
    def expect(self, pattern):
390
410
        if type(pattern) is not list:
391
411
            pattern = [pattern]
392
412
        try:
393
 
            prompt = next(self.prompts)
 
413
            prompt = self._send(self._send_line)
 
414
            self._send_line = None
394
415
        except StopIteration:
395
416
            if pexpect.EOF not in pattern:
396
417
                raise
413
434
        super(PromptingExpectChild, self).expect(regex)
414
435
 
415
436
    def sendline(self, line=''):
 
437
        if self._send_line is not None:
 
438
            raise ValueError('Sendline called twice with no expect.')
416
439
        full_match = self.match.group(0)
417
440
        self.values[full_match] = line.rstrip()
418
441
        self.lines.append((full_match, line))
 
442
        self._send_line = line
419
443
 
420
444
 
421
445
class RegisterHost(PromptingExpectChild):
489
513
 
490
514
    def iter_prompts(self):
491
515
        while True:
492
 
            yield self.TYPE
493
 
            if self.values[self.TYPE] != 'bogus':
 
516
            provider_type = yield self.TYPE
 
517
            if provider_type != 'bogus':
494
518
                break
495
519
        while True:
496
 
            yield self.name_prompt
497
 
            if '/' not in self.values[self.name_prompt]:
 
520
            name = yield self.name_prompt
 
521
            if '/' not in name:
498
522
                break
499
 
        if self.values[self.TYPE] == 'maas':
500
 
            yield self.API_ENDPOINT
501
 
            endpoint = self.values[self.API_ENDPOINT]
502
 
            while len(endpoint) > 1000:
503
 
                yield self.cant_validate(endpoint)
504
 
        elif self.values[self.TYPE] == 'manual':
505
 
            yield self.HOST
506
 
            endpoint = self.values[self.HOST]
507
 
            while len(endpoint) > 1000:
508
 
                yield self.cant_validate(endpoint)
509
 
        elif self.values[self.TYPE] == 'openstack':
510
 
            yield self.CLOUD_ENDPOINT
511
 
            endpoint = self.values[self.CLOUD_ENDPOINT]
 
523
        if provider_type == 'maas':
 
524
            endpoint = yield self.API_ENDPOINT
 
525
            while len(endpoint) > 1000:
 
526
                yield self.cant_validate(endpoint)
 
527
        elif provider_type == 'manual':
 
528
            endpoint = yield self.HOST
 
529
            while len(endpoint) > 1000:
 
530
                yield self.cant_validate(endpoint)
 
531
        elif provider_type == 'openstack':
 
532
            endpoint = yield self.CLOUD_ENDPOINT
512
533
            while len(endpoint) > 1000:
513
534
                yield self.cant_validate(endpoint)
514
535
            while True:
515
 
                yield self.AUTH
516
 
                if 'invalid' not in self.values[self.AUTH]:
 
536
                auth = yield self.AUTH
 
537
                if 'invalid' not in auth:
517
538
                    break
518
 
            while self.values.get(self.ANOTHER_REGION) != 'n':
 
539
            while True:
519
540
                yield self.REGION_NAME
520
 
                yield self.REGION_ENDPOINT
521
 
                endpoint = self.values[self.REGION_ENDPOINT]
 
541
                endpoint = yield self.REGION_ENDPOINT
522
542
                if len(endpoint) > 1000:
523
543
                    yield self.cant_validate(endpoint)
524
 
                yield self.ANOTHER_REGION
525
 
        if self.values['Select cloud type:'] == 'vsphere':
526
 
            yield self.CLOUD_ENDPOINT
527
 
            endpoint = self.values[self.CLOUD_ENDPOINT]
 
544
                if (yield self.ANOTHER_REGION) == 'n':
 
545
                    break
 
546
        elif provider_type == 'vsphere':
 
547
            endpoint = yield self.CLOUD_ENDPOINT
528
548
            if len(endpoint) > 1000:
529
549
                yield self.cant_validate(endpoint)
530
 
            while self.values.get(self.ANOTHER_REGION) != 'n':
 
550
            while True:
531
551
                yield self.REGION_NAME
532
 
                yield self.ANOTHER_REGION
 
552
                if (yield self.ANOTHER_REGION) == 'n':
 
553
                    break
533
554
 
534
555
    def close(self):
535
556
        cloud = {