github.com/jdhenke/godel@v0.0.0-20161213181855-abeb3861bf0d/apps/distgo/config/config_test.go (about) 1 // Copyright 2016 Palantir Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package config_test 16 17 import ( 18 "strings" 19 "testing" 20 21 "github.com/palantir/pkg/matcher" 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 25 "github.com/palantir/godel/apps/distgo/config" 26 "github.com/palantir/godel/apps/distgo/params" 27 "github.com/palantir/godel/apps/distgo/pkg/osarch" 28 ) 29 30 func TestReadConfig(t *testing.T) { 31 for i, currCase := range []struct { 32 yml string 33 json string 34 want func() config.Project 35 }{ 36 { 37 yml: ` 38 products: 39 test: 40 build: 41 main-pkg: ./cmd/test 42 output-dir: build 43 build-args-script: | 44 YEAR=$(date +%Y) 45 echo "-ldflags" 46 echo "-X" 47 echo "main.year=$YEAR" 48 version-var: main.version 49 environment: 50 foo: bar 51 baz: 1 52 bool: TRUE 53 os-archs: 54 - os: "darwin" 55 arch: "amd64" 56 - os: "linux" 57 arch: "amd64" 58 dist: 59 output-dir: dist 60 input-dir: resources/input 61 dist-type: 62 type: sls 63 info: 64 manifest-template-file: resources/input/manifest.yml 65 product-type: service.v1 66 yml-validation-exclude: 67 names: 68 - foo 69 paths: 70 - bar 71 exclude: 72 names: 73 - ".*test" 74 paths: 75 - "vendor" 76 `, 77 json: `{"exclude":{"names":["distgo"],"paths":["generated_src"]}}`, 78 want: func() config.Project { 79 return config.Project{ 80 Products: map[string]config.Product{ 81 "test": { 82 Build: config.Build{ 83 MainPkg: "./cmd/test", 84 OutputDir: "build", 85 BuildArgsScript: `YEAR=$(date +%Y) 86 echo "-ldflags" 87 echo "-X" 88 echo "main.year=$YEAR" 89 `, 90 VersionVar: "main.version", 91 Environment: map[string]string{ 92 "foo": "bar", 93 "baz": "1", 94 "bool": "TRUE", 95 }, 96 OSArchs: []osarch.OSArch{ 97 { 98 OS: "darwin", 99 Arch: "amd64", 100 }, 101 { 102 OS: "linux", 103 Arch: "amd64", 104 }, 105 }, 106 }, 107 Dist: []config.Dist{{ 108 OutputDir: "dist", 109 InputDir: "resources/input", 110 DistType: config.DistInfo{ 111 Type: string(params.SLSDistType), 112 Info: config.SLSDist{ 113 ManifestTemplateFile: "resources/input/manifest.yml", 114 ProductType: "service.v1", 115 YMLValidationExclude: matcher.NamesPathsCfg{ 116 Names: []string{"foo"}, 117 Paths: []string{"bar"}, 118 }, 119 }, 120 }, 121 }}, 122 }, 123 }, 124 Exclude: matcher.NamesPathsCfg{ 125 Names: []string{`.*test`, `distgo`}, 126 Paths: []string{`vendor`, `generated_src`}, 127 }, 128 } 129 }, 130 }, 131 { 132 yml: ` 133 products: 134 test: 135 dist: 136 dist-type: 137 type: rpm 138 info: 139 config-files: 140 - /usr/lib/systemd/system/orchestrator.service 141 before-install-script: | 142 /usr/bin/getent group orchestrator || /usr/sbin/groupadd \ 143 -g 380 orchestrator 144 /usr/bin/getent passwd orchestrator || /usr/sbin/useradd -r \ 145 -d /var/lib/orchestrator -g orchestrator -u 380 -m \ 146 -s /sbin/nologin orchestrator 147 after-install-script: | 148 systemctl daemon-reload 149 after-remove-script: | 150 systemctl daemon-reload 151 `, 152 want: func() config.Project { 153 return config.Project{ 154 Products: map[string]config.Product{ 155 "test": { 156 Dist: []config.Dist{ 157 { 158 DistType: config.DistInfo{ 159 Type: string(params.RPMDistType), 160 Info: config.RPMDist{ 161 ConfigFiles: []string{"/usr/lib/systemd/system/orchestrator.service"}, 162 BeforeInstallScript: "" + 163 "/usr/bin/getent group orchestrator || /usr/sbin/groupadd \\\n" + 164 " -g 380 orchestrator\n" + 165 "/usr/bin/getent passwd orchestrator || /usr/sbin/useradd -r \\\n" + 166 " -d /var/lib/orchestrator -g orchestrator -u 380 -m \\\n" + 167 " -s /sbin/nologin orchestrator\n", 168 AfterInstallScript: "systemctl daemon-reload\n", 169 AfterRemoveScript: "systemctl daemon-reload\n", 170 }, 171 }, 172 }, 173 }, 174 }, 175 }, 176 } 177 }, 178 }, 179 { 180 yml: ` 181 products: 182 test: 183 dist: 184 - dist-type: 185 type: sls 186 info: 187 manifest-template-file: resources/input/manifest.yml 188 - dist-type: 189 type: rpm 190 info: 191 after-install-script: | 192 systemctl daemon-reload 193 publish: 194 group-id: com.palantir.pcloud 195 almanac: 196 metadata: 197 k: "v" 198 tags: 199 - "borked" 200 `, 201 want: func() config.Project { 202 return config.Project{ 203 Products: map[string]config.Product{ 204 "test": { 205 Dist: []config.Dist{{ 206 DistType: config.DistInfo{ 207 Type: string(params.SLSDistType), 208 Info: config.SLSDist{ 209 ManifestTemplateFile: "resources/input/manifest.yml", 210 }, 211 }, 212 }, { 213 DistType: config.DistInfo{ 214 Type: string(params.RPMDistType), 215 Info: config.RPMDist{ 216 AfterInstallScript: "systemctl daemon-reload\n", 217 }, 218 }, 219 }}, 220 DefaultPublish: config.Publish{ 221 GroupID: "com.palantir.pcloud", 222 Almanac: config.Almanac{ 223 Metadata: map[string]string{"k": "v"}, 224 Tags: []string{"borked"}, 225 }, 226 }, 227 }, 228 }, 229 } 230 }, 231 }, 232 } { 233 // load config 234 got, err := config.LoadRawConfig(unindent(currCase.yml), currCase.json) 235 require.NoError(t, err, "Case %d", i) 236 237 // require that it is valid 238 _, err = got.ToParams() 239 require.NoError(t, err, "Case %d", i) 240 241 assert.Equal(t, currCase.want(), got, "Case %d", i) 242 } 243 } 244 245 func TestFilteredProducts(t *testing.T) { 246 for i, currCase := range []struct { 247 cfg func() params.Project 248 want map[string]params.Product 249 }{ 250 { 251 cfg: func() params.Project { 252 excludeCfg := matcher.NamesPathsCfg{ 253 Paths: []string{"vendor"}, 254 } 255 return params.Project{ 256 Products: map[string]params.Product{ 257 "test": { 258 Build: params.Build{ 259 MainPkg: "./test/main", 260 }, 261 }, 262 "vendored": { 263 Build: params.Build{ 264 MainPkg: "./vendor/test/main", 265 }, 266 }, 267 }, 268 Exclude: excludeCfg.Matcher(), 269 } 270 }, 271 want: map[string]params.Product{ 272 "test": { 273 Build: params.Build{ 274 MainPkg: "./test/main", 275 }, 276 }, 277 }, 278 }, 279 } { 280 got := currCase.cfg().FilteredProducts() 281 assert.Equal(t, currCase.want, got, "Case %d", i) 282 } 283 } 284 285 func unindent(input string) string { 286 return strings.Replace(input, "\n\t\t\t", "\n", -1) 287 }