2019-05-06 23:18:10 +02:00
|
|
|
package config
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
import (
|
2019-06-03 22:58:15 +02:00
|
|
|
"context"
|
2019-05-03 18:57:28 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-01-06 02:14:17 +01:00
|
|
|
"sync"
|
2019-05-03 18:57:28 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
2020-01-06 14:32:15 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2019-12-02 00:30:58 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-05-03 18:57:28 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TestDataZip = "/tmp/photoprism/testdata.zip"
|
2019-05-04 00:53:02 +02:00
|
|
|
TestDataURL = "https://dl.photoprism.org/fixtures/testdata.zip"
|
2019-07-05 13:47:48 +02:00
|
|
|
TestDataHash = "a217ac5242de2189ffb414d819b628c7957c67d7"
|
2019-05-03 18:57:28 +02:00
|
|
|
)
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
var testConfig *Config
|
2020-01-06 02:14:17 +01:00
|
|
|
var once sync.Once
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
func testDataPath(assetsPath string) string {
|
|
|
|
return assetsPath + "/testdata"
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
func NewTestParams() *Params {
|
2020-01-12 14:00:56 +01:00
|
|
|
assetsPath := fs.ExpandFilename("../../assets")
|
2019-05-04 05:25:00 +02:00
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
testDataPath := testDataPath(assetsPath)
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
c := &Params{
|
2019-11-12 05:49:10 +01:00
|
|
|
Public: true,
|
|
|
|
ReadOnly: false,
|
2019-12-15 17:19:16 +01:00
|
|
|
HideNSFW: false,
|
|
|
|
UploadNSFW: false,
|
2019-06-06 14:44:29 +02:00
|
|
|
DarktableBin: "/usr/bin/darktable-cli",
|
2019-05-03 18:57:28 +02:00
|
|
|
AssetsPath: assetsPath,
|
|
|
|
CachePath: testDataPath + "/cache",
|
|
|
|
OriginalsPath: testDataPath + "/originals",
|
|
|
|
ImportPath: testDataPath + "/import",
|
|
|
|
ExportPath: testDataPath + "/export",
|
|
|
|
DatabaseDriver: "mysql",
|
2019-11-07 20:31:50 +01:00
|
|
|
DatabaseDsn: "photoprism:photoprism@tcp(photoprism-db:4001)/photoprism?parseTime=true",
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:00:15 +02:00
|
|
|
func NewTestParamsError() *Params {
|
2020-01-12 14:00:56 +01:00
|
|
|
assetsPath := fs.ExpandFilename("../..")
|
2019-07-17 12:00:15 +02:00
|
|
|
|
|
|
|
testDataPath := testDataPath("../../assets")
|
|
|
|
|
|
|
|
c := &Params{
|
|
|
|
DarktableBin: "/usr/bin/darktable-cli",
|
|
|
|
AssetsPath: assetsPath,
|
|
|
|
CachePath: testDataPath + "/cache",
|
|
|
|
OriginalsPath: testDataPath + "/originals",
|
|
|
|
ImportPath: testDataPath + "/import",
|
|
|
|
ExportPath: testDataPath + "/export",
|
|
|
|
DatabaseDriver: "mysql",
|
2019-11-07 20:31:50 +01:00
|
|
|
DatabaseDsn: "photoprism:photoprism@tcp(photoprism-db:4001)/photoprism?parseTime=true",
|
2019-07-17 12:00:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
func TestConfig() *Config {
|
2020-01-06 02:14:17 +01:00
|
|
|
once.Do(func() {
|
2019-05-06 23:18:10 +02:00
|
|
|
testConfig = NewTestConfig()
|
2020-01-06 02:14:17 +01:00
|
|
|
})
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
return testConfig
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
func NewTestConfig() *Config {
|
2019-12-02 00:30:58 +01:00
|
|
|
log.SetLevel(logrus.DebugLevel)
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
c := &Config{config: NewTestParams()}
|
2019-06-03 22:58:15 +02:00
|
|
|
err := c.Init(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed init config: %v", err)
|
|
|
|
}
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2020-01-06 02:14:17 +01:00
|
|
|
c.DropTables()
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
c.MigrateDb()
|
2020-01-06 02:14:17 +01:00
|
|
|
|
|
|
|
c.ImportSQL(c.ExamplesPath() + "/fixtures.sql")
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
thumb.JpegQuality = c.ThumbQuality()
|
2020-01-13 11:07:09 +01:00
|
|
|
thumb.PreRenderSize = c.ThumbSize()
|
|
|
|
thumb.MaxRenderSize = c.ThumbLimit()
|
|
|
|
thumb.Algorithm = c.ThumbAlgorithm()
|
2020-01-06 14:32:15 +01:00
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:00:15 +02:00
|
|
|
func NewTestErrorConfig() *Config {
|
2019-12-02 00:30:58 +01:00
|
|
|
log.SetLevel(logrus.DebugLevel)
|
2019-07-17 12:00:15 +02:00
|
|
|
|
|
|
|
c := &Config{config: NewTestParamsError()}
|
|
|
|
err := c.Init(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed init config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.MigrateDb()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
// Returns example cli config for testing
|
2019-05-03 18:57:28 +02:00
|
|
|
func CliTestContext() *cli.Context {
|
2019-05-06 23:18:10 +02:00
|
|
|
config := NewTestParams()
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Bool("debug", false, "doc")
|
|
|
|
globalSet.String("config-file", config.ConfigFile, "doc")
|
|
|
|
globalSet.String("assets-path", config.AssetsPath, "doc")
|
|
|
|
globalSet.String("originals-path", config.OriginalsPath, "doc")
|
2019-06-06 14:44:29 +02:00
|
|
|
globalSet.String("darktable-cli", config.DarktableBin, "doc")
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
app := cli.NewApp()
|
|
|
|
|
|
|
|
c := cli.NewContext(app, globalSet, nil)
|
|
|
|
|
|
|
|
c.Set("config-file", config.ConfigFile)
|
|
|
|
c.Set("assets-path", config.AssetsPath)
|
|
|
|
c.Set("originals-path", config.OriginalsPath)
|
2019-06-06 14:44:29 +02:00
|
|
|
c.Set("darktable-cli", config.DarktableBin)
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) RemoveTestData(t *testing.T) {
|
2019-05-03 18:57:28 +02:00
|
|
|
os.RemoveAll(c.ImportPath())
|
|
|
|
os.RemoveAll(c.ExportPath())
|
|
|
|
os.RemoveAll(c.OriginalsPath())
|
|
|
|
os.RemoveAll(c.CachePath())
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) DownloadTestData(t *testing.T) {
|
2020-01-12 14:00:56 +01:00
|
|
|
if fs.FileExists(TestDataZip) {
|
|
|
|
hash := fs.Hash(TestDataZip)
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
if hash != TestDataHash {
|
|
|
|
os.Remove(TestDataZip)
|
2019-05-04 05:25:00 +02:00
|
|
|
t.Logf("removed outdated test data zip file (fingerprint %s)\n", hash)
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(TestDataZip) {
|
2019-05-04 05:25:00 +02:00
|
|
|
fmt.Printf("downloading latest test data zip file from %s\n", TestDataURL)
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if err := fs.Download(TestDataZip, TestDataURL); err != nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
fmt.Printf("Download failed: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) UnzipTestData(t *testing.T) {
|
2020-01-12 14:00:56 +01:00
|
|
|
if _, err := fs.Unzip(TestDataZip, testDataPath(c.AssetsPath())); err != nil {
|
2019-05-04 05:25:00 +02:00
|
|
|
t.Logf("could not unzip test data: %s\n", err.Error())
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) InitializeTestData(t *testing.T) {
|
2019-05-04 05:25:00 +02:00
|
|
|
t.Log("initializing test data")
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
c.RemoveTestData(t)
|
|
|
|
|
|
|
|
c.DownloadTestData(t)
|
|
|
|
|
|
|
|
c.UnzipTestData(t)
|
|
|
|
}
|