Add tests for reading config files

This commit is contained in:
Aaron Lindsay 2017-12-09 21:06:57 -05:00
parent ac0ff45f7f
commit ac8afec6c1
7 changed files with 226 additions and 48 deletions

View File

@ -59,8 +59,9 @@ script:
- touch $GOPATH/src/github.com/aclindsa/moneygo/internal/handlers/cusip_list.csv
# Build and test MoneyGo
- go generate -v github.com/aclindsa/moneygo/internal/handlers
- go test -v -covermode=count -coverpkg github.com/aclindsa/moneygo/internal/config,github.com/aclindsa/moneygo/internal/handlers,github.com/aclindsa/moneygo/internal/models,github.com/aclindsa/moneygo/internal/store,github.com/aclindsa/moneygo/internal/store/db -coverprofile=coverage.out github.com/aclindsa/moneygo/internal/integration
- go test -v -covermode=count -coverpkg github.com/aclindsa/moneygo/internal/config,github.com/aclindsa/moneygo/internal/handlers,github.com/aclindsa/moneygo/internal/models,github.com/aclindsa/moneygo/internal/store,github.com/aclindsa/moneygo/internal/store/db -coverprofile=integration_coverage.out github.com/aclindsa/moneygo/internal/integration
- go test -v -covermode=count -coverpkg github.com/aclindsa/moneygo/internal/config,github.com/aclindsa/moneygo/internal/handlers,github.com/aclindsa/moneygo/internal/models,github.com/aclindsa/moneygo/internal/store,github.com/aclindsa/moneygo/internal/store/db -coverprofile=config_coverage.out github.com/aclindsa/moneygo/internal/config
# Report the test coverage
after_script:
- $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN
- $GOPATH/bin/goveralls -coverprofile=integration_coverage.out,config_coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN

View File

@ -65,14 +65,14 @@ cusip_list.csv file and re-run the `go generate ...` command.
MoneyGo requires HTTPS or FCGI (no HTTP). Before starting the server, you will
want to edit the example configuration file
(src/github.com/aclindsa/moneygo/example_config.ini) to point to your own SSL
certificate/key OR set 'generate-certs-if-absent = true' in the '[http]' section
of the config file.
(src/github.com/aclindsa/moneygo/internal/config/example_config.ini) to point to
your own SSL certificate/key OR set 'generate-certs-if-absent = true' in the
'[http]' section of the config file.
Then, assuming you're in the same directory you ran the above installation
commands from, running MoneyGo is as easy as:
./bin/moneygo -config src/github.com/aclindsa/moneygo/example_config.ini
./bin/moneygo -config src/github.com/aclindsa/moneygo/internal/config/example_config.ini
You should then be able to explore MoneyGo by visiting https://localhost:8443 in
your browser. Editing the configuration file supplied will allow you to modify

View File

@ -1,42 +0,0 @@
[moneygo]
# Whether to serve as FastCGI (default is false, for HTTPS)
fcgi = false
# Port on which to serve HTTPS or FCGI
port = 8443
# Base directory for serving files out of. This should point to the root of the
# moneygo source directory
base-directory = src/github.com/aclindsa/moneygo/
# Type of database being used (sqlite3, mysql, postgres)
db-type = sqlite3
# 'Data Source Name' for the database being used. This is driver-specific. See
# the following examples and external resources for more information about
# configuring this for your particular database configuration:
#
# Sqlite example DSN: "file:moneygo.sqlite?cache=shared&mode=rwc"
# MySQL documentation: https://github.com/go-sql-driver/mysql/#dsn-data-source-name
# example DSN: "user:password@tcp(localhost)/dbname&parseTime=true"
# (Note: MySQL DSN's *must* include the
# "parseTime=true" parameter)
# Postgres documentation: https://godoc.org/github.com/lib/pq
# example DSN: "postgres://user:password@localhost/dbname"
db-dsn = file:moneygo.sqlite?cache=shared&mode=rwc
[https]
# If 'fcgi = false', the following paths to a SSL certificate and the paired
# private key are used when serving HTTPS
cert-file = ./cert.pem
key-file = ./key.pem
# Attempt to generate self-signed certificates if the certificate files
# specified above are missing or invalid. This should *never* be set to 'true'
# for any environment where security is important (including but not limited to
# production systems)
generate-certs-if-absent = false
# A CSV list of hostnames to generate the above certs for
generate-certs-hosts = localhost,127.0.0.1

