~sseman/juju-ci-tools/model-change-watcher-py3

« back to all changes in this revision

Viewing changes to fakejuju.py

  • Committer: Seman Said
  • Date: 2016-12-08 23:39:07 UTC
  • mfrom: (1771.1.5 mega-watcher-py3)
  • Revision ID: seman.said@canonical.com-20161208233907-1e1wskxzugdhmvpw
Merged.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
__metaclass__ = type
23
23
 
24
24
 
25
 
def check_juju_output(func):
26
 
    def wrapper(*args, **kwargs):
27
 
        result = func(*args, **kwargs)
28
 
        if 'service' in result:
29
 
            raise AssertionError('Result contained service')
30
 
        return result
31
 
    return wrapper
32
 
 
33
 
 
34
25
class ControllerOperation(Exception):
35
26
 
36
27
    def __init__(self, operation):
146
137
 
147
138
    def add_machine(self, host_name=None, machine_id=None):
148
139
        if machine_id is None:
149
 
            machine_id = str(self.machine_id_iter.next())
 
140
            machine_id = str(next(self.machine_id_iter))
150
141
        self.machines.add(machine_id)
151
142
        if host_name is None:
152
143
            host_name = '{}.example.com'.format(machine_id)
732
723
        if model is not None:
733
724
            full_args.extend(['-m', model])
734
725
        full_args.extend(args)
735
 
        self.log.log(level, ' '.join(full_args))
 
726
        self.log.log(level, u' '.join(full_args))
736
727
 
737
728
    def juju(self, command, args, used_feature_flags,
738
729
             juju_home, model=None, check=True, timeout=None, extra_env=None):
739
730
        if 'service' in command:
740
731
            raise Exception('Command names must not contain "service".')
741
 
        if isinstance(args, basestring):
 
732
        # Python 2 and 3 compatibility
 
733
        try:
 
734
            argtype = basestring
 
735
        except NameError:
 
736
            argtype = str
 
737
        if isinstance(args, argtype):
742
738
            args = (args,)
743
739
        self._log_command(command, args, model)
744
740
        if model is not None:
867
863
        self.juju(command, args, used_feature_flags,
868
864
                  juju_home, model, timeout=timeout)
869
865
 
870
 
    @check_juju_output
871
866
    def get_juju_output(self, command, args, used_feature_flags, juju_home,
872
867
                        model=None, timeout=None, user_name=None,
873
868
                        merge_stderr=False):
910
905
                status_dict = model_state.get_status_dict()
911
906
                # Parsing JSON is much faster than parsing YAML, and JSON is a
912
907
                # subset of YAML, so emit JSON.
913
 
                return json.dumps(status_dict)
 
908
                return json.dumps(status_dict).encode('utf-8')
914
909
            if command == 'create-backup':
915
910
                self.controller_state.require_controller('backup', model)
916
911
                return 'juju-backup-0.tar.gz'