def constraints_to_thrift(constraints): """Convert a python dictionary to a set of Constraint thrift objects.""" result = set() for attribute, constraint_value in constraints.items(): assert isinstance(attribute, Compatibility.string) and (isinstance( constraint_value, Compatibility.string)), ( "Both attribute name and value in constraints must be string") constraint = Constraint() constraint.name = attribute task_constraint = TaskConstraint() if constraint_value.startswith('limit:'): task_constraint.limit = LimitConstraint() try: task_constraint.limit.limit = int( constraint_value.replace('limit:', '', 1)) except ValueError: print('%s is not a valid limit value, must be integer' % constraint_value) raise else: # Strip off the leading negation if present. negated = constraint_value.startswith('!') if negated: constraint_value = constraint_value[1:] task_constraint.value = ValueConstraint( negated, set(constraint_value.split(','))) constraint.constraint = task_constraint result.add(constraint) return result
def constraints_to_thrift(constraints): """Convert a python dictionary to a set of Constraint thrift objects.""" result = set() for attribute, constraint_value in constraints.items(): assert isinstance(attribute, Compatibility.string) and ( isinstance(constraint_value, Compatibility.string)), ( "Both attribute name and value in constraints must be string") constraint = Constraint() constraint.name = attribute task_constraint = TaskConstraint() if constraint_value.startswith('limit:'): task_constraint.limit = LimitConstraint() try: task_constraint.limit.limit = int(constraint_value.replace('limit:', '', 1)) except ValueError: print('%s is not a valid limit value, must be integer' % constraint_value) raise else: # Strip off the leading negation if present. negated = constraint_value.startswith('!') if negated: constraint_value = constraint_value[1:] task_constraint.value = ValueConstraint(negated, set(constraint_value.split(','))) constraint.constraint = task_constraint result.add(constraint) return result
def test_show_job_update_diff_with_task_diff(self): config = self.get_job_config() self._fake_context.get_job_config = Mock(return_value=config) formatter = DiffFormatter(self._fake_context, config) local_task = self.create_scheduled_tasks()[0].assignedTask.task local_task.constraints = set([Constraint(name='host'), Constraint(name='rack')]) self._mock_api.get_job_update_diff.return_value = self.get_job_update_diff_result() formatter.show_job_update_diff(self._mock_options.instance_spec.instance, local_task) assert self._mock_api.get_job_update_diff.mock_calls == [ call(config, self._mock_options.instance_spec.instance) ] assert "\n".join(self._fake_context.get_out()) == textwrap.dedent("""\ This job update will: add instances: [10], [12-14] update instances: [11] with diff:\n 2c2,3 < 'constraints': None, --- > 'constraints': [ Constraint(name='host', constraint=None), > Constraint(name='rack', constraint=None)], \n not change instances: [0-9]""")
def test_diff_unordered_configs(self): """Diff between two config objects with different repr but identical content works ok.""" from_config = self.make_task_configs()[0] from_config.constraints = set([ Constraint(name='value', constraint=ValueConstraint(values=set(['1', '2']))), Constraint(name='limit', constraint=TaskConstraint(limit=LimitConstraint( limit=int(10)))) ]) from_config.taskLinks = {'task1': 'link1', 'task2': 'link2'} from_config.metadata = set( [Metadata(key='k2', value='v2'), Metadata(key='k1', value='v1')]) from_config.executorConfig = ExecutorConfig(name='test', data='test data') from_config.requestedPorts = set(['3424', '142', '45235']) # Deepcopy() almost guarantees from_config != to_config due to a different sequence of # dict insertions. That in turn generates unequal json objects. The ideal here would be to # assert to_config != from_config but that would produce a flaky test as I have observed # the opposite on rare occasions as the ordering is not stable between test runs. to_config = deepcopy(from_config) diff_result = self._updater._diff_configs(from_config, to_config) assert diff_result == "", ('diff result must be empty but was: %s' % diff_result)