1
example_config.ini Symbolic link
View File

@ -0,0 +1 @@
internal/config/testdata/sqlite_https_config.ini

View File

@ -0,0 +1,92 @@
package config_test
import (
"github.com/aclindsa/moneygo/internal/config"
"testing"
)
func TestSqliteHTTPSConfig(t *testing.T) {
cfg, err := config.ReadConfig("./testdata/sqlite_https_config.ini")
if err != nil {
t.Fatalf("Unexpected error parsing config: %s\n", err)
}
if cfg.MoneyGo.Fcgi {
t.Errorf("MoneyGo.Fcgi unexpectedly true")
}
if cfg.MoneyGo.Port != 8443 {
t.Errorf("MoneyGo.Port %d instead of 8443", cfg.MoneyGo.Port)
}
if cfg.MoneyGo.Basedir != "src/github.com/aclindsa/moneygo/" {
t.Errorf("MoneyGo.Basedir not correct")
}
if cfg.MoneyGo.DBType != config.SQLite {
t.Errorf("MoneyGo.DBType not config.SQLite")
}
if cfg.MoneyGo.DSN != "file:moneygo.sqlite?cache=shared&mode=rwc" {
t.Errorf("MoneyGo.DSN not correct")
}
if cfg.Https.CertFile != "./cert.pem" {
t.Errorf("Https.CertFile '%s', not ./cert.pem", cfg.Https.CertFile)
}
if cfg.Https.KeyFile != "./key.pem" {
t.Errorf("Https.KeyFile '%s', not ./key.pem", cfg.Https.KeyFile)
}
if cfg.Https.GenerateCerts {
t.Errorf("Https.GenerateCerts not false")
}
if cfg.Https.GenerateCertsHosts != "localhost,127.0.0.1" {
t.Errorf("Https.GenerateCertsHosts '%s', not localhost", cfg.Https.GenerateCertsHosts)
}
}
func TestPostgresFcgiConfig(t *testing.T) {
cfg, err := config.ReadConfig("./testdata/postgres_fcgi_config.ini")
if err != nil {
t.Fatalf("Unexpected error parsing config: %s\n", err)
}
if !cfg.MoneyGo.Fcgi {
t.Errorf("MoneyGo.Fcgi unexpectedly false")
}
if cfg.MoneyGo.Port != 9001 {
t.Errorf("MoneyGo.Port %d instead of 9001", cfg.MoneyGo.Port)
}
if cfg.MoneyGo.Basedir != "src/github.com/aclindsa/moneygo/" {
t.Errorf("MoneyGo.Basedir not correct")
}
if cfg.MoneyGo.DBType != config.Postgres {
t.Errorf("MoneyGo.DBType not config.Postgres")
}
if cfg.MoneyGo.DSN != "postgres://moneygo_test@localhost/moneygo_test?sslmode=disable" {
t.Errorf("MoneyGo.DSN not correct")
}
}
func TestGenerateCertsConfig(t *testing.T) {
cfg, err := config.ReadConfig("./testdata/generate_certs_config.ini")
if err != nil {
t.Fatalf("Unexpected error parsing config: %s\n", err)
}
if cfg.Https.CertFile != "./local_cert.pem" {
t.Errorf("Https.CertFile '%s', not ./local_cert.pem", cfg.Https.CertFile)
}
if cfg.Https.KeyFile != "./local_key.pem" {
t.Errorf("Https.KeyFile '%s', not ./local_key.pem", cfg.Https.KeyFile)
}
if !cfg.Https.GenerateCerts {
t.Errorf("Https.GenerateCerts not true")
}
if cfg.Https.GenerateCertsHosts != "example.com" {
t.Errorf("Https.GenerateCertsHosts '%s', not example.com", cfg.Https.GenerateCertsHosts)
}
}
func TestNonexistentConfig(t *testing.T) {
cfg, err := config.ReadConfig("./testdata/nonexistent_config.ini")
if err == nil || cfg != nil {
t.Fatalf("Expected error parsing nonexistent config")
}
}

View File

