Skip to content

Commit

Permalink
e2e: tray handler helpers refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianriobo authored and anjannath committed Oct 4, 2021
1 parent 81d14bf commit 6b0278e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 70 deletions.
8 changes: 2 additions & 6 deletions test/extended/crc/ux/tray/const.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !windows

package tray

const (
Expand All @@ -15,9 +13,7 @@ const (

userKubeadmin string = "kubeadmin"
userDeveloper string = "developer"
)

const (
uxCheckAccessibilityDuration = "2s"
uxCheckAccessibilityRetry = 10
trayClusterStateRetries int = 15
trayClusterStateTimeout int = 90
)
13 changes: 10 additions & 3 deletions test/extended/crc/ux/tray/tray.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package tray

import (
"fmt"
)

type Tray interface {
Install() error
IsInstalled() error
Expand All @@ -18,7 +22,10 @@ type Tray interface {
ConnectClusterAsDeveloper() error
}

type Element struct {
Name string
AXIdentifier string
func getElement(name string, elements map[string]string) (string, error) {
identifier, ok := elements[name]
if ok {
return identifier, nil
}
return "", fmt.Errorf("element '%s', Can not be accessed from the tray", name)
}
80 changes: 19 additions & 61 deletions test/extended/crc/ux/tray/tray_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,27 @@ const (
bundleIdentifier string = "com.redhat.codeready.containers"
appPath string = "/Applications/CodeReady Containers.app"

trayClusterStateTimeout = "90"
uxCheckAccessibilityDuration = "2s"
uxCheckAccessibilityRetry = 10
)

var (
elements = []Element{
{
Name: actionStart,
AXIdentifier: "start"},
{
Name: actionStop,
AXIdentifier: "stop"},
{
Name: actionDelete,
AXIdentifier: "delete"},
{
Name: actionQuit,
AXIdentifier: "quit"},
{
Name: fieldState,
AXIdentifier: "cluster_status"},
{
Name: userKubeadmin,
AXIdentifier: "kubeadmin_login"},
{
Name: userDeveloper,
AXIdentifier: "developer_login"},
}
elements = map[string]string{
actionStart: "start",
actionStop: "stop",
actionDelete: "delete",
actionQuit: "quit",
fieldState: "cluster_status",
userKubeadmin: "kubeadmin_login",
userDeveloper: "developer_login"}
)

type applescriptHandler struct {
bundleLocation *string
pullSecretFileLocation *string
}

func NewTray(bundleLocationValue *string, pullSecretFileLocationValue *string) Tray {
func NewTray(bundleLocationValue, pullSecretFileLocationValue *string) Tray {
if runtime.GOOS == "darwin" {
return applescriptHandler{
bundleLocation: bundleLocationValue,
Expand Down Expand Up @@ -117,11 +103,13 @@ func (a applescriptHandler) SetPullSecret() error {
}

func (a applescriptHandler) IsClusterRunning() error {
return waitTrayShowsFieldWithValue(fieldState, stateRunning)
return util.MatchWithRetry(stateRunning, checkTrayShowsStatusValue,
trayClusterStateRetries, trayClusterStateTimeout)
}

func (a applescriptHandler) IsClusterStopped() error {
return waitTrayShowsFieldWithValue(fieldState, stateStopped)
return util.MatchWithRetry(stateStopped, checkTrayShowsStatusValue,
trayClusterStateRetries, trayClusterStateTimeout)
}

func (a applescriptHandler) CopyOCLoginCommandAsKubeadmin() error {
Expand Down Expand Up @@ -156,46 +144,16 @@ func clickOnElement(elementName string, scriptName string) error {
return err
}
return applescript.ExecuteApplescript(
scriptName, bundleIdentifier, element.AXIdentifier)
scriptName, bundleIdentifier, element)
}

func waitTrayShowsFieldWithValue(field string, expectedValue string) error {
retryCount := 15
iterationDuration, extraDuration, err :=
util.GetRetryParametersFromTimeoutInSeconds(retryCount, trayClusterStateTimeout)
if err != nil {
return err
}
for i := 0; i < retryCount; i++ {
err := checkTrayShowsFieldWithValue(field, expectedValue)
if err == nil {
return nil
}
time.Sleep(iterationDuration)
}
if extraDuration != 0 {
time.Sleep(extraDuration)
}
return fmt.Errorf("Tray did not showed %s ", expectedValue)
}

func checkTrayShowsFieldWithValue(field string, expectedValue string) error {
element, err := getElement(field, elements)
func checkTrayShowsStatusValue(expectedValue string) error {
element, err := getElement(fieldState, elements)
if err != nil {
return err
}
return applescript.ExecuteApplescriptReturnShouldMatch(
expectedValue, getTrayFieldlValue, bundleIdentifier, element.AXIdentifier)
}

func getElement(name string, elements []Element) (Element, error) {
for _, e := range elements {
if name == e.Name {
return e, nil
}
}
return Element{},
fmt.Errorf("element '%s', Can not be accessed from the tray", name)
expectedValue, getTrayFieldlValue, bundleIdentifier, element)
}

func checkAccessible(uxIsAccessible func() error, component string) error {
Expand Down

0 comments on commit 6b0278e

Please sign in to comment.