Skip to content

Commit

Permalink
Use local python http server instead of github
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhoyer committed Nov 21, 2024
1 parent d00fe24 commit 6a237a4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
6 changes: 3 additions & 3 deletions tests/core/environment-file/data/main.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
environment-file:
- env
- env.yaml
- https://raw.githubusercontent.com/teemtee/tmt/22a46a4a6760517e3eadbbff0c9bebdb95442760/tests/core/env/data/vars.yaml
- http://localhost:8000/vars.yaml
/bad:
environment-file:
- bad
Expand All @@ -31,7 +31,7 @@
/fetch:
/good:
environment-file:
- https://raw.githubusercontent.com/teemtee/tmt/22a46a4a6760517e3eadbbff0c9bebdb95442760/tests/core/env/data/vars.yaml
- http://localhost:8000/vars.yaml
/bad:
environment-file:
- https://raw.githubusercontent.com/teemtee/tmt/22a46a4a6760517e3eadbbff0c9bebdb95442760/tests/core/env/data/wrong.yaml
- http://localhost:8000/wrong.yaml
14 changes: 11 additions & 3 deletions tests/core/environment-file/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ rlJournalStart
rlPhaseStartSetup
rlRun 'pushd data'
rlRun 'set -o pipefail'
# Copy vars.yaml from env/data
rlRun 'cp ../../env/data/vars.yaml .'
# Start local server
python3 ../../../utils/http_server.py &
server_pid=$!
rlPhaseEnd

good="plan --name /plan/good"
Expand Down Expand Up @@ -46,15 +51,18 @@ rlJournalStart
# Good
rlRun -s "tmt plan show fetch/good"
rlAssertGrep "STR: O" $rlRun_LOG
rlAssertGrep "INT: 0" $rlRun_LOG
rlAssertGrep "INT: 0" $rlRun_LOG
# Bad
rlRun -s "tmt plan show fetch/bad" 2
rlAssertGrep "Failed to extract variables from URL 'http://localhost:8000/wrong.yaml'." $rlRun_LOG
rlAssertGrep "Failed to extract variables from URL 'http://localhost:8000/wrong.yaml'." $rlRun_LOG
rlAssertGrep "404 Client Error: Not Found for url: http://localhost:8000/wrong.yaml" $rlRun_LOG
rlPhaseEnd

rlPhaseStartCleanup
rlRun 'rm output'
# Kill the server
kill $server_pid
# Clean up copied files
rlRun 'rm vars.yaml output'
rlRun 'popd'
rlPhaseEnd
rlJournalEnd
3 changes: 1 addition & 2 deletions tests/prepare/ansible/data/plan.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ prepare:
- name: Ansible we want to test
order: 85
how: ansible
# This is the very same playbook as the local one, just living in tmt's repository.
playbook: https://raw.githubusercontent.com/teemtee/tmt/main/tests/prepare/ansible/data/playbook.yml
playbook: http://localhost:8000/playbook.yml
extra-args: '-vvv -e ansible_remote_tmp=/tmp'
5 changes: 5 additions & 0 deletions tests/prepare/ansible/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ rlJournalStart

rlRun "pushd data"
rlRun "run=\$(mktemp -d)" 0 "Create run directory"
# Start local server in current directory (data)
python3 ../../../utils/http_server.py &
server_pid=$!
rlPhaseEnd

while IFS= read -r image; do
Expand Down Expand Up @@ -55,6 +58,8 @@ rlJournalStart
done <<< "$IMAGES"

rlPhaseStartCleanup
# Kill the server
kill $server_pid
rlRun "rm -rf $run" 0 "Removing run directory"
rlRun "popd"
rlPhaseEnd
Expand Down
34 changes: 34 additions & 0 deletions tests/utils/http_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import http.server
import socketserver
import threading


class Handler(http.server.SimpleHTTPRequestHandler):
def send_error(self, code, message=None):
"""Override to match GitHub's error format"""
self.send_response(code)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.send_header("X-Content-Type-Options", "nosniff")
self.end_headers()
if code == 404:
self.wfile.write(b"404 Client Error: Not Found")


def run_server(port=8000):
"""Run HTTP server in a daemon thread.
Args:
port: Port number to listen on (default: 8000)
"""
httpd = socketserver.TCPServer(("", port), Handler)
server_thread = threading.Thread(target=httpd.serve_forever)
server_thread.daemon = True
server_thread.start()


if __name__ == '__main__':
run_server()
# Keep the script running
import time
while True:
time.sleep(1)

0 comments on commit 6a237a4

Please sign in to comment.