From ecc755f791f164d20f01b2d3976c72787dd08c73 Mon Sep 17 00:00:00 2001 From: Farshid Tavakolizadeh Date: Thu, 29 Feb 2024 13:57:48 +0200 Subject: [PATCH] Refactor environment variables (#21) * Refactor env var, create a package * Reorder functions --- env/env.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ utils/env.go | 40 ----------------------------- utils/refresh.go | 9 ++++--- utils/setup.go | 42 ------------------------------- utils/snap.go | 4 --- 5 files changed, 70 insertions(+), 90 deletions(-) create mode 100644 env/env.go delete mode 100644 utils/env.go delete mode 100644 utils/setup.go diff --git a/env/env.go b/env/env.go new file mode 100644 index 0000000..14f9d69 --- /dev/null +++ b/env/env.go @@ -0,0 +1,65 @@ +package env + +import ( + "os" + "strconv" +) + +// Environment variables, used to override defaults +const ( + // Channel/Revision of the service snap (has default) + envSnapChannel = "SNAP_CHANNEL" + + // Path to snap instead, used for testing a local snap instead of + // downloading from the store + envSnapPath = "SNAP_PATH" + + // Toggle the teardown operations during tests (has default) + envTeardown = "TEARDOWN" +) + +var ( + // Defaults + snapChannel = "latest/edge" + snapPath = "" + teardown = true +) + +// SnapChannel returns the set snap channel +func SnapChannel() string { + return snapChannel +} + +// SnapPath returns the set path to a local snap +func SnapPath() string { + return snapPath +} + +// SkipTeardownRemoval return +func Teardown() (skip bool) { + return teardown +} + +func init() { + loadEnvVars() +} + +// Read environment variables and perform type conversion/casting +func loadEnvVars() { + + if v := os.Getenv(envSnapChannel); v != "" { + snapChannel = v + } + + if v := os.Getenv(envSnapPath); v != "" { + snapPath = v + } + + if v := os.Getenv(envTeardown); v != "" { + var err error + teardown, err = strconv.ParseBool(v) + if err != nil { + panic(err) + } + } +} diff --git a/utils/env.go b/utils/env.go deleted file mode 100644 index 9ad2c45..0000000 --- a/utils/env.go +++ /dev/null @@ -1,40 +0,0 @@ -package utils - -import ( - "os" - "strconv" -) - -const ( - // environment variables - // used to override defaults - serviceChannelEnv = "SERVICE_CHANNEL" // channel/revision of the service snap (has default) - localServiceSnapEnv = "LOCAL_SERVICE_SNAP" // path to local service snap to be tested instead of downloading from a channel - - skipTeardownRemovalEnv = "SKIP_TEARDOWN_REMOVAL" // skip the removal of snaps during teardown -) - -var ( - // global defaults - ServiceChannel = "latest/edge" - LocalServiceSnapPath = "" - SkipTeardownRemoval = false -) - -func init() { - if v := os.Getenv(serviceChannelEnv); v != "" { - ServiceChannel = v - } - - if v := os.Getenv(localServiceSnapEnv); v != "" { - LocalServiceSnapPath = v - } - - if v := os.Getenv(skipTeardownRemovalEnv); v != "" { - var err error - SkipTeardownRemoval, err = strconv.ParseBool(v) - if err != nil { - panic(err) - } - } -} diff --git a/utils/refresh.go b/utils/refresh.go index 1cd5827..7e7ce0e 100644 --- a/utils/refresh.go +++ b/utils/refresh.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/canonical/matter-snap-testing/env" "github.com/stretchr/testify/require" ) @@ -13,8 +14,8 @@ func TestRefresh(t *testing.T, snapName string) { const stableChannel = "latest/stable" - if ServiceChannel == stableChannel { - t.Skipf("Skip refresh on same channel: %s", ServiceChannel) + if env.SnapChannel() == stableChannel { + t.Skipf("Skip refresh on same channel: %s", env.SnapChannel()) } // remove and install the older stable revision @@ -25,14 +26,14 @@ func TestRefresh(t *testing.T, snapName string) { t.Cleanup(func() { SnapRemove(t, snapName) - SnapInstallFromStore(t, snapName, ServiceChannel) + SnapInstallFromStore(t, snapName, env.SnapChannel()) }) originalVersion := SnapVersion(t, snapName) originalRevision := SnapRevision(t, snapName) t.Run("check services", func(t *testing.T) { - SnapRefresh(t, snapName, ServiceChannel) + SnapRefresh(t, snapName, env.SnapChannel()) refreshVersion := SnapVersion(t, snapName) refreshRevision = SnapRevision(t, snapName) diff --git a/utils/setup.go b/utils/setup.go deleted file mode 100644 index 097856c..0000000 --- a/utils/setup.go +++ /dev/null @@ -1,42 +0,0 @@ -package utils - -import ( - "log" - "time" -) - -// SetupServiceTests setup up the environment for testing -// It returns a teardown function to be called at the end of the tests -func SetupServiceTests(snapName string) (teardown func(), err error) { - log.Println("[CLEAN]") - SnapRemove(nil, - snapName, - ) - - log.Println("[SETUP]") - start := time.Now() - - teardown = func() { - log.Println("[TEARDOWN]") - SnapDumpLogs(nil, start, snapName) - - log.Println("Removing installed snap:", !SkipTeardownRemoval) - if !SkipTeardownRemoval { - SnapRemove(nil, - snapName, - ) - } - } - - if LocalServiceSnap() { - err = SnapInstallFromFile(nil, LocalServiceSnapPath) - } else { - err = SnapInstallFromStore(nil, snapName, ServiceChannel) - } - if err != nil { - teardown() - return - } - - return -} diff --git a/utils/snap.go b/utils/snap.go index 66211a5..f5ee62c 100644 --- a/utils/snap.go +++ b/utils/snap.go @@ -209,7 +209,3 @@ func SnapServicesActive(t *testing.T, name string) bool { )) return strings.TrimSpace(out) == "active" } - -func LocalServiceSnap() bool { - return LocalServiceSnapPath != "" -}