mirror of
https://github.com/jc21/nginx-proxy-manager.git
synced 2024-08-30 18:22:48 +00:00
Add more unit tests
This commit is contained in:
parent
6584f87dd2
commit
db95a465c8
51
backend/internal/cache/cache.go
vendored
51
backend/internal/cache/cache.go
vendored
@ -1,51 +0,0 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"npm/internal/entity/setting"
|
||||
"npm/internal/logger"
|
||||
)
|
||||
|
||||
// Cache is a memory cache
|
||||
type Cache struct {
|
||||
Settings *map[string]setting.Model
|
||||
}
|
||||
|
||||
// Status is the status of last update
|
||||
type Status struct {
|
||||
LastUpdate time.Time
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// NewCache will create and return a new Cache object
|
||||
func NewCache() *Cache {
|
||||
return &Cache{
|
||||
Settings: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh will refresh all cache items
|
||||
func (c *Cache) Refresh() {
|
||||
c.RefreshSettings()
|
||||
}
|
||||
|
||||
// Clear will clear the cache
|
||||
func (c *Cache) Clear() {
|
||||
c.Settings = nil
|
||||
}
|
||||
|
||||
// RefreshSettings will refresh the settings from db
|
||||
func (c *Cache) RefreshSettings() {
|
||||
logger.Info("Cache refreshing Settings")
|
||||
/*
|
||||
c.ProductOffers = client.GetProductOffers()
|
||||
|
||||
if c.ProductOffers != nil {
|
||||
c.Status["product_offers"] = Status{
|
||||
LastUpdate: time.Now(),
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type acmesh struct {
|
||||
Home string `json:"home" envconfig:"optional,default=/data/.acme.sh"`
|
||||
ConfigHome string `json:"config_home" envconfig:"optional,default=/data/.acme.sh/config"`
|
||||
CertHome string `json:"cert_home" envconfig:"optional,default=/data/.acme.sh/certs"`
|
||||
}
|
||||
|
||||
// GetWellknown returns the well known path
|
||||
func (a *acmesh) GetWellknown() string {
|
||||
return fmt.Sprintf("%s/.well-known", a.Home)
|
||||
}
|
@ -18,7 +18,9 @@ func Init(version, commit *string) {
|
||||
fmt.Printf("%+v\n", err)
|
||||
}
|
||||
|
||||
initLogger()
|
||||
if err := initLogger(); err != nil {
|
||||
logger.Error("LoggerConfigurationError", err)
|
||||
}
|
||||
}
|
||||
|
||||
// InitIPRanges will initialise the config for the ipranges command
|
||||
@ -26,12 +28,13 @@ func InitIPRanges(version, commit *string) error {
|
||||
Version = *version
|
||||
Commit = *commit
|
||||
err := envconfig.InitWithPrefix(&Configuration, "NPM")
|
||||
// nolint: errcheck, gosec
|
||||
initLogger()
|
||||
return err
|
||||
}
|
||||
|
||||
// Init initialises the Log object and return it
|
||||
func initLogger() {
|
||||
func initLogger() error {
|
||||
// this removes timestamp prefixes from logs
|
||||
golog.SetFlags(0)
|
||||
|
||||
@ -46,14 +49,10 @@ func initLogger() {
|
||||
logLevel = logger.InfoLevel
|
||||
}
|
||||
|
||||
err := logger.Configure(&logger.Config{
|
||||
return logger.Configure(&logger.Config{
|
||||
LogThreshold: logLevel,
|
||||
Formatter: Configuration.Log.Format,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error("LoggerConfigurationError", err)
|
||||
}
|
||||
}
|
||||
|
||||
// GetLogLevel returns the logger const level
|
||||
|
129
backend/internal/config/config_test.go
Normal file
129
backend/internal/config/config_test.go
Normal file
@ -0,0 +1,129 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"npm/internal/logger"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
t.Setenv("NPM_DATA_FOLDER", "/path/to/some/data/folder")
|
||||
t.Setenv("NPM_LOG_LEVEL", "warn")
|
||||
t.Setenv("NPM_DB_DRIVER", "postgres")
|
||||
t.Setenv("NPM_DB_HOST", "1.1.1.1")
|
||||
t.Setenv("NPM_DB_PORT", "5432")
|
||||
t.Setenv("NPM_DB_USERNAME", "rootuser")
|
||||
t.Setenv("NPM_DB_PASSWORD", "4metoremember")
|
||||
t.Setenv("NPM_DB_NAME", "npm")
|
||||
t.Setenv("NPM_DISABLE_IPV4", "false")
|
||||
t.Setenv("NPM_DISABLE_IPV6", "true")
|
||||
|
||||
version := "999.999.999"
|
||||
commit := "abcd123"
|
||||
Init(&version, &commit)
|
||||
err := InitIPRanges(&version, &commit)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, "/path/to/some/data/folder", Configuration.DataFolder)
|
||||
assert.Equal(t, false, Configuration.DisableIPV4)
|
||||
assert.Equal(t, true, Configuration.DisableIPV6)
|
||||
assert.Equal(t, "/data/.acme.sh", Configuration.Acmesh.Home)
|
||||
assert.Equal(t, "disable", Configuration.DB.SSLMode)
|
||||
assert.Equal(t, logger.WarnLevel, logger.GetLogLevel())
|
||||
|
||||
assert.Equal(t, "postgres", Configuration.DB.Driver)
|
||||
assert.Equal(t, "1.1.1.1", Configuration.DB.Host)
|
||||
assert.Equal(t, 5432, Configuration.DB.Port)
|
||||
assert.Equal(t, "rootuser", Configuration.DB.Username)
|
||||
assert.Equal(t, "4metoremember", Configuration.DB.Password)
|
||||
assert.Equal(t, "npm", Configuration.DB.Name)
|
||||
assert.Equal(t, "postgres", Configuration.DB.GetDriver())
|
||||
}
|
||||
|
||||
func TestConnectURLs(t *testing.T) {
|
||||
type want struct {
|
||||
gorm string
|
||||
dbmate string
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
envs []string
|
||||
want want
|
||||
}{
|
||||
{
|
||||
name: "sqlite",
|
||||
envs: []string{
|
||||
"NPM_DB_DRIVER=sqlite",
|
||||
"NPM_DATA_FOLDER=/path/to/data",
|
||||
},
|
||||
want: want{
|
||||
gorm: "/path/to/data/nginxproxymanager.db",
|
||||
dbmate: "sqlite:/path/to/data/nginxproxymanager.db",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "postgres",
|
||||
envs: []string{
|
||||
"NPM_DB_DRIVER=postgres",
|
||||
"NPM_DB_HOST=2.2.2.2",
|
||||
"NPM_DB_PORT=9824",
|
||||
"NPM_DB_USERNAME=postgresuser",
|
||||
"NPM_DB_PASSWORD=pgpass",
|
||||
"NPM_DB_SSLMODE=strict",
|
||||
"NPM_DB_NAME=npm",
|
||||
},
|
||||
want: want{
|
||||
gorm: "host=2.2.2.2 user=postgresuser password=pgpass dbname=npm port=9824 sslmode=strict TimeZone=UTC",
|
||||
dbmate: "postgres://postgresuser:pgpass@2.2.2.2:9824/npm?sslmode=strict",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "mysql",
|
||||
envs: []string{
|
||||
"NPM_DB_DRIVER=mysql",
|
||||
"NPM_DB_HOST=3.3.3.3",
|
||||
"NPM_DB_PORT=3307",
|
||||
"NPM_DB_USERNAME=mysqluser",
|
||||
"NPM_DB_PASSWORD=mypass",
|
||||
"NPM_DB_NAME=npm",
|
||||
},
|
||||
want: want{
|
||||
gorm: "mysqluser:mypass@tcp(3.3.3.3:3307)/npm?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
dbmate: "mysql://mysqluser:mypass@3.3.3.3:3307/npm",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
version := "999.999.999"
|
||||
commit := "abcd123"
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
for _, env := range tt.envs {
|
||||
parts := strings.Split(env, "=")
|
||||
if len(parts) == 2 {
|
||||
t.Setenv(parts[0], parts[1])
|
||||
}
|
||||
}
|
||||
Init(&version, &commit)
|
||||
assert.Equal(t, tt.want.gorm, Configuration.DB.GetGormConnectURL())
|
||||
assert.Equal(t, tt.want.dbmate, Configuration.DB.GetDBMateConnectURL())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateDataFolders(t *testing.T) {
|
||||
t.Setenv("NPM_DATA_FOLDER", "/tmp/npmtest")
|
||||
|
||||
version := "999.999.999"
|
||||
commit := "abcd123"
|
||||
Init(&version, &commit)
|
||||
CreateDataFolders()
|
||||
|
||||
_, err := os.Stat("/tmp/npmtest/nginx/hosts")
|
||||
assert.Nil(t, err)
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"npm/internal/logger"
|
||||
)
|
||||
|
||||
@ -15,11 +16,6 @@ var IsSetup bool
|
||||
|
||||
var logLevel logger.Level
|
||||
|
||||
type log struct {
|
||||
Level string `json:"level" envconfig:"optional,default=info"`
|
||||
Format string `json:"format" envconfig:"optional,default=nice"`
|
||||
}
|
||||
|
||||
// Configuration is the main configuration object
|
||||
var Configuration struct {
|
||||
DataFolder string `json:"data_folder" envconfig:"optional,default=/data"`
|
||||
@ -29,3 +25,19 @@ var Configuration struct {
|
||||
DB db `json:"db"`
|
||||
Log log `json:"log"`
|
||||
}
|
||||
|
||||
type log struct {
|
||||
Level string `json:"level" envconfig:"optional,default=info"`
|
||||
Format string `json:"format" envconfig:"optional,default=nice"`
|
||||
}
|
||||
|
||||
type acmesh struct {
|
||||
Home string `json:"home" envconfig:"optional,default=/data/.acme.sh"`
|
||||
ConfigHome string `json:"config_home" envconfig:"optional,default=/data/.acme.sh/config"`
|
||||
CertHome string `json:"cert_home" envconfig:"optional,default=/data/.acme.sh/certs"`
|
||||
}
|
||||
|
||||
// GetWellknown returns the well known path
|
||||
func (a *acmesh) GetWellknown() string {
|
||||
return fmt.Sprintf("%s/.well-known", a.Home)
|
||||
}
|
||||
|
14
backend/internal/config/vars_test.go
Normal file
14
backend/internal/config/vars_test.go
Normal file
@ -0,0 +1,14 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAcmeshGetWellknown(t *testing.T) {
|
||||
a := acmesh{
|
||||
Home: "/data/.acme.sh",
|
||||
}
|
||||
assert.Equal(t, "/data/.acme.sh/.well-known", a.GetWellknown())
|
||||
}
|
Loading…
Reference in New Issue
Block a user