Skip to content

Commit

Permalink
add tests for events (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmulholla authored Mar 24, 2021
1 parent bfc3f50 commit 3ed7181
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/v200/apiTest/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func Test_StarterProjects(t *testing.T) {
apiUtils.RunTest(testContent, t)
}

func Test_Events(t *testing.T) {
testContent := commonUtils.TestContent{}
testContent.AddEvents = true
testContent.FileName = commonUtils.GetDevFileName()
apiUtils.RunTest(testContent, t)
}

func Test_Everything(t *testing.T) {
testContent := commonUtils.TestContent{}
testContent.CommandTypes = []schema.CommandType{
Expand All @@ -99,6 +106,7 @@ func Test_Everything(t *testing.T) {
testContent.StarterProjectTypes = []schema.ProjectSourceType{
schema.GitProjectSourceType,
schema.ZipProjectSourceType}
testContent.AddEvents = true
testContent.FileName = commonUtils.GetDevFileName()
apiUtils.RunTest(testContent, t)
}
13 changes: 13 additions & 0 deletions test/v200/utils/common/command_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ func (testDevFile *TestDevfile) SetCompositeCommandValues(command *schema.Comman
testDevFile.commandUpdated(*command)
}

// SetCompositeCommandCommands set the commands in a composite command to a specific type
func (testDevFile *TestDevfile) SetCompositeCommandCommands(command *schema.Command, commandType schema.CommandType) {
compositeCommand := command.Composite
compositeCommand.Commands = nil
numCommands := GetRandomNumber(1, 3)
for i := 0; i < numCommands; i++ {
command := testDevFile.AddCommand(commandType)
compositeCommand.Commands = append(compositeCommand.Commands, command.Id)
LogInfoMessage(fmt.Sprintf("....... command %d of %d : %s", i, numCommands, command.Id))
}
testDevFile.commandUpdated(*command)
}

// createApplyCommand creates an apply command in a schema structure
func (testDevFile *TestDevfile) createApplyCommand() *schema.Command {

Expand Down
89 changes: 89 additions & 0 deletions test/v200/utils/common/events-utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package common

import (
"fmt"

schema "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
)

// EventsAdded adds event to the test schema and notifies a registered follower
func (devfile *TestDevfile) EventsAdded(events *schema.Events) {
LogInfoMessage(fmt.Sprintf("events added"))
devfile.SchemaDevFile.Events = events
if devfile.Follower != nil {
devfile.Follower.AddEvent(*events)
}
}

// EventsUpdated notifies a registered follower that the events have been updated
func (devfile *TestDevfile) EventsUpdated(events *schema.Events) {
LogInfoMessage(fmt.Sprintf("events updated"))
if devfile.Follower != nil {
devfile.Follower.UpdateEvent(*events)
}
}

// AddEvents adds events in the test schema structure and populates it with random attributes
func (devfile *TestDevfile) AddEvents() schema.Events {
events := schema.Events{}
devfile.EventsAdded(&events)
devfile.SetEventsValues(&events)
return events
}

// SetEventsValues randomly adds/modifies attributes of the supplied events
func (devfile *TestDevfile) SetEventsValues(events *schema.Events) {
if GetRandomDecision(4, 1) {
numPreStart := GetRandomNumber(1, 5)
LogInfoMessage(fmt.Sprintf(" ....... add %d command(s) to PreStart event", numPreStart))
for i := 0; i < numPreStart; i++ {
if GetRandomDecision(4, 1) {
events.PreStart = append(events.PreStart, devfile.AddCommand(schema.ApplyCommandType).Id)
} else {
compositeCommand := devfile.AddCommand(schema.CompositeCommandType)
devfile.SetCompositeCommandCommands(&compositeCommand, schema.ApplyCommandType)
events.PreStart = append(events.PreStart, compositeCommand.Id)
}
}
}
if GetRandomDecision(4, 1) {
numPostStart := GetRandomNumber(1, 5)
LogInfoMessage(fmt.Sprintf(" ....... add %d command(s) to PostStart event", numPostStart))
for i := 0; i < numPostStart; i++ {
if GetRandomDecision(4, 1) {
events.PostStart = append(events.PostStart, devfile.AddCommand(schema.ExecCommandType).Id)
} else {
compositeCommand := devfile.AddCommand(schema.CompositeCommandType)
devfile.SetCompositeCommandCommands(&compositeCommand, schema.ExecCommandType)
events.PostStart = append(events.PostStart, compositeCommand.Id)
}
}
}
if GetRandomDecision(4, 1) {
numPreStop := GetRandomNumber(1, 5)
LogInfoMessage(fmt.Sprintf(" ....... add %d command(s) to PreStop event", numPreStop))
for i := 0; i < numPreStop; i++ {
if GetRandomDecision(4, 1) {
events.PreStop = append(events.PreStop, devfile.AddCommand(schema.ExecCommandType).Id)
} else {
compositeCommand := devfile.AddCommand(schema.CompositeCommandType)
devfile.SetCompositeCommandCommands(&compositeCommand, schema.ExecCommandType)
events.PreStop = append(events.PreStop, compositeCommand.Id)
}
}
}
if GetRandomDecision(4, 1) {
numPostStop := GetRandomNumber(1, 5)
LogInfoMessage(fmt.Sprintf(" ....... add %d command(s) to PostStop event", numPostStop))
for i := 0; i < numPostStop; i++ {
if GetRandomDecision(4, 1) {
events.PostStop = append(events.PostStop, devfile.AddCommand(schema.ApplyCommandType).Id)
} else {
compositeCommand := devfile.AddCommand(schema.CompositeCommandType)
devfile.SetCompositeCommandCommands(&compositeCommand, schema.ApplyCommandType)
events.PostStop = append(events.PostStop, compositeCommand.Id)
}
}
}
devfile.EventsUpdated(events)
}
5 changes: 5 additions & 0 deletions test/v200/utils/common/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type TestContent struct {
ComponentTypes []schema.ComponentType
ProjectTypes []schema.ProjectSourceType
StarterProjectTypes []schema.ProjectSourceType
AddEvents bool
FileName string
EditContent bool
}
Expand Down Expand Up @@ -296,6 +297,10 @@ func (testDevfile *TestDevfile) RunTest(testContent TestContent, t *testing.T) {
}
}

if testContent.AddEvents {
testDevfile.AddEvents()
}

err := testDevfile.Validator.WriteAndValidate(testDevfile)
if err != nil {
t.Fatalf(LogErrorMessage(fmt.Sprintf("ERROR verifying devfile : %s : %v", testContent.FileName, err)))
Expand Down

0 comments on commit 3ed7181

Please sign in to comment.