From 8a94a451203a2b0850ac47029d3b7fdc6fb5e5a3 Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 26 Oct 2023 10:45:08 +1100 Subject: [PATCH] #1247 #1248 Feat: Cypress UI testing + Postgres Arg for installer --- devcontainer-example/docker-compose.yml | 52 ++++---- development/installer.py | 13 +- docs/development.md | 8 +- install_x11_deps.sh | 150 ++++++++++++------------ 4 files changed, 110 insertions(+), 113 deletions(-) diff --git a/devcontainer-example/docker-compose.yml b/devcontainer-example/docker-compose.yml index fa7dfe2c..8d9be8e9 100644 --- a/devcontainer-example/docker-compose.yml +++ b/devcontainer-example/docker-compose.yml @@ -39,31 +39,33 @@ services: ports: - 8000-8005:8000-8005 - 9000-9005:9000-9005 - ui-tester: - # pass custom command to start Cypress otherwise it will use the entrypoint - # specified in the Cypress Docker image. - # also pass "--project " so that when Cypress opens - # it can find file "cypress.json" and show integration specs - # https://on.cypress.io/command-line#cypress-open - entrypoint: 'sleep infinity' - image: "docker.io/cypress/included:latest" - #command: sleep infinity - environment: - - SHELL=/bin/bash - # get the IP address of the host machine and allow X11 to accept - # incoming connections from that IP address - # IP=$(ipconfig getifaddr en0) or mac or \ - # IP=$($(hostname -I | awk '{print $1}') ) for Ubuntu - # /usr/X11/bin/xhost + $IP - # then pass the environment variable DISPLAY to show Cypress GUI on the host system - # DISPLAY=$IP:0 - - DISPLAY - volumes: - # for Cypress to communicate with the X11 server pass this socket file - # in addition to any other mapped volumes - - /tmp/.X11-unix:/tmp/.X11-unix - - ..:/workspace:z,cached - network_mode: "host" + # enable the below service if you need Cypress UI Tests to be executed + # Before enabling ensure install_x11_deps.sh has been executed and display variable is exported. + # Run install_x11_deps.sh again if DISPLAY is not set + # ui-tester: + # # pass custom command to start Cypress otherwise it will use the entrypoint + # # specified in the Cypress Docker image. + # # also pass "--project " so that when Cypress opens + # # it can find file "cypress.json" and show integration specs + # # https://on.cypress.io/command-line#cypress-open + # entrypoint: 'sleep infinity' + # image: "docker.io/cypress/included:latest" + # environment: + # - SHELL=/bin/bash + # # get the IP address of the host machine and allow X11 to accept + # # incoming connections from that IP address + # # IP=$(ipconfig getifaddr en0) or mac or \ + # # IP=$($(hostname -I | awk '{print $1}') ) for Ubuntu + # # /usr/X11/bin/xhost + $IP + # # then pass the environment variable DISPLAY to show Cypress GUI on the host system + # # DISPLAY=$IP:0 + # - DISPLAY + # volumes: + # # for Cypress to communicate with the X11 server pass this socket file + # # in addition to any other mapped volumes + # - /tmp/.X11-unix:/tmp/.X11-unix + # - ..:/workspace:z,cached + # network_mode: "host" volumes: mariadb-data: #postgresql-data: diff --git a/development/installer.py b/development/installer.py index 4c33ce46..350f5353 100755 --- a/development/installer.py +++ b/development/installer.py @@ -106,12 +106,13 @@ def get_args_parser(): default="admin", ) parser.add_argument( + "-d", "--db-type", action="store", type=str, help="Database type to use (e.g., mariadb or postgres)", default="mariadb", # Set your default database type here - ) + ) return parser @@ -119,7 +120,6 @@ def init_bench_if_not_exist(args): if os.path.exists(args.bench_name): cprint("Bench already exists. Only site will be created", level=3) return - try: env = os.environ.copy() if args.py_version: @@ -135,7 +135,6 @@ def init_bench_if_not_exist(args): init_command += f"--frappe-path={args.frappe_repo} " init_command += f"--frappe-branch={args.frappe_branch} " init_command += f"--apps_path={args.apps_json} " - #init_command += f"--db_type={args.db_type} " #--db-type postgres init_command += args.bench_name print(init_command) command = [ @@ -200,13 +199,13 @@ def init_bench_if_not_exist(args): def create_site_in_bench(args): - mariadb_socket_option="" + mariadb_socket_option = "" if "mariadb" == args.db_type: cprint("Set db_host", level=3) subprocess.call( ["bench", "set-config", "-g", "db_host", "mariadb"], cwd=os.getcwd() + "/" + args.bench_name, - ) + ) new_site_cmd = [ "bench", "new-site", @@ -221,7 +220,7 @@ def create_site_in_bench(args): subprocess.call( ["bench", "set-config", "-g", "db_host", "postgresql"], cwd=os.getcwd() + "/" + args.bench_name, - ) + ) new_site_cmd = [ "bench", "new-site", @@ -235,9 +234,7 @@ def create_site_in_bench(args): apps.remove("frappe") for app in apps: new_site_cmd.append(f"--install-app={app}") - new_site_cmd.append(args.site_name) - cprint(f"Creating Site {args.site_name} ...", level=2) subprocess.call( new_site_cmd, diff --git a/docs/development.md b/docs/development.md index 344b3523..35a54677 100644 --- a/docs/development.md +++ b/docs/development.md @@ -393,15 +393,15 @@ To run cypress based UI tests in a docker environment, follow the below steps: ```shell sudo ./install_x11_deps.sh ``` + This script will install required deps, enable X11Forwarding and restart SSH daemon and export `DISPLAY` variable. 2. Run X11 service `startx` or `xquartz` 3. Start docker compose services. -4. SSH into ui-tester service using `docker exec.. ` command -5. Export CYPRESS_baseUrl and other required env variables +4. SSH into ui-tester service using `docker exec..` command +5. Export CYPRESS_baseUrl and other required env variables 6. Start Cypress UI console by issuing `cypress run command` > More references : [Cypress Official Documentation](https://www.cypress.io/blog/2019/05/02/run-cypress-with-a-single-docker-command) -> Ensure DISPLAY enviroment is always exported. - +> Ensure DISPLAY environment is always exported. \ No newline at end of file diff --git a/install_x11_deps.sh b/install_x11_deps.sh index a88606a0..a66b2042 100755 --- a/install_x11_deps.sh +++ b/install_x11_deps.sh @@ -6,101 +6,99 @@ set -e # Check if the script is running with root privileges if [ "$EUID" -ne 0 ]; then - echo "Error: This script requires root privileges. Please run it as a superuser" - exit 1 + echo "Error: This script requires root privileges. Please run it as a superuser" + exit 1 fi # Function to restart SSH service (Linux) restart_ssh_linux() { - if command -v service >/dev/null 2>&1; then - sudo service ssh restart - else - sudo systemctl restart ssh - fi + if command -v service >/dev/null 2>&1; then + sudo service ssh restart + else + sudo systemctl restart ssh + fi } # Function to restart SSH service (macOS) restart_ssh_macos() { - launchctl stop com.openssh.sshd - launchctl start com.openssh.sshd + launchctl stop com.openssh.sshd + launchctl start com.openssh.sshd } update_x11_forwarding() { - if grep -q "X11Forwarding yes" /etc/ssh/sshd_config; then - echo "X11Forwarding is already set to 'yes' in ssh_config." - else - if [[ "$OSTYPE" == "linux-gnu" ]]; then - # Linux: Use sed for Linux - sudo sed -i 's/#\?X11Forwarding.*/X11Forwarding yes/' /etc/ssh/sshd_config - elif [[ "$OSTYPE" == "darwin"* ]]; then - # macOS: Use sed for macOS - sudo sed -i -E 's/#X11Forwarding.*/X11Forwarding yes/' /etc/ssh/sshd_config - restart_ssh_macos - fi - if [[ "$OSTYPE" == "linux-gnu" ]]; then - restart_ssh_linux - fi + if grep -q "X11Forwarding yes" /etc/ssh/sshd_config; then + echo "X11Forwarding is already set to 'yes' in ssh_config." + else + if [[ "$OSTYPE" == "linux-gnu" ]]; then + # Linux: Use sed for Linux + sudo sed -i 's/#\?X11Forwarding.*/X11Forwarding yes/' /etc/ssh/sshd_config + elif [[ "$OSTYPE" == "darwin"* ]]; then + # macOS: Use sed for macOS + sudo sed -i -E 's/#X11Forwarding.*/X11Forwarding yes/' /etc/ssh/sshd_config + restart_ssh_macos fi + if [[ "$OSTYPE" == "linux-gnu" ]]; then + restart_ssh_linux + fi + fi } # Determine the operating system if [[ "$OSTYPE" == "linux-gnu" ]]; then - # Linux - if command -v startx >/dev/null 2>&1; then - echo "X11 is already installed." + # Linux + if command -v startx >/dev/null 2>&1; then + echo "X11 is already installed." + else + # Check which package manager is available + if command -v apt-get >/dev/null 2>&1; then + install_command="sudo apt-get update && sudo apt-get install xorg openbox" + elif command -v dnf >/dev/null 2>&1; then + install_command="sudo dnf install xorg-x11-server-Xorg openbox" else - # Check which package manager is available - if command -v apt-get >/dev/null 2>&1; then - package_manager="apt" - elif command -v dnf >/dev/null 2>&1; then - package_manager="dnf" - else - echo "Error: Unable to determine the package manager. Manual installation required." - exit 1 - fi - - # Install X11 and Openbox based on the package manager - if [ "$package_manager" = "apt" ]; then - sudo apt-get update - sudo apt-get install xorg openbox - elif [ "$package_manager" = "dnf" ]; then - sudo dnf install xorg-x11-server-Xorg openbox - fi - - # Check if X11 and Openbox were installed successfully - if [ $? -eq 0 ]; then - echo "X11 and Openbox have been successfully installed." - else - echo "Error: Failed to install X11 and Openbox." - exit 1 - fi + echo "Error: Unable to determine the package manager. Manual installation required." + exit 1 fi - - # Call the function to update X11Forwarding - update_x11_forwarding - - # Get the IP address of the host dynamically - host_ip=$(hostname -I | awk '{print $1}') - xhost + $host_ip && xhost + local: - # Set the DISPLAY variable to the host IP - export DISPLAY="$host_ip:0.0" - echo "DISPLAY variable set to $DISPLAY" -elif [[ "$OSTYPE" == "darwin"* ]]; then - # macOS - if command -v xquartz >/dev/null 2>&1; then - echo "XQuartz is already installed." + fi + # Check if the installation command is defined + if [ -n "$install_command" ]; then + # Execute the installation command + if $install_command; then + echo "X11 and Openbox have been successfully installed." else - echo "Error: XQuartz is required for X11 forwarding on macOS. Please install XQuartz manually." - exit 1 + echo "Error: Failed to install X11 and Openbox." + exit 1 fi - - # Call the function to update X11Forwarding - update_x11_forwarding - - # Export the DISPLAY variable for macOS - export DISPLAY=:0 - echo "DISPLAY variable set to $DISPLAY" -else - echo "Error: Unsupported operating system." + else + echo "Error: Unsupported package manager." exit 1 + fi + + # Call the function to update X11Forwarding + update_x11_forwarding + + # Get the IP address of the host dynamically + host_ip=$(hostname -I | awk '{print $1}') + xhost + "$host_ip" && xhost + local: + # Set the DISPLAY variable to the host IP + export DISPLAY="$host_ip:0.0" + echo "DISPLAY variable set to $DISPLAY" + +elif [[ "$OSTYPE" == "darwin"* ]]; then + # macOS + if command -v xquartz >/dev/null 2>&1; then + echo "XQuartz is already installed." + else + echo "Error: XQuartz is required for X11 forwarding on macOS. Please install XQuartz manually." + exit 1 + fi + + # Call the function to update X11Forwarding + update_x11_forwarding + + # Export the DISPLAY variable for macOS + export DISPLAY=:0 + echo "DISPLAY variable set to $DISPLAY" +else + echo "Error: Unsupported operating system." + exit 1 fi