Esempio n. 1
0
// Check check rule or all rules for some service
func Check(target string) error {
	if target == "" {
		return errors.New("You must difine mock file or service name")
	}

	targetService, targetMock, targetDir := rules.ParsePath(target)

	serviceDir := path.Join(knf.GetS(DATA_RULE_DIR), targetService)

	if !fsutil.IsExist(serviceDir) {
		return fmtc.Errorf("Service %s is not exist", targetService)
	}

	var ruleInfoSlice []*RuleInfo

	if targetMock != "" {
		ruleInfoSlice = append(ruleInfoSlice, &RuleInfo{targetService, targetMock, targetDir})
	} else {
		mockFiles := fsutil.ListAllFiles(serviceDir, true,
			&fsutil.ListingFilter{MatchPatterns: []string{"*.mock"}},
		)

		for _, mockFile := range mockFiles {
			mockPath := path.Join(targetService, strings.Replace(mockFile, ".mock", "", -1))

			_, targetMock, targetDir := rules.ParsePath(mockPath)

			ruleInfoSlice = append(ruleInfoSlice, &RuleInfo{targetService, targetMock, targetDir})
		}
	}

	if len(ruleInfoSlice) == 0 {
		fmtc.Println("\n{y}No mock's were found{!}\n")
		return nil
	}

	var maxProblemType = PROBLEM_NONE

	for _, rule := range ruleInfoSlice {
		maxProblemType = mathutil.MaxU8(checkRule(rule), maxProblemType)
	}

	if maxProblemType > PROBLEM_NONE {
		fmtutil.Separator(false)
	}

	switch maxProblemType {
	case PROBLEM_NONE:
		fmtc.Printf("\n{g}%s{!}\n\n", okMessages[rand.Int(len(okMessages))])
	case PROBLEM_WARN:
		fmtc.Printf("{y}%s{!}\n\n", warnMessages[rand.Int(len(warnMessages))])
	case PROBLEM_ERR:
		fmtc.Printf("{r}%s{!}\n\n", errorMessages[rand.Int(len(errorMessages))])
	}

	return nil
}
Esempio n. 2
0
// Load load and parse all rules
func (obs *Observer) Load() bool {
	var ok = true

	for _, r := range obs.uriMap {
		if !fsutil.IsExist(r.Path) {
			delete(obs.uriMap, r.Request.URI)
			delete(obs.wcMap, r.Path)
			delete(obs.errMap, r.Path)
			delete(obs.pathMap, r.Path)
			delete(obs.nameMap[r.Service], r.FullName)

			// If no one rule found for service, remove it's own map
			if len(obs.nameMap[r.Service]) == 0 {
				delete(obs.nameMap, r.Service)
			}

			log.Info("Rule %s unloaded (mock file deleted)", r.PrettyPath)

			continue
		}

		mtime, _ := fsutil.GetMTime(r.Path)

		if r.ModTime.UnixNano() != mtime.UnixNano() {
			rule, err := Parse(obs.ruleDir, r.Service, r.Dir, r.Name)

			if err != nil {
				log.Error(err.Error())
				ok = false
				continue
			}

			// URI can be changed, remove rule from uri map anyway
			delete(obs.uriMap, r.Request.URI)
			delete(obs.errMap, r.Path)

			if r.IsWildcard {
				delete(obs.wcMap, r.Path)
			}

			obs.uriMap[rule.Request.URI] = rule
			obs.pathMap[rule.Path] = rule

			if rule.IsWildcard {
				obs.wcMap[rule.Path] = rule
			}

			log.Info("Rule %s reloaded", rule.PrettyPath)
		}
	}

	if !fsutil.CheckPerms("DRX", obs.ruleDir) {
		log.Error("Can't read directory with rules (%s)", obs.ruleDir)
		return false
	}

	rules := fsutil.ListAllFiles(
		obs.ruleDir, true,
		&fsutil.ListingFilter{
			MatchPatterns: []string{"*.mock"},
		},
	)

	if len(rules) == 0 {
		return ok
	}

	if !obs.checkRules(rules) {
		ok = false
	}

	return ok
}