From 3513bd285ec95dcb812291b89b6ac9cb2dc0a7e2 Mon Sep 17 00:00:00 2001 From: Yang Chiu Date: Tue, 12 Dec 2023 09:10:20 +0800 Subject: [PATCH] ci: fix build test env image system-wide python package installation error Signed-off-by: Yang Chiu --- test_framework/Dockerfile.setup | 3 +- test_framework/scripts/longhorn-setup.sh | 7 +- .../scripts/rancher/webdriver/main.py | 138 ------------------ 3 files changed, 5 insertions(+), 143 deletions(-) delete mode 100644 test_framework/scripts/rancher/webdriver/main.py diff --git a/test_framework/Dockerfile.setup b/test_framework/Dockerfile.setup index a507e256c2..d4c035e2f1 100644 --- a/test_framework/Dockerfile.setup +++ b/test_framework/Dockerfile.setup @@ -25,8 +25,7 @@ RUN wget -q https://storage.googleapis.com/kubernetes-release/release/$KUBECTL_V wget -q "https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64" && \ mv yq_linux_amd64 /usr/local/bin/yq && \ chmod +x /usr/local/bin/yq && \ - apk add openssl openssh-client ca-certificates git rsync bash curl jq chromium chromium-chromedriver python3 py3-pip && \ - pip3 install -U selenium==3.141.0 && \ + apk add openssl openssh-client ca-certificates git rsync bash curl jq && \ ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa && \ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 && \ chmod 700 get_helm.sh && \ diff --git a/test_framework/scripts/longhorn-setup.sh b/test_framework/scripts/longhorn-setup.sh index e39c04924a..ab3149d6c3 100755 --- a/test_framework/scripts/longhorn-setup.sh +++ b/test_framework/scripts/longhorn-setup.sh @@ -89,9 +89,10 @@ install_rancher() { get_rancher_api_key() { - python3 "${TF_VAR_tf_workspace}/scripts/rancher/webdriver/main.py" "${RANCHER_HOSTNAME}" "${RANCHER_BOOTSTRAP_PASSWORD}" - RANCHER_ACCESS_KEY=`cat "${PWD}/access_key"` - RANCHER_SECRET_KEY=`cat "${PWD}/secret_key"` + TOKEN=$(curl -X POST -s -k "https://${RANCHER_HOSTNAME}/v3-public/localproviders/local?action=login" -H 'Content-Type: application/json' -d "{\"username\":\"admin\", \"password\":\"${RANCHER_BOOTSTRAP_PASSWORD}\", \"responseType\": \"json\"}" | jq -r '.token' | tr -d '"') + ARR=(${TOKEN//:/ }) + RANCHER_ACCESS_KEY=${ARR[0]} + RANCHER_SECRET_KEY=${ARR[1]} } diff --git a/test_framework/scripts/rancher/webdriver/main.py b/test_framework/scripts/rancher/webdriver/main.py deleted file mode 100644 index b57fb7ed63..0000000000 --- a/test_framework/scripts/rancher/webdriver/main.py +++ /dev/null @@ -1,138 +0,0 @@ -import os -import sys - -from selenium import webdriver -from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException -from selenium.webdriver.common.by import By -from selenium.webdriver.support import expected_conditions -from selenium.webdriver.support.wait import WebDriverWait - -wait_timeout = 10 -click_retry_timeout = 20 - - -def navigate_and_wait_for(target, expect): - success = False - retry = 0 - max_retry = 10 - while success is not True and retry < max_retry: - try: - driver.get(target) - wait_for(expect) - success = True - except ElementClickInterceptedException as e: - raise e - except Exception as e: - print(f'[retry {retry}] try to navigate to {target} and wait for {expect} failed ... {repr(e)} {e}') - retry += 1 - - -def wait_for(target): - try: - WebDriverWait(driver, wait_timeout).until( - expected_conditions.presence_of_element_located((By.XPATH, elements[target])) - ) - return True - except Exception as e: - print(f'wait for {target} error: {e}') - return False - - -def click(target): - driver.find_element_by_xpath(elements[target]).click() - - -def click_and_wait(target, expect): - success = False - retry = 0 - max_retry = 10 - while success is not True and retry < max_retry: - try: - _target = driver.find_element_by_xpath(elements[target]) - _target.click() - WebDriverWait(driver, click_retry_timeout).until( - expected_conditions.presence_of_element_located((By.XPATH, elements[expect])) - ) - success = True - except ElementClickInterceptedException as e: - raise e - except Exception as e: - print(f'[retry {retry}] try to click {target} and wait for {expect} failed ... {repr(e)} {e}') - retry += 1 - - -def send_keys(target, keys): - _target = driver.find_element_by_xpath(elements[target]) - _target.send_keys(keys) - - -def get_element(element): - content = driver.find_element_by_xpath(elements[element]).text - return content - - -elements = { - 'username_input': '//*[@id="username"]', - 'password_input': '//*[@type="password"]', - 'login': '//button[@id="submit"]', - 'agree': '(//*[contains(@class, "checkbox-custom")])[2]', - 'continue': '//*[@type="submit" and not(@disabled)]', - 'local_cluster': '//*[contains(@href, "/local")]', - 'create_api_key': '//button[contains(text(), "Create API Key")]', - 'create_confirm': '//button//*[contains(text() ,"Create")]', - 'access_key': '//*[contains(@class, "with-copy")][1]/span', - 'secret_key': '//*[contains(@class, "with-copy")][2]/span', - 'create_done': '//button//*[contains(text() ,"Done")]' -} - -if __name__ == '__main__': - - url = 'https://' + sys.argv[1] - login_url = url + '/dashboard/auth/login' - account_url = url + '/dashboard/account' - - options = webdriver.ChromeOptions() - prefs = { - 'profile.default_content_setting_values.notifications': 2 - } - options.add_experimental_option('prefs', prefs) - options.add_argument('--headless') - options.add_argument('--ignore-certificate-errors') - options.add_argument('--no-sandbox') - options.add_argument('window-size=1920,1200') - - driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=options) - - success = False - _retry = 0 - _max_retry = 3 - - while not success and _retry < _max_retry: - try: - print(login_url) - driver.get(login_url) - try: - wait_for('username_input') - send_keys('username_input', 'admin') - except Exception as e: - print(f'no username field {e}') - send_keys('password_input', sys.argv[2]) - click_and_wait('login', 'agree') - click_and_wait('agree', 'continue') - click_and_wait('continue', 'local_cluster') - navigate_and_wait_for(account_url, 'create_api_key') - click_and_wait('create_api_key', 'create_confirm') - click_and_wait('create_confirm', 'access_key') - access_key = get_element('access_key') - secret_key = get_element('secret_key') - click('create_done') - with open('access_key', 'w') as f: - f.write(str(access_key)) - with open('secret_key', 'w') as f: - f.write(str(secret_key)) - success = True - except Exception as e: - print(f'parsing error: {e}') - _retry += 1 - - driver.quit()