github.com/jvandenbroek/directory_stat_exporter@v0.2.0/cfg/config_test.go (about) 1 package cfg 2 3 import ( 4 "fmt" 5 "gopkg.in/yaml.v2" 6 "io/ioutil" 7 "os" 8 "testing" 9 ) 10 11 func TestGetConfig(t *testing.T) { 12 t.Run("given no config.yml when called then it will still return valid default configuration settings", func(t *testing.T) { 13 cwd, _ := os.Getwd() 14 tmpDir, _ := ioutil.TempDir("", "dirstat_config_test") 15 _ = os.Chdir(tmpDir) 16 17 cwd2, _ := os.Getwd() 18 fmt.Println(cwd2) 19 20 config := GetConfig("") 21 22 if config.ServicePort != "9999" { 23 t.Fail() 24 t.Errorf("the configuration returned must define the default service port 9999") 25 } 26 27 if config.CacheTime != 5 { 28 t.Fail() 29 t.Errorf("the configuration returned must return the default cachetime 5") 30 t.Errorf(" it returned %v instead.", config.CacheTime) 31 } 32 33 _ = os.Chdir(cwd) 34 }) 35 36 t.Run("given config.yml is in executable directory when called then it will return config from yml file", func(t *testing.T) { 37 cwd, _ := os.Getwd() 38 tmpDir, _ := ioutil.TempDir("", "dirstat_config_test") 39 _ = os.Chdir(tmpDir) 40 41 cwd2, _ := os.Getwd() 42 fmt.Println(cwd2) 43 44 configSrc := Config{ 45 ServicePort: "9997", 46 } 47 48 out, _ := yaml.Marshal(configSrc) 49 50 _ = ioutil.WriteFile("config.yml", []byte(out), 0644) 51 52 config := GetConfig("") 53 54 if config.ServicePort != "9997" { 55 t.Fail() 56 t.Errorf("the configuration returned must return the service port 9997, defined in config.yml") 57 t.Errorf(" it returned %v instead.", config.ServicePort) 58 } 59 60 _ = os.Chdir(cwd) 61 }) 62 63 t.Run("given config.yml is in executable directory when called then it will return config from yml file with default values", func(t *testing.T) { 64 cwd, _ := os.Getwd() 65 tmpDir, _ := ioutil.TempDir("", "dirstat_config_test") 66 _ = os.Chdir(tmpDir) 67 68 cwd2, _ := os.Getwd() 69 fmt.Println(cwd2) 70 71 configSrc := Config{ 72 ServicePort: "9997", 73 } 74 75 out, _ := yaml.Marshal(configSrc) 76 77 _ = ioutil.WriteFile("config.yml", []byte(out), 0644) 78 79 config := GetConfig("") 80 81 if config.ServicePort != "9997" { 82 t.Fail() 83 t.Errorf("the configuration returned must return the service port 9997, defined in config.yml") 84 t.Errorf(" it returned %v instead.", config.ServicePort) 85 } 86 87 if config.CacheTime != 5 { 88 t.Fail() 89 t.Errorf("the configuration returned must containt the default cachetime with value of 5") 90 t.Errorf(" it returned %v instead.", config.CacheTime) 91 } 92 93 _ = os.Chdir(cwd) 94 }) 95 96 t.Run("given a custom config.yml not in the current directory when called with its path as argument then it will return config from custom yml file", func(t *testing.T) { 97 cwd, _ := os.Getwd() 98 tmpDir, _ := ioutil.TempDir("", "dirstat_config_test") 99 _ = os.Chdir(tmpDir) 100 101 cwd2, _ := os.Getwd() 102 fmt.Println(cwd2) 103 104 configSrc := Config{ 105 ServicePort: "9994", 106 CacheTime: 7, 107 } 108 109 out, _ := yaml.Marshal(configSrc) 110 111 customConfigFile := "myconfig.yml" 112 _ = ioutil.WriteFile(customConfigFile, []byte(out), 0644) 113 114 config := GetConfig(customConfigFile) 115 116 if config.ServicePort != "9994" { 117 t.Fail() 118 t.Errorf("the configuration returned must return the service port 9997, defined in config.yml") 119 t.Errorf(" it returned %v instead.", config.ServicePort) 120 } 121 122 if config.CacheTime != configSrc.CacheTime { 123 t.Fail() 124 t.Errorf("the configuration returned must return the cache time %v, defined in config.yml", configSrc.CacheTime) 125 t.Errorf(" it returned %v instead.", config.CacheTime) 126 } 127 128 _ = os.Chdir(cwd) 129 }) 130 }