From 3ed7181f8e8cc71e3baa26baf0b4a8b5b580310d Mon Sep 17 00:00:00 2001 From: Martin Mulholland <71444914+mmulholla@users.noreply.github.com> Date: Wed, 24 Mar 2021 16:44:02 -0400 Subject: [PATCH] add tests for events (#368) --- test/v200/apiTest/api_test.go | 8 ++ test/v200/utils/common/command_test_utils.go | 13 +++ test/v200/utils/common/events-utils.go | 89 ++++++++++++++++++++ test/v200/utils/common/test_utils.go | 5 ++ 4 files changed, 115 insertions(+) create mode 100644 test/v200/utils/common/events-utils.go diff --git a/test/v200/apiTest/api_test.go b/test/v200/apiTest/api_test.go index a39126a3f..ae1980934 100644 --- a/test/v200/apiTest/api_test.go +++ b/test/v200/apiTest/api_test.go @@ -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{ @@ -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) } diff --git a/test/v200/utils/common/command_test_utils.go b/test/v200/utils/common/command_test_utils.go index 70d9fbdca..6918ce2ac 100644 --- a/test/v200/utils/common/command_test_utils.go +++ b/test/v200/utils/common/command_test_utils.go @@ -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 { diff --git a/test/v200/utils/common/events-utils.go b/test/v200/utils/common/events-utils.go new file mode 100644 index 000000000..54506ffb3 --- /dev/null +++ b/test/v200/utils/common/events-utils.go @@ -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) +} diff --git a/test/v200/utils/common/test_utils.go b/test/v200/utils/common/test_utils.go index ba8c2ce01..887d18c6d 100644 --- a/test/v200/utils/common/test_utils.go +++ b/test/v200/utils/common/test_utils.go @@ -69,6 +69,7 @@ type TestContent struct { ComponentTypes []schema.ComponentType ProjectTypes []schema.ProjectSourceType StarterProjectTypes []schema.ProjectSourceType + AddEvents bool FileName string EditContent bool } @@ -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)))