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
func setupLog() {
	var err error

	if arg.GetB(ARG_DAEMON) {
		err = log.Set(knf.GetS(LOG_FILE), knf.GetM(LOG_PERMS, 0644))

		if err != nil {
			fmt.Printf("Can't setup logger: %v\n", err)
			os.Exit(1)
		}
	}

	err = log.MinLevel(knf.GetS(LOG_LEVEL, "info"))

	if err != nil {
		fmt.Printf("Can't set log level: %v\n", err)
	}
}
Esempio n. 3
0
func runServer() {
	observer := rules.NewObserver(knf.GetS(DATA_RULE_DIR))
	observer.AutoHead = knf.GetB(PROCESSING_AUTO_HEAD)
	observer.Start(knf.GetI(DATA_CHECK_DELAY))

	err := server.Start(observer, APP+"/"+VER, arg.GetS(ARG_PORT))

	if err != nil {
		printError(err.Error())
		os.Exit(1)
	}
}
Esempio n. 4
0
func updatePerms(dirPath, mockPath string) {
	if knf.GetS(ACCESS_USER) != "" || knf.GetS(ACCESS_GROUP) != "" {
		dirOwnerUID, dirOwnerGID, _ := fsutil.GetOwner(dirPath)
		mockOwnerUID, mockOwnerGID, _ := fsutil.GetOwner(mockPath)

		if knf.GetS(ACCESS_USER) != "" {
			userInfo, err := system.LookupUser(knf.GetS(ACCESS_USER))

			if err == nil {
				dirOwnerUID = userInfo.UID
				mockOwnerUID = userInfo.UID
			}
		}

		if knf.GetS(ACCESS_GROUP) != "" {
			groupInfo, err := system.LookupGroup(knf.GetS(ACCESS_GROUP))

			if err == nil {
				dirOwnerGID = groupInfo.GID
				mockOwnerGID = groupInfo.GID
			}
		}

		os.Chown(dirPath, dirOwnerUID, dirOwnerGID)
		os.Chown(mockPath, mockOwnerUID, mockOwnerGID)
	}

	os.Chmod(dirPath, knf.GetM(ACCESS_MOCK_DIR_PERMS))
	os.Chmod(mockPath, knf.GetM(ACCESS_MOCK_PERMS))
}
Esempio n. 5
0
func showRuleInfo(rule *rules.Rule) {
	if rule.Desc == "" {
		fmtc.Printf("\n  {*}%s{!} {s}(Description is empty){!}\n", rule.FullName+".mock")
	} else {
		fmtc.Printf("\n  {*}%s{!} - %s\n", rule.FullName+".mock", rule.Desc)
	}

	host := rule.Request.Host

	if host == "" {
		if knf.GetS(LISTING_HOST) != "" {
			host = knf.GetS(LISTING_HOST)
		} else {
			host = ip
		}
	}

	if knf.GetS(LISTING_SCHEME) != "" {
		host = knf.GetS(LISTING_SCHEME) + "://" + host
	}

	if knf.GetS(LISTING_PORT) != "" {
		host = host + ":" + knf.GetS(LISTING_PORT)
	}

	fmtc.Printf("  {s}%s %s%s{!}\n", rule.Request.Method, host, rule.Request.URL)
}
Esempio n. 6
0
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)
}
Esempio n. 7
0
func listMocks(args []string) {
	var service = ""

	// Suppress observer logging
	log.Set(os.DevNull, 0)

	if len(args) != 0 {
		service = args[0]
	}

	observer := rules.NewObserver(knf.GetS(DATA_RULE_DIR))
	err := listing.List(observer, service)

	if err != nil {
		printError(err.Error())
		os.Exit(1)
	}
}
Esempio n. 8
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
}
Esempio n. 9
0
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
}