예제 #1
0
// findFile try to find log file
func findFile(file string) string {
	if fsutil.IsExist(file) {
		return file
	}

	configPath := fsutil.ProperPath("FRS", confPaths)

	if configPath == "" {
		return file
	}

	config, err := knf.Read(configPath)
	logDir := config.GetS("data:log-dir")

	if err != nil || logDir == "" {
		return file
	}

	if !strings.Contains(file, ".log") {
		file += ".log"
	}

	if fsutil.CheckPerms("FRS", path.Join(logDir, file)) {
		return path.Join(logDir, file)
	}

	return file
}
예제 #2
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
}
예제 #3
0
// checkRule parse rule, run validators and print check results
func checkRule(ruleInfo *RuleInfo) uint8 {
	var problems []*Problem

	rule, err := rules.Parse(knf.GetS(DATA_RULE_DIR), ruleInfo.Service, ruleInfo.Dir, ruleInfo.Name)

	if err != nil {
		problems = append(problems,
			&Problem{
				Type: PROBLEM_ERR,
				Info: "Parsing error",
				Desc: err.Error(),
			},
		)
	} else {

		validators := []Validator{
			checkDescription,
			checkWildcard,
			checkMethod,
			checkStatusCode,
			checkContent,
		}

		problems = append(problems, execValidators(validators, rule)...)
	}

	if len(problems) == 0 {
		return PROBLEM_NONE
	}

	fmtutil.Separator(false, path.Join(ruleInfo.Service, ruleInfo.Dir, ruleInfo.Name))
	renderProblems(problems)

	var maxProblemType = PROBLEM_NONE

	for _, problem := range problems {
		maxProblemType = mathutil.MaxU8(problem.Type, maxProblemType)
	}

	return maxProblemType
}
예제 #4
0
파일: gen.go 프로젝트: essentialkaos/mockka
func Make(name string) error {
	if !fsutil.IsWritable(knf.GetS(DATA_RULE_DIR)) {
		return fmt.Errorf("Directory %s must be writable.", knf.GetS(DATA_RULE_DIR))
	}

	if name == "" {
		return errors.New("You must difine mock file name (service1/mock1 for example)")
	}

	if !strings.Contains(name, "/") {
		return errors.New("You must difine mock file name as <service-id>/<mock-name>.")
	}

	template := knf.GetS(TEMPLATE_PATH)
	ruleDir := knf.GetS(DATA_RULE_DIR)
	dirName := path.Dir(name)
	fullPath := path.Join(ruleDir, name)

	if !strings.Contains(fullPath, ".mock") {
		fullPath += ".mock"
	}

	if fsutil.IsExist(fullPath) {
		return fmt.Errorf("File %s already exist\n", fullPath)
	}

	if template == "" || !fsutil.CheckPerms("FRS", template) {
		return createMock(MOCK_TEMPLATE, dirName, fullPath)
	}

	templData, err := ioutil.ReadFile(template)

	if err != nil {
		return fmt.Errorf("Can't read template content from %s: %v", template, err)
	}

	return createMock(string(templData), dirName, fullPath)
}
예제 #5
0
파일: gen.go 프로젝트: essentialkaos/mockka
func createMock(content, dirName, fullPath string) error {
	serviceDir := path.Join(knf.GetS(DATA_RULE_DIR), dirName)

	err := os.MkdirAll(serviceDir, 0755)

	if err != nil {
		return fmt.Errorf("Can't create directory %s", serviceDir)
	}

	mf, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE, 0644)

	if err != nil {
		return fmt.Errorf("Can't create file %s", fullPath)
	}

	defer mf.Close()

	mf.WriteString(content)

	updatePerms(serviceDir, fullPath)

	return nil
}
예제 #6
0
func (obs *Observer) checkRules(rules []string) bool {
	var ok = true

RULELOOP:
	for _, rulePath := range rules {

		service, mockFile, dir := ParsePath(rulePath)

		if mockFile == "" {
			log.Error("Can't use rule %s: rule file must be placed inside service directory (e.g. rules/my-service/rule.mock)", rulePath)
			continue
		}

		fullPath := path.Join(obs.ruleDir, rulePath)
		mockName := strings.Replace(mockFile, ".mock", "", -1)

		if obs.pathMap[fullPath] != nil {
			continue
		}

		rule, err := Parse(obs.ruleDir, service, dir, mockName)

		if err != nil {

			if obs.errMap[fullPath] != true {
				log.Error("Can't parse rule %s: %v", path.Join(service, dir, mockName), err)
				obs.errMap[fullPath] = true
				ok = false
			}

			continue
		}

		for _, r := range obs.wcMap {
			if r.Request.Method != rule.Request.Method {
				continue
			}

			if r.Request.Host != rule.Request.Host {
				continue
			}

			if urlutil.EqualPatterns(r.Request.NURL, rule.Request.NURL) {
				if obs.errMap[rule.Path] != true {
					log.Error("Rule intersection: rule %s and rule %s have same result urls", r.PrettyPath, rule.PrettyPath)
					obs.errMap[rule.Path] = true
					ok = false
				}

				continue RULELOOP
			}
		}

		delete(obs.errMap, rule.Path)

		obs.uriMap[rule.Request.URI] = rule
		obs.pathMap[rule.Path] = rule
		obs.srvMap[service] = true

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

		if obs.nameMap[service] == nil {
			obs.nameMap[service] = make(RuleMap)
		}

		obs.nameMap[service][rule.FullName] = rule

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

	return ok
}