Skip to content

Commit

Permalink
Improve tests in 02-webapps
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Sep 28, 2023
1 parent 64f965e commit 5e076dd
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
27 changes: 24 additions & 3 deletions 99-labs/02-webapps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ 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)`

> **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
> 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.
We have also created an experimental test that starts the web service before executing the tests and stops it afterwards. At this point this test merely serves for us to understand how we can write totally self-contained test; please execute it and get back to us whether it worked fine.
>**Check**
>
> Run the below test to make sure that you have successfully completed the first exercise. If all goes well, you should see the output `PASS`.
Expand Down Expand Up @@ -539,6 +550,19 @@ Extend the web app to also return the version of Go used to compile the web serv
- package up the YAML manifest of the `helloworld` Service into `deploy/kubernetes-service.yaml` and apply it with `kubectl apply -f deploy/kubernetes-service.yaml` (make sure to set the Service type to `LoadBalancer`).
3. Learn the external IP of the service and test with `curl`.
>**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
> 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
> 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.
The next experimental test is trying its best to build the container image using your Dockerfile and deploy it into Minikube using your manifest. Since there are many moving parts there is a good chance something will go wrong, therefore at this point it merely serves for us to gather data points for improving our tests in the future. If the test fails, try to understand what's missing (e.g., if `minikube tunnel` is already running the test will most probably fail). If it still fails do not worry too much: that's most probably our fault. Just tell us what went wrong and test by hand.
>**Check**
>
> Run the below test to make sure that you have successfully completed the exercise. If all goes well, you should see the output `PASS`.
Expand All @@ -547,9 +571,6 @@ Extend the web app to also return the version of Go used to compile the web serv
> PASS
> ```
> **Note**
> The test is trying its best to build the container image using your Dockerfile and deploy it into Minikube using your manifest, but since there are many moving parts there is a good chance something will go wrong. If the test fails, try to understand what's missing. If it still fails do not worry too much: that's most probably our fault. Just tell us what went wrong and test by hand.
<!-- Local Variables: -->
<!-- mode: markdown; coding: utf-8 -->
<!-- eval: (auto-fill-mode -1) -->
Expand Down
23 changes: 23 additions & 0 deletions 99-labs/code/helloworld/helloworld_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build helloworld

package main

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

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

func TestHelloWorld(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 .* running Go version .*$")
assert.Regexp(t, r, string(body), "response")
}
33 changes: 33 additions & 0 deletions 99-labs/code/helloworld/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strings"
"testing"
Expand All @@ -26,6 +28,37 @@ import (
// }
// }

func testHTTP(t *testing.T, api, method, body string) (*http.Response, error) {
addr := "localhost"
if os.Getenv("EXTERNAL_IP") != "" {
addr = os.Getenv("EXTERNAL_IP")
}
port := "8080"
if os.Getenv("EXTERNAL_PORT") != "" {
port = os.Getenv("EXTERNAL_PORT")
}

uri := fmt.Sprintf("http://%s:%s/%s", addr, port, api)

var req *http.Request
var err error
if method == "POST" {
var b *bytes.Buffer
if body == "" {
b = bytes.NewBuffer([]byte(`{"sender":"c","receiver":"a", "amount": 4}`))
} else {
b = bytes.NewBuffer([]byte(body))
}
req, err = http.NewRequest(method, uri, b)
} else {
req, err = http.NewRequest(method, uri, nil)
}

assert.NoError(t, err, "create req")

return http.DefaultClient.Do(req)
}

func execCmd(t *testing.T, cmd string, args ...string) (string, string) {
return execCmdContext(context.Background(), t, cmd, args...)
}
Expand Down

0 comments on commit 5e076dd

Please sign in to comment.