ezshare/config/config_test.go

252 lines
6.1 KiB
Go
Raw Normal View History

2021-12-08 08:42:12 +00:00
package config_test
import (
"os"
2021-12-08 08:42:12 +00:00
"strings"
"testing"
"gitea.benny.dog/torjus/ezshare/config"
)
var configStrExample = `
########################
# Server configuration #
########################
[Server]
# Set server log-level
# Must be one of: DEBUG, INFO, WARN, ERROR
# Default: INFO
LogLevel = "INFO"
# Server hostname
# Used for generating links
# Required
Hostname = "localhost"
# Endpoint reachable by clients
# Fetched by clients for automatic setup
# Required
GRPCEndpoint = "localhost:50051"
# File store configuration
[Server.FileStore]
# How server stores file
# Must be one of: filesystem, memory, bolt
# Required
Type = "bolt"
[Server.FileStore.Bolt]
# Where the bolt-db is stored
# Required if store-type is bolt
Path = "/data/files.db"
[Server.FileStore.Filesystem]
# Where files are stored
# Required if store-type is filesystem
Dir = "/data"
[Server.DataStore]
# What store to use for users, certs and binaries
# Must be one of: memory, bolt
# Required
Type = "bolt"
[Server.DataStore.Bolt]
# Path to bolt database-file
# Required if Server.Datastore is bolt
Path = "/data/users.db"
# GRPC Configuration
[Server.GRPC]
# Address to listen to
# Default: :50051
ListenAddr = ":50051"
# GRPC Certificate Configuration
[Server.GRPC.CACerts]
# Path of PEM-encoded certificate file
CertificatePath = "/data/ca.pem"
# Path of PEM-encoded private key
# Must be of type ecdsa
CertificateKeyPath = "/data/ca.key"
[Server.GRPC.Certs]
# Path of PEM-encoded certificate file
CertificatePath = "/data/server.pem"
# Path of PEM-encoded private key
# Must be of type ecdsa
CertificateKeyPath = "/data/server.key"
[Server.HTTP]
# Address to listen to
# Default: :8089
ListenAddr = ":8089"
########################
# Client configuration #
########################
[Client]
# Server used if not specified using command-line
DefaultServer = "localhost:50051"
# Path to PEM-encoder server-certificate
ServerCertPath = "/data/server.pem"
[Client.Certs]
# Path of PEM-encoded certificate file
CertificatePath = "/data/client.pem"
# Path of PEM-encoded private key
# Must be of type ecdsa
CertificateKeyPath = "/data/client.key"
`
var configStrValidClient = `
[Client]
DefaultServer = "localhost:50051"
ServerCertPath = "/data/server.pem"
[Client.Certs]
CertificatePath = "/data/client.pem"
CertificateKeyPath = "/data/client.key"
`
var configStrValidServerMinimal = `
[Server]
LogLevel = "INFO"
Hostname = "localhost"
GRPCEndpoint = "localhost:50051"
[Server.FileStore]
Type = "memory"
[Server.DataStore]
Type = "memory"
[Server.GRPC]
ListenAddr = ":50051"
[Server.GRPC.CACerts]
CertificatePath = "/data/ca.pem"
CertificateKeyPath = "/data/ca.key"
[Server.GRPC.Certs]
CertificatePath = "/data/server.pem"
CertificateKeyPath = "/data/server.key"
[Server.HTTP]
ListenAddr = ":8089"
`
var configStrInvalidServerMissingStoreConfig = `
[Server]
LogLevel = "INFO"
Hostname = "localhost"
GRPCEndpoint = "localhost:50051"
[Server.FileStore]
Type = "bolt"
[Server.DataStore]
Type = "memory"
[Server.GRPC]
ListenAddr = ":50051"
[Server.GRPC.CACerts]
CertificatePath = "/data/ca.pem"
CertificateKeyPath = "/data/ca.key"
[Server.GRPC.Certs]
CertificatePath = "/data/server.pem"
CertificateKeyPath = "/data/server.key"
[Server.HTTP]
ListenAddr = ":8089"
`
func TestConfig(t *testing.T) {
t.Run("TestValid", func(t *testing.T) {
testCases := []struct {
Name string
ConfigString string
ValidForClient bool
ValidForServer bool
}{
{
Name: "ExampleConfig",
ConfigString: configStrExample,
ValidForClient: true,
ValidForServer: true,
},
{
Name: "ServerValidMinimal",
ConfigString: configStrValidServerMinimal,
ValidForServer: true,
},
{
Name: "ClientValidMinimal",
ConfigString: configStrValidClient,
ValidForClient: true,
},
{
Name: "ServerInvalidMissingStoreConfig",
ConfigString: configStrInvalidServerMissingStoreConfig,
},
}
for _, c := range testCases {
t.Run(c.Name, func(t *testing.T) {
sr := strings.NewReader(c.ConfigString)
cfg, err := config.FromReader(sr)
if err != nil {
t.Fatalf("Error reading config: %s", err)
}
2021-12-08 12:12:16 +00:00
if c.ValidForClient && !(cfg.Client.Valid() == nil) {
t.Errorf("Valid config ValidClientConfig returned wrong result: %s", cfg.Client.Valid())
2021-12-08 08:42:12 +00:00
}
2021-12-08 12:12:16 +00:00
if !c.ValidForClient && (cfg.Client.Valid() == nil) {
t.Errorf("Invalid config ValidClientConfig returned wrong result: %s", cfg.Client.Valid())
2021-12-08 08:42:12 +00:00
}
2021-12-08 12:12:16 +00:00
if c.ValidForServer && !(cfg.Server.Valid() == nil) {
t.Errorf("Valid config ValidServerConfig returned wrong result: %s", cfg.Server.Valid())
2021-12-08 08:42:12 +00:00
}
2021-12-08 12:12:16 +00:00
if !c.ValidForServer && (cfg.Server.Valid() == nil) {
t.Errorf("Invalid config ValidServerConfig returned wrong result: %s", cfg.Server.Valid())
2021-12-08 08:42:12 +00:00
}
})
}
})
t.Run("FromEnv", func(t *testing.T) {
// Unset any existing ezshare env vars
for _, env := range os.Environ() {
if strings.HasPrefix(env, "EZSHARE") {
os.Unsetenv(env)
}
}
cfg := config.FromDefault()
// Test Server.LogLevel
if cfg.Server.LogLevel == "WARN" {
t.Errorf("Loglevel is WARN before updating from env.")
}
os.Setenv("EZSHARE_SERVER_LOGLEVEL", "WARN")
cfg.UpdateFromEnv()
if cfg.Server.LogLevel != "WARN" {
t.Errorf("Loglevel is not WARN after updating from env.")
}
// Test Server.Hostname
hostname := "https://share.example.org"
os.Setenv("EZSHARE_SERVER_HOSTNAME", hostname)
cfg.UpdateFromEnv()
if cfg.Server.Hostname != hostname {
t.Errorf("Hostname is incorrect after updating from env.")
}
// Test Server.Datastore.Bolt.Path
boltPath := "/data/bolt.db"
os.Setenv("EZSHARE_SERVER_DATASTORE_BOLT_PATH", boltPath)
cfg.UpdateFromEnv()
if cfg.Server.DataStoreConfig.BoltStoreConfig.Path != boltPath {
t.Errorf("Bolt path is incorrect after updating from env.")
}
// Test Server.Datastore.Bolt.Path
caCertPath := "/data/cert.pem"
os.Setenv("EZSHARE_SERVER_GRPC_CACERTS_CERTIFICATEKEYPATH", caCertPath)
cfg.UpdateFromEnv()
if cfg.Server.GRPC.CACerts.CertificateKeyPath != caCertPath {
t.Errorf("GPRC CA Cert path is incorrect after updating from env.")
}
})
2021-12-08 08:42:12 +00:00
}