Skip to content

Commit

Permalink
test: add e2e tests for sparrow
Browse files Browse the repository at this point in the history
* test: add health check e2e test
* test: add latency check e2e test
* test: add dns check e2e test
* test: add file loader e2e test
* fix: add missing version in openapi spec
* chore: use go-yaml instead of yaml.v3
* chore: skip go files in bash e2e tests

Signed-off-by: lvlcn-t <[email protected]>
  • Loading branch information
lvlcn-t committed Nov 15, 2024
1 parent b9e9a80 commit e4d47a1
Show file tree
Hide file tree
Showing 30 changed files with 783 additions and 32 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/e2e_checks.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: E2E - Test checks
name: E2E - Traceroute Check

on:
push:
on: [push]

permissions:
contents: read
Expand Down Expand Up @@ -48,7 +47,7 @@ jobs:
uses: goreleaser/goreleaser-action@v6
with:
version: latest
args: build --single-target --clean --snapshot --config .goreleaser-ci.yaml
args: build --single-target --clean --snapshot --config .goreleaser-ci.yaml

- name: Run e2e tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
hooks:
- id: go-mod-tidy-repo
- id: go-test-repo-mod
args: [-race, -count=1, -timeout 30s]
args: [-race, -count=1, -timeout 30s, "-test.short"]
- id: go-vet-repo-mod
- id: go-fumpt-repo
args: [-l, -w]
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"go.testFlags": [
"-race",
"-cover",
"-count=1"
"-count=1",
"-timeout=120s",
]
}
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func NewCmdRun() *cobra.Command {

// run is the entry point to start the sparrow
func run() func(cmd *cobra.Command, args []string) error {
return func(_ *cobra.Command, _ []string) error {
cfg := &config.Config{}
return func(cmd *cobra.Command, _ []string) error {
cfg := &config.Config{Version: cmd.Root().Version}
err := viper.Unmarshal(cfg)
if err != nil {
return fmt.Errorf("failed to parse config: %w", err)
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23
require (
github.com/getkin/kin-openapi v0.128.0
github.com/go-chi/chi/v5 v5.1.0
github.com/goccy/go-yaml v1.13.8
github.com/google/go-cmp v0.6.0
github.com/jarcoal/httpmock v1.3.1
github.com/prometheus/client_golang v1.20.5
Expand All @@ -20,7 +21,6 @@ require (
golang.org/x/net v0.31.0
golang.org/x/sys v0.27.0
google.golang.org/grpc v1.68.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -36,6 +36,7 @@ require (
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.3.1 // indirect
Expand Down Expand Up @@ -65,4 +66,5 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/go-viper/mapstructure/v2 v2.1.0 h1:gHnMa2Y/pIxElCH2GlZZ1lZSsn6XMtufpGyP1XxdC/w=
github.com/go-viper/mapstructure/v2 v2.1.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/goccy/go-yaml v1.13.8 h1:ftugzaplJyFaFwfyVNeq1XQOBxmlp8zazmuiobaCXbk=
github.com/goccy/go-yaml v1.13.8/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 h1:ad0vkEBuk23VJzZR9nkLVG0YAoN9coASF1GusYX6AlU=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0/go.mod h1:igFoXX2ELCW06bol23DWPB5BEWfZISOzSP5K2sbLea0=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
)

type Config struct {
// Version is the version of the sparrow.
// This is set at build time by using -ldflags "-X main.version=x.x.x"
// and is not part of the configuration file or flags.
Version string `yaml:"-" mapstructure:"-"`
// SparrowName is the DNS name of the sparrow
SparrowName string `yaml:"name" mapstructure:"name"`
// Loader is the configuration for the loader
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

"github.com/caas-team/sparrow/internal/logger"
"github.com/caas-team/sparrow/pkg/checks/runtime"
"gopkg.in/yaml.v3"
"github.com/goccy/go-yaml"
)

var _ Loader = (*FileLoader)(nil)
Expand Down Expand Up @@ -120,7 +120,7 @@ func (f *FileLoader) getRuntimeConfig(ctx context.Context) (cfg runtime.Config,
return cfg, fmt.Errorf("failed to read config file: %w", err)
}

if err := yaml.Unmarshal(b, &cfg); err != nil {
if err := yaml.UnmarshalContext(ctx, b, &cfg); err != nil {
log.Error("Failed to parse config file", "error", err)
return cfg, fmt.Errorf("failed to parse config file: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/caas-team/sparrow/pkg/checks/health"
"github.com/caas-team/sparrow/pkg/checks/runtime"
"github.com/caas-team/sparrow/pkg/config/test"
"gopkg.in/yaml.v3"
"github.com/goccy/go-yaml"
)

func TestNewFileLoader(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/caas-team/sparrow/internal/helper"
"github.com/caas-team/sparrow/internal/logger"
"github.com/caas-team/sparrow/pkg/checks/runtime"
"gopkg.in/yaml.v3"
"github.com/goccy/go-yaml"
)

type HttpLoader struct {
Expand Down Expand Up @@ -138,7 +138,7 @@ func (hl *HttpLoader) getRuntimeConfig(ctx context.Context) (cfg runtime.Config,
}
log.Debug("Successfully got response")

if err := yaml.Unmarshal(b, &cfg); err != nil {
if err := yaml.UnmarshalContext(ctx, b, &cfg); err != nil {
log.Error("Could not unmarshal response", "error", err.Error())
return cfg, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
"github.com/caas-team/sparrow/internal/logger"
"github.com/caas-team/sparrow/pkg/checks/health"
"github.com/caas-team/sparrow/pkg/checks/runtime"
"github.com/goccy/go-yaml"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)

func TestHttpLoader_GetRuntimeConfig(t *testing.T) {
Expand Down
13 changes: 11 additions & 2 deletions pkg/sparrow/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,21 @@ type ChecksController struct {
cResult chan checks.ResultDTO
cErr chan error
done chan struct{}
// version is the version of the sparrow.
// This is set at build time by using -ldflags "-X main.version=x.x.x".
version string
}

// NewChecksController creates a new ChecksController.
func NewChecksController(dbase db.DB, m metrics.Provider) *ChecksController {
func NewChecksController(dbase db.DB, m metrics.Provider, version string) *ChecksController {
return &ChecksController{
db: dbase,
metrics: m,
checks: runtime.Checks{},
cResult: make(chan checks.ResultDTO, 8), //nolint:mnd // Buffered channel to avoid blocking the checks
cErr: make(chan error, 1),
done: make(chan struct{}, 1),
version: version,
}
}

Expand Down Expand Up @@ -170,16 +174,20 @@ func (cc *ChecksController) UnregisterCheck(ctx context.Context, check checks.Ch
}

var oapiBoilerplate = openapi3.T{
// this object should probably be user defined
OpenAPI: "3.0.0",
Info: &openapi3.Info{
Title: "Sparrow Metrics API",
Version: "0.5.0",
Description: "Serves metrics collected by sparrows checks",
Contact: &openapi3.Contact{
URL: "https://caas.telekom.de",
Email: "[email protected]",
Name: "CaaS Team",
},
License: &openapi3.License{
Name: "Apache 2.0",
URL: "http://www.apache.org/licenses/LICENSE-2.0",
},
},
Paths: &openapi3.Paths{
Extensions: make(map[string]any),
Expand All @@ -196,6 +204,7 @@ var oapiBoilerplate = openapi3.T{
func (cc *ChecksController) GenerateCheckSpecs(ctx context.Context) (openapi3.T, error) {
log := logger.FromContext(ctx)
doc := oapiBoilerplate
doc.Info.Version = cc.version
for _, c := range cc.checks.Iter() {
name := c.Name()
ref, err := c.Schema()
Expand Down
10 changes: 5 additions & 5 deletions pkg/sparrow/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestRun_CheckRunError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cc := NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}))
cc := NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}), "")
mockCheck := &checks.CheckMock{
NameFunc: func() string { return "mockCheck" },
RunFunc: func(ctx context.Context, cResult chan checks.ResultDTO) error {
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestRun_CheckRunError(t *testing.T) {
func TestRun_ContextCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

cc := NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}))
cc := NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}), "")

done := make(chan struct{})
go func() {
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestChecksController_Reconcile(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cc := NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}))
cc := NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}), "")

for _, c := range tt.checks {
cc.checks.Add(c)
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestChecksController_RegisterCheck(t *testing.T) {
{
name: "register one check",
setup: func() *ChecksController {
return NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}))
return NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}), "")
},
check: health.NewCheck(),
},
Expand Down Expand Up @@ -274,7 +274,7 @@ func TestChecksController_UnregisterCheck(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cc := NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}))
cc := NewChecksController(db.NewInMemory(), metrics.New(metrics.Config{}), "")

