예제 #1
0
func TestGetNode(t *testing.T) {
	root, cleanup, err := utesting.CreateDirectoryTree(map[string]string{
		"/foo/node.json": `{"Type":"core.Foo"}`,
		"/foo/bar/empty": ``},
		"TestGetNode")
	if err != nil {
		t.Fatalf("Could not create directory tree: ", err)
	}
	defer cleanup()
	ret, err := getNode(root, "/foo")
	expected := `{"Path":"/foo","Type":"core.Foo"}`
	if err != nil {
		t.Errorf("Got error: %v", err)
	} else if string(ret) != expected {
		t.Fatalf(`getNode(%q, "/foo") = %v, nil, should be %v, nil`,
			root, string(ret), expected)
	}
	ret, err = getNode(root, "/foo/bar")
	expected = `{"Path":"/foo/bar","Type":"core.Path"}`
	if err != nil {
		t.Errorf("Got error: %v", err)
	} else if string(ret) != expected {
		t.Fatalf(`getNode(%q, "/foo/bar") = %v, nil, should be %v, nil`,
			root, string(ret), expected)
	}
	ret, err = getNode(root, "/unavailable")
	if err != nil {
		t.Errorf("Got error: %v", err)
	} else if ret != nil {
		t.Errorf(`getNode(%q, "/unavailable") = %v, nil, should be nil, nil`,
			root, string(ret))
	}
}
예제 #2
0
func TestLoadSiteSettings(t *testing.T) {
	files := map[string]string{
		"/etc/sites/example/site.yaml": `
title: "Monsti CMS Example Site"
hosts: ["localhost:8080"]
directories:
  data: ../../../bar
`,
		"/linked_site/site.yaml": `
title: "Linked Example Site"
`}
	root, cleanup, err := mtest.CreateDirectoryTree(files, "TestLoadSettings")
	if err != nil {
		t.Fatalf("Could not create test files: %v", err)
	}
	defer cleanup()
	err = os.Symlink(filepath.Join(root, "linked_site"),
		filepath.Join(root, "etc", "sites", "linked"))
	if err != nil {
		t.Fatalf("Could not create symlink to site config: %v", err)
	}
	sites, err := loadSiteSettings(filepath.Join(root, "etc", "sites"))
	if err != nil {
		t.Fatalf("Could not load site settings: %v", err)
	}
	if _, ok := sites["example"]; len(sites) != 2 || !ok {
		t.Fatalf("Should find two sites, but found %v", len(sites))
	}
	entry := sites["example"]
	if entry.Title != "Monsti CMS Example Site" {
		t.Errorf("settings.Sites[\"Example\"] should be "+
			`"Monsti CMS Example Site", but is %q`, entry.Title)
	}
	if entry.Locale != "en" {
		t.Errorf(`Default locale is not "en"`)
	}
	if len(entry.Hosts) != 1 || entry.Hosts[0] != "localhost:8080" {
		entry := sites["example"]
		if entry.Title != "Monsti CMS Example Site" {
			t.Errorf("settings.Sites[\"Example\"] should be "+
				`"Monsti CMS Example Site", but is %q`, entry.Title)
		}
		if len(entry.Hosts) != 1 || entry.Hosts[0] != "localhost:8080" {
			t.Errorf(`settings.Sites["example"].Hosts == %v, should be`+
				` ["localhost:8080"]`, entry.Hosts)
		}
		t.Errorf(`settings.Sites["example"].Hosts == %v, should be`+
			` ["localhost:8080"]`, entry.Hosts)
	}
}
예제 #3
0
func TestGetChildren(t *testing.T) {
	root, cleanup, err := utesting.CreateDirectoryTree(map[string]string{
		"/foo/node.json": `{"title":"Node Foo","shorttitle":"Foo"}`,
		"/foo/child1/node.json": `{"title":"Node a Foo Child 1",` +
			`"shorttitle":"a Foo Child 1"}`,
		"/foo/child2/node.json": `{"title":"Node Foo Child 2",` +
			`"shorttitle":"Foo Child 2"}`,
		"/foo/child2/child1/node.json": `{"title":"Node a Foo Child 2 Child 1",` +
			`"shorttitle":"a Foo Child 2 Child 1"}`,
		"/bar/node.json": `{"title":"Node Bar","order":"2",` +
			`"shorttitle":"Bar"}`}, "TestGetChildren")
	if err != nil {
		t.Fatalf("Could not create directory tree: ", err)
	}
	defer cleanup()
	err = os.Symlink("child2", filepath.Join(root, "/foo/child3"))
	if err != nil {
		t.Fatalf("Could not create symlink: %v", err)
	}
	tests := []struct {
		Path     string
		Children []string
	}{
		{"/foo", []string{"/foo/child1", "/foo/child2", "/foo/child3"}},
		{"/bar", []string{}}}
	for _, test := range tests {
		ret, err := getChildren(root, test.Path)
		if err != nil {
			t.Errorf(`getChildren(%q, %q) = %v, %v, should be _, nil`,
				root, test.Path, ret, err)
		}
		if len(ret) != len(test.Children) {
			t.Errorf(`getChildren(%q) returns %v items (%v), should be %v`,
				test.Path, len(ret), ret, len(test.Children))
		}
		for i := range ret {
			if strings.Contains(test.Children[i], string(ret[i])) {
				t.Errorf(`Item %v of getChildren(%q) is %v, should be %v`,
					i, test.Path, ret[i], test.Children[i])
			}
		}
	}
}
예제 #4
0
func TestCacheExpire(t *testing.T) {
	root, cleanup, err := utesting.CreateDirectoryTree(map[string]string{}, "TestCache")
	if err != nil {
		t.Fatalf("Could not create directory tree: ", err)
	}
	defer cleanup()
	err = toCache(root, "/foo", "foo.foo", []byte("test"),
		&service.CacheMods{Expire: time.Now().AddDate(-1, 0, 0)})
	if err != nil {
		t.Fatalf("Could not cache data: %v", err)
	}
	ret, _, err := fromCache(root, "/foo", "foo.foo")
	if err != nil {
		t.Fatalf("Could not get cached data: %v", err)
	}
	if ret != nil {
		t.Errorf("Cache should have been expired.")
	}
}
예제 #5
0
func WriteUser(t *testing.T) {
	root, cleanup, err := utesting.CreateDirectoryTree(map[string]string{
		"/users.json": `{"foo":{"password":"******"}}`}, "TestWriteUser")
	if err != nil {
		t.Fatalf("Could not create directory tree: ", err)
	}
	defer cleanup()
	user := service.User{Login: "******", Password: "******"}

	err = writeUser(&user, root)
	if err != nil {
		t.Fatalf("Error writing changed user: %v", err)
	}
	userChanged, err := getUser(user.Login, root)
	if err != nil {
		t.Fatalf("Error reading changed user: %v", err)
	}
	if *userChanged != user {
		t.Errorf("Users differ: %v\n %v", user, userChanged)
	}
}
예제 #6
0
func TestCacheMods(t *testing.T) {
	root, cleanup, err := utesting.CreateDirectoryTree(map[string]string{}, "TestCache")
	if err != nil {
		t.Fatalf("Could not create directory tree: ", err)
	}
	defer cleanup()
	mods := &service.CacheMods{
		Deps:   []service.CacheDep{{Node: "/foo/bar"}},
		Expire: time.Now().AddDate(1, 0, 0)}
	err = toCache(root, "/foo", "foo.foo", []byte("test"), mods)
	if err != nil {
		t.Fatalf("Could not cache data: %v", err)
	}
	_, retMods, err := fromCache(root, "/foo", "foo.foo")
	if err != nil {
		t.Fatalf("Could not get cached data: %v", err)
	}
	mods.Deps = nil
	if !reflect.DeepEqual(mods, retMods) {
		t.Errorf("Returned cachemods should be %v, got %v", mods, retMods)
	}
}
예제 #7
0
func TestCache(t *testing.T) {
	root, cleanup, err := utesting.CreateDirectoryTree(map[string]string{}, "TestCache")
	if err != nil {
		t.Fatalf("Could not create directory tree: ", err)
	}
	defer cleanup()
	err = toCache(root, "/foo/bar/cruz", "foo.some_cache", []byte("test"), nil)
	if err != nil {
		t.Fatalf("Could not cache data: %v", err)
	}
	err = toCache(root, "/foo/bar", "foo.another_cache", []byte("test2"),
		&service.CacheMods{Deps: []service.CacheDep{{Node: "/foo/bar/cruz"}}})
	if err != nil {
		t.Fatalf("Could not cache data: %v", err)
	}
	err = toCache(root, "/foo", "foo.another_cache", []byte("test3"),
		&service.CacheMods{Deps: []service.CacheDep{{Node: "/foo/bar/cruz"}}})
	if err != nil {
		t.Fatalf("Could not cache data: %v", err)
	}
	ret, _, err := fromCache(root, "/foo", "foo.another_cache")
	if err != nil {
		t.Fatalf("Could not get cached data: %v", err)
	}
	if !reflect.DeepEqual(ret, []byte("test3")) {
		t.Fatalf("test3 should be in cache, got %v", string(ret))
	}
	err = markDep(root, service.CacheDep{Node: "/foo/bar/cruz"}, 0)
	if err != nil {
		t.Fatalf("Could not mark dep: %v", err)
	}
	ret, _, err = fromCache(root, "/foo", "foo.another_cache")
	if err != nil {
		t.Fatalf("Could not get cached data: %v", err)
	}
	if ret != nil {
		t.Fatalf("Cache should be nil, got %v", string(ret))
	}
}
예제 #8
0
func TestGetConfig(t *testing.T) {
	root, cleanup, err := utesting.CreateDirectoryTree(map[string]string{
		"/foo.json": `{"foo":{"foobar":"foobarvalue"},"bar":"barvalue"}`,
	}, "TestGetSection")
	if err != nil {
		t.Fatalf("Could not create directory tree: ", err)
	}
	defer cleanup()
	tests := []struct{ Name, Value string }{
		{"", `{"Value":{"foo":{"foobar":"foobarvalue"},"bar":"barvalue"}}`},
		{"foo", `{"Value":{"foobar":"foobarvalue"}}`},
		{"foo.foobar", `{"Value":"foobarvalue"}`},
		{"bar", `{"Value":"barvalue"}`},
		{"unknown", `{"Value": null}`},
	}
	ret, err := getConfig(filepath.Join(root, "nonexisting.json"), "foo")
	if err != nil || ret != nil {
		t.Errorf("getConfig for non existing config file should"+
			"return nil,nil, got %v,%v", ret, err)
	}
	for _, test := range tests {
		unmarshal := func(in []byte) (out interface{}) {
			if err = json.Unmarshal(in, &out); err != nil {
				t.Errorf("Could not unmarshal for %v: %v", test.Name, err)
			}
			return
		}
		ret, err := getConfig(filepath.Join(root, "foo.json"), test.Name)
		switch {
		case err != nil:
			t.Errorf("getConfig(_, %q) returned error: %v", test.Name, err)
		case !reflect.DeepEqual(unmarshal(ret), unmarshal([]byte(test.Value))):
			t.Errorf("getConfig(_, %q) = `%s`, _ should be `%s`", test.Name, ret,
				test.Value)
		}
	}
}
예제 #9
0
func TestGetIncludes(t *testing.T) {
	root, cleanup, err := mtesting.CreateDirectoryTree(map[string]string{
		"/first/include":                        "one\ntwo\nfour\n\n",
		"/first/master.include":                 "three",
		"/first/foo/bar/include":                "four",
		"/first/foo/bar/cruz/include":           "five",
		"/first/foo/bar/cruz/template.include":  "six",
		"/second/include":                       "seven",
		"/second/foo/bar/cruz/template.include": "eight"}, "TestGetIncludes")
	if err != nil {
		t.Fatalf("Could not create test directory tree: %v", err)
	}
	defer cleanup()
	includes, err := getIncludes([]string{filepath.Join(root, "first"),
		filepath.Join(root, "second")},
		"foo/bar/cruz/template")
	sort.Strings(includes)
	expected := []string{
		"eight", "five", "four", "one", "seven", "six", "two"}
	if !reflect.DeepEqual(includes, expected) || err != nil {
		t.Errorf("getIncludes returned: %v, %v should be %v, nil",
			includes, err, expected)
	}
}
예제 #10
0
func TestCacheMarkDescend(t *testing.T) {
	root, cleanup, err := utesting.CreateDirectoryTree(map[string]string{}, "TestCache")
	if err != nil {
		t.Fatalf("Could not create directory tree: ", err)
	}
	defer cleanup()
	err = toCache(root, "/foo/bar/cruz", "foo.some_cache", []byte("test"), nil)
	if err != nil {
		t.Fatalf("Could not cache data: %v", err)
	}
	err = toCache(root, "/foo/bar", "foo.some_cache", []byte("test2"), nil)
	if err != nil {
		t.Fatalf("Could not cache data: %v", err)
	}

	// Descend one level
	var ret []byte
	err = toCache(root, "/foo", "foo.another_cache", []byte("test3"),
		&service.CacheMods{Deps: []service.CacheDep{{Node: "/foo", Descend: 1}}})
	if err != nil {
		t.Fatalf("Could not cache data: %v", err)
	}
	err = markDep(root, service.CacheDep{Node: "/foo/bar/cruz"}, 0)
	if err != nil {
		t.Fatalf("Could not mark dep: %v", err)
	}
	ret, _, err = fromCache(root, "/foo", "foo.another_cache")
	if err != nil {
		t.Fatalf("Could not get cached data: %v", err)
	}
	if ret == nil {
		t.Errorf("Cache should not be nil")
	}
	err = markDep(root, service.CacheDep{Node: "/foo/bar"}, 0)
	if err != nil {
		t.Fatalf("Could not mark dep: %v", err)
	}
	ret, _, err = fromCache(root, "/foo", "foo.another_cache")
	if err != nil {
		t.Fatalf("Could not get cached data: %v", err)
	}
	if ret != nil {
		t.Errorf("Cache should be nil, got %v", string(ret))
	}

	// Descend all levels
	err = toCache(root, "/foo", "foo.another_cache", []byte("test3"),
		&service.CacheMods{Deps: []service.CacheDep{{Node: "/foo", Descend: -1}}})
	if err != nil {
		t.Fatalf("Could not cache data: %v", err)
	}
	err = markDep(root, service.CacheDep{Node: "/foo/bar/cruz"}, 0)
	if err != nil {
		t.Fatalf("Could not mark dep: %v", err)
	}
	ret, _, err = fromCache(root, "/foo", "foo.another_cache")
	if err != nil {
		t.Fatalf("Could not get cached data: %v", err)
	}
	if ret != nil {
		t.Errorf("Cache should be nil, got %v", string(ret))
	}
}