Example #1
0
    def test_nested_op_api(self):
        inventory = make_inventory()
        state = State(inventory, Config())

        connect_all(state)

        somehost = inventory.get_host("somehost")

        ctx_state.set(state)
        ctx_host.set(somehost)

        pyinfra.is_cli = True

        try:
            outer_result = server.shell(commands="echo outer")
            assert outer_result.combined_output_lines is None

            def callback():
                inner_result = server.shell(commands="echo inner")
                assert inner_result.combined_output_lines is not None

            python.call(function=callback)

            assert len(state.get_op_order()) == 2

            run_ops(state)

            assert len(state.get_op_order()) == 3
            assert state.results[somehost]["success_ops"] == 3
            assert outer_result.combined_output_lines is not None

            disconnect_all(state)
        finally:
            pyinfra.is_cli = False
Example #2
0
    def test_context_host_attr(self):
        host_obj = _create_host()
        ctx_host.set(host_obj)

        with self.assertRaises(AttributeError):
            host_obj.hello

        setattr(host, "hello", "world")
        assert host_obj.hello == host.hello
Example #3
0
    def test_context_host_class_attr(self):
        host_obj = _create_host()
        ctx_host.set(host_obj)
        assert ctx_host.isset() is True

        with self.assertRaises(AttributeError):
            host_obj.hello

        setattr(Host, "hello", "class_world")
        setattr(host_obj, "hello", "instance_world")

        assert host.hello == host.hello

        # Reset and check fallback to class variable
        ctx_host.reset()
        assert ctx_host.isset() is False
        assert host.hello == "class_world"
Example #4
0
    def test_cli_op_line_numbers(self):
        inventory = make_inventory()
        state = State(inventory, Config())
        connect_all(state)

        state.current_deploy_filename = __file__

        pyinfra.is_cli = True
        ctx_state.set(state)

        # Add op to both hosts
        for name in ("anotherhost", "somehost"):
            ctx_host.set(inventory.get_host(name))
            server.shell(
                "echo hi")  # note this is called twice but on *the same line*

        # Add op to just the second host - using the context modules such that
        # it replicates a deploy file.
        ctx_host.set(inventory.get_host("anotherhost"))
        first_context_hash = server.user("anotherhost_user").hash

        # Add op to just the first host - using the context modules such that
        # it replicates a deploy file.
        ctx_host.set(inventory.get_host("somehost"))
        second_context_hash = server.user("somehost_user").hash

        ctx_state.reset()
        ctx_host.reset()

        pyinfra.is_cli = False

        # Ensure there are two ops
        op_order = state.get_op_order()
        assert len(op_order) == 3

        # And that the two ops above were called in the expected order
        assert op_order[1] == first_context_hash
        assert op_order[2] == second_context_hash

        # Ensure somehost has two ops and anotherhost only has the one
        assert len(state.ops[inventory.get_host("somehost")]) == 2
        assert len(state.ops[inventory.get_host("anotherhost")]) == 2
Example #5
0
 def test_context_host_str(self):
     host_obj = _create_host()
     ctx_host.set(host_obj)
     assert str(host_obj) == "None"
Example #6
0
 def test_context_host_repr(self):
     host_obj = _create_host()
     ctx_host.set(host_obj)
     assert repr(host) == "ContextObject(Host):Host(None)"
Example #7
0
 def test_context_host_eq(self):
     host_obj = _create_host()
     ctx_host.set(host_obj)
     assert host == host_obj