|
7 | 7 | import secrets |
8 | 8 | import string |
9 | 9 | import subprocess |
10 | | -import tempfile |
11 | 10 |
|
12 | 11 | import juju.unit |
13 | 12 | import yaml |
@@ -830,142 +829,6 @@ async def get_cluster_status(unit: Unit, cluster_set: bool | None = False) -> di |
830 | 829 | return result.get("status", {}) |
831 | 830 |
|
832 | 831 |
|
833 | | -async def delete_file_or_directory_in_unit(ops_test: OpsTest, unit_name: str, path: str) -> bool: |
834 | | - """Delete a file in the provided unit. |
835 | | -
|
836 | | - Args: |
837 | | - ops_test: The ops test framework |
838 | | - unit_name: The name unit on which to delete the file from |
839 | | - path: The path of file or directory to delete |
840 | | -
|
841 | | - Returns: |
842 | | - boolean indicating success |
843 | | - """ |
844 | | - if path.strip() in ["/", "."]: |
845 | | - return |
846 | | - |
847 | | - try: |
848 | | - return_code, _, _ = await ops_test.juju( |
849 | | - "ssh", unit_name, "sudo", "find", path, "-maxdepth", "1", "-delete" |
850 | | - ) |
851 | | - |
852 | | - return return_code == 0 |
853 | | - except Exception: |
854 | | - return False |
855 | | - |
856 | | - |
857 | | -async def write_content_to_file_in_unit( |
858 | | - ops_test: OpsTest, unit: Unit, path: str, content: str |
859 | | -) -> None: |
860 | | - """Write content to the file in the provided unit. |
861 | | -
|
862 | | - Args: |
863 | | - ops_test: The ops test framework |
864 | | - unit: THe unit in which to write to file in |
865 | | - path: The path at which to write the content to |
866 | | - content: The content to write to the file. |
867 | | - """ |
868 | | - with tempfile.NamedTemporaryFile(mode="w") as temp_file: |
869 | | - temp_file.write(content) |
870 | | - temp_file.flush() |
871 | | - |
872 | | - await unit.scp_to(temp_file.name, "/tmp/file") |
873 | | - |
874 | | - return_code, _, _ = await ops_test.juju("ssh", unit.name, "sudo", "mv", "/tmp/file", path) |
875 | | - assert return_code == 0 |
876 | | - |
877 | | - return_code, _, _ = await ops_test.juju( |
878 | | - "ssh", unit.name, "sudo", "chown", "snap_daemon:root", path |
879 | | - ) |
880 | | - assert return_code == 0 |
881 | | - |
882 | | - |
883 | | -async def read_contents_from_file_in_unit(ops_test: OpsTest, unit: Unit, path: str) -> str: |
884 | | - """Read contents from file in the provided unit. |
885 | | -
|
886 | | - Args: |
887 | | - ops_test: The ops test framework |
888 | | - unit: The unit in which to read file from |
889 | | - path: The path from which to read content from |
890 | | -
|
891 | | - Returns: |
892 | | - the contents of the file |
893 | | - """ |
894 | | - return_code, _, _ = await ops_test.juju("ssh", unit.name, "sudo", "cp", path, "/tmp/file") |
895 | | - assert return_code == 0 |
896 | | - |
897 | | - return_code, _, _ = await ops_test.juju( |
898 | | - "ssh", unit.name, "sudo", "chown", "ubuntu:ubuntu", "/tmp/file" |
899 | | - ) |
900 | | - assert return_code == 0 |
901 | | - |
902 | | - with tempfile.NamedTemporaryFile(mode="r+") as temp_file: |
903 | | - await unit.scp_from("/tmp/file", temp_file.name) |
904 | | - |
905 | | - temp_file.seek(0) |
906 | | - |
907 | | - contents = "" |
908 | | - for line in temp_file: |
909 | | - contents += line |
910 | | - contents += "\n" |
911 | | - |
912 | | - return_code, _, _ = await ops_test.juju("ssh", unit.name, "sudo", "rm", "/tmp/file") |
913 | | - assert return_code == 0 |
914 | | - |
915 | | - return contents |
916 | | - |
917 | | - |
918 | | -async def ls_la_in_unit(ops_test: OpsTest, unit_name: str, directory: str) -> list[str]: |
919 | | - """Returns the output of ls -la in unit. |
920 | | -
|
921 | | - Args: |
922 | | - ops_test: The ops test framework |
923 | | - unit_name: The name of unit in which to run ls -la |
924 | | - path: The path from which to run ls -la |
925 | | -
|
926 | | - Returns: |
927 | | - a list of files returned by ls -la |
928 | | - """ |
929 | | - return_code, output, _ = await ops_test.juju("ssh", unit_name, "sudo", "ls", "-la", directory) |
930 | | - assert return_code == 0 |
931 | | - |
932 | | - ls_output = output.split("\n")[1:] |
933 | | - |
934 | | - return [ |
935 | | - line.strip("\r") |
936 | | - for line in ls_output |
937 | | - if len(line.strip()) > 0 and line.split()[-1] not in [".", ".."] |
938 | | - ] |
939 | | - |
940 | | - |
941 | | -async def stop_running_flush_mysql_cronjobs(ops_test: OpsTest, unit_name: str) -> None: |
942 | | - """Stop running any logrotate jobs that may have been triggered by cron. |
943 | | -
|
944 | | - Args: |
945 | | - ops_test: The ops test object passed into every test case |
946 | | - unit_name: The name of the unit to be tested |
947 | | - """ |
948 | | - # send TERM signal to mysql daemon, which trigger shutdown process |
949 | | - await ops_test.juju( |
950 | | - "ssh", |
951 | | - unit_name, |
952 | | - "sudo", |
953 | | - "pkill", |
954 | | - "-15", |
955 | | - "-f", |
956 | | - "logrotate -f /etc/logrotate.d/flush_mysql_logs", |
957 | | - ) |
958 | | - |
959 | | - # hold execution until process is stopped |
960 | | - try: |
961 | | - for attempt in Retrying(stop=stop_after_attempt(45), wait=wait_fixed(2)): |
962 | | - with attempt: |
963 | | - if await get_process_pid(ops_test, unit_name, "logrotate"): |
964 | | - raise Exception |
965 | | - except RetryError as e: |
966 | | - raise Exception("Failed to stop the flush_mysql_logs logrotate process.") from e |
967 | | - |
968 | | - |
969 | 832 | def get_unit_by_index(app_name: str, units: list, index: int): |
970 | 833 | """Get unit by index. |
971 | 834 |
|
|
0 commit comments