Testing gitlab-runner run command in a separate shell?

1.8k Views Asked by At

Context

While testing the functions of an installation script that automatically installs and runs GitLab and GitLab runners, I experienced some difficulties running the GitLab runner in the background an proceeding with the tests. (My intention is to simply run the runner in the background, and then to run a few tests that verify the service behaves as expected). However, when my test executes the function that executes the sudo gitlab-runner run command, the test hangs, waiting on the service to stop, (but the service should not stop).

Function

# Run GitLab runner service
run_gitlab_runner_service() {
    run_command=$(sudo gitlab-runner run &)
    echo "service is running"
}

The output of the test is:

 Configuration loaded                                builds=0                                                                                     7/7

And then the cursor just keeps blinking while the last runner is successfully awaiting instructions.

Test Code

@test "Test if the GitLab Runner CI service is running correctly." {
    run_service_output=$(run_gitlab_runner_service)
    EXPECTED_OUTPUT="service is running"
        
    assert_equal "$run_service_output" "$EXPECTED_OUTPUT"
}

However, when inspecting the regular output of the sudo gitlab-runner run command, I observe that it starts a new line/shell/something without asking. For example:

(base) name@name:~$ sudo gitlab-runner run &
[1] 84799
(base) name@name:~$ Runtime platform                                    arch=amd64 os=linux pid=84800 revision=8925d9a0 version=14.1.0
Starting multi-runner from /etc/gitlab-runner/config.toml...  builds=0
Running in system-mode.                            
                                                   
Configuration loaded                                builds=0
listen_address not defined, metrics & debug endpoints disabled  builds=0
[session_server].listen_address not defined, session endpoints disabled  builds=0

basically, the output (base) name@name:~$ Runtime platform arch=amd64 should never stop (until manually terminated), so I think the test is waiting on that shell/output to complete.

Note

For completeness, I do not type the (base) name@name:~$ Runtime platform arch=amd64 .. output, that somehow is pushed to the terminal in the new line some how. I do not know exactly why this isn't just under the previous/original (base) name@name:~$ that ran the sudo gitlab-runner run & command.

Question

How can I ensure the function and test proceed after starting the gitlab-runner run service?

2

There are 2 best solutions below

0
a.t. On BEST ANSWER

A solution was found by redirecting the output of the command to /dev/null.

Code

# Run GitLab runner service
run_gitlab_runner_service() {
    output=$(nohup sudo gitlab-runner run &>/dev/null &)
    echo "service is running"
}
3
Davide Madrisan On

I think you should use a slightly different syntax in order to run gitlab-runner in background

# Run GitLab runner service
run_gitlab_runner_service() {
    ( run_command="$(sudo gitlab-runner run)" ) &
    echo "service is running"
}