Example #1
0
func appFromExampleConfig(t *testing.T) (*Application, func()) {
	var path, err = utils.ProjectPath()
	if err != nil {
		t.Fatalf("Was not able to find the project dir: %s", err)
	}

	path1, cleanup1 := testutils.GetTestFolder(t)
	path2, cleanup2 := testutils.GetTestFolder(t)

	//!TODO: maybe construct an config ourselves
	// We are using the example config for this test. This might not be
	// so great an idea. But I tried to construct a config programatically
	// for about an hour and a half and I failed.
	var configGetter = configGetterForExampleConfig(path, path1, path2)
	app, err := New(types.AppVersion{}, configGetter)
	if err != nil {
		t.Fatalf("Error creating an app: %s", err)
	}

	if err := app.initFromConfig(); err != nil {
		t.Fatalf("Error initializing app: %s", err)
	}

	return app, func() {
		cleanup1()
		cleanup2()
	}
}
Example #2
0
// New creates and returns a ready to used ServerStatusHandler.
func New(cfg *config.Handler, l *types.Location, next types.RequestHandler) (*ServerStatusHandler, error) {
	var s = defaultSettings
	if err := json.Unmarshal(cfg.Settings, &s); err != nil {
		return nil, fmt.Errorf("error while parsing settings for handler.status - %s", err)
	}

	// In case of:
	//  * the path is missing and it is relative
	//		or
	//  * the path is not a directory
	// we try to guess the project's root and use s.Path as a relative to it
	// one in hope it will match the templates' directory.
	if st, err := os.Stat(s.Path); (err != nil && err.(*os.PathError) != nil &&
		!strings.HasPrefix(s.Path, "/")) || (err == nil && !st.IsDir()) {

		projPath, err := utils.ProjectPath()
		if err == nil {
			fullPath := path.Join(projPath, s.Path)
			if st, err := os.Stat(fullPath); err == nil && st.IsDir() {
				s.Path = fullPath
			}
		}
	}

	var statusFilePath = path.Join(s.Path, "status_page.html")
	var tmpl, err = template.ParseFiles(statusFilePath)
	if err != nil {
		return nil, fmt.Errorf("error on opening %s - %s", statusFilePath, err)
	}

	return &ServerStatusHandler{
		tmpl: tmpl,
		loc:  l,
	}, nil
}
Example #3
0
func TestExampleConfig(t *testing.T) {
	t.Parallel()
	path, err := utils.ProjectPath()

	if err != nil {
		t.Fatalf("Was not able to find project path: %s", err)
	}

	if _, err := Parse("not-present-config.json"); err == nil {
		t.Errorf("Expected error when parsing non existing config but got nil")
	}

	examplePath := filepath.Join(path, "config.example.json")
	cfg, err := Parse(examplePath)
	if err != nil {
		t.Fatalf("Parsing the example config returned: %s", err)
	}

	if err := cfg.Validate(); err != nil {
		t.Errorf("Example config verification had error: %s", err)
	}

}
func getConfigGetter(tmpPath string) func() (*config.Config, error) {
	return func() (*config.Config, error) {
		path, err := utils.ProjectPath()
		if err != nil {
			return nil, fmt.Errorf("Was not able to find project path: %s", err)
		}

		cfg, err := config.Parse(filepath.Join(path, "config.example.json"))
		if err != nil {
			return nil, fmt.Errorf("Parsing the example config returned: %s", err)
		}

		for k := range cfg.CacheZones {
			// Fix and create storage paths
			cfg.CacheZones[k].Path = filepath.Join(tmpPath, k)
			if err := os.Mkdir(cfg.CacheZones[k].Path, os.FileMode(0700|os.ModeDir)); err != nil {
				return nil, err
			}
		}

		return cfg, nil
	}
}