Skip to content

Commit c5433f6

Browse files
committed
feat(cypress): enhance kubeconfig selection with Downloads search
- Add current kubeconfig usage prompt - Search Downloads for *kubeconfig* files - Show numbered list for easy selection - Allow custom path input as fallback
1 parent 91c3145 commit c5433f6

2 files changed

Lines changed: 133 additions & 8 deletions

File tree

web/cypress/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ node_modules/ -> dependencies will be installed at runtime here
1717
## Running locally
1818

1919
### Export necessary variables
20-
in order to run Cypress tests, we need to export some environment variables that Cypress can read then pass down to our tests, currently we have following environment variables defined and used.
20+
in order to run Cypress tests, we need to export some environment variables that Cypress can read then pass down to our tests, currently we have following environment variables defined and used.
2121

2222
Using a non-admin user.
2323
```bash
@@ -62,6 +62,28 @@ Set the following var to use custom Monitoring Console Plugin UI plugin image. T
6262
export CYPRESS_MCP_CONSOLE_IMAGE=<Monitoring Console Plugin image>
6363
```
6464

65+
### Environment Configuration Script
66+
67+
The `configure-env.sh` script provides an interactive way to set up all the required environment variables. This script eliminates the need to manually export each variable and helps find the correct kubeconfig file.
68+
69+
**Features:**
70+
- Automatic prompting for all CYPRESS_ variables
71+
- Automatic discovery and numbered selection of `*kubeconfig*` files in `$HOME/Downloads` dir
72+
73+
**Usage:**
74+
```bash
75+
# Note: source command requires Bash shell
76+
source ./configure-env.sh
77+
```
78+
To export variables directly (Bash only).
79+
80+
**File generation**
81+
```bash
82+
./configure-env.sh
83+
```
84+
Creates an export file you can source later. (`source "export-env.sh`)
85+
86+
6587
### Before running cypress
6688
- Make sure cluster's kubeconfig file is located at the correct environment variable / path you have exported
6789
- The file to run Monitoring Plugin tests: bvt.cy.ts

