Files
athens/pkg/config/config_test.go
Manu Gupta b600753926 Check for file perms as provided in the conf module (#784)
* Check for file perms as provided in the conf module

* Fix whitespace

* Check for file permissions

* Use only Lstat for error handling and continue if file does not exist

* Update comments to make sure that the todo is more understandable

* conflicts resolved
2018-10-26 12:53:05 -07:00

415 lines
11 KiB
Go

package config
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
const exampleConfigPath = "../../config.dev.toml"
func compareConfigs(parsedConf *Config, expConf *Config, t *testing.T) {
opts := cmpopts.IgnoreTypes(StorageConfig{}, ProxyConfig{})
eq := cmp.Equal(parsedConf, expConf, opts)
if !eq {
t.Errorf("Parsed Example configuration did not match expected values. Expected: %+v. Actual: %+v", expConf, parsedConf)
}
eq = cmp.Equal(parsedConf.Proxy, expConf.Proxy)
if !eq {
t.Errorf("Parsed Example Proxy configuration did not match expected values. Expected: %+v. Actual: %+v", expConf.Proxy, parsedConf.Proxy)
}
compareStorageConfigs(parsedConf.Storage, expConf.Storage, t)
}
func compareStorageConfigs(parsedStorage *StorageConfig, expStorage *StorageConfig, t *testing.T) {
eq := cmp.Equal(parsedStorage.CDN, expStorage.CDN)
if !eq {
t.Errorf("Parsed Example Storage configuration did not match expected values. Expected: %+v. Actual: %+v", expStorage.CDN, parsedStorage.CDN)
}
eq = cmp.Equal(parsedStorage.Mongo, expStorage.Mongo)
if !eq {
t.Errorf("Parsed Example Storage configuration did not match expected values. Expected: %+v. Actual: %+v", expStorage.Mongo, parsedStorage.Mongo)
}
eq = cmp.Equal(parsedStorage.Minio, expStorage.Minio)
if !eq {
t.Errorf("Parsed Example Storage configuration did not match expected values. Expected: %+v. Actual: %+v", expStorage.Minio, parsedStorage.Minio)
}
eq = cmp.Equal(parsedStorage.Disk, expStorage.Disk)
if !eq {
t.Errorf("Parsed Example Storage configuration did not match expected values. Expected: %+v. Actual: %+v", expStorage.Disk, parsedStorage.Disk)
}
eq = cmp.Equal(parsedStorage.GCP, expStorage.GCP)
if !eq {
t.Errorf("Parsed Example Storage configuration did not match expected values. Expected: %+v. Actual: %+v", expStorage.GCP, parsedStorage.GCP)
}
eq = cmp.Equal(parsedStorage.S3, expStorage.S3)
if !eq {
t.Errorf("Parsed Example Storage configuration did not match expected values. Expected: %+v. Actual: %+v", expStorage.S3, parsedStorage.S3)
}
}
func TestEnvOverrides(t *testing.T) {
expProxy := ProxyConfig{
StorageType: "minio",
GlobalEndpoint: "mytikas.gomods.io",
Port: ":7000",
FilterOff: false,
BasicAuthUser: "testuser",
BasicAuthPass: "testpass",
ForceSSL: true,
ValidatorHook: "testhook.io",
PathPrefix: "prefix",
NETRCPath: "/test/path/.netrc",
HGRCPath: "/test/path/.hgrc",
}
expConf := &Config{
GoEnv: "production",
GoGetWorkers: 10,
ProtocolWorkers: 10,
LogLevel: "info",
BuffaloLogLevel: "info",
GoBinary: "go11",
CloudRuntime: "gcp",
FilterFile: "filter2.conf",
TimeoutConf: TimeoutConf{
Timeout: 30,
},
Proxy: &expProxy,
Storage: &StorageConfig{},
}
envVars := getEnvMap(expConf)
envVarBackup := map[string]string{}
for k, v := range envVars {
oldVal := os.Getenv(k)
envVarBackup[k] = oldVal
os.Setenv(k, v)
}
conf := &Config{}
err := envOverride(conf)
if err != nil {
t.Fatalf("Env override failed: %v", err)
}
deleteInvalidStorageConfigs(conf.Storage)
compareConfigs(conf, expConf, t)
restoreEnv(envVarBackup)
}
func TestStorageEnvOverrides(t *testing.T) {
globalTimeout := 300
expStorage := &StorageConfig{
CDN: &CDNConfig{
Endpoint: "cdnEndpoint",
TimeoutConf: TimeoutConf{
Timeout: globalTimeout,
},
},
Disk: &DiskConfig{
RootPath: "/my/root/path",
},
GCP: &GCPConfig{
ProjectID: "gcpproject",
Bucket: "gcpbucket",
TimeoutConf: TimeoutConf{
Timeout: globalTimeout,
},
},
Minio: &MinioConfig{
Endpoint: "minioEndpoint",
Key: "minioKey",
Secret: "minioSecret",
EnableSSL: false,
Bucket: "minioBucket",
TimeoutConf: TimeoutConf{
Timeout: globalTimeout,
},
},
Mongo: &MongoConfig{
URL: "mongoURL",
CertPath: "/test/path",
TimeoutConf: TimeoutConf{
Timeout: globalTimeout,
},
},
S3: &S3Config{
Region: "s3Region",
Key: "s3Key",
Secret: "s3Secret",
Token: "s3Token",
Bucket: "s3Bucket",
TimeoutConf: TimeoutConf{
Timeout: globalTimeout,
},
},
}
envVars := getEnvMap(&Config{Storage: expStorage})
envVarBackup := map[string]string{}
for k, v := range envVars {
oldVal := os.Getenv(k)
envVarBackup[k] = oldVal
os.Setenv(k, v)
}
conf := &Config{}
err := envOverride(conf)
if err != nil {
t.Fatalf("Env override failed: %v", err)
}
setStorageTimeouts(conf.Storage, globalTimeout)
deleteInvalidStorageConfigs(conf.Storage)
compareStorageConfigs(conf.Storage, expStorage, t)
restoreEnv(envVarBackup)
}
// TestParseExampleConfig validates that all the properties in the example configuration file
// can be parsed and validated without any environment variables
func TestParseExampleConfig(t *testing.T) {
// initialize all struct pointers so we get all applicable env variables
emptyConf := &Config{
Proxy: &ProxyConfig{},
Storage: &StorageConfig{
CDN: &CDNConfig{},
Disk: &DiskConfig{},
GCP: &GCPConfig{},
Minio: &MinioConfig{
EnableSSL: false,
},
Mongo: &MongoConfig{},
S3: &S3Config{},
},
}
// unset all environment variables
envVars := getEnvMap(emptyConf)
envVarBackup := map[string]string{}
for k := range envVars {
oldVal := os.Getenv(k)
envVarBackup[k] = oldVal
os.Unsetenv(k)
}
globalTimeout := 300
expProxy := &ProxyConfig{
StorageType: "memory",
GlobalEndpoint: "http://localhost:3001",
Port: ":3000",
FilterOff: true,
BasicAuthUser: "",
BasicAuthPass: "",
}
expStorage := &StorageConfig{
CDN: &CDNConfig{
Endpoint: "cdn.example.com",
TimeoutConf: TimeoutConf{
Timeout: globalTimeout,
},
},
Disk: &DiskConfig{
RootPath: "/path/on/disk",
},
GCP: &GCPConfig{
ProjectID: "MY_GCP_PROJECT_ID",
Bucket: "MY_GCP_BUCKET",
TimeoutConf: TimeoutConf{
Timeout: globalTimeout,
},
},
Minio: &MinioConfig{
Endpoint: "127.0.0.1:9001",
Key: "minio",
Secret: "minio123",
EnableSSL: false,
Bucket: "gomods",
TimeoutConf: TimeoutConf{
Timeout: globalTimeout,
},
},
Mongo: &MongoConfig{
URL: "mongodb://127.0.0.1:27017",
CertPath: "",
TimeoutConf: TimeoutConf{
Timeout: globalTimeout,
},
InsecureConn: false,
},
S3: &S3Config{
Region: "MY_AWS_REGION",
Key: "MY_AWS_ACCESS_KEY_ID",
Secret: "MY_AWS_SECRET_ACCESS_KEY",
Token: "",
Bucket: "MY_S3_BUCKET_NAME",
TimeoutConf: TimeoutConf{
Timeout: globalTimeout,
},
},
}
expConf := &Config{
GoEnv: "development",
LogLevel: "debug",
BuffaloLogLevel: "debug",
GoBinary: "go",
GoGetWorkers: 30,
ProtocolWorkers: 30,
CloudRuntime: "none",
FilterFile: "filter.conf",
TimeoutConf: TimeoutConf{
Timeout: 300,
},
Proxy: expProxy,
Storage: expStorage,
}
absPath, err := filepath.Abs(exampleConfigPath)
if err != nil {
t.Errorf("Unable to construct absolute path to example config file")
}
parsedConf, err := ParseConfigFile(absPath)
if err != nil {
t.Errorf("Unable to parse example config file: %+v", err)
}
compareConfigs(parsedConf, expConf, t)
restoreEnv(envVarBackup)
}
func getEnvMap(config *Config) map[string]string {
envVars := map[string]string{
"GO_ENV": config.GoEnv,
"GO_BINARY_PATH": config.GoBinary,
"ATHENS_GOGET_WORKERS": strconv.Itoa(config.GoGetWorkers),
"ATHENS_PROTOCOL_WORKERS": strconv.Itoa(config.ProtocolWorkers),
"ATHENS_LOG_LEVEL": config.LogLevel,
"BUFFALO_LOG_LEVEL": config.BuffaloLogLevel,
"ATHENS_CLOUD_RUNTIME": config.CloudRuntime,
"ATHENS_FILTER_FILE": config.FilterFile,
"ATHENS_TIMEOUT": strconv.Itoa(config.Timeout),
"ATHENS_TRACE_EXPORTER": config.TraceExporterURL,
}
proxy := config.Proxy
if proxy != nil {
envVars["ATHENS_STORAGE_TYPE"] = proxy.StorageType
envVars["ATHENS_GLOBAL_ENDPOINT"] = proxy.GlobalEndpoint
envVars["PORT"] = proxy.Port
envVars["PROXY_FILTER_OFF"] = strconv.FormatBool(proxy.FilterOff)
envVars["BASIC_AUTH_USER"] = proxy.BasicAuthUser
envVars["BASIC_AUTH_PASS"] = proxy.BasicAuthPass
envVars["PROXY_FORCE_SSL"] = strconv.FormatBool(proxy.ForceSSL)
envVars["ATHENS_PROXY_VALIDATOR"] = proxy.ValidatorHook
envVars["ATHENS_PATH_PREFIX"] = proxy.PathPrefix
envVars["ATHENS_NETRC_PATH"] = proxy.NETRCPath
envVars["ATHENS_HGRC_PATH"] = proxy.HGRCPath
}
storage := config.Storage
if storage != nil {
if storage.CDN != nil {
envVars["CDN_ENDPOINT"] = storage.CDN.Endpoint
}
if storage.Disk != nil {
envVars["ATHENS_DISK_STORAGE_ROOT"] = storage.Disk.RootPath
}
if storage.GCP != nil {
envVars["GOOGLE_CLOUD_PROJECT"] = storage.GCP.ProjectID
envVars["ATHENS_STORAGE_GCP_BUCKET"] = storage.GCP.Bucket
}
if storage.Minio != nil {
envVars["ATHENS_MINIO_ENDPOINT"] = storage.Minio.Endpoint
envVars["ATHENS_MINIO_ACCESS_KEY_ID"] = storage.Minio.Key
envVars["ATHENS_MINIO_SECRET_ACCESS_KEY"] = storage.Minio.Secret
envVars["ATHENS_MINIO_USE_SSL"] = strconv.FormatBool(storage.Minio.EnableSSL)
envVars["ATHENS_MINIO_BUCKET_NAME"] = storage.Minio.Bucket
}
if storage.Mongo != nil {
envVars["ATHENS_MONGO_STORAGE_URL"] = storage.Mongo.URL
envVars["ATHENS_MONGO_CERT_PATH"] = storage.Mongo.CertPath
envVars["ATHENS_MONGO_INSECURE"] = strconv.FormatBool(storage.Mongo.InsecureConn)
}
if storage.S3 != nil {
envVars["AWS_REGION"] = storage.S3.Region
envVars["AWS_ACCESS_KEY_ID"] = storage.S3.Key
envVars["AWS_SECRET_ACCESS_KEY"] = storage.S3.Secret
envVars["AWS_SESSION_TOKEN"] = storage.S3.Token
envVars["ATHENS_S3_BUCKET_NAME"] = storage.S3.Bucket
}
}
return envVars
}
func restoreEnv(envVars map[string]string) {
for k, v := range envVars {
if v != "" {
os.Setenv(k, v)
} else {
os.Unsetenv(k)
}
}
}
func Test_checkFilePerms(t *testing.T) {
f1, err := ioutil.TempFile(os.TempDir(), "prefix-")
if err != nil {
t.FailNow()
}
defer os.Remove(f1.Name())
err = os.Chmod(f1.Name(), 0777)
f2, err := ioutil.TempFile(os.TempDir(), "prefix-")
if err != nil {
t.FailNow()
}
defer os.Remove(f2.Name())
err = os.Chmod(f2.Name(), 0600)
type args struct {
files []string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"should not have an error on empty file name",
args{
[]string{"", f2.Name()},
},
false,
},
{
"should have an error if all the files have incorrect permissions",
args{
[]string{f1.Name(), f1.Name(), f1.Name()},
},
true,
},
{
"should have an error when at least 1 file has wrong permissions",
args{
[]string{f2.Name(), f1.Name()},
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := checkFilePerms(tt.args.files...); (err != nil) != tt.wantErr {
t.Errorf("checkFilePerms() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}