| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package stores
- import (
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "path/filepath"
- "testing"
- . "github.com/smartystreets/goconvey/convey"
- "github.com/torkelo/grafana-pro/pkg/models"
- )
- func TestFileStore(t *testing.T) {
- GivenFileStore("When saving a dashboard", t, func(store *fileStore) {
- dashboard := models.NewDashboard("hello")
- err := store.Save(dashboard)
- Convey("should be saved to disk", func() {
- So(err, ShouldBeNil)
- _, err = os.Stat(store.getFilePathForDashboard("hello"))
- So(err, ShouldBeNil)
- })
- })
- GivenFileStore("When getting a saved dashboard", t, func(store *fileStore) {
- copyDashboardToTempData("default.json", "", store.dashDir)
- dash, err := store.GetById("default")
- Convey("should be read from disk", func() {
- So(err, ShouldBeNil)
- So(dash, ShouldNotBeNil)
- So(dash.Title(), ShouldEqual, "Grafana Play Home")
- })
- })
- GivenFileStore("when getting dashboard with capital letters", t, func(store *fileStore) {
- copyDashboardToTempData("annotations.json", "", store.dashDir)
- dash, err := store.GetById("AnnoTations")
- Convey("should be read from disk", func() {
- So(err, ShouldBeNil)
- So(dash, ShouldNotBeNil)
- So(dash.Title(), ShouldEqual, "Annotations")
- })
- })
- GivenFileStore("When copying dashboards into data dir", t, func(store *fileStore) {
- copyDashboardToTempData("annotations.json", "", store.dashDir)
- copyDashboardToTempData("default.json", "", store.dashDir)
- copyDashboardToTempData("graph-styles.json", "", store.dashDir)
- store.scanFiles()
- Convey("scan should generate index of all dashboards", func() {
- result, err := store.Query("*")
- So(err, ShouldBeNil)
- So(len(result), ShouldEqual, 3)
- })
- })
- }
- func copyDashboardToTempData(name string, destName string, dir string) {
- if destName == "" {
- destName = name
- }
- source, _ := filepath.Abs("../../data/dashboards/" + name)
- dest := filepath.Join(dir, destName)
- err := copyFile(dest, source)
- if err != nil {
- panic(fmt.Sprintf("failed to copy file %v", name))
- }
- }
- func GivenFileStore(desc string, t *testing.T, f func(store *fileStore)) {
- Convey(desc, t, func() {
- tempDir, _ := ioutil.TempDir("", "store")
- store := NewFileStore(tempDir)
- f(store)
- Reset(func() {
- os.RemoveAll(tempDir)
- })
- })
- }
- func copyFile(dst, src string) error {
- in, err := os.Open(src)
- if err != nil {
- return err
- }
- defer in.Close()
- out, err := os.Create(dst)
- if err != nil {
- return err
- }
- defer out.Close()
- _, err = io.Copy(out, in)
- cerr := out.Close()
- if err != nil {
- return err
- }
- return cerr
- }
|