@ -0,0 +1,42 @@
[moneygo]
# Whether to serve as FastCGI (default is false, for HTTPS)
fcgi = false
# Port on which to serve HTTPS or FCGI
port = 8443
# Base directory for serving files out of. This should point to the root of the
# moneygo source directory
base-directory = src/github.com/aclindsa/moneygo/
# Type of database being used (sqlite3, mysql, postgres)
db-type = sqlite3
# 'Data Source Name' for the database being used. This is driver-specific. See
# the following examples and external resources for more information about
# configuring this for your particular database configuration:
#
# Sqlite example DSN: "file:moneygo.sqlite?cache=shared&mode=rwc"
# MySQL documentation: https://github.com/go-sql-driver/mysql/#dsn-data-source-name
# example DSN: "user:password@tcp(localhost)/dbname&parseTime=true"
# (Note: MySQL DSN's *must* include the
# "parseTime=true" parameter)
# Postgres documentation: https://godoc.org/github.com/lib/pq
# example DSN: "postgres://user:password@localhost/dbname"
db-dsn = file:moneygo.sqlite?cache=shared&mode=rwc
[https]
# If 'fcgi = false', the following paths to a SSL certificate and the paired
# private key are used when serving HTTPS
cert-file = ./local_cert.pem
key-file = ./local_key.pem
# Attempt to generate self-signed certificates if the certificate files
# specified above are missing or invalid. This should *never* be set to 'true'
# for any environment where security is important (including but not limited to
# production systems)
generate-certs-if-absent = true
# A CSV list of hostnames to generate the above certs for
generate-certs-hosts = example.com

View File

@ -0,0 +1,42 @@
[moneygo]
# Whether to serve as FastCGI (default is false, for HTTPS)
fcgi = true
# Port on which to serve HTTPS or FCGI
port = 9001
# Base directory for serving files out of. This should point to the root of the
# moneygo source directory
base-directory = src/github.com/aclindsa/moneygo/
# Type of database being used (sqlite3, mysql, postgres)
db-type = postgres
# 'Data Source Name' for the database being used. This is driver-specific. See
# the following examples and external resources for more information about
# configuring this for your particular database configuration:
#
# Sqlite example DSN: "file:moneygo.sqlite?cache=shared&mode=rwc"
# MySQL documentation: https://github.com/go-sql-driver/mysql/#dsn-data-source-name
# example DSN: "user:password@tcp(localhost)/dbname&parseTime=true"
# (Note: MySQL DSN's *must* include the
# "parseTime=true" parameter)
# Postgres documentation: https://godoc.org/github.com/lib/pq
# example DSN: "postgres://user:password@localhost/dbname"
db-dsn = postgres://moneygo_test@localhost/moneygo_test?sslmode=disable
[https]
# If 'fcgi = false', the following paths to a SSL certificate and the paired
# private key are used when serving HTTPS
cert-file = ./cert.pem
key-file = ./key.pem
# Attempt to generate self-signed certificates if the certificate files
# specified above are missing or invalid. This should *never* be set to 'true'
# for any environment where security is important (including but not limited to
# production systems)
generate-certs-if-absent = false
# A CSV list of hostnames to generate the above certs for
generate-certs-hosts = localhost,127.0.0.1

View File

@ -0,0 +1,42 @@
[moneygo]
# Whether to serve as FastCGI (default is false, for HTTPS)
fcgi = false
# Port on which to serve HTTPS or FCGI
port = 8443
# Base directory for serving files out of. This should point to the root of the
# moneygo source directory
base-directory = src/github.com/aclindsa/moneygo/
# Type of database being used (sqlite3, mysql, postgres)
db-type = sqlite3
# 'Data Source Name' for the database being used. This is driver-specific. See
# the following examples and external resources for more information about
# configuring this for your particular database configuration:
#
# Sqlite example DSN: "file:moneygo.sqlite?cache=shared&mode=rwc"
# MySQL documentation: https://github.com/go-sql-driver/mysql/#dsn-data-source-name
# example DSN: "user:password@tcp(localhost)/dbname&parseTime=true"
# (Note: MySQL DSN's *must* include the
# "parseTime=true" parameter)
# Postgres documentation: https://godoc.org/github.com/lib/pq
# example DSN: "postgres://user:password@localhost/dbname"
db-dsn = file:moneygo.sqlite?cache=shared&mode=rwc
[https]
# If 'fcgi = false', the following paths to a SSL certificate and the paired
# private key are used when serving HTTPS
cert-file = ./cert.pem
key-file = ./key.pem
# Attempt to generate self-signed certificates if the certificate files
# specified above are missing or invalid. This should *never* be set to 'true'
# for any environment where security is important (including but not limited to
# production systems)
generate-certs-if-absent = false
# A CSV list of hostnames to generate the above certs for
generate-certs-hosts = localhost,127.0.0.1