go.temporal.io/server@v1.23.0/common/config/loader_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package config 26 27 import ( 28 "os" 29 "testing" 30 31 "github.com/stretchr/testify/require" 32 "github.com/stretchr/testify/suite" 33 34 "go.temporal.io/server/tests/testutils" 35 ) 36 37 const fileMode = os.FileMode(0644) 38 39 type ( 40 LoaderSuite struct { 41 *require.Assertions 42 suite.Suite 43 } 44 45 itemsConfig struct { 46 Item1 string `yaml:"item1"` 47 Item2 string `yaml:"item2"` 48 } 49 50 testConfig struct { 51 Items itemsConfig `yaml:"items"` 52 } 53 ) 54 55 func TestLoaderSuite(t *testing.T) { 56 suite.Run(t, new(LoaderSuite)) 57 } 58 59 func (s *LoaderSuite) SetupTest() { 60 s.Assertions = require.New(s.T()) 61 } 62 63 func (s *LoaderSuite) TestBaseYaml() { 64 dir := testutils.MkdirTemp(s.T(), "", "loader.testBaseYaml") 65 66 data := buildConfig("", "") 67 err := os.WriteFile(path(dir, "base.yaml"), []byte(data), fileMode) 68 s.Nil(err) 69 70 envs := []string{"", "prod"} 71 zones := []string{"", "us-east-1a"} 72 73 for _, env := range envs { 74 for _, zone := range zones { 75 var cfg testConfig 76 err = Load(env, dir, zone, &cfg) 77 s.Nil(err) 78 s.Equal("hello__", cfg.Items.Item1) 79 s.Equal("world__", cfg.Items.Item2) 80 } 81 } 82 } 83 84 func (s *LoaderSuite) TestHierarchy() { 85 dir := testutils.MkdirTemp(s.T(), "", "loader.testHierarchy") 86 87 s.createFile(dir, "base.yaml", "", "") 88 s.createFile(dir, "development.yaml", "development", "") 89 s.createFile(dir, "prod.yaml", "prod", "") 90 s.createFile(dir, "prod_dca.yaml", "prod", "dca") 91 92 testCases := []struct { 93 env string 94 zone string 95 item1 string 96 item2 string 97 }{ 98 {"", "", "hello_development_", "world_development_"}, 99 {"", "dca", "hello_development_", "world_development_"}, 100 {"", "pdx", "hello_development_", "world_development_"}, 101 {"development", "", "hello_development_", "world_development_"}, 102 {"development", "dca", "hello_development_", "world_development_"}, 103 {"development", "pdx", "hello_development_", "world_development_"}, 104 {"prod", "", "hello_prod_", "world_prod_"}, 105 {"prod", "dca", "hello_prod_dca", "world_prod_dca"}, 106 {"prod", "pdx", "hello_prod_", "world_prod_"}, 107 } 108 109 for _, tc := range testCases { 110 var cfg testConfig 111 err := Load(tc.env, dir, tc.zone, &cfg) 112 s.Nil(err) 113 s.Equal(tc.item1, cfg.Items.Item1) 114 s.Equal(tc.item2, cfg.Items.Item2) 115 } 116 } 117 118 func (s *LoaderSuite) TestInvalidPath() { 119 var cfg testConfig 120 err := Load("prod", "", "", &cfg) 121 s.NotNil(err) 122 } 123 124 func (s *LoaderSuite) createFile(dir string, file string, env string, zone string) { 125 err := os.WriteFile(path(dir, file), []byte(buildConfig(env, zone)), fileMode) 126 s.Nil(err) 127 } 128 129 func buildConfig(env, zone string) string { 130 item1 := concat("hello", concat(env, zone)) 131 item2 := concat("world", concat(env, zone)) 132 return ` 133 items: 134 item1: ` + item1 + ` 135 item2: ` + item2 136 }