cc.UnregisterCheck(context.Background(), tt.check)

Expand Down
2 changes: 1 addition & 1 deletion pkg/sparrow/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"github.com/caas-team/sparrow/internal/logger"
"github.com/caas-team/sparrow/pkg/api"
"github.com/go-chi/chi/v5"
"github.com/goccy/go-yaml"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/yaml.v3"
)

type encoder interface {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sparrow/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/caas-team/sparrow/pkg/db"
"github.com/getkin/kin-openapi/openapi3"
"github.com/go-chi/chi/v5"
"gopkg.in/yaml.v3"
"github.com/goccy/go-yaml"
)

func TestSparrow_handleOpenAPI(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sparrow/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func New(cfg *config.Config) *Sparrow {
db: dbase,
api: api.New(cfg.Api),
metrics: m,
controller: NewChecksController(dbase, m),
controller: NewChecksController(dbase, m, cfg.Version),
cRuntime: make(chan runtime.Config, 1),
cErr: make(chan error, 1),
cDone: make(chan struct{}, 1),
Expand Down
18 changes: 11 additions & 7 deletions scripts/run_e2e_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ EXIT_CODE=0

MAX_RETRY=3

for i in $(ls e2e); do
for ATTEMPT in $(seq 1 $MAX_RETRY ); do
echo "[$ATTEMPT/$MAX_RETRY] Running test e2e/$i"
cd e2e/$i
./test.sh
for i in $(ls -d test/e2e/*); do
if [ ! -d $i ] || [ ! -f $i/test.sh ]; then
continue
fi

for ATTEMPT in $(seq 1 $MAX_RETRY); do
echo "[$ATTEMPT/$MAX_RETRY] Running test $i"
cd $i
./test.sh
TEST_EXIT_CODE=$?
cd $root
if [ $TEST_EXIT_CODE -eq 0 ]; then
break
elif [ $ATTEMPT -eq $MAX_RETRY ]; then
EXIT_CODE=1
fi
done
done
done

exit $EXIT_CODE
exit $EXIT_CODE
1 change: 1 addition & 0 deletions test/e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata/*
Loading

0 comments on commit e4d47a1

Please sign in to comment.