Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: e2e-test #104

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ jobs:
go-version: "1.21"
- name: run tests
run: make test
e2e-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: "1.21"
- name: run e2e-tests
run: make e2e-test
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ test: tidy
@echo "Running unit tests..."
@go test ./... -coverprofile=${COVERAGE_DIR}/coverage.out

e2e-test: tidy
@echo "Running e2e-test tests..."
@go test -v ./test/e2e_test/...

test-coverage: test
@echo "Generating coverage report..."
@go tool cover -html=${COVERAGE_DIR}/coverage.out
Expand Down
12 changes: 6 additions & 6 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ const configFlag = "config"
// Config contains the application configuration.
type Config struct {
Log logger.LogConfig `mapstructure:"log"`
Syncer syncerConf `mapstructure:"syncer"`
Service serveConfig `mapstructure:"service"`
Syncer SyncerConf `mapstructure:"syncer"`
Service ServeConfig `mapstructure:"service"`
PGConnStr string `mapstructure:"pg_conn_str" default:"postgres://postgres@localhost:5432/entropy?sslmode=disable"`
Telemetry telemetry.Config `mapstructure:"telemetry"`
}

type syncerConf struct {
type SyncerConf struct {
SyncInterval time.Duration `mapstructure:"sync_interval" default:"1s"`
RefreshInterval time.Duration `mapstructure:"refresh_interval" default:"3s"`
ExtendLockBy time.Duration `mapstructure:"extend_lock_by" default:"5s"`
SyncBackoffInterval time.Duration `mapstructure:"sync_backoff_interval" default:"5s"`
MaxRetries int `mapstructure:"max_retries" default:"5"`
}

type serveConfig struct {
type ServeConfig struct {
Host string `mapstructure:"host" default:""`
Port int `mapstructure:"port" default:"8080"`

Expand All @@ -50,9 +50,9 @@ type clientConfig struct {
Host string `mapstructure:"host" default:"localhost:8080"`
}

func (serveCfg serveConfig) httpAddr() string { return serveCfg.HTTPAddr }
func (serveCfg ServeConfig) httpAddr() string { return serveCfg.HTTPAddr }

func (serveCfg serveConfig) grpcAddr() string {
func (serveCfg ServeConfig) grpcAddr() string {
return fmt.Sprintf("%s:%d", serveCfg.Host, serveCfg.Port)
}

Expand Down
65 changes: 35 additions & 30 deletions cli/serve.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"time"

"github.com/newrelic/go-agent/v3/newrelic"
Expand Down Expand Up @@ -39,42 +40,46 @@ func cmdServe() *cobra.Command {
return err
}

err = logger.Setup(&cfg.Log)
if err != nil {
return err
}
return StartServer(cmd.Context(), cfg, migrate, spawnWorker)
})

telemetry.Init(cmd.Context(), cfg.Telemetry)
nrApp, err := newrelic.NewApplication(
newrelic.ConfigAppName(cfg.Telemetry.ServiceName),
newrelic.ConfigLicense(cfg.Telemetry.NewRelicAPIKey),
)
return cmd
}

func StartServer(ctx context.Context, cfg Config, migrate, spawnWorker bool) error {
err := logger.Setup(&cfg.Log)
if err != nil {
return err
}

store := setupStorage(cfg.PGConnStr, cfg.Syncer, cfg.Service)
moduleService := module.NewService(setupRegistry(), store)
resourceService := core.New(store, moduleService, time.Now, cfg.Syncer.SyncBackoffInterval, cfg.Syncer.MaxRetries)
telemetry.Init(ctx, cfg.Telemetry)
nrApp, err := newrelic.NewApplication(
newrelic.ConfigAppName(cfg.Telemetry.ServiceName),
newrelic.ConfigLicense(cfg.Telemetry.NewRelicAPIKey),
)

if migrate {
if migrateErr := runMigrations(cmd.Context(), cfg); migrateErr != nil {
return migrateErr
}
}
store := setupStorage(cfg.PGConnStr, cfg.Syncer, cfg.Service)
moduleService := module.NewService(setupRegistry(), store)
resourceService := core.New(store, moduleService, time.Now, cfg.Syncer.SyncBackoffInterval, cfg.Syncer.MaxRetries)

if spawnWorker {
go func() {
if runErr := resourceService.RunSyncer(cmd.Context(), cfg.Syncer.SyncInterval); runErr != nil {
zap.L().Error("syncer exited with error", zap.Error(err))
}
}()
if migrate {
if migrateErr := runMigrations(ctx, cfg); migrateErr != nil {
return migrateErr
}
}

return entropyserver.Serve(cmd.Context(),
cfg.Service.httpAddr(), cfg.Service.grpcAddr(),
nrApp, resourceService, moduleService,
)
})
if spawnWorker {
go func() {
if runErr := resourceService.RunSyncer(ctx, cfg.Syncer.SyncInterval); runErr != nil {
zap.L().Error("syncer exited with error", zap.Error(err))
}
}()
}

return cmd
return entropyserver.Serve(ctx,
cfg.Service.httpAddr(), cfg.Service.grpcAddr(),
nrApp, resourceService, moduleService,
)
}

