예제 #1
0
파일: Setup.go 프로젝트: ThingFu/hub
func loadRules(env api.Environment) {
	log.Println("[INFO] Dividing by Zero")
	rulesService := container.Instance().RulesManager()
	filepath.Walk(env.GetHome()+"/rules", func(path string, f os.FileInfo, err error) error {
		if !f.IsDir() {
			content, err := ioutil.ReadFile(path)
			if err != nil {
				panic(err)
			}

			var rule api.Rule
			err = json.Unmarshal(content, &rule)

			if err != nil {
				log.Printf("[ERROR] Parsing Rule %s\n", path)
				return nil
			}
			rule.Id = utils.RandomString(7)

			rule.Path = path[strings.LastIndex(path, "/")+1:]

			log.Printf("[INFO] Registering rule file: %s\n", rule.Name)

			rulesService.RegisterRule(rule)
		}
		return nil
	})
}
예제 #2
0
func TestHourlyCondition(t *testing.T) {
	d := new(conditions.Hourly)

	when := new(api.RuleWhen)
	facts := new(api.RuleFacts)
	rule := new(api.Rule)

	if !d.Evaluate(when, facts, rule) {
		t.Errorf("Hourly Condition: Evaluation failed")
	}

	rule.LastRun = tests.NewTime("now")
	if d.Evaluate(when, facts, rule) {
		t.Errorf("Hourly Condition: Evaluation failed")
	}

	rule.LastRun = tests.NewTime("a_minute_Ago")
	if d.Evaluate(when, facts, rule) {
		t.Errorf("Hourly Condition: Evaluation failed")
	}

	rule.LastRun = tests.NewTime("hour_ago")
	if !d.Evaluate(when, facts, rule) {
		t.Errorf("Hourly Condition: Evaluation failed")
	}

	rule.LastRun = tests.NewTime("2hours_ago")
	if !d.Evaluate(when, facts, rule) {
		t.Errorf("Hourly Condition: Evaluation failed")
	}
}
예제 #3
0
func TestDailyCondition(t *testing.T) {
	d := new(conditions.Daily)

	when := new(api.RuleWhen)
	facts := new(api.RuleFacts)
	rule := new(api.Rule)

	if !d.Evaluate(when, facts, rule) {
		t.Errorf("Daily Condition: Evaluation failed")
	}

	rule.LastRun = tests.NewTime("now")
	if d.Evaluate(when, facts, rule) {
		t.Errorf("Daily Condition: Evaluation failed")
	}

	rule.LastRun = tests.NewTime("yesterday")
	if !d.Evaluate(when, facts, rule) {
		t.Errorf("Daily Condition: Evaluation failed")
	}
}