0

I am trying to test if my service running in docker container well, so I want to call url 'http://localhost:3000/test' and check if it is 200. In all other cases I want to fail my bitbucket pipeline.

So at first I have tried to curl it, but I dont get any output or error.

curl -f "http://localhost:3000/test" || exit 1

But it passes it anyway, even if url is bad or container is not running at all.

Then I have tried to call wget right in container but I doesn't succeed too.

docker exec -ti -t -i mycontainer sh -c "wget http://localhost:3000/test || exit 1" || exit 1

It fails on the input device is not a TTY

Thanks for your help.

Yannick
  • 813
  • 8
  • 17

2 Answers2

1

Here's a simpler solution that I suggested in the comments of the other answer. If all you want to do is exit the script on error, you can simply test for the exit status:

$ wget -q --spider http://localhost:3000/ || exit 1

Heck, if you want more control, and error reporting you could even test the exit status using $?. Wget returns quite information exit statuses. Quoting the manual here:

EXIT STATUS Wget may return one of several error codes if it encounters problems.

   0   No problems occurred.

   1   Generic error code.

   2   Parse error---for instance, when parsing command-line options, the .wgetrc or .netrc...

   3   File I/O error.

   4   Network failure.

   5   SSL verification failure.

   6   Username/password authentication failure.

   7   Protocol errors.

   8   Server issued an error response.

   With the exceptions of 0 and 1, the lower-numbered exit codes take precedence
   over higher-numbered ones, when multiple types of errors are encountered.
darnir
  • 4,870
  • 4
  • 32
  • 47
0

First remove -ti -t -i since in a pipe there is no tty device (terminal). (-ti is the same as -t -i)

Now your script can check if the url is reachable or not, to check for a 200 response code, use

wget -q --spider --server-response 'http://localhost:3000/' "${url}" 2>&1 | diff -q <(echo 200) <(awk 'NR==1{print $2}')

the first part is well explained here, then diff to see if the response code is 200.

Your final command looks like

docker exec mycontainer sh -c "wget -q --spider --server-response 'http://localhost:3000/' "${url}" 2>&1 | diff -q "echo 200" <(awk 'NR==1{print $2}') || exit 1"

EDIT

diff is a bit overkill here, so try this

wget -q --spider --server-response 'http://localhost:3000/' "${url}" 2>&1 | [ code`awk 'NR==1{print $2}'` == code200 ] || exit 1

The prefix "code" is used to suppress the warning when wget can't connect, awk will return nothing causing [ ] to complain.

Siyu
  • 11,187
  • 4
  • 43
  • 55
  • So it failed with ```Usage: diff [-abBdiNqrTstw] [-L LABEL] [-S FILE] [-U LINES] FILE1 FILE2 Compare files line by line and output the differences between them. This implementation supports unified diffs only.``` My images is based on node:10.12 and may be that it doesn't have this functionality. – Vadim Torosyan Nov 30 '18 at 14:33
  • Why use diff when you can simply check the return code when you use wget in --spider mode? If the file exists, it will return 0, else 8 if it was a 404 or any other server error. – darnir Nov 30 '18 at 16:32
  • @darnir I've tried but it returns a lot of things, even with `wget -nv --spider 'http://localhost:3000/' ` I got `2018-11-30 17:42:05 URL: http://localhost:3000/ 200 OK` ` – Siyu Nov 30 '18 at 16:43
  • 1
    No, you just need to do `wget --spider http://localhost:3000/ || exit 1`. You could check for the exit code using `$?` and see if you only want to exit on a server error or other errors as well. – darnir Nov 30 '18 at 21:32
  • You are totally right! Much cleaner, I guess the only advantage of mine is it does not print out anything. – Siyu Nov 30 '18 at 21:37
  • @Siyu: Sure, just add a `-q` to the command line I provided. See the answer I wrote – darnir Dec 03 '18 at 12:32