def init_still_acts_like_superclass_init(self): # No required args assert isinstance(MockContext().config, Config) config = Config(overrides={"foo": "bar"}) # Posarg assert MockContext(config).config is config # Kwarg assert MockContext(config=config).config is config
def echo_hides_extra_sudo_flags(self): skip() # see TODO in sudo() re: clean output display config = Config(overrides={"runner": _Dummy}) Context(config=config).sudo("nope", echo=True) output = sys.stdout.getvalue() sys.__stderr__.write(repr(output) + "\n") assert "-S" not in output assert Context().sudo.prompt not in output assert "sudo nope" in output
def base_case(self): # NOTE: Assumes a user whose password is 'mypass' has been created # & added to passworded (not passwordless) sudo configuration; and # that this user is the one running the test suite. Only for # running on Travis, basically. if not os.environ.get("TRAVIS", False): skip() config = Config({"sudo": {"password": "******"}}) result = Context(config=config).sudo("whoami", hide=True) assert result.stdout.strip() == "root"
def load_name_defaults_to_config_tasks_collection_name(self): "load() name defaults to config.tasks.collection_name" class MockLoader(_BasicLoader): def find(self, name): # Sanity assert name == "simple_ns_list" return super(MockLoader, self).find(name) config = Config({"tasks": {"collection_name": "simple_ns_list"}}) loader = MockLoader(config=config) # More sanity: expect simple_ns_list.py (not tasks.py) mod, path = loader.load() assert mod.__file__ == os.path.join(support, "simple_ns_list.py")
def config_only(self, Local): runner = Local.return_value # Set a config-driven list of watchers watcher = self.watcher_klass() overrides = {"run": {"watchers": [watcher]}} config = Config(overrides=overrides) Context(config=config).sudo("whoami") # Expect that sudo() extracted that config value & put it into # the kwarg level. (See comment in sudo() about why...) watchers = runner.run.call_args[1]["watchers"] # Will raise ValueError if not in the list watchers.remove(watcher) # Only remaining item in list should be our sudo responder assert len(watchers) == 1 assert isinstance(watchers[0], FailingResponder) assert watchers[0].pattern == self.escaped_prompt
def raises_auth_failure_when_failure_detected(self): with patch("raft.context.FailingResponder") as klass: unacceptable = Mock(side_effect=ResponseNotAccepted) klass.return_value.submit = unacceptable excepted = False try: config = Config(overrides={"sudo": {"password": "******"}}) Context(config=config).sudo("meh", hide=True) except AuthFailure as e: # Basic sanity checks; most of this is really tested in # Runner tests. assert e.result.exited is None expected = "The password submitted to prompt '[sudo] password: '******'t use except/else as that masks other real exceptions, # such as incorrectly unhandled ThreadErrors if not excepted: assert False, "Did not raise AuthFailure!"
def config_use_does_not_modify_config(self, Local): runner = Local.return_value watcher = self.watcher_klass() overrides = {"run": {"watchers": [watcher]}} config = Config(overrides=overrides) Context(config=config).sudo("whoami") # Here, 'watchers' is _the same object_ as was passed into # run(watchers=...). watchers = runner.run.call_args[1]["watchers"] # We want to make sure that what's in the config we just # generated, is untouched by the manipulation done inside # sudo(). # First, that they aren't the same obj err = "Found sudo() reusing config watchers list directly!" assert watchers is not config.run.watchers, err # And that the list is as it was before (i.e. it is not both # our watcher and the sudo()-added one) err = "Our config watchers list was modified!" assert config.run.watchers == [watcher], err
def both_kwarg_and_config(self, Local): runner = Local.return_value # Set a config-driven list of watchers conf_watcher = self.watcher_klass() overrides = {"run": {"watchers": [conf_watcher]}} config = Config(overrides=overrides) # AND supply a DIFFERENT kwarg-driven list of watchers kwarg_watcher = self.watcher_klass() Context(config=config).sudo("whoami", watchers=[kwarg_watcher]) # Expect that the kwarg watcher and our internal one were the # final result. watchers = runner.run.call_args[1]["watchers"] # Will raise ValueError if not in the list. .remove() uses # identity testing, so two instances of self.watcher_klass will # be different values here. watchers.remove(kwarg_watcher) # Only remaining item in list should be our sudo responder assert len(watchers) == 1 assert conf_watcher not in watchers # Extra sanity assert isinstance(watchers[0], FailingResponder) assert watchers[0].pattern == self.escaped_prompt
def may_configure_config_via_constructor(self): config = Config({"tasks": {"collection_name": "mytasks"}}) loader = _BasicLoader(config=config) assert loader.config.tasks.collection_name == "mytasks"
def setup(self): config = Config(defaults={"foo": "bar", "biz": {"baz": "boz"}}) self.c = Context(config=config)
def user_kwarg_wins_over_config(self, Local): runner = Local.return_value config = Config(overrides={"sudo": {"user": "******"}}) Context(config=config).sudo("whoami", user="******") cmd = "sudo -S -p '[sudo] password: ' -H -u calrissian whoami" assert runner.run.call_args[0][0] == cmd
def honors_config_for_user_value(self, Local): runner = Local.return_value config = Config(overrides={"sudo": {"user": "******"}}) Context(config=config).sudo("whoami") cmd = "sudo -S -p '[sudo] password: ' -H -u rando whoami" assert runner.run.call_args[0][0] == cmd
def setup(self): self.escaped_prompt = re.escape(Config().sudo.prompt)
def start_point_is_configurable_via_config(self): config = Config({"tasks": {"search_root": "nowhere"}}) assert FSLoader(config=config).start == "nowhere"
def honors_runner_config_setting(self): runner_class = Mock() config = Config({"runners": {"local": runner_class}}) c = Context(config) c.run("foo") assert runner_class.mock_calls == [call(c), call().run("foo")]
def honors_config_for_prompt_value(self, Local): runner = Local.return_value config = Config(overrides={"sudo": {"prompt": "FEED ME: "}}) Context(config=config).sudo("whoami") cmd = "sudo -S -p 'FEED ME: ' whoami" assert runner.run.call_args[0][0] == cmd
def honors_configured_sudo_password(self): config = Config(overrides={"sudo": {"password": "******"}}) expected = [(self.escaped_prompt, "secret\n")] self._expect_responses(expected, config=config)
def config_attr_may_be_overwritten_at_runtime(self): new_config = Config(defaults={"foo": "notbar"}) self.c.config = new_config assert self.c.foo == "notbar"
def sudo_password_kwarg_wins_over_config(self): config = Config(overrides={"sudo": {"password": "******"}}) kwargs = {"password": "******"} expected = [(self.escaped_prompt, "secret\n")] self._expect_responses(expected, config=config, kwargs=kwargs)
def creates_a_new_Context_from_given_config(self): conf = Config(defaults={"foo": "bar"}) c = Call(_).make_context(conf) assert isinstance(c, Context) assert c.foo == "bar"
def allows_collection_and_config(self): coll = Collection() conf = Config() e = Executor(collection=coll, config=conf) assert e.collection is coll assert e.config is conf