{"id":20843837,"url":"https://github.com/num30/golang-service-test","last_synced_at":"2025-05-09T01:47:50.325Z","repository":{"id":111572645,"uuid":"535293283","full_name":"num30/golang-service-test","owner":"num30","description":"This is an example of and integration test for Golang Rest API service ","archived":false,"fork":false,"pushed_at":"2024-02-07T12:01:21.000Z","size":41,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-09T01:47:44.737Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/num30.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-09-11T12:35:13.000Z","updated_at":"2024-03-11T14:24:33.000Z","dependencies_parsed_at":"2023-11-28T14:30:01.901Z","dependency_job_id":"809337be-a608-450a-96e2-de917b3bd812","html_url":"https://github.com/num30/golang-service-test","commit_stats":null,"previous_names":["num30/golang-service-test"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/num30%2Fgolang-service-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/num30%2Fgolang-service-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/num30%2Fgolang-service-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/num30%2Fgolang-service-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/num30","download_url":"https://codeload.github.com/num30/golang-service-test/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253176440,"owners_count":21866142,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T02:07:31.368Z","updated_at":"2025-05-09T01:47:50.307Z","avatar_url":"https://github.com/num30.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# API Service Test Example\n\n## Objective\n\nService tests are automated tests executed against an instance of running service, usually after deployment, to ensure that the service works properly with other parts of the system. For example, if a service uses DB then we want to test its interface and ensure that the data from DB is returned.  \n\nIn [testing pyramid](https://martinfowler.com/articles/practical-test-pyramid.html) they are located between unit-test and end-to-end tests. The main target of those tests is service API so it's fairly easy to write them which makes them a crucial part of software quality assurance\n\n## Scope \n\nThis repository demonstrates one of the approaches to write, run and maintain service tests for a Golang service. \n\n## What is in the repository?\n\nIn this repo you will find: \n- simple [Rest API server](/pkg/router/router.go)\n- [service test](/test/stest/rest_service_test.go) for the API\n- [Docker file for service test build](Test.Dockerfile)\n- Script to build [service test image](/Makefile)\n- [Github Action pipeline](/.github/workflows/build.yaml) to build and push service test image \n\n## Let's break it down\nWe have our basic api service declared in [router.go](/pkg/router/router.go) file with three methods:\n - `GET /ping` - returns 200 OK and pong in response\n - `GET /boxes/{box_id}` - returns Json with box content by it's ID\n - `PUT /boxes/{box_id}` - updates box content by it's ID\n\nWe have a [test](/test/stest/rest_service_test.go) that checks that our service works as expected. Test is written as a go tests the only difference is that we don't access any methods directly but use HTTP calls to call our service.  \n\n\n\n## Building Test Image \nRunning locally is good for one time verification. In order to run test as part of CI/CD pipeline we need to build and push service test image. To do that we will build a test binary first \n```\nenv CGO_ENABLED=0 go test ./test/stest -tags servicetest -v -c -o bin/service-test\n```\n\nthen we will build a docker image using [Test.Dockerfile](Test.Dockerfile) created specifically for service tests. \n\n```\ndocker build -f Test.Dockerfile -t service-test .\n```\n\nYou probably want to do it in a pipeline so [here is an example of Github Action pipeline](.github/workflows/build.yaml) that does that for you.\nWe build application image with two tags `[short-commit-sha]` and `[branch-name]-[build-number]` test image is being built with the same tags but with  `-test` suffix. That way we use same Docker repository for both application and test images. You may consider having separate repository although I would suggest to stick to some naming conventions. This will make it easier to deal with running the test later. \n\n\n## Runnig Tests\n\n### Locally\nOur service test have a `servicetest` build tag that prevents our test from running with `go test ./...` command. \n\nTo run our test locally: \n```\ngo test ./test/stest -tags servicetest  -v -count=1\n```\n\n### Docker\n\nTest are packaged into a docker image so we can run them in a container. This docker container is easy to run in a K8s as part of helm test, k8s job, or as a step in a CD pipeline.\n\n\n### Helm Chart \n\nIf you use helm charts to deploy you application then you can use [helm test](https://helm.sh/docs/topics/chart_tests/) to run your service tests. Here is an example of [helm test file](/helm/boxes-api/templates/tests/test-service.yaml). \nThe advantage of this approach is tha you can run test by executing `helm test` command. \n\nTry this example in Kubernetes by running:\n```\nhelm install int-example  boxes-api \nhelm test int-example\nkubectl logs int-example-boxes-api-test-service\n```\n\n## Results \nThe test results are reported in a log of a container.  \nHowever, if you want to aggregate test results and keep the history of execution then you make consider using [testhub](https://github.com/testhub-io/testhub) for that. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnum30%2Fgolang-service-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnum30%2Fgolang-service-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnum30%2Fgolang-service-test/lists"}