Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tasks: Test posted statuses in mock PR and image refresh #592

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tasks/mock-github
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ from task.test_mock_server import MockHandler, MockServer

repo = None
sha = None
log = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you'd like to add some more types...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH not today when we have a crapton of stuff to catch up with. I'd like to reserve this to the next weeks -- I also want to rewrite run-local.sh as proper pytest.



class Handler(MockHandler):
def do_GET(self):
if log is not None:
print("GET", self.path, file=log)
log.flush()

if self.path in self.server.data:
self.replyJson(self.server.data[self.path])
elif self.path.startswith(f'/repos/{repo}/pulls?'):
Expand All @@ -39,6 +44,10 @@ class Handler(MockHandler):
self.send_error(404, 'Mock Not Found: ' + self.path)

def do_POST(self):
if log is not None:
print("POST", self.path, self.rfile.read1().decode(), file=log)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...why not (also) GET?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mostly meant to validate actions. GET requests don't change the state, they are already sufficiently defined in terms of which paths do_GET handles in the test, and they are also somewhat of an implementation detail. I can add them to the log of course, but I wouldn't have anything to assert on them. For human inspection it's nice though, I agree.

Added.

log.flush()

if self.path.startswith(f'/repos/{repo}/statuses/{sha}'):
self.replyJson({})
# new SHA from mock-pushed PR #2 for image-refresh
Expand All @@ -63,6 +72,7 @@ argparser.add_argument('--print-pr-event', action='store_true',
help="Print GitHub webhook pull_request event and exit")
argparser.add_argument('--print-image-refresh-event', action='store_true',
help="Print GitHub webhook issue event for an image-refresh and exit")
argparser.add_argument('--log', metavar="PATH", help="Log requests to this file")
argparser.add_argument('repo', metavar='USER/PROJECT', help="GitHub user/org and project name")
argparser.add_argument('sha', help="SHA to test in repo for the mock PR")
args = argparser.parse_args()
Expand Down Expand Up @@ -125,6 +135,8 @@ if args.print_image_refresh_event:
}, indent=4))
exit(0)

if args.log:
log = open(args.log, 'w')
temp = tempfile.TemporaryDirectory()
cache_dir = os.path.join(temp.name, 'cache')
os.environ['XDG_CACHE_HOME'] = cache_dir
Expand Down
41 changes: 32 additions & 9 deletions tasks/run-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -301,19 +301,21 @@ test_image() {
test_mock_pr() {
podman cp "$MYDIR/mock-github" cockpituous-tasks:/work/bots/mock-github
create_job_runner_config mock

# test mock PR against our checkout, so that cloning will work
SHA=$(podman exec -i cockpituous-tasks git -C bots rev-parse HEAD)

podman exec -i cockpituous-tasks sh -euxc "
cd bots
# test mock PR against our checkout, so that cloning will work
SHA=\$(git rev-parse HEAD)
# start mock GH server
PYTHONPATH=. ./mock-github cockpit-project/bots \$SHA &
PYTHONPATH=. ./mock-github --log /tmp/mock.log cockpit-project/bots $SHA &
GH_MOCK_PID=\$!
export GITHUB_API=$GHAPI_URL_POD
until curl --silent --fail \$GITHUB_API/repos/cockpit-project/bots; do sleep 0.1; done
# simulate GitHub webhook event, put that into the webhook queue
PYTHONPATH=. ./mock-github --print-pr-event cockpit-project/bots \$SHA | \
PYTHONPATH=. ./mock-github --print-pr-event cockpit-project/bots $SHA | \
./publish-queue --amqp $AMQP_POD --create --queue webhook
./inspect-queue --amqp $AMQP_POD
Expand All @@ -337,6 +339,13 @@ test_mock_pr() {
echo "--------------- mock PR test log end -------------"
assert_in 'Test run finished, return code: 0\|Job ran successfully' "$LOG"
assert_in 'Running on:.*cockpituous' "$LOG"

# 3 status updates posted
# FIXME: assert JSON more precisely once we rewrite in Python
GH_MOCK_LOG="$(podman exec cockpituous-tasks cat /tmp/mock.log)"
assert_in "POST /repos/cockpit-project/bots/statuses/$SHA .*description.*Not yet tested" "$GH_MOCK_LOG"
assert_in "POST /repos/cockpit-project/bots/statuses/$SHA .*description.*In progress \\[cockpituous\\]" "$GH_MOCK_LOG"
assert_in "POST /repos/cockpit-project/bots/statuses/$SHA .*state.*success" "$GH_MOCK_LOG"
}

test_pr() {
Expand Down Expand Up @@ -400,19 +409,19 @@ test_mock_image_refresh() {
podman cp "$MYDIR/mock-git-push" cockpituous-tasks:/usr/local/bin/git
create_job_runner_config mock

# test mock PR against our checkout, so that cloning will work
SHA=$(podman exec -i cockpituous-tasks git -C bots rev-parse HEAD)

podman exec -i cockpituous-tasks sh -euxc "
cd bots
# test mock PR against our checkout, so that cloning will work
SHA=\$(git rev-parse HEAD)
# start mock GH server
PYTHONPATH=. ./mock-github cockpit-project/bots \$SHA &
PYTHONPATH=. ./mock-github --log /tmp/mock.log cockpit-project/bots $SHA &
GH_MOCK_PID=\$!
export GITHUB_API=$GHAPI_URL_POD
until curl --silent --fail \$GITHUB_API/repos/cockpit-project/bots; do sleep 0.1; done
# simulate GitHub webhook event, put that into the webhook queue
PYTHONPATH=. ./mock-github --print-image-refresh-event cockpit-project/bots \$SHA | \
PYTHONPATH=. ./mock-github --print-image-refresh-event cockpit-project/bots $SHA | \
./publish-queue --amqp $AMQP_POD --create --queue webhook
./inspect-queue --amqp $AMQP_POD
Expand Down Expand Up @@ -460,6 +469,20 @@ test_mock_image_refresh() {
grep "^fakeimage" /tmp/foonux.raw
rm /tmp/foonux.raw
'

# status updates posted to original bots SHA on which the image got triggered
# FIXME: assert JSON more precisely once we rewrite in Python (unpredictable JSON order)
GH_MOCK_LOG="$(podman exec cockpituous-tasks cat /tmp/mock.log)"
assert_in "POST /repos/cockpit-project/bots/statuses/$SHA .*image-refresh/foonux" "$GH_MOCK_LOG"
assert_in "POST /repos/cockpit-project/bots/statuses/$SHA .*In progress \\[cockpituous\\]" "$GH_MOCK_LOG"
assert_in "POST /repos/cockpit-project/bots/statuses/$SHA .*success" "$GH_MOCK_LOG"

# and forwarded to the converted PR (new SHA)
assert_in "POST /repos/cockpit-project/bots/statuses/a1b2c3 .*success.*Forwarded status.*target_url" "$GH_MOCK_LOG"

# posts new comment with log
assert_in "POST /repos/cockpit-project/bots/issues/2/comments .*Success. Log: https.*" "$GH_MOCK_LOG"

}

test_queue() {
Expand Down