diff --git a/.semaphore/semaphore.yml b/.semaphore/semaphore.yml index 37631d5..9cc2e36 100644 --- a/.semaphore/semaphore.yml +++ b/.semaphore/semaphore.yml @@ -1,85 +1,52 @@ -# Use the latest stable version of Semaphore 2.0 YML syntax: version: v1.0 - -# Name your pipeline. In case you connect multiple pipelines with promotions, -# the name will help you differentiate between, for example, a CI build phase -# and delivery phases. name: Semaphore Go CI example - -# An agent defines the environment in which your code runs. -# It is a combination of one of available machine types and operating -# system images. -# See https://docs.semaphoreci.com/article/20-machine-types -# and https://docs.semaphoreci.com/article/32-ubuntu-1804-image agent: machine: type: e1-standard-2 os_image: ubuntu2004 - -# Blocks are the heart of a pipeline and are executed sequentially. -# Each block has a task that defines one or more jobs. Jobs define the -# commands to execute. -# See https://docs.semaphoreci.com/article/62-concepts blocks: - name: Build project task: jobs: - - name: go get & build - commands: - # Download code from Git repository. This step is mandatory if the - # job is to work with your code. - - checkout - - # Set version of Go: - - sem-version go 1.19 - - go get - - go build -o ./bin/main - - # Store the binary in cache to reuse in further blocks. - # More about caching: https://docs.semaphoreci.com/article/54-toolbox-reference#cache - - cache store $(checksum main.go) bin - + - name: go get & build + commands: + - checkout + - sem-version go 1.19 + - go get + - go build -o ./bin/main + - cache store $(checksum main.go) bin + secrets: + - name: test-home - name: Check code style task: jobs: - - name: gofmt - commands: - # Each job on Semaphore starts from a clean environment, so we - # repeat the required project setup. - - checkout - - sem-version go 1.19 - - gofmt main.go | diff --ignore-tab-expansion main.go - - + - name: gofmt + commands: + - checkout + - sem-version go 1.19 + - gofmt main.go | diff --ignore-tab-expansion main.go - - name: Run tests task: - # This block runs two jobs in parallel and they both share common - # setup steps. We can group them in a prologue. - # See https://docs.semaphoreci.com/article/50-pipeline-yaml#prologue prologue: commands: - checkout - sem-version go 1.19 jobs: - - name: go test - commands: - # Start Postgres database service. - # See https://docs.semaphoreci.com/ci-cd-environment/sem-service-managing-databases-and-services-on-linux/ - - sem-service start postgres - - psql -p 5432 -h localhost -U postgres -c "CREATE DATABASE s2" - - go install gotest.tools/gotestsum@latest - - gotestsum --junitfile junit.xml ./... - - - name: Test web server - commands: - # Restore compiled binary which we created in the first block: - - cache restore $(checksum main.go) - - ./bin/main 8001 & - - curl --silent localhost:8001/time | grep "The current time is" + - name: go test + commands: + - sem-service start postgres + - psql -p 5432 -h localhost -U postgres -c "CREATE DATABASE s2" + - go install gotest.tools/gotestsum@latest + - gotestsum --junitfile junit.xml ./... + - name: Test web server + commands: + - cache restore $(checksum main.go) + - ./bin/main 8001 & + - 'curl --silent localhost:8001/time | grep "The current time is"' epilogue: always: commands: - test-results publish junit.xml - after_pipeline: task: jobs: diff --git a/main_test.go b/main_test.go index a2adcef..a02eee8 100644 --- a/main_test.go +++ b/main_test.go @@ -6,6 +6,8 @@ import ( "net/http" "net/http/httptest" "testing" + "math/rand" + "time" ) func createTable() { @@ -145,3 +147,27 @@ func Test_record(t *testing.T) { } dropTable() } + +func TestAPI(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + IsFlaky := rand.Intn(2) == 0 + if !IsFlaky { + t.Error("API Test failed. Reason: timeout") + } +} + +func TestFeatureA(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + IsFlaky := rand.Intn(2) == 0 + if !IsFlaky { + t.Error("FeatureA failed. Reason: resource not ready") + } +} + +func TestFeatureB(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + IsFlaky := rand.Intn(2) == 0 + if !IsFlaky { + t.Error("FeatureB failed. Reason: connection failed") + } +} \ No newline at end of file