Skip to content

Commit

Permalink
e2e: refactor around CRCHome variable
Browse files Browse the repository at this point in the history
  • Loading branch information
jsliacan authored and anjannath committed Mar 1, 2023
1 parent 3326fdc commit 836238b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
21 changes: 8 additions & 13 deletions test/e2e/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
)

var (
CRCHome string
CRCExecutable string
userProvidedBundle bool
bundleName string
Expand Down Expand Up @@ -61,7 +60,7 @@ func InitializeTestSuite(tctx *godog.TestSuiteContext) {
}

usr, _ := user.Current()
CRCHome = filepath.Join(usr.HomeDir, ".crc")
util.CRCHome = filepath.Join(usr.HomeDir, ".crc")

// init CRCExecutable if no location provided by user
if CRCExecutable == "" {
Expand Down Expand Up @@ -108,7 +107,7 @@ func InitializeTestSuite(tctx *godog.TestSuiteContext) {

if cleanupHome {
// remove $HOME/.crc
err = util.RemoveCRCHome(CRCHome)
err = util.RemoveCRCHome()
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down Expand Up @@ -452,7 +451,7 @@ func InitializeScenario(s *godog.ScenarioContext) {

// CRC related steps
s.Step(`^removing CRC home directory succeeds$`,
RemoveCRCHome)
util.RemoveCRCHome)
s.Step(`^starting CRC with default bundle (succeeds|fails)$`,
StartCRCWithDefaultBundleSucceedsOrFails)
s.Step(`^starting CRC with custom bundle (succeeds|fails)$`,
Expand Down Expand Up @@ -538,10 +537,6 @@ func WaitForClusterInState(state string) error {
return crcCmd.WaitForClusterInState(state)
}

func RemoveCRCHome() error {
return util.RemoveCRCHome(CRCHome)
}

func CheckHTTPResponseWithRetry(retryCount int, retryWait string, address string, expectedStatusCode int) error {
var err error

Expand Down Expand Up @@ -610,7 +605,7 @@ func CheckCRCStatus(state string) error {

func DeleteFileFromCRCHome(fileName string) error {

theFile := filepath.Join(CRCHome, fileName)
theFile := filepath.Join(util.CRCHome, fileName)

if _, err := os.Stat(theFile); os.IsNotExist(err) {
return nil
Expand All @@ -624,7 +619,7 @@ func DeleteFileFromCRCHome(fileName string) error {

func FileExistsInCRCHome(fileName string) error {

theFile := filepath.Join(CRCHome, fileName)
theFile := filepath.Join(util.CRCHome, fileName)

_, err := os.Stat(theFile)
if os.IsNotExist(err) {
Expand Down Expand Up @@ -668,7 +663,7 @@ func ConfigFileInCRCHomeContainsKeyMatchingValue(format string, configFile strin
if expectedValue == "current bundle" {
expectedValue = fmt.Sprintf(".*%s", bundleName)
}
configPath := filepath.Join(CRCHome, configFile)
configPath := filepath.Join(util.CRCHome, configFile)

config, err := util.GetFileContent(configPath)
if err != nil {
Expand All @@ -694,7 +689,7 @@ func ConfigFileInCRCHomeContainsKeyMatchingValue(format string, configFile strin

func ConfigFileInCRCHomeContainsKey(format string, configFile string, condition string, keyPath string) error {

configPath := filepath.Join(CRCHome, configFile)
configPath := filepath.Join(util.CRCHome, configFile)

config, err := util.GetFileContent(configPath)
if err != nil {
Expand Down Expand Up @@ -854,7 +849,7 @@ func EnsureUserIsLoggedIntoClusterSucceedsOrFails(expected string) error {
func SetConfigPropertyToValueSucceedsOrFails(property string, value string, expected string) error {
if value == "current bundle" {
if !userProvidedBundle {
value = filepath.Join(CRCHome, "cache", bundleName)
value = filepath.Join(util.CRCHome, "cache", bundleName)
} else {
value = bundleLocation
}
Expand Down
23 changes: 11 additions & 12 deletions test/extended/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
Expand All @@ -15,6 +14,10 @@ import (
"github.com/crc-org/crc/pkg/download"
)

var (
CRCHome string
)

func CopyFilesToTestDir() error {
cwd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -127,23 +130,23 @@ func DownloadBundle(bundleLocation string, bundleDestination string, bundleName
return filename, nil
}

func RemoveCRCHome(crcHome string) error {
keepFile := filepath.Join(crcHome, ".keep")
func RemoveCRCHome() error {
keepFile := filepath.Join(CRCHome, ".keep")
_, err := os.Stat(keepFile)
if err != nil { // cannot get keepFile's status
err = os.RemoveAll(crcHome)
err = os.RemoveAll(CRCHome)

if err != nil {
fmt.Printf("Problem deleting CRC home folder %s.\n", crcHome)
fmt.Printf("Problem deleting CRC home folder %s.\n", CRCHome)
return err
}

fmt.Printf("Deleted CRC home folder %s.\n", crcHome)
fmt.Printf("Deleted CRC home folder %s.\n", CRCHome)
return nil

}
// keepFile exists
return fmt.Errorf("folder %s not removed as per request: %s present", crcHome, keepFile)
return fmt.Errorf("folder %s not removed as per request: %s present", CRCHome, keepFile)
}

// MatchWithRetry will execute match function with expression as arg
Expand Down Expand Up @@ -180,11 +183,7 @@ func MatchRepetitionsWithRetry(expression string, match func(string) error, matc

// GetBundlePath returns a path to the cached bundle, depending on the preset
func GetBundlePath(preset preset.Preset) string {

usr, _ := user.Current()
crcHome := filepath.Join(usr.HomeDir, ".crc")

bundle := constants.GetDefaultBundle(preset)
return filepath.Join(crcHome, "cache", bundle)
return filepath.Join(CRCHome, "cache", bundle)

}

0 comments on commit 836238b

Please sign in to comment.