Skip to content

Commit

Permalink
tests/e2e: Generalize GetEnv (openservicemesh#1844)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduser25 authored Oct 16, 2020
1 parent e3faa73 commit 321febf
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 39 deletions.
15 changes: 8 additions & 7 deletions ci/cmd/maestro.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/openservicemesh/osm/demo/cmd/common"
"github.com/openservicemesh/osm/pkg/constants"
"github.com/openservicemesh/osm/pkg/logger"
"github.com/openservicemesh/osm/pkg/utils"
)

var log = logger.NewPretty("ci/maestro")
Expand All @@ -36,14 +37,14 @@ var (
bookstoreV2Selector = fmt.Sprintf("%s=%s", selectorKey, bookstoreV2Label)
bookWarehouseSelector = fmt.Sprintf("%s=%s", selectorKey, bookWarehouseLabel)

osmNamespace = common.GetEnv(maestro.OSMNamespaceEnvVar, "osm-system")
bookbuyerNS = common.GetEnv(maestro.BookbuyerNamespaceEnvVar, "bookbuyer")
bookthiefNS = common.GetEnv(maestro.BookthiefNamespaceEnvVar, "bookthief")
bookstoreNS = common.GetEnv(maestro.BookstoreNamespaceEnvVar, "bookstore")
bookWarehouseNS = common.GetEnv(common.BookwarehouseNamespaceEnvVar, "bookwarehouse")
osmNamespace = utils.GetEnv(maestro.OSMNamespaceEnvVar, "osm-system")
bookbuyerNS = utils.GetEnv(maestro.BookbuyerNamespaceEnvVar, "bookbuyer")
bookthiefNS = utils.GetEnv(maestro.BookthiefNamespaceEnvVar, "bookthief")
bookstoreNS = utils.GetEnv(maestro.BookstoreNamespaceEnvVar, "bookstore")
bookWarehouseNS = utils.GetEnv(common.BookwarehouseNamespaceEnvVar, "bookwarehouse")

maxPodWaitString = common.GetEnv(maestro.WaitForPodTimeSecondsEnvVar, "30")
maxOKWaitString = common.GetEnv(maestro.WaitForOKSecondsEnvVar, "30")
maxPodWaitString = utils.GetEnv(maestro.WaitForPodTimeSecondsEnvVar, "30")
maxOKWaitString = utils.GetEnv(maestro.WaitForOKSecondsEnvVar, "30")
meshName = osmNamespace

// Mesh namespaces
Expand Down
5 changes: 3 additions & 2 deletions demo/cmd/bookbuyer/bookbuyer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/openservicemesh/osm/demo/cmd/common"
"github.com/openservicemesh/osm/pkg/logger"
"github.com/openservicemesh/osm/pkg/utils"
)