func setupRegistry() module.Registry {
Expand All @@ -96,7 +101,7 @@ func setupRegistry() module.Registry {
return registry
}

func setupStorage(pgConStr string, syncCfg syncerConf, serveCfg serveConfig) *postgres.Store {
func setupStorage(pgConStr string, syncCfg SyncerConf, serveCfg ServeConfig) *postgres.Store {
store, err := postgres.Open(pgConStr, syncCfg.RefreshInterval, syncCfg.ExtendLockBy, serveCfg.PaginationSizeDefault, serveCfg.PaginationPageDefault)
if err != nil {
zap.L().Fatal("failed to connect to Postgres database",
Expand Down
65 changes: 43 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ require (
github.com/Masterminds/squirrel v1.5.4
github.com/ghodss/yaml v1.0.0
github.com/go-playground/validator/v10 v10.15.4
github.com/google/go-cmp v0.5.9
github.com/google/go-cmp v0.6.0
github.com/gorilla/mux v1.8.0
github.com/goto/salt v0.3.3
github.com/goto/salt v0.3.7
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0
github.com/jmoiron/sqlx v1.3.5
Expand All @@ -24,20 +24,37 @@ require (
github.com/newrelic/newrelic-opencensus-exporter-go v0.4.0
github.com/rs/xid v1.5.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/xeipuuv/gojsonschema v1.2.0
go.opencensus.io v0.24.0
go.uber.org/zap v1.26.0
google.golang.org/api v0.141.0
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb
google.golang.org/grpc v1.58.1
google.golang.org/protobuf v1.31.0
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97
google.golang.org/grpc v1.60.1
google.golang.org/protobuf v1.32.0
gopkg.in/yaml.v2 v2.4.0
gotest.tools v2.2.0+incompatible
helm.sh/helm/v3 v3.12.3
k8s.io/api v0.28.2
k8s.io/apimachinery v0.28.2
k8s.io/client-go v0.28.2
sigs.k8s.io/kind v0.23.0
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/authzed/authzed-go v0.7.0 // indirect
github.com/authzed/grpcutil v0.0.0-20230908193239-4286bb1d6403 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/containerd/continuity v0.4.2 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
github.com/jzelinskie/stringz v0.0.0-20210414224931-d6a8ce844a70 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/ory/dockertest/v3 v3.9.1
golang.org/x/mod v0.11.0 // indirect
golang.org/x/tools v0.10.0 // indirect
)

require (
Expand Down Expand Up @@ -72,7 +89,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-errors/errors v1.5.0 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
Expand Down Expand Up @@ -127,8 +144,8 @@ require (
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
Expand All @@ -142,7 +159,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
Expand All @@ -151,13 +168,13 @@ require (
github.com/yuin/goldmark-emoji v1.0.2 // indirect
go.starlark.net v0.0.0-20230912135651-745481cf39ed // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/oauth2 v0.12.0
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/oauth2 v0.13.0
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand All @@ -184,11 +201,13 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
github.com/Microsoft/hcsshim v0.11.0 // indirect
github.com/alessio/shellescape v1.4.1 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cli/safeexec v1.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
Expand All @@ -198,20 +217,22 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/newrelic/csec-go-agent v0.4.0 // indirect
github.com/newrelic/newrelic-telemetry-sdk-go v0.8.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/prometheus/statsd_exporter v0.24.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.50.0 // indirect
go.opentelemetry.io/otel v1.18.0 // indirect
go.opentelemetry.io/otel/metric v1.18.0 // indirect
go.opentelemetry.io/otel/trace v1.18.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
golang.org/x/arch v0.5.0 // indirect
google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230913181813-007df8e322eb // indirect
google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 // indirect
)
Loading
Loading