Skip to content
Draft
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
74 changes: 47 additions & 27 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ db_host=hyperfleet-db.$(namespace)
db_port=5432
db_user:=hyperfleet
db_password:=foobar-bizz-buzz
db_password_file=${PWD}/secrets/db.password
db_sslmode:=disable
db_image?=docker.io/library/postgres:14.2

Expand Down Expand Up @@ -152,29 +151,23 @@ install: check-gopath generate-all
)
.PHONY: install

# Initialize secrets directory with default values
secrets:
@mkdir -p secrets
@printf "localhost" > secrets/db.host
@printf "$(db_name)" > secrets/db.name
@printf "$(db_password)" > secrets/db.password
@printf "$(db_port)" > secrets/db.port
@printf "$(db_user)" > secrets/db.user
@printf "ocm-hyperfleet-testing" > secrets/ocm-service.clientId
@printf "your-client-secret-here" > secrets/ocm-service.clientSecret
@printf "your-token-here" > secrets/ocm-service.token
@echo "Secrets directory initialized with default values"
.PHONY: secrets

# Runs the unit tests.
#
# Args:
# TESTFLAGS: Flags to pass to `go test`. The `-v` argument is always passed.
#
# Examples:
# make test TESTFLAGS="-run TestSomething"
test: install secrets $(GOTESTSUM)
OCM_ENV=unit_testing $(GOTESTSUM) --format $(TEST_SUMMARY_FORMAT) -- -p 1 -v $(TESTFLAGS) \
test: install $(GOTESTSUM)
OCM_ENV=unit_testing \
HYPERFLEET_APP_NAME=hyperfleet-api-test \
HYPERFLEET_OCM_MOCK=true \
HYPERFLEET_OCM_DEBUG=false \
HYPERFLEET_OCM_BASE_URL=https://api.integration.openshift.com \
HYPERFLEET_SERVER_HTTPS_ENABLED=false \
HYPERFLEET_METRICS_HTTPS_ENABLED=false \
HYPERFLEET_AUTH_AUTHZ_ENABLED=true \
$(GOTESTSUM) --format $(TEST_SUMMARY_FORMAT) -- -p 1 -v $(TESTFLAGS) \
./pkg/... \
./cmd/...
.PHONY: test
Expand All @@ -186,8 +179,16 @@ test: install secrets $(GOTESTSUM)
#
# Examples:
# make test-unit-json TESTFLAGS="-run TestSomething"
ci-test-unit: install secrets $(GOTESTSUM)
OCM_ENV=unit_testing $(GOTESTSUM) --jsonfile-timing-events=$(unit_test_json_output) --format $(TEST_SUMMARY_FORMAT) -- -p 1 -v $(TESTFLAGS) \
ci-test-unit: install $(GOTESTSUM)
OCM_ENV=unit_testing \
HYPERFLEET_APP_NAME=hyperfleet-api-test \
HYPERFLEET_OCM_MOCK=true \
HYPERFLEET_OCM_DEBUG=false \
HYPERFLEET_OCM_BASE_URL=https://api.integration.openshift.com \
HYPERFLEET_SERVER_HTTPS_ENABLED=false \
HYPERFLEET_METRICS_HTTPS_ENABLED=false \
HYPERFLEET_AUTH_AUTHZ_ENABLED=true \
$(GOTESTSUM) --jsonfile-timing-events=$(unit_test_json_output) --format $(TEST_SUMMARY_FORMAT) -- -p 1 -v $(TESTFLAGS) \
./pkg/... \
./cmd/...
.PHONY: ci-test-unit
Expand All @@ -202,8 +203,17 @@ ci-test-unit: install secrets $(GOTESTSUM)
# make test-integration TESTFLAGS="-run TestAccounts" acts as TestAccounts* and run TestAccountsGet, TestAccountsPost, etc.
# make test-integration TESTFLAGS="-run TestAccountsGet" runs TestAccountsGet
# make test-integration TESTFLAGS="-short" skips long-run tests
ci-test-integration: install secrets $(GOTESTSUM)
TESTCONTAINERS_RYUK_DISABLED=true OCM_ENV=integration_testing $(GOTESTSUM) --jsonfile-timing-events=$(integration_test_json_output) --format $(TEST_SUMMARY_FORMAT) -- -p 1 -ldflags -s -v -timeout 1h $(TESTFLAGS) \
ci-test-integration: install $(GOTESTSUM)
TESTCONTAINERS_RYUK_DISABLED=true \
OCM_ENV=integration_testing \
HYPERFLEET_APP_NAME=hyperfleet-api-integration-test \
HYPERFLEET_OCM_MOCK=true \
HYPERFLEET_OCM_DEBUG=false \
HYPERFLEET_OCM_BASE_URL=https://api.integration.openshift.com \
HYPERFLEET_SERVER_HTTPS_ENABLED=false \
HYPERFLEET_METRICS_HTTPS_ENABLED=false \
HYPERFLEET_AUTH_AUTHZ_ENABLED=true \
$(GOTESTSUM) --jsonfile-timing-events=$(integration_test_json_output) --format $(TEST_SUMMARY_FORMAT) -- -p 1 -ldflags -s -v -timeout 1h $(TESTFLAGS) \
./test/integration
.PHONY: ci-test-integration