const (
Expand All @@ -30,7 +31,7 @@ var (
log = logger.NewPretty(participantName)
port = flag.Int("port", 80, "port on which this app is listening for incoming HTTP")
path = flag.String("path", ".", "path to the HTML template")
numConnectionsStr = common.GetEnv("CI_CLIENT_CONCURRENT_CONNECTIONS", "1")
numConnectionsStr = utils.GetEnv("CI_CLIENT_CONCURRENT_CONNECTIONS", "1")
)

type handler struct {
Expand All @@ -40,7 +41,7 @@ type handler struct {
}

func getIdentity() string {
return common.GetEnv("IDENTITY", "Bookbuyer")
return utils.GetEnv("IDENTITY", "Bookbuyer")
}

func renderTemplate(w http.ResponseWriter) {
Expand Down
3 changes: 2 additions & 1 deletion demo/cmd/bookthief/bookthief.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/openservicemesh/osm/demo/cmd/common"
"github.com/openservicemesh/osm/pkg/logger"
"github.com/openservicemesh/osm/pkg/utils"
)

const (
Expand Down Expand Up @@ -46,7 +47,7 @@ func renderTemplate(w http.ResponseWriter) {
}
}
func getIdentity() string {
return common.GetEnv("IDENTITY", "Bookthief")
return utils.GetEnv("IDENTITY", "Bookthief")
}

type handler struct {
Expand Down
20 changes: 6 additions & 14 deletions demo/cmd/common/books.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/openservicemesh/osm/pkg/logger"
"github.com/openservicemesh/osm/pkg/utils"
)

const (
Expand All @@ -29,10 +30,10 @@ var (
// enableEgress determines whether egress is enabled
enableEgress = os.Getenv(EnableEgressEnvVar) == "true"

sleepDurationBetweenRequestsSecondsStr = GetEnv("CI_SLEEP_BETWEEN_REQUESTS_SECONDS", "1")
minSuccessThresholdStr = GetEnv("CI_MIN_SUCCESS_THRESHOLD", "1")
maxIterationsStr = GetEnv("CI_MAX_ITERATIONS_THRESHOLD", "0") // 0 for unlimited
bookstoreServiceName = GetEnv("BOOKSTORE_SVC", "bookstore")
sleepDurationBetweenRequestsSecondsStr = utils.GetEnv("CI_SLEEP_BETWEEN_REQUESTS_SECONDS", "1")
minSuccessThresholdStr = utils.GetEnv("CI_MIN_SUCCESS_THRESHOLD", "1")
maxIterationsStr = utils.GetEnv("CI_MAX_ITERATIONS_THRESHOLD", "0") // 0 for unlimited
bookstoreServiceName = utils.GetEnv("BOOKSTORE_SVC", "bookstore")
bookstoreNamespace = os.Getenv(BookstoreNamespaceEnvVar)
warehouseServiceName = "bookwarehouse"
bookwarehouseNamespace = os.Getenv(BookwarehouseNamespaceEnvVar)
Expand Down Expand Up @@ -93,15 +94,6 @@ func RestockBooks(amount int) {
log.Info().Msgf("RestockBooks (%s) finished w/ status: %s %d ", chargeAccountURL, resp.Status, resp.StatusCode)
}

// GetEnv is much like os.Getenv() but with a default value.
func GetEnv(envVar string, defaultValue string) string {
val := os.Getenv(envVar)
if val == "" {
return defaultValue
}
return val
}

// GetBooks reaches out to the bookstore and buys/steals books. This is invoked by the bookbuyer and the bookthief.
func GetBooks(participantName string, meshExpectedResponseCode int, booksCount *int64, booksCountV1 *int64, booksCountV2 *int64) {
minSuccessThreshold, maxIterations, sleepDurationBetweenRequests := getEnvVars(participantName)
Expand Down Expand Up @@ -285,7 +277,7 @@ func getEnvVars(participantName string) (minSuccessThreshold int64, maxIteration

// GetExpectedResponseCodeFromEnvVar returns the expected response code based on the given environment variable
func GetExpectedResponseCodeFromEnvVar(envVar, defaultValue string) int {
expectedRespCodeStr := GetEnv(envVar, defaultValue)
expectedRespCodeStr := utils.GetEnv(envVar, defaultValue)
expectedRespCode, err := strconv.ParseInt(expectedRespCodeStr, 10, 0)
if err != nil {
log.Fatal().Err(err).Msgf("Could not convert environment variable %s='%s' to int", envVar, expectedRespCodeStr)
Expand Down
13 changes: 13 additions & 0 deletions pkg/utils/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import "os"

// GetEnv is a convenience wrapper for os.Getenv() with additional default value return
// when empty or unset
func GetEnv(envVar string, defaultValue string) string {
val := os.Getenv(envVar)
if val == "" {
return defaultValue
}
return val
}
32 changes: 32 additions & 0 deletions pkg/utils/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetEnv(t *testing.T) {
const (
EnvVarName = "TEST_VAR"
EnvVarValue = "test_value"
EnvVarDefaultValue = "default_value"
)

assert := assert.New(t)

// make sure the variable is unset before starting the test
assert.NoError(os.Unsetenv(EnvVarName))

// Expect Default when not set
assert.Equal(EnvVarDefaultValue, GetEnv(EnvVarName, EnvVarDefaultValue))

// Set it, expect actual value when set
assert.NoError(os.Setenv(EnvVarName, EnvVarValue))
assert.Equal(EnvVarValue, GetEnv(EnvVarName, EnvVarDefaultValue))

// Unset it, expect default again
assert.NoError(os.Unsetenv(EnvVarName))
assert.Equal(EnvVarDefaultValue, GetEnv(EnvVarName, EnvVarDefaultValue))
}
20 changes: 5 additions & 15 deletions tests/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

. "github.com/onsi/ginkgo"
"github.com/openservicemesh/osm/pkg/utils"

"github.com/docker/docker/client"
"github.com/pkg/errors"
Expand All @@ -36,6 +37,8 @@ const (
registrySecretName = "acr-creds"
// constant, default name for the mesh
defaultMeshName = "osm-system"
// default image tag
defaultImageTag = "latest"
)

// OsmTestData stores common state, variables and flags for the test at hand
Expand Down Expand Up @@ -85,21 +88,8 @@ func registerFlags(td *OsmTestData) {
flag.StringVar(&td.ctrRegistryUser, "ctrRegistryUser", os.Getenv("CTR_REGISTRY_USER"), "Container registry")
flag.StringVar(&td.ctrRegistryPassword, "ctrRegistrySecret", os.Getenv("CTR_REGISTRY_PASSWORD"), "Container registry secret")

flag.StringVar(&td.osmImageTag, "osmImageTag", func() string {
tmp := os.Getenv("CTR_TAG")
if len(tmp) != 0 {
return tmp
}
return "latest"
}(), "OSM image tag")

flag.StringVar(&td.osmNamespace, "meshName", func() string {
tmp := os.Getenv("K8S_NAMESPACE")
if len(tmp) != 0 {
return tmp
}
return defaultMeshName
}(), "OSM mesh name")
flag.StringVar(&td.osmImageTag, "osmImageTag", utils.GetEnv("CTR_TAG", defaultImageTag), "OSM image tag")
flag.StringVar(&td.osmNamespace, "osmNamespace", utils.GetEnv("K8S_NAMESPACE", defaultMeshName), "OSM mesh name")
}

// AreRegistryCredsPresent checks if Registry Credentials are present
Expand Down

0 comments on commit 321febf

Please sign in to comment.