|
1 | 1 | #!/usr/bin/env bats |
2 | 2 |
|
3 | | -load 'install_grott.bash' |
| 3 | +load 'grott-install.bash' |
4 | 4 | load 'helpers.bash' |
5 | 5 |
|
6 | | -# Define some common variables |
7 | | -INSTALL_TYPE="install" |
8 | | -USERNAME="testuser" |
9 | | -INSTALL_DIR="/home/${USERNAME}/grott" |
10 | | -SERVICE_FILE="/etc/systemd/system/grott.service" |
11 | | -TEMP_BASEDIR="/tmp/openhabian_test_bats" |
12 | | - |
13 | | -setup() { |
14 | | - # Create a temporary directory for the installation |
15 | | - mkdir -p "$BATS_TEST_TMPDIR/home/$USERNAME" |
16 | | - |
17 | | - # Set INSTALL_DIR to the temporary location for isolation |
18 | | - INSTALL_DIR="$BATS_TEST_TMPDIR/home/$USERNAME/grott" |
19 | | - |
20 | | - # Override SERVICE_FILE for testing |
21 | | - SERVICE_FILE="$BATS_TEST_TMPDIR/etc/systemd/system/grott.service" |
22 | | - mkdir -p "$BATS_TEST_TMPDIR/etc/systemd/system" |
23 | | - |
24 | | - # Create mock systemd service and grott.ini templates |
25 | | - mkdir -p "${TEMP_BASEDIR}/includes" |
26 | | - echo "[Unit]" > "${TEMP_BASEDIR}/includes/grott.service" |
27 | | - echo "Description=Grott Proxy" >> "${TEMP_BASEDIR}/includes/grott.service" |
28 | | - echo "User=%USERNAME" >> "${TEMP_BASEDIR}/includes/grott.service" |
29 | | - echo "WorkingDirectory=%INSTALL_DIR" >> "${TEMP_BASEDIR}/includes/grott.service" |
30 | | - |
31 | | - echo "[Grott]" > "${TEMP_BASEDIR}/includes/grott.ini" |
32 | | - echo "exturl=%EXT_URL" >> "${TEMP_BASEDIR}/includes/grott.ini" |
33 | | - |
34 | | - # Redirect BASEDIR to the temporary location |
35 | | - export BASEDIR="$TEMP_BASEDIR" |
36 | | - |
37 | | - # Mock external commands |
38 | | - export -f ip=mock_ip_route_get |
39 | | - export -f apt=mock_apt_update # Mock apt update and apt install through apt |
40 | | - export -f sudo=mock_sudo_command # Wrap sudo |
41 | | - export -f pip3=mock_pip3_install # Mock pip3 install |
42 | | - export -f curl=mock_curl |
43 | | - export -f systemctl=mock_systemctl_command # Wrap systemctl |
44 | | - export -f whiptail=mock_whiptail |
45 | | - |
46 | | - # Set username for the script to use our testuser |
47 | | - export username="$USERNAME" |
48 | | -} |
49 | | - |
50 | | -teardown() { |
51 | | - # Clean up the temporary installation directory |
52 | | - rm -rf "$INSTALL_DIR" |
53 | | - rm -rf "$TEMP_BASEDIR" |
54 | | - rm -rf "$BATS_TEST_TMPDIR/etc" |
55 | | - |
56 | | - # Unset mocked functions |
57 | | - unset -f ip |
58 | | - unset -f apt |
59 | | - unset -f sudo |
60 | | - unset -f pip3 |
61 | | - unset -f curl |
62 | | - unset -f systemctl |
63 | | - unset -f whiptail |
64 | | - unset username |
65 | | -} |
66 | | - |
67 | | -# Helper for wrapping sudo |
68 | | -mock_sudo_command() { |
69 | | - local cmd="$1" |
70 | | - shift |
71 | | - case "$cmd" in |
72 | | - apt) |
73 | | - mock_apt_update "$@" |
74 | | - ;; |
75 | | - pip3) |
76 | | - mock_pip3_install "$@" |
77 | | - ;; |
78 | | - rm) |
79 | | - mock_sudo_rm "$@" |
80 | | - ;; |
81 | | - systemctl) |
82 | | - mock_systemctl_command "$@" |
83 | | - ;; |
84 | | - *) |
85 | | - # Default behavior for unmocked sudo commands |
86 | | - sudo "$cmd" "$@" |
87 | | - ;; |
88 | | - esac |
| 6 | +setup_file() { |
| 7 | + export BASEDIR="${BATS_TEST_DIRNAME}/.." |
89 | 8 | } |
90 | 9 |
|
91 | | -# Helper for wrapping systemctl commands |
92 | | -mock_systemctl_command() { |
93 | | - local cmd="$1" |
94 | | - shift |
95 | | - case "$cmd" in |
96 | | - daemon-reexec) |
97 | | - mock_systemctl_daemon_reexec "$@" |
98 | | - ;; |
99 | | - enable) |
100 | | - mock_systemctl_enable "$@" |
101 | | - ;; |
102 | | - start) |
103 | | - mock_systemctl_start "$@" |
104 | | - ;; |
105 | | - is-active) |
106 | | - mock_systemctl_is_active "$@" |
107 | | - ;; |
108 | | - is-enabled) |
109 | | - mock_systemctl_is_enabled "$@" |
110 | | - ;; |
111 | | - daemon-reload) |
112 | | - echo "systemctl daemon-reload" |
113 | | - return 0 |
114 | | - ;; |
115 | | - *) |
116 | | - # Default behavior for unmocked systemctl commands |
117 | | - systemctl "$cmd" "$@" |
118 | | - ;; |
119 | | - esac |
| 10 | +teardown_file() { |
| 11 | + unset BASEDIR |
| 12 | + systemctl kill grott.service || true |
120 | 13 | } |
121 | 14 |
|
122 | | -@test "install_grott: Install with valid 'install' argument" { |
123 | | - run install_grott "install" |
124 | | - |
125 | | - assert_equal "$status" 0 |
126 | | - assert_output --partial "[openHABian] Installing Grott Proxy" |
127 | | - |
128 | | - # Verify directories and files were created |
129 | | - run stat "$INSTALL_DIR" |
130 | | - assert_equal "$status" 0 |
131 | | - |
132 | | - run stat "$INSTALL_DIR/grott.py" |
133 | | - assert_equal "$status" 0 |
134 | | - |
135 | | - run stat "$INSTALL_DIR/grottext.py" |
136 | | - assert_equal "$status" 0 |
137 | | - |
138 | | - run stat "$SERVICE_FILE" |
139 | | - assert_equal "$status" 0 |
140 | | - |
141 | | - run stat "$INSTALL_DIR/grott.ini" |
142 | | - assert_equal "$status" 0 |
143 | | - |
144 | | - # Verify the service file content |
145 | | - run cat "$SERVICE_FILE" |
146 | | - assert_output --partial "Description=Grott Proxy" |
147 | | - assert_output --partial "User=${USERNAME}" |
148 | | - assert_output --partial "WorkingDirectory=${INSTALL_DIR}" |
149 | | - |
150 | | - # Verify grott.ini content (check EXT_URL substitution) |
151 | | - run cat "$INSTALL_DIR/grott.ini" |
152 | | - assert_output --partial "exturl=http://192.168.1.100:8080/growatt" |
153 | | -} |
154 | | - |
155 | | -@test "install_grott: Remove Grott with 'remove' argument" { |
156 | | - # Simulate an existing installation |
157 | | - mkdir -p "$INSTALL_DIR" |
158 | | - touch "$INSTALL_DIR/grott.py" |
159 | | - echo "Unit]Description=Grott Proxy" > "$SERVICE_FILE" |
160 | | - |
161 | | - run install_grott "remove" |
162 | | - |
163 | | - assert_equal "$status" 0 |
164 | | - assert_output --partial "[openHABian] Removing Grott Proxy..." |
165 | | - |
166 | | - # Verify files and directories are removed |
167 | | - run stat "$INSTALL_DIR" |
168 | | - assert_equal "$status" 1 # Should indicate the directory doesn't exist |
169 | | - |
170 | | - run stat "$SERVICE_FILE" |
171 | | - assert_equal "$status" 1 # Should indicate the file doesn't exist |
172 | | -} |
173 | | - |
174 | | -@test "install_grott: Invalid install type argument" { |
175 | | - run install_grott "invalid_type" |
176 | | - |
177 | | - assert_equal "$status" 1 |
178 | | - assert_output --partial "Error: Invalid install type 'invalid_type'." |
179 | | -} |
180 | | - |
181 | | -@test "install_grott: Missing install type argument" { |
182 | | - run install_grott |
183 | | - |
184 | | - assert_equal "$status" 1 |
185 | | - assert_output --partial "Error: Please supply install type." |
186 | | -} |
187 | | - |
188 | | -@test "install_grott: handle invalid IP address" { |
189 | | - # Override the mock to return an invalid IP |
190 | | - mock_ip_route_get() { |
191 | | - echo "something is not an ip address" |
192 | | - } |
193 | | - export -f ip=mock_ip_route_get |
194 | | - |
195 | | - run install_grott "install" |
196 | | - assert_equal "$status" 1 |
197 | | - assert_output --partial "Error: Invalid IP address format or range:" |
198 | | -} |
199 | | - |
200 | | -@test "install_grott: handle mkdir failure" { |
201 | | - # Mock sudo mkdir to fail |
202 | | - mock_sudo_mkdir() { |
203 | | - return 1 |
204 | | - } |
205 | | - export -f sudo=mock_sudo_mkdir |
206 | | - |
207 | | - run install_grott "install" |
208 | | - assert_equal "$status" 1 |
209 | | - assert_output --partial "Error: Failed to create installation directory" |
210 | | -} |
211 | | - |
212 | | -@test "install_grott: handle download_grott_files failure" { |
213 | | - # Mock curl to fail |
214 | | - mock_curl() { |
215 | | - return 1 |
216 | | - } |
217 | | - export -f curl=mock_curl |
218 | | - |
219 | | - run install_grott "install" |
220 | | - assert_equal "$status" 1 |
221 | | - assert_output --partial "Failed to download Grott files." |
222 | | -} |
223 | | - |
224 | | -@test "download_grott_files: Verify files are downloaded" { |
225 | | - local temp_dir="$BATS_TEST_TMPDIR/downloads" |
226 | | - mkdir -p "$temp_dir" |
227 | | - |
228 | | - run download_grott_files "$temp_dir" |
229 | | - assert_equal "$status" 0 |
230 | | - |
231 | | - # Verify all expected files were touched by the mock curl |
232 | | - run stat "$temp_dir/grott.py" |
233 | | - assert_equal "$status" 0 |
| 15 | +@test "destructive-grott_install" { |
| 16 | + echo -e "# ${COL_CYAN}$(timestamp) [openHABian] Grott Proxy installation starting...${COL_DEF}" >&3 |
| 17 | + run install_grott "install" 3>&- |
| 18 | + if [ "$status" -ne 0 ]; then echo "$output" >&3; fi |
| 19 | + [ "$status" -eq 0 ] |
| 20 | + echo -e "# ${COL_GREEN}$(timestamp) [openHABian] Grott Proxy installation successful.${COL_DEF}" >&3 |
234 | 21 |
|
235 | | - run stat "$temp_dir/grottext.py" |
236 | | - assert_equal "$status" 0 |
| 22 | + echo -e "# ${COL_CYAN}$(timestamp) [openHABian] Checking if Grott Proxy service is running...${COL_DEF}" >&3 |
| 23 | + run systemctl is-active --quiet grott.service |
| 24 | + if [ "$status" -ne 0 ]; then echo "$output" >&3; fi |
| 25 | + [ "$status" -eq 0 ] |
| 26 | + echo -e "# ${COL_GREEN}$(timestamp) [openHABian] Grott Proxy service is running.${COL_DEF}" >&3 |
237 | 27 | } |
0 commit comments