Expand All @@ -217,8 +227,20 @@ ci-test-integration: install secrets $(GOTESTSUM)
# make test-integration TESTFLAGS="-run TestAccounts" acts as TestAccounts* and run TestAccountsGet, TestAccountsPost, etc.
# make test-integration TESTFLAGS="-run TestAccountsGet" runs TestAccountsGet
# make test-integration TESTFLAGS="-short" skips long-run tests
test-integration: install secrets $(GOTESTSUM)
TESTCONTAINERS_RYUK_DISABLED=true OCM_ENV=integration_testing $(GOTESTSUM) --format $(TEST_SUMMARY_FORMAT) -- -p 1 -ldflags -s -v -timeout 1h $(TESTFLAGS) \
test-integration: install $(GOTESTSUM)
TESTCONTAINERS_RYUK_DISABLED=true \
OCM_ENV=integration_testing \
HYPERFLEET_APP_NAME=hyperfleet-api-integration-test \
HYPERFLEET_OCM_MOCK=true \
HYPERFLEET_OCM_DEBUG=false \
HYPERFLEET_OCM_BASE_URL=https://api.integration.openshift.com \
HYPERFLEET_SERVER_HTTPS_ENABLED=false \
HYPERFLEET_METRICS_HTTPS_ENABLED=false \
HYPERFLEET_SERVER_AUTH_AUTHZ_ENABLED=false \
HYPERFLEET_DATABASE_NAME=$(db_name) \
HYPERFLEET_DATABASE_USERNAME=$(db_user) \
HYPERFLEET_DATABASE_PASSWORD=$(db_password) \
$(GOTESTSUM) --format $(TEST_SUMMARY_FORMAT) -- -p 1 -ldflags -s -v -timeout 1h $(TESTFLAGS) \
./test/integration
.PHONY: test-integration

