func TestNewActions(t *testing.T) { a := NewActions(nil) assert.IsType(t, a, &ActionRepository{}) assert.Equal(t, a.EnabledActions, Actions()) assert.Equal(t, a, repository) ClearActions() assert.Nil(t, repository.EnabledActions) assert.Nil(t, Actions()) }
func TestBroadCast(t *testing.T) { l := listener.NewMockListener() e := NewEmitter(l) e.actions = make(map[string]actions.Action) msg := "message" actionNum := 10 var count uint64 var wg sync.WaitGroup // Prepare mock actions for i := 1; i <= actionNum; i++ { actionName := fmt.Sprintf("receiver_%d", i) a := actions.NewMockAction().(*actions.MockAction) a.SetName(actionName) e.actions[fmt.Sprintf("mock_%d", i)] = a } for i := 1; i <= actionNum; i++ { wg.Add(1) go func(j int) { defer wg.Done() select { case ev, ok := <-e.actions[fmt.Sprintf("mock_%d", j)].Ch(): if ok { assert.Equal(t, ev, msg, "received broadcasted message") atomic.AddUint64(&count, 1) } } }(i) } e.BroadCast() e.l.(*listener.MockListener).Ch <- msg close(e.l.Stopped()) wg.Wait() expected := strconv.Itoa(actionNum) actual := strconv.FormatUint(atomic.LoadUint64(&count), 10) assert.Equal(t, expected, actual, "All Actions received message") }
func TestApplyConfig(t *testing.T) { NewActions(nil) actionName := "mock" configKey := "mock-config-value" configValue := "value" addr := &ActionAddress{ NewFunc: NewMockAction, ConfigUnits: []*ConfigUnit{ &ConfigUnit{ Name: configKey, }, }, } addr.SetConfig(configKey, configValue) RegisterAction(addr) cfg := make(map[string]string) cfg[configKey] = configValue ApplyConfig(cfg) registered := repository.EnabledActions[actionName] configured := registered.(*MockAction).ConfigValue assert.Equal(t, configured, configValue) ClearActions() }
func TestSystemWithDockerEvents(t *testing.T) { l := listener.NewMockListener() l.StartProduceDockerEvents() e := emitter.NewEmitter(l) as := actions.NewActions(nil).EnabledActions // Setup actions addr := &actions.ActionAddress{ NewFunc: actions.NewMockAction, } actions.RegisterAction(addr) actions.ActivateActions() // Load actions to Emitter e.LoadActions(as) // BroadCast message e.BroadCast() <-e.Stopped() <-actions.Actions()["mock"].(*actions.MockAction).Stopped assert.Len(t, actions.Actions()["mock"].(*actions.MockAction).Memory, 10, "produced message size") event := actions.Actions()["mock"].(*actions.MockAction).Memory[0].(*docker.APIEvents) assert.Equal(t, event.From, "base:latest") actions.ClearActions() }
func TestParseActionsConfig(t *testing.T) { config := NewConfig() args := []string{"-mock-config-value=testvalue"} config.Parse(args) assert.Equal(t, config.actionsConfig["mock-config-value"], "testvalue") }
func TestParseEnabledActions(t *testing.T) { config := NewConfig() args := []string{"--actions=a,b,c"} config.Parse(args) assert.Equal(t, config.enabledActions[0], "a") }
func TestEnabledActionsFromFlags(t *testing.T) { args := []string{"-actions=mock"} fs := flag.NewFlagSet("test", flag.PanicOnError) fs.Var(NewActionNames(), "actions", "") fs.Parse(args) enabledActions, _ := EnabledActionsFromFlags(fs, "actions") assert.Equal(t, enabledActions, []string{"mock"}) }