Skip to content

Commit

Permalink
Fix failing test in 02-webapps
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Sep 28, 2023
1 parent 0ad1996 commit 7da551f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
10 changes: 5 additions & 5 deletions 99-labs/02-webapps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
)

func HelloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world!")
fmt.Fprint(w, "Hello, world!\n")
}

func main() {
Expand Down Expand Up @@ -83,13 +83,13 @@ Modify the program to return the hostname of the server it is running at. This r
}
```
- store the returned hostname in the global variable `hostname` so that the HTTP handler, which runs in a separate function and so cannot reach `h`, will access it: `hostname = h`
- finally, modify the HTTP handler `HelloHandler` to add value of the global `hostname` variable to the response: `fmt.Fprintf(w, "Hello world from %s!", hostname)`
- finally, modify the HTTP handler `HelloHandler` to add value of the global `hostname` variable to the response: `fmt.Fprintf(w, "Hello world from %s!\n", hostname)`

> **Check**
>
> Run the below test to check whether you have successfully completed the task. If all goes well, you should see the output `PASS`.
> ``` sh
> go test ./... --tags=helloworld -v -count 1
> go test ./... --tags=helloworldhostname -v -count 1
> PASS
> ```
> Make sure the web service is running locally (i.e., execute `go run main.go` before running the test): the test issues requests to the HTTP server and checks whether the response is as expected.
Expand Down Expand Up @@ -542,7 +542,7 @@ Extend the web app to also return the version of Go used to compile the web serv
- use `runtime.Version()` to obtain the Go version as a string (make sure to browse the [documentation of the `runtime` package](https://pkg.go.dev/runtime), there are lots of useful functions in it);
- don't forget to `import` the `runtime` package first;
- store the string returned by `runtime.Version()` in a global variable named `version` (make sure you actually declare the variable before calling `main` with `var version string`);
- generate the HTTP response in `HelloHandler` as follows: `fmt.Fprintf(w, "Hello world from %s running Go version %s!", hostname, version)`
- generate the HTTP response in `HelloHandler` as follows: `fmt.Fprintf(w, "Hello world from %s running Go version %s!\n", hostname, version)`
- run locally with `go run main.go` and test with `curl http://localhost:8080`.
2. Rebuild and redeploy the container image
- re-run `minikube image build ...`,
Expand All @@ -556,7 +556,7 @@ Extend the web app to also return the version of Go used to compile the web serv
> ``` sh
> export EXTERNAL_IP=$(kubectl get service helloworld -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
> export EXTERNAL_PORT=80
> go test ./... --tags=helloworld -v -count 1
> go test ./... --tags=helloworldgoversion -v -count 1
> PASS
> ```
> Make sure the web service is running inside Kubernetes: the test issues requests to the Kubernetes Service and checks whether the response is as expected.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build helloworld
//go:build helloworldgoversion

package main

Expand All @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestHelloWorld(t *testing.T) {
func TestHelloWorldWithGoVersion(t *testing.T) {
res, err := testHTTP(t, "/", "GET", "")
assert.NoError(t, err, "GET")
assert.Equal(t, http.StatusOK, res.StatusCode, "status code")
Expand Down
23 changes: 23 additions & 0 deletions 99-labs/code/helloworld/helloworld_with_hostname_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build helloworldhostname

package main

import (
"io"
"net/http"
"regexp"
"testing"

"github.com/stretchr/testify/assert"
)

func TestHelloWorldWithHosname(t *testing.T) {
res, err := testHTTP(t, "/", "GET", "")
assert.NoError(t, err, "GET")
assert.Equal(t, http.StatusOK, res.StatusCode, "status code")

body, err := io.ReadAll(res.Body)
assert.NoError(t, err, "read response body")
r := regexp.MustCompile("^Hello world from .*$")
assert.Regexp(t, r, string(body), "response")
}

0 comments on commit 7da551f

Please sign in to comment.