Expand Down Expand Up @@ -268,8 +290,7 @@ run/docs:
clean:
rm -rf \
$(binary) \
data/generated/openapi/*.json \
secrets \
data/generated/openapi/*.json
.PHONY: clean

.PHONY: cmds
Expand All @@ -284,8 +305,7 @@ cmds:


.PHONY: db/setup
db/setup: secrets
@echo $(db_password) > $(db_password_file)
db/setup:
$(container_tool) run --name psql-hyperfleet -e POSTGRES_DB=$(db_name) -e POSTGRES_USER=$(db_user) -e POSTGRES_PASSWORD=$(db_password) -p $(db_port):5432 -d $(db_image)

.PHONY: db/login
Expand Down
15 changes: 0 additions & 15 deletions cmd/hyperfleet-api/environments/e_development.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ func (e *devEnvImpl) OverrideDatabase(c *Database) error {
}

func (e *devEnvImpl) OverrideConfig(c *config.ApplicationConfig) error {
c.Server.EnableJWT = false
c.Server.EnableHTTPS = false
return nil
}

Expand All @@ -34,16 +32,3 @@ func (e *devEnvImpl) OverrideHandlers(h *Handlers) error {
func (e *devEnvImpl) OverrideClients(c *Clients) error {
return nil
}

func (e *devEnvImpl) Flags() map[string]string {
return map[string]string{
"v": "10",
"enable-authz": "false",
"ocm-debug": "false",
"enable-ocm-mock": "true",
"enable-https": "false",
"enable-metrics-https": "false",
"api-server-hostname": "localhost",
"api-server-bindaddress": "localhost:8000",
}
}
13 changes: 0 additions & 13 deletions cmd/hyperfleet-api/environments/e_integration_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,3 @@ func (e *integrationTestingEnvImpl) OverrideHandlers(h *Handlers) error {
func (e *integrationTestingEnvImpl) OverrideClients(c *Clients) error {
return nil
}

func (e *integrationTestingEnvImpl) Flags() map[string]string {
return map[string]string{
"v": "0",
"logtostderr": "true",
"ocm-base-url": "https://api.integration.openshift.com",
"enable-https": "false",
"enable-metrics-https": "false",
"enable-authz": "true",
"ocm-debug": "false",
"enable-ocm-mock": "true",
}
}
8 changes: 0 additions & 8 deletions cmd/hyperfleet-api/environments/e_production.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,3 @@ func (e *productionEnvImpl) OverrideHandlers(h *Handlers) error {
func (e *productionEnvImpl) OverrideClients(c *Clients) error {
return nil
}

func (e *productionEnvImpl) Flags() map[string]string {
return map[string]string{
"v": "1",
"ocm-debug": "false",
"enable-ocm-mock": "false",
}
}
13 changes: 0 additions & 13 deletions cmd/hyperfleet-api/environments/e_unit_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,3 @@ func (e *unitTestingEnvImpl) OverrideHandlers(h *Handlers) error {
func (e *unitTestingEnvImpl) OverrideClients(c *Clients) error {
return nil
}

func (e *unitTestingEnvImpl) Flags() map[string]string {
return map[string]string{
"v": "0",
"logtostderr": "true",
"ocm-base-url": "https://api.integration.openshift.com",
"enable-https": "false",
"enable-metrics-https": "false",
"enable-authz": "true",
"ocm-debug": "false",
"enable-ocm-mock": "true",
}
}
48 changes: 15 additions & 33 deletions cmd/hyperfleet-api/environments/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package environments

import (
"os"
"strings"

"github.com/golang/glog"
"github.com/spf13/pflag"

"github.com/openshift-hyperfleet/hyperfleet-api/cmd/hyperfleet-api/environments/registry"
"github.com/openshift-hyperfleet/hyperfleet-api/pkg/client/ocm"
Expand All @@ -17,8 +15,8 @@ func init() {
once.Do(func() {
environment = &Env{}

// Create the configuration
environment.Config = config.NewApplicationConfig()
// DO NOT create Config here
// Config will be provided by commands via Initialize()
environment.Name = GetEnvironmentStrFromEnv()

environments = map[string]EnvironmentImpl{
Expand All @@ -31,10 +29,8 @@ func init() {
}

// EnvironmentImpl defines a set of behaviors for an OCM environment.
// Each environment provides a set of flags for basic set/override of the environment
// and configuration functions for each component type.
// Each environment provides configuration functions for each component type.
type EnvironmentImpl interface {
Flags() map[string]string
OverrideConfig(c *config.ApplicationConfig) error
OverrideServices(s *Services) error
OverrideDatabase(s *Database) error
Expand All @@ -54,37 +50,30 @@ func Environment() *Env {
return environment
}

// AddFlags Adds environment flags, using the environment's config struct, to the flagset 'flags'
func (e *Env) AddFlags(flags *pflag.FlagSet) error {
e.Config.AddFlags(flags)
return setConfigDefaults(flags, environments[e.Name].Flags())
}

// Initialize loads the environment's resources
// This should be called after the e.Config has been set appropriately though AddFlags and pasing, done elsewhere
// The environment does NOT handle flag parsing
func (e *Env) Initialize() error {
// Initialize loads the environment's resources with pre-loaded configuration
// Configuration must be loaded by the caller using config.LoadConfig()
func (e *Env) Initialize(appConfig *config.ApplicationConfig) error {
glog.Infof("Initializing %s environment", e.Name)

envImpl, found := environments[e.Name]
if !found {
glog.Fatalf("Unknown runtime environment: %s", e.Name)
}

// Store the provided config
e.Config = appConfig

// Allow environment to apply runtime overrides (e.g., DB_DEBUG for tests)
if err := envImpl.OverrideConfig(e.Config); err != nil {
glog.Fatalf("Failed to configure ApplicationConfig: %s", err)
}

messages := environment.Config.ReadFiles()
if len(messages) != 0 {
glog.Fatalf("unable to read configuration files:\n%s", strings.Join(messages, "\n"))
}

// each env will set db explicitly because the DB impl has a `once` init section
// Initialize database with config
if err := envImpl.OverrideDatabase(&e.Database); err != nil {
glog.Fatalf("Failed to configure Database: %s", err)
}

// Initialize clients
err := e.LoadClients()
if err != nil {
return err
Expand All @@ -93,16 +82,19 @@ func (e *Env) Initialize() error {
glog.Fatalf("Failed to configure Clients: %s", err)
}

// Initialize services
e.LoadServices()
if err := envImpl.OverrideServices(&e.Services); err != nil {
glog.Fatalf("Failed to configure Services: %s", err)
}

// Seed data
seedErr := e.Seed()
if seedErr != nil {
return seedErr
}

// Initialize handlers
if err := envImpl.OverrideHandlers(&e.Handlers); err != nil {
glog.Fatalf("Failed to configure Handlers: %s", err)
}
Expand Down Expand Up @@ -161,13 +153,3 @@ func (e *Env) Teardown() {
}
}
}

func setConfigDefaults(flags *pflag.FlagSet, defaults map[string]string) error {
for name, value := range defaults {
if err := flags.Set(name, value); err != nil {
glog.Errorf("Error setting flag %s: %v", name, err)
return err
}
}
return nil
}
35 changes: 29 additions & 6 deletions cmd/hyperfleet-api/environments/framework_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package environments

import (
"os"
"os/exec"
"reflect"
"testing"

"github.com/spf13/pflag"

"github.com/openshift-hyperfleet/hyperfleet-api/pkg/config"
)

func BenchmarkGetDynos(b *testing.B) {
Expand All @@ -23,16 +26,36 @@ func BenchmarkGetDynos(b *testing.B) {
}

func TestLoadServices(t *testing.T) {
env := Environment()
err := env.AddFlags(pflag.CommandLine)
// Set required environment variables for testing
os.Setenv("HYPERFLEET_APP_NAME", "hyperfleet-api-test")
os.Setenv("HYPERFLEET_OCM_MOCK", "true")
os.Setenv("HYPERFLEET_OCM_DEBUG", "false")
os.Setenv("HYPERFLEET_OCM_BASE_URL", "https://api.integration.openshift.com")
os.Setenv("HYPERFLEET_SERVER_HTTPS_ENABLED", "false")
os.Setenv("HYPERFLEET_METRICS_HTTPS_ENABLED", "false")
os.Setenv("HYPERFLEET_AUTH_AUTHZ_ENABLED", "true")

// Create config
appConfig := config.NewApplicationConfig()

// Create viper and configure flags
v := config.NewCommandConfig()
appConfig.ConfigureFlags(v, pflag.CommandLine)

pflag.Parse()

// Load config
loadedConfig, err := config.LoadConfig(v, pflag.CommandLine)
if err != nil {
t.Errorf("Unable to add flags for testing environment: %s", err.Error())
t.Errorf("Failed to load configuration: %v", err)
return
}
pflag.Parse()
err = env.Initialize()

// Initialize environment with loaded config
env := Environment()
err = env.Initialize(loadedConfig)
if err != nil {
t.Errorf("Unable to load testing environment: %s", err.Error())
t.Errorf("Unable to initialize testing environment: %s", err.Error())
return
}

Expand Down
Loading