Skip to content

Commit

Permalink
Refactor environment variables (#21)
Browse files Browse the repository at this point in the history
* Refactor env var, create a package

* Reorder functions
  • Loading branch information
farshidtz authored Feb 29, 2024
1 parent a81b13d commit ecc755f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 90 deletions.
65 changes: 65 additions & 0 deletions env/env.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
40 changes: 0 additions & 40 deletions utils/env.go

This file was deleted.

9 changes: 5 additions & 4 deletions utils/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/canonical/matter-snap-testing/env"
"github.com/stretchr/testify/require"
)

Expand All @@ -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
Expand All @@ -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)

Expand Down
42 changes: 0 additions & 42 deletions utils/setup.go

This file was deleted.

4 changes: 0 additions & 4 deletions utils/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,3 @@ func SnapServicesActive(t *testing.T, name string) bool {
))
return strings.TrimSpace(out) == "active"
}

func LocalServiceSnap() bool {
return LocalServiceSnapPath != ""
}

0 comments on commit ecc755f

Please sign in to comment.