-
Notifications
You must be signed in to change notification settings - Fork 233
/
uxsuite.go
237 lines (218 loc) · 6.57 KB
/
uxsuite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package ux
import (
"flag"
"fmt"
"os"
"github.com/code-ready/crc/test/extended/crc/cmd"
"github.com/code-ready/crc/test/extended/crc/ux/installer"
"github.com/code-ready/crc/test/extended/crc/ux/notification"
"github.com/code-ready/crc/test/extended/crc/ux/tray"
"github.com/code-ready/crc/test/extended/util"
"github.com/cucumber/godog"
"github.com/cucumber/messages-go/v10"
)
var trayHandler tray.Tray
var installerHandler installer.Installer
var notificationHandler notification.Notification
var currentUserPassword string
var installerPath string
const (
clusterStateWaitTimeout int = 200
clusterStateWaitRetries int = 10
)
// FeatureContext defines godog.Suite steps for the test suite.
func FeatureContext(s *godog.Suite, bundleLocation *string, pullSecretFile *string) {
trayHandler = tray.NewTray(bundleLocation, pullSecretFile)
installerHandler = installer.NewInstaller(¤tUserPassword, &installerPath)
notificationHandler = notification.NewNotification()
if handlersAreInitialized() {
s.Step(`^install CRC tray$`,
trayHandler.Install)
s.Step(`^reboot is required$`,
installerHandler.RebootRequired)
s.Step(`^tray should be installed$`,
trayHandler.IsInstalled)
s.Step(`^tray icon should be accessible$`,
trayHandler.IsAccessible)
s.Step(`^fresh tray installation$`,
guaranteeFreshInstallation)
s.Step(`^(.*) the cluster from the tray$`,
clickTrayButton)
s.Step(`^cluster should be (.*)$`,
waitForClusterInState)
s.Step(`^set the pull secret$`,
trayHandler.SetPullSecret)
s.Step(`^tray should show cluster as (.*)$`,
checkClusterStateOnTray)
s.Step(`^(.*) CRC from installer$`,
executeActionFromInstaller)
s.Step(`^.*CRC (.*) installed$`,
crcInstalled)
s.Step(`^user should get notified with cluster state as (.*)$`,
getNotification)
s.Step(`^a (.*) cluster$`,
guaranteeClusterHasState)
s.Step(`^using copied oc login command for (.*)$`,
copyOCLoginCommandByUser)
s.Step(`^user is connected to the cluster as (.*)$`,
connectClusterAsUser)
s.BeforeScenario(func(*messages.Pickle) {
copyRequiredResources(tray.RequiredResourcesPath)
copyRequiredResources(installer.RequiredResourcesPath)
copyRequiredResources(notification.RequiredResourcesPath)
})
}
}
func ParseFlags() {
flag.StringVar(¤tUserPassword, "user-password", "",
"Current user password. User should be sudoer to handle the installation")
flag.StringVar(&installerPath, "installer-path", "",
"Full path for installer")
}
func handlersAreInitialized() bool {
return trayHandler != nil &&
installerHandler != nil &&
notificationHandler != nil
}
func copyRequiredResources(requiredResources func() (string, error)) {
requiredResourcesPath, err := requiredResources()
if err != nil {
os.Exit(1)
}
if requiredResourcesPath != "" {
err = util.CopyResourcesFromPath(requiredResourcesPath)
if err != nil {
os.Exit(1)
}
}
}
func guaranteeFreshInstallation() error {
err := trayHandler.IsInstalled()
if err != nil {
return err
}
err = trayHandler.IsAccessible()
if err != nil {
return err
}
err = trayHandler.IsClusterStopped()
if err != nil {
return err
}
err = cmd.CheckMachineNotExists()
if err != nil {
return err
}
return notificationHandler.ClearNotifications()
}
func guaranteeClusterHasState(state string) error {
switch state {
case "running", "stopped":
err := util.MatchWithRetry(state, clusterHasState,
clusterStateWaitRetries, clusterStateWaitTimeout)
if err != nil {
return err
}
default:
return fmt.Errorf("%s state is not defined as valid cluster state", state)
}
return notificationHandler.ClearNotifications()
}
func clusterHasState(expectedState string) error {
return cmd.CheckCRCStatus(expectedState)
}
func clickTrayButton(action string) error {
if trayHandler == nil {
return fmt.Errorf("tray handler should be initialized. Check if tray is supported on your OS")
}
switch action {
case "start":
return trayHandler.ClickStart()
case "stop":
return trayHandler.ClickStop()
case "delete":
return trayHandler.ClickDelete()
case "quit":
return trayHandler.ClickQuit()
default:
return fmt.Errorf("%s action can not be managed through the tray", action)
}
}
func checkClusterStateOnTray(state string) error {
if trayHandler == nil {
return fmt.Errorf("tray handler should be initialized. Check if tray is supported on your OS")
}
switch state {
case "running":
return trayHandler.IsClusterRunning()
case "stopped":
return trayHandler.IsClusterStopped()
default:
return fmt.Errorf("%s state is not defined as valid cluster state", state)
}
}
func executeActionFromInstaller(action string) error {
if installerHandler == nil {
return fmt.Errorf("installer handler should be initialized. Check if installer is supported on your OS")
}
switch action {
case "install":
return installerHandler.Install()
default:
return fmt.Errorf("%s action is not supported by the installer", action)
}
}
func waitForClusterInState(state string) error {
return cmd.WaitForClusterInState(state)
}
func crcInstalled(state string) error {
switch state {
case "is":
return cmd.CheckCRCExecutableState(cmd.CRCExecutableInstalled)
case "is not":
return cmd.CheckCRCExecutableState(cmd.CRCExecutableNotInstalled)
default:
return fmt.Errorf("%s state is not defined as valid CRC installation state", state)
}
}
func getNotification(action string) error {
if notificationHandler == nil {
return fmt.Errorf("notification handler should be initialized. Check if tray is supported on your OS")
}
switch action {
case "running":
return notificationHandler.GetClusterRunning()
case "stopped":
return notificationHandler.GetClusterStopped()
case "deleted":
return notificationHandler.GetClusterDeleted()
default:
return fmt.Errorf("%s action will not be notified", action)
}
}
func copyOCLoginCommandByUser(user string) error {
if trayHandler == nil {
return fmt.Errorf("tray handler should be initialized. Check if tray is supported on your OS")
}
switch user {
case "kubeadmin":
return trayHandler.CopyOCLoginCommandAsKubeadmin()
case "developer":
return trayHandler.CopyOCLoginCommandAsDeveloper()
default:
return fmt.Errorf("tray can not provide login command for user %s", user)
}
}
func connectClusterAsUser(user string) error {
if trayHandler == nil {
return fmt.Errorf("tray handler should be initialized. Check if tray is supported on your OS")
}
switch user {
case "kubeadmin":
return trayHandler.ConnectClusterAsKubeadmin()
case "developer":
return trayHandler.ConnectClusterAsDeveloper()
default:
return fmt.Errorf("can not connect to cluster as user %s", user)
}
}