Run generates binding contexts for hook tests
(initialState string)
| 92 | |
| 93 | // Run generates binding contexts for hook tests |
| 94 | func (b *BindingContextController) Run(initialState string) (GeneratedBindingContexts, error) { |
| 95 | b.mu.Lock() |
| 96 | defer b.mu.Unlock() |
| 97 | if b.started.Load() { |
| 98 | return GeneratedBindingContexts{}, fmt.Errorf("attempt to start an already started runner, it cannot be started twice") |
| 99 | } |
| 100 | |
| 101 | err := b.Controller.SetInitialState(initialState) |
| 102 | if err != nil { |
| 103 | return GeneratedBindingContexts{}, err |
| 104 | } |
| 105 | |
| 106 | if b.Hook == nil { |
| 107 | testHook := hook.NewHook("test", "test", false, false, "", b.logger.Named("hook")) |
| 108 | testHook, err = testHook.LoadConfig([]byte(b.HookConfig)) |
| 109 | if err != nil { |
| 110 | return GeneratedBindingContexts{}, fmt.Errorf("couldn't load or validate hook configuration: %v", err) |
| 111 | } |
| 112 | b.Hook = testHook |
| 113 | } |
| 114 | |
| 115 | b.HookCtrl = controller.NewHookController() |
| 116 | b.HookCtrl.InitKubernetesBindings(b.Hook.GetConfig().OnKubernetesEvents, b.KubeEventsManager, b.logger.Named("kubernetes-bindings")) |
| 117 | b.HookCtrl.InitScheduleBindings(b.Hook.GetConfig().Schedules, b.ScheduleManager) |
| 118 | b.HookCtrl.EnableScheduleBindings() |
| 119 | |
| 120 | b.Hook.WithHookController(b.HookCtrl) |
| 121 | |
| 122 | cc := NewContextCombiner() |
| 123 | err = b.HookCtrl.HandleEnableKubernetesBindings(context.Background(), func(info controller.BindingExecutionInfo) { |
| 124 | if info.KubernetesBinding.ExecuteHookOnSynchronization { |
| 125 | cc.AddBindingContext(types.OnKubernetesEvent, info) |
| 126 | } |
| 127 | }) |
| 128 | if err != nil { |
| 129 | return GeneratedBindingContexts{}, fmt.Errorf("couldn't enable kubernetes bindings: %v", err) |
| 130 | } |
| 131 | |
| 132 | b.HookCtrl.UnlockKubernetesEvents() |
| 133 | b.started.Store(true) |
| 134 | |
| 135 | time.Sleep(50 * time.Millisecond) |
| 136 | return cc.CombinedAndUpdated(b.HookCtrl) |
| 137 | } |
| 138 | |
| 139 | func (b *BindingContextController) ChangeState(newState string) (GeneratedBindingContexts, error) { |
| 140 | b.mu.Lock() |