|
| 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 | +} |
0 commit comments