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"
|
|
|
|
"os"
|
2020-01-06 02:14:17 +01:00
|
|
|
"sync"
|
2019-05-03 18:57:28 +02:00
|
|
|
"testing"
|
2020-04-30 14:32:48 +02:00
|
|
|
"time"
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
2020-04-30 14:32:48 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-01-06 14:32:15 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2020-04-30 15:41:47 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/capture"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2019-05-03 18:57:28 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// define constants used for testing the config package
|
2019-05-03 18:57:28 +02:00
|
|
|
const (
|
|
|
|
TestDataZip = "/tmp/photoprism/testdata.zip"
|
2019-05-04 00:53:02 +02:00
|
|
|
TestDataURL = "https://dl.photoprism.org/fixtures/testdata.zip"
|
2020-01-26 10:22:27 +01:00
|
|
|
TestDataHash = "be394d5bee8a5634d415e9e0663eef20b5604510" // sha1sum
|
2019-05-03 18:57:28 +02:00
|
|
|
)
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
var testConfig *Config
|
2020-04-13 18:08:21 +02:00
|
|
|
var testConfigOnce sync.Once
|
2020-04-09 19:39:09 +02:00
|
|
|
var testConfigMutex sync.Mutex
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
func testDataPath(assetsPath string) string {
|
|
|
|
return assetsPath + "/testdata"
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewTestParams inits valid params used for testing
|
2019-05-06 23:18:10 +02:00
|
|
|
func NewTestParams() *Params {
|
2020-01-31 15:29:06 +01:00
|
|
|
assetsPath := fs.Abs("../../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{
|
2020-04-30 15:41:47 +02:00
|
|
|
Debug: true,
|
2019-11-12 05:49:10 +01:00
|
|
|
Public: true,
|
|
|
|
ReadOnly: false,
|
2020-01-13 16:48:32 +01:00
|
|
|
DetectNSFW: true,
|
2019-12-15 17:19:16 +01:00
|
|
|
UploadNSFW: false,
|
2019-06-06 14:44:29 +02:00
|
|
|
DarktableBin: "/usr/bin/darktable-cli",
|
2020-04-30 15:41:47 +02:00
|
|
|
ExifToolBin: "/usr/bin/exiftool",
|
2019-05-03 18:57:28 +02:00
|
|
|
AssetsPath: assetsPath,
|
|
|
|
CachePath: testDataPath + "/cache",
|
|
|
|
OriginalsPath: testDataPath + "/originals",
|
|
|
|
ImportPath: testDataPath + "/import",
|
2020-04-06 22:50:04 +02:00
|
|
|
TempPath: testDataPath + "/temp",
|
2019-05-03 18:57:28 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewTestParamsError inits invalid params used for testing
|
2019-07-17 12:00:15 +02:00
|
|
|
func NewTestParamsError() *Params {
|
2020-01-31 15:29:06 +01:00
|
|
|
assetsPath := fs.Abs("../..")
|
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",
|
2020-04-06 22:50:04 +02:00
|
|
|
TempPath: testDataPath + "/temp",
|
2019-07-17 12:00:15 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
func SetNewTestConfig() {
|
|
|
|
testConfig = NewTestConfig()
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// TestConfig inits the global testConfig if it was not already initialised
|
2019-05-06 23:18:10 +02:00
|
|
|
func TestConfig() *Config {
|
2020-04-13 18:08:21 +02:00
|
|
|
testConfigOnce.Do(SetNewTestConfig)
|
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
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewTestConfig inits valid config used for testing
|
2019-05-06 23:18:10 +02:00
|
|
|
func NewTestConfig() *Config {
|
2020-04-30 15:41:47 +02:00
|
|
|
defer log.Debug(capture.Time(time.Now(), "config: new test config created"))
|
|
|
|
|
2020-04-09 19:39:09 +02:00
|
|
|
testConfigMutex.Lock()
|
|
|
|
defer testConfigMutex.Unlock()
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
c := &Config{params: NewTestParams()}
|
2020-04-14 14:28:47 +02:00
|
|
|
c.initSettings()
|
2020-04-30 15:41:47 +02:00
|
|
|
|
|
|
|
if err := c.Init(context.Background()); err != nil {
|
|
|
|
log.Fatalf("config: %s", err.Error())
|
2019-06-03 22:58:15 +02:00
|
|
|
}
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2020-01-06 02:14:17 +01:00
|
|
|
c.DropTables()
|
|
|
|
|
2020-04-30 14:32:48 +02:00
|
|
|
// Make sure changes have been written to disk.
|
2020-04-30 15:41:47 +02:00
|
|
|
time.Sleep(250 * time.Millisecond)
|
2020-04-30 14:32:48 +02:00
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
c.MigrateDb()
|
2020-01-06 02:14:17 +01:00
|
|
|
|
2020-04-30 14:32:48 +02:00
|
|
|
// Make sure changes have been written to disk.
|
2020-04-30 15:41:47 +02:00
|
|
|
time.Sleep(250 * time.Millisecond)
|
2020-04-30 14:32:48 +02:00
|
|
|
|
|
|
|
entity.CreateTestFixtures(c.Db())
|
|
|
|
|
|
|
|
// TODO: Remove when new test fixtures are ready
|
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()
|
2020-01-13 13:46:05 +01:00
|
|
|
thumb.Filter = c.ThumbFilter()
|
2020-01-06 14:32:15 +01:00
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewTestErrorConfig inits invalid config used for testing
|
2019-07-17 12:00:15 +02:00
|
|
|
func NewTestErrorConfig() *Config {
|
2020-04-13 18:08:21 +02:00
|
|
|
c := &Config{params: NewTestParamsError()}
|
2019-07-17 12:00:15 +02:00
|
|
|
err := c.Init(context.Background())
|
|
|
|
if err != nil {
|
2020-04-30 15:41:47 +02:00
|
|
|
log.Fatalf("config: %s", err.Error())
|
2019-07-17 12:00:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c.MigrateDb()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// CliTestContext 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")
|
2020-01-31 15:29:06 +01:00
|
|
|
globalSet.String("import-path", config.OriginalsPath, "doc")
|
2020-04-06 22:50:04 +02:00
|
|
|
globalSet.String("temp-path", config.OriginalsPath, "doc")
|
2020-01-31 15:29:06 +01:00
|
|
|
globalSet.String("cache-path", config.OriginalsPath, "doc")
|
2019-06-06 14:44:29 +02:00
|
|
|
globalSet.String("darktable-cli", config.DarktableBin, "doc")
|
2020-01-13 16:48:32 +01:00
|
|
|
globalSet.Bool("detect-nsfw", config.DetectNSFW, "doc")
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
app := cli.NewApp()
|
2020-04-28 18:20:33 +02:00
|
|
|
app.Version = "test"
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
c := cli.NewContext(app, globalSet, nil)
|
|
|
|
|
2020-04-30 15:41:47 +02:00
|
|
|
LogError(c.Set("config-file", config.ConfigFile))
|
|
|
|
LogError(c.Set("assets-path", config.AssetsPath))
|
|
|
|
LogError(c.Set("originals-path", config.OriginalsPath))
|
|
|
|
LogError(c.Set("import-path", config.ImportPath))
|
|
|
|
LogError(c.Set("temp-path", config.TempPath))
|
|
|
|
LogError(c.Set("cache-path", config.CachePath))
|
|
|
|
LogError(c.Set("darktable-cli", config.DarktableBin))
|
|
|
|
LogError(c.Set("detect-nsfw", "true"))
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// RemoveTestData deletes files in import, export, originals and cache folders
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) RemoveTestData(t *testing.T) {
|
2020-04-30 16:44:04 +02:00
|
|
|
if err := os.RemoveAll(c.ImportPath()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(c.TempPath()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(c.OriginalsPath()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(c.CachePath()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// DownloadTestData downloads test data from photoprism.org server
|
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 {
|
2020-04-30 16:44:04 +02:00
|
|
|
if err := os.Remove(TestDataZip); err != nil {
|
|
|
|
t.Fatalf("config: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("config: removed outdated test data zip file (fingerprint %s)", hash)
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(TestDataZip) {
|
2020-04-30 16:44:04 +02:00
|
|
|
t.Logf("config: downloading latest test data zip file from %s", 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 {
|
2020-04-30 16:44:04 +02:00
|
|
|
t.Fatalf("config: test data download failed: %s", err.Error())
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// UnzipTestData in default test folder
|
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 {
|
2020-04-30 16:44:04 +02:00
|
|
|
t.Fatalf("config: could not unzip test data: %s", err.Error())
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// InitializeTestData using testing constant
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) InitializeTestData(t *testing.T) {
|
2020-04-30 16:44:04 +02:00
|
|
|
defer t.Logf(capture.Time(time.Now(), "config: initialized test data"))
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
c.RemoveTestData(t)
|
|
|
|
|
|
|
|
c.DownloadTestData(t)
|
|
|
|
|
|
|
|
c.UnzipTestData(t)
|
|
|
|
}
|