github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/go/config/config_test.go (about) 1 // Copyright 2016 Attic Labs, Inc. All rights reserved. 2 // Licensed under the Apache License, version 2.0: 3 // http://www.apache.org/licenses/LICENSE-2.0 4 5 package config 6 7 import ( 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "strings" 12 "testing" 13 14 "github.com/attic-labs/noms/go/spec" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 const ( 19 nbsSpec = "nbs:./local" 20 memSpec = "mem" 21 httpSpec = "http://test.com:8080/foo" 22 nbsAbsSpec = "nbs:/tmp/noms" 23 remoteAlias = "origin" 24 ) 25 26 var ( 27 ctestRoot = os.TempDir() 28 29 ldbConfig = &Config{ 30 "", 31 map[string]DbConfig{ 32 DefaultDbAlias: {nbsSpec}, 33 remoteAlias: {httpSpec}, 34 }, 35 } 36 37 httpConfig = &Config{ 38 "", 39 map[string]DbConfig{ 40 DefaultDbAlias: {httpSpec}, 41 remoteAlias: {nbsSpec}, 42 }, 43 } 44 45 memConfig = &Config{ 46 "", 47 map[string]DbConfig{ 48 DefaultDbAlias: {memSpec}, 49 remoteAlias: {httpSpec}, 50 }, 51 } 52 53 ldbAbsConfig = &Config{ 54 "", 55 map[string]DbConfig{ 56 DefaultDbAlias: {nbsAbsSpec}, 57 remoteAlias: {httpSpec}, 58 }, 59 } 60 ) 61 62 type paths struct { 63 home string 64 config string 65 } 66 67 func getPaths(assert *assert.Assertions, base string) paths { 68 abs, err := filepath.Abs(ctestRoot) 69 assert.NoError(err) 70 abs, err = filepath.EvalSymlinks(ctestRoot) 71 assert.NoError(err) 72 home := filepath.Join(abs, base) 73 config := filepath.Join(home, NomsConfigFile) 74 return paths{home, config} 75 } 76 77 func qualifyFilePath(assert *assert.Assertions, path string) string { 78 p, err := filepath.Abs(path) 79 assert.NoError(err) 80 return p 81 } 82 83 func assertDbSpecsEquiv(assert *assert.Assertions, expected string, actual string) { 84 e, err := spec.ForDatabase(expected) 85 assert.NoError(err) 86 if e.Protocol != "nbs" { 87 assert.Equal(expected, actual) 88 } else { 89 a, err := spec.ForDatabase(actual) 90 assert.NoError(err) 91 assert.Equal(e.Protocol, a.Protocol, actual) 92 if filepath.IsAbs(e.DatabaseName) { 93 assert.Equal(e.DatabaseName, a.DatabaseName, actual) 94 } else { 95 // If the original path is relative, it will return as absolute. 96 // All we do here is ensure that the path suffix is the same. 97 eName := strings.TrimPrefix(e.DatabaseName, ".") 98 assert.True(strings.HasSuffix(a.DatabaseName, eName), 99 "expected: %s; actual: %s", eName, actual) 100 } 101 } 102 } 103 104 func validateConfig(assert *assert.Assertions, file string, e *Config, a *Config) { 105 assert.Equal(qualifyFilePath(assert, file), qualifyFilePath(assert, a.File)) 106 assert.Equal(len(e.Db), len(a.Db)) 107 for k, er := range e.Db { 108 ar, ok := a.Db[k] 109 assert.True(ok) 110 assertDbSpecsEquiv(assert, er.Url, ar.Url) 111 } 112 } 113 114 func writeConfig(assert *assert.Assertions, c *Config, home string) string { 115 file, err := c.WriteTo(home) 116 assert.NoError(err, home) 117 return file 118 } 119 120 func TestConfig(t *testing.T) { 121 assert := assert.New(t) 122 path := getPaths(assert, "home") 123 writeConfig(assert, ldbConfig, path.home) 124 125 // Test from home 126 assert.NoError(os.Chdir(path.home)) 127 c, err := FindNomsConfig() 128 assert.NoError(err, path.config) 129 validateConfig(assert, path.config, ldbConfig, c) 130 131 // Test from subdir 132 subdir := filepath.Join(path.home, "subdir") 133 assert.NoError(os.MkdirAll(subdir, os.ModePerm)) 134 assert.NoError(os.Chdir(subdir)) 135 c, err = FindNomsConfig() 136 assert.NoError(err, path.config) 137 validateConfig(assert, path.config, ldbConfig, c) 138 139 // Test from subdir with intervening .nomsconfig directory 140 nomsDir := filepath.Join(subdir, NomsConfigFile) 141 err = os.MkdirAll(nomsDir, os.ModePerm) 142 assert.NoError(err, nomsDir) 143 assert.NoError(os.Chdir(subdir)) 144 c, err = FindNomsConfig() 145 assert.NoError(err, path.config) 146 validateConfig(assert, path.config, ldbConfig, c) 147 } 148 149 func TestUnreadableConfig(t *testing.T) { 150 // BUG 3816 151 if os.Getenv("DOCKER") != "" { 152 t.Skip("Skipping testing in Docker environment") 153 } 154 155 assert := assert.New(t) 156 path := getPaths(assert, "home.unreadable") 157 writeConfig(assert, ldbConfig, path.home) 158 assert.NoError(os.Chmod(path.config, 0333)) // write-only 159 assert.NoError(os.Chdir(path.home)) 160 _, err := FindNomsConfig() 161 assert.Error(err, path.config) 162 } 163 164 func TestNoConfig(t *testing.T) { 165 assert := assert.New(t) 166 path := getPaths(assert, "home.none") 167 assert.NoError(os.MkdirAll(path.home, os.ModePerm)) 168 assert.NoError(os.Chdir(path.home)) 169 _, err := FindNomsConfig() 170 assert.Equal(NoConfig, err) 171 } 172 173 func TestBadConfig(t *testing.T) { 174 assert := assert.New(t) 175 path := getPaths(assert, "home.bad") 176 cfile := writeConfig(assert, ldbConfig, path.home) 177 // overwrite with something invalid 178 assert.NoError(ioutil.WriteFile(cfile, []byte("invalid config"), os.ModePerm)) 179 assert.NoError(os.Chdir(path.home)) 180 _, err := FindNomsConfig() 181 assert.Error(err, path.config) 182 } 183 184 func TestQualifyingPaths(t *testing.T) { 185 assert := assert.New(t) 186 path := getPaths(assert, "home") 187 assert.NoError(os.Chdir(path.home)) 188 189 for _, tc := range []*Config{httpConfig, memConfig, ldbAbsConfig} { 190 writeConfig(assert, tc, path.home) 191 ac, err := FindNomsConfig() 192 assert.NoError(err, path.config) 193 validateConfig(assert, path.config, tc, ac) 194 } 195 } 196 197 func TestCwd(t *testing.T) { 198 assert := assert.New(t) 199 cwd, err := os.Getwd() 200 assert.NoError(err) 201 cwd = filepath.Join(cwd, "test") 202 abs, err := filepath.Abs("test") 203 assert.NoError(err) 204 205 assert.Equal(cwd, abs) 206 }