web/cypress/configure-env.sh

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,119 @@ main() {
259259
fi
260260
login_users="$login_user:$login_pass"
261261

262-
local kubeconfig_default
262+
# First ask if user wants to use currently set kubeconfig
263+
local kubeconfig
263264
if [[ -n "$def_kubeconfig" ]]; then
264-
kubeconfig_default="$def_kubeconfig"
265-
elif [[ -f "$HOME/Downloads/kubeconfig" ]]; then
266-
kubeconfig_default="$HOME/Downloads/kubeconfig"
265+
echo "Current kubeconfig: $def_kubeconfig"
266+
local use_current
267+
use_current=$(ask_yes_no "Use current kubeconfig?" "y")
268+
269+
if [[ "$use_current" == "y" ]]; then
270+
kubeconfig="$def_kubeconfig"
271+
else
272+
# User declined current, try to find kubeconfigs from Downloads
273+
if [[ -d "$HOME/Downloads" ]]; then
274+
local kubeconfig_files
275+
mapfile -t kubeconfig_files < <(ls -t "$HOME/Downloads"/*kubeconfig* 2>/dev/null | head -10)
276+
277+
if [[ ${#kubeconfig_files[@]} -gt 0 ]]; then
278+
echo ""
279+
echo "Available kubeconfig files in Downloads:"
280+
for i in "${!kubeconfig_files[@]}"; do
281+
local file_size
282+
file_size=$(du -h "${kubeconfig_files[$i]}" 2>/dev/null | cut -f1)
283+
echo " $((i+1))) ${kubeconfig_files[$i]##*/} (${file_size:-unknown size})"
284+
done
285+
echo " $(( ${#kubeconfig_files[@]} + 1 ))) Enter custom path"
286+
287+
local choice
288+
while true; do
289+
choice=$(ask "Choose kubeconfig (1-$(( ${#kubeconfig_files[@]} + 1 )))" "")
290+
if [[ "$choice" =~ ^[0-9]+$ ]] && [[ "$choice" -ge 1 ]] && [[ "$choice" -le $(( ${#kubeconfig_files[@]} + 1 )) ]]; then
291+
break
292+
fi
293+
echo "Please enter a number between 1 and $(( ${#kubeconfig_files[@]} + 1 ))." 1>&2
294+
done
295+
296+
if [[ "$choice" -le ${#kubeconfig_files[@]} ]]; then
297+
kubeconfig="${kubeconfig_files[$((choice-1))]}"
298+
echo "Selected: $kubeconfig"
299+
else
300+
# User chose to enter custom path
301+
local manual_default
302+
if [[ -f "$HOME/Downloads/kubeconfig" ]]; then
303+
manual_default="$HOME/Downloads/kubeconfig"
304+
else
305+
manual_default=""
306+
fi
307+
308+
kubeconfig=$(ask_required "Enter custom kubeconfig path" "$manual_default")
309+
fi
310+
else
311+
# No kubeconfig files found in Downloads, ask manually
312+
local manual_default
313+
if [[ -f "$HOME/Downloads/kubeconfig" ]]; then
314+
manual_default="$HOME/Downloads/kubeconfig"
315+
else
316+
manual_default=""
317+
fi
318+
319+
kubeconfig=$(ask_required "Path to kubeconfig" "$manual_default")
320+
fi
321+
else
322+
# Downloads directory doesn't exist, ask manually
323+
local manual_default
324+
if [[ -f "$HOME/Downloads/kubeconfig" ]]; then
325+
manual_default="$HOME/Downloads/kubeconfig"
326+
else
327+
manual_default=""
328+
fi
329+
330+
kubeconfig=$(ask_required "Path to kubeconfig" "$manual_default")
331+
fi
332+
fi
267333
else
268-
kubeconfig_default=""
334+
# No current kubeconfig set, try to find kubeconfigs from Downloads
335+
if [[ -d "$HOME/Downloads" ]]; then
336+
local kubeconfig_files
337+
mapfile -t kubeconfig_files < <(ls -t "$HOME/Downloads"/*kubeconfig* 2>/dev/null | head -10)
338+
339+
if [[ ${#kubeconfig_files[@]} -gt 0 ]]; then
340+
echo ""
341+
echo "Available kubeconfig files in Downloads:"
342+
for i in "${!kubeconfig_files[@]}"; do
343+
local file_size
344+
file_size=$(du -h "${kubeconfig_files[$i]}" 2>/dev/null | cut -f1)
345+
echo " $((i+1))) ${kubeconfig_files[$i]##*/} (${file_size:-unknown size})"
346+
done
347+
echo " $(( ${#kubeconfig_files[@]} + 1 ))) Enter custom path"
348+
349+
local choice
350+
while true; do
351+
choice=$(ask "Choose kubeconfig (1-$(( ${#kubeconfig_files[@]} + 1 )))" "")
352+
if [[ "$choice" =~ ^[0-9]+$ ]] && [[ "$choice" -ge 1 ]] && [[ "$choice" -le $(( ${#kubeconfig_files[@]} + 1 )) ]]; then
353+
break
354+
fi
355+
echo "Please enter a number between 1 and $(( ${#kubeconfig_files[@]} + 1 ))." 1>&2
356+
done
357+
358+
if [[ "$choice" -le ${#kubeconfig_files[@]} ]]; then
359+
kubeconfig="${kubeconfig_files[$((choice-1))]}"
360+
echo "Selected: $kubeconfig"
361+
else
362+
# User chose to enter custom path
363+
kubeconfig=$(ask_required "Enter custom kubeconfig path" "")
364+
fi
365+
else
366+
# No kubeconfig files found in Downloads, ask manually
367+
kubeconfig=$(ask_required "Path to kubeconfig" "")
368+
fi
369+
else
370+
# Downloads directory doesn't exist, ask manually
371+
kubeconfig=$(ask_required "Path to kubeconfig" "")
372+
fi
269373
fi
270-
local kubeconfig
271-
kubeconfig=$(ask_required "Path to kubeconfig" "$kubeconfig_default")
374+
272375
if [[ ! -f "$kubeconfig" ]]; then
273376
echo "Warning: file does not exist at $kubeconfig" 1>&2
274377
fi

0 commit comments

Comments
 (0)