示例#1
0
    def test_deploy(self):
        task_file_path = path.join("tasks", "a_task.py")
        nested_task_path = path.join("tasks", "another_task.py")
        correct_op_name_and_host_names = [
            ("First main operation", True),  # true for all hosts
            ("Second main operation", ("somehost",)),
            ("{0} | First task operation".format(task_file_path), ("anotherhost",)),
            ("{0} | Task order loop 1".format(task_file_path), ("anotherhost",)),
            ("{0} | 2nd Task order loop 1".format(task_file_path), ("anotherhost",)),
            ("{0} | Task order loop 2".format(task_file_path), ("anotherhost",)),
            ("{0} | 2nd Task order loop 2".format(task_file_path), ("anotherhost",)),
            (
                "{0} | {1} | Second task operation".format(task_file_path, nested_task_path),
                ("anotherhost",),
            ),
            ("{0} | First task operation".format(task_file_path), True),
            ("{0} | Task order loop 1".format(task_file_path), True),
            ("{0} | 2nd Task order loop 1".format(task_file_path), True),
            ("{0} | Task order loop 2".format(task_file_path), True),
            ("{0} | 2nd Task order loop 2".format(task_file_path), True),
            ("{0} | {1} | Second task operation".format(task_file_path, nested_task_path), True),
            ("My deploy | First deploy operation", True),
            ("My deploy | My nested deploy | First nested deploy operation", True),
            ("My deploy | Second deploy operation", True),
            ("Loop-0 main operation", True),
            ("Loop-1 main operation", True),
            ("Third main operation", True),
            ("Order loop 1", True),
            ("2nd Order loop 1", True),
            ("Order loop 2", True),
            ("2nd Order loop 2", True),
            ("Final limited operation", ("somehost",)),
        ]

        # Run 3 iterations of the test - each time shuffling the order of the
        # hosts - ensuring that the ordering has no effect on the operation order.
        for _ in range(3):
            ctx_state.reset()

            hosts = ["somehost", "anotherhost", "someotherhost"]
            shuffle(hosts)

            result = run_cli(
                ",".join(hosts),
                path.join("tests", "deploy", "deploy.py"),
                f'--chdir={path.join("tests", "deploy")}',
            )
            assert result.exit_code == 0, result.stdout

            self._assert_op_data(correct_op_name_and_host_names)
示例#2
0
    def test_interdependent_deploy(self):
        ctx_state.reset()

        result = run_cli(
            "somehost",
            path.join("tests", "deploy", "deploy_interdependent.py"),
            f'--chdir={path.join("tests", "deploy")}',
        )
        assert result.exit_code == 0, result.stdout

        # Check every operation had commands/changes - this ensures that each
        # combo (add/remove/add) always had changes.
        for host, ops in state.ops.items():
            for _, op in ops.items():
                assert len(op["commands"]) > 0
示例#3
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
示例#4
0
    def test_random_deploy(self):
        correct_op_name_and_host_names = [
            ("First main operation", True),
            ("Second main somehost operation", ("somehost",)),
            ("Second main anotherhost operation", ("anotherhost",)),
            ("Function call operation", True),
            ("Third main operation", True),
            ("First nested operation", True),
            ("Second nested anotherhost operation", ("anotherhost",)),
            ("Second nested somehost operation", ("somehost",)),
        ]

        # Run 3 iterations of the test - each time shuffling the order of the
        # hosts - ensuring that the ordering has no effect on the operation order.
        for _ in range(3):
            ctx_state.reset()

            hosts = ["somehost", "anotherhost", "someotherhost"]
            shuffle(hosts)

            result = run_cli(
                ",".join(hosts),
                path.join("tests", "deploy", "deploy_random.py"),
                f'--chdir={path.join("tests", "deploy")}',
            )
            assert result.exit_code == 0, result.stdout

            self._assert_op_data(correct_op_name_and_host_names)

            for hostname, expected_fact_count in (
                ("somehost", 2),
                ("anotherhost", 0),
                ("someotherhost", 1),
            ):
                host = inventory.get_host(hostname)
                assert len(host.facts) == expected_fact_count
示例#5
0
 def setUp(self):
     ctx_state.reset()