Skip to content

Commit 3ed7181

Browse files
authored
add tests for events (#368)
1 parent bfc3f50 commit 3ed7181

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

test/v200/apiTest/api_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ func Test_StarterProjects(t *testing.T) {
8484
apiUtils.RunTest(testContent, t)
8585
}
8686

87+
func Test_Events(t *testing.T) {
88+
testContent := commonUtils.TestContent{}
89+
testContent.AddEvents = true
90+
testContent.FileName = commonUtils.GetDevFileName()
91+
apiUtils.RunTest(testContent, t)
92+
}
93+
8794
func Test_Everything(t *testing.T) {
8895
testContent := commonUtils.TestContent{}
8996
testContent.CommandTypes = []schema.CommandType{
@@ -99,6 +106,7 @@ func Test_Everything(t *testing.T) {
99106
testContent.StarterProjectTypes = []schema.ProjectSourceType{
100107
schema.GitProjectSourceType,
101108
schema.ZipProjectSourceType}
109+
testContent.AddEvents = true
102110
testContent.FileName = commonUtils.GetDevFileName()
103111
apiUtils.RunTest(testContent, t)
104112
}

test/v200/utils/common/command_test_utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ func (testDevFile *TestDevfile) SetCompositeCommandValues(command *schema.Comman
182182
testDevFile.commandUpdated(*command)
183183
}
184184

185+
// SetCompositeCommandCommands set the commands in a composite command to a specific type
186+
func (testDevFile *TestDevfile) SetCompositeCommandCommands(command *schema.Command, commandType schema.CommandType) {
187+
compositeCommand := command.Composite
188+
compositeCommand.Commands = nil
189+
numCommands := GetRandomNumber(1, 3)
190+
for i := 0; i < numCommands; i++ {
191+
command := testDevFile.AddCommand(commandType)
192+
compositeCommand.Commands = append(compositeCommand.Commands, command.Id)
193+
LogInfoMessage(fmt.Sprintf("....... command %d of %d : %s", i, numCommands, command.Id))
194+
}
195+
testDevFile.commandUpdated(*command)
196+
}
197+
185198
// createApplyCommand creates an apply command in a schema structure
186199
func (testDevFile *TestDevfile) createApplyCommand() *schema.Command {
187200

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package common
2+
3+
import (
4+
"fmt"
5+
6+
schema "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
7+
)
8+
9+
// EventsAdded adds event to the test schema and notifies a registered follower
10+
func (devfile *TestDevfile) EventsAdded(events *schema.Events) {
11+
LogInfoMessage(fmt.Sprintf("events added"))
12+
devfile.SchemaDevFile.Events = events
13+
if devfile.Follower != nil {
14+
devfile.Follower.AddEvent(*events)
15+
}
16+
}
17+
18+
// EventsUpdated notifies a registered follower that the events have been updated
19+
func (devfile *TestDevfile) EventsUpdated(events *schema.Events) {
20+
LogInfoMessage(fmt.Sprintf("events updated"))
21+
if devfile.Follower != nil {
22+
devfile.Follower.UpdateEvent(*events)
23+
}
24+
}
25+
26+
// AddEvents adds events in the test schema structure and populates it with random attributes
27+
func (devfile *TestDevfile) AddEvents() schema.Events {
28+
events := schema.Events{}
29+
devfile.EventsAdded(&events)
30+
devfile.SetEventsValues(&events)
31+
return events
32+
}
33+
34+
// SetEventsValues randomly adds/modifies attributes of the supplied events
35+
func (devfile *TestDevfile) SetEventsValues(events *schema.Events) {
36+
if GetRandomDecision(4, 1) {
37+
numPreStart := GetRandomNumber(1, 5)
38+
LogInfoMessage(fmt.Sprintf(" ....... add %d command(s) to PreStart event", numPreStart))
39+
for i := 0; i < numPreStart; i++ {
40+
if GetRandomDecision(4, 1) {
41+
events.PreStart = append(events.PreStart, devfile.AddCommand(schema.ApplyCommandType).Id)
42+
} else {
43+
compositeCommand := devfile.AddCommand(schema.CompositeCommandType)
44+
devfile.SetCompositeCommandCommands(&compositeCommand, schema.ApplyCommandType)
45+
events.PreStart = append(events.PreStart, compositeCommand.Id)
46+
}
47+
}
48+
}
49+
if GetRandomDecision(4, 1) {
50+
numPostStart := GetRandomNumber(1, 5)
51+
LogInfoMessage(fmt.Sprintf(" ....... add %d command(s) to PostStart event", numPostStart))
52+
for i := 0; i < numPostStart; i++ {
53+
if GetRandomDecision(4, 1) {
54+
events.PostStart = append(events.PostStart, devfile.AddCommand(schema.ExecCommandType).Id)
55+
} else {
56+
compositeCommand := devfile.AddCommand(schema.CompositeCommandType)
57+
devfile.SetCompositeCommandCommands(&compositeCommand, schema.ExecCommandType)
58+
events.PostStart = append(events.PostStart, compositeCommand.Id)
59+
}
60+
}
61+
}
62+
if GetRandomDecision(4, 1) {
63+
numPreStop := GetRandomNumber(1, 5)
64+
LogInfoMessage(fmt.Sprintf(" ....... add %d command(s) to PreStop event", numPreStop))
65+
for i := 0; i < numPreStop; i++ {
66+
if GetRandomDecision(4, 1) {
67+
events.PreStop = append(events.PreStop, devfile.AddCommand(schema.ExecCommandType).Id)
68+
} else {
69+
compositeCommand := devfile.AddCommand(schema.CompositeCommandType)
70+
devfile.SetCompositeCommandCommands(&compositeCommand, schema.ExecCommandType)
71+
events.PreStop = append(events.PreStop, compositeCommand.Id)
72+
}
73+
}
74+
}
75+
if GetRandomDecision(4, 1) {
76+
numPostStop := GetRandomNumber(1, 5)
77+
LogInfoMessage(fmt.Sprintf(" ....... add %d command(s) to PostStop event", numPostStop))
78+
for i := 0; i < numPostStop; i++ {
79+
if GetRandomDecision(4, 1) {
80+
events.PostStop = append(events.PostStop, devfile.AddCommand(schema.ApplyCommandType).Id)
81+
} else {
82+
compositeCommand := devfile.AddCommand(schema.CompositeCommandType)
83+
devfile.SetCompositeCommandCommands(&compositeCommand, schema.ApplyCommandType)
84+
events.PostStop = append(events.PostStop, compositeCommand.Id)
85+
}
86+
}
87+
}
88+
devfile.EventsUpdated(events)
89+
}

test/v200/utils/common/test_utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type TestContent struct {
6969
ComponentTypes []schema.ComponentType
7070
ProjectTypes []schema.ProjectSourceType
7171
StarterProjectTypes []schema.ProjectSourceType
72+
AddEvents bool
7273
FileName string
7374
EditContent bool
7475
}
@@ -296,6 +297,10 @@ func (testDevfile *TestDevfile) RunTest(testContent TestContent, t *testing.T) {
296297
}
297298
}
298299

300+
if testContent.AddEvents {
301+
testDevfile.AddEvents()
302+
}
303+
299304
err := testDevfile.Validator.WriteAndValidate(testDevfile)
300305
if err != nil {
301306
t.Fatalf(LogErrorMessage(fmt.Sprintf("ERROR verifying devfile : %s : %v", testContent.FileName, err)))

0 commit comments

Comments
 (0)