github.com/RedHatInsights/insights-content-service@v1.0.0/conf/configuration_test.go (about) 1 /* 2 Copyright © 2020, 2021, 2022 Red Hat, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package conf_test 18 19 import ( 20 "os" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 25 "github.com/RedHatInsights/insights-content-service/conf" 26 ) 27 28 func init() { 29 err := os.Chdir("..") 30 if err != nil { 31 panic(err) 32 } 33 } 34 35 func mustLoadConfiguration(t *testing.T, path string) { 36 err := conf.LoadConfiguration(path) 37 if err != nil { 38 t.Fatal(err) 39 } 40 } 41 42 func mustSetEnv(t *testing.T, key, val string) { 43 err := os.Setenv(key, val) 44 if err != nil { 45 t.Fatal(err) 46 } 47 } 48 49 func loadProperConfigFile(t *testing.T) { 50 os.Clearenv() 51 mustLoadConfiguration(t, "tests/config") 52 } 53 54 // TestLoadConfiguration loads a configuration file for testing 55 func TestLoadConfiguration(t *testing.T) { 56 loadProperConfigFile(t) 57 } 58 59 // TestLoadBrokenConfiguration loads a configuration file for testing 60 func TestLoadBrokenConfiguraion(t *testing.T) { 61 os.Clearenv() 62 err := conf.LoadConfiguration("tests/config_improper_format") 63 if err == nil { 64 t.Fatal("Broken configuration file should be detected") 65 } 66 } 67 68 // TestLoadGroupsConfiguration tests loading the groups configuration sub-tree 69 func TestLoadGroupsConfiguration(t *testing.T) { 70 loadProperConfigFile(t) 71 72 GroupsCfg := conf.GetGroupsConfiguration() 73 74 assert.Equal(t, "groups_config.yaml", GroupsCfg.ConfigPath) 75 } 76 77 // TestLoadServerConfiguration tests loading the server configuration sub-tree 78 func TestLoadServerConfiguration(t *testing.T) { 79 loadProperConfigFile(t) 80 81 serverCfg := conf.GetServerConfiguration() 82 83 assert.Equal(t, ":8080", serverCfg.Address) 84 assert.Equal(t, "/api/v1/", serverCfg.APIPrefix) 85 } 86 87 // TestLoadContentPathConfiguration tests loading the content path configuration 88 func TestLoadContentPathConfiguration(t *testing.T) { 89 loadProperConfigFile(t) 90 91 contentPath := conf.GetContentPathConfiguration() 92 93 assert.Equal(t, "/rules-content", contentPath) 94 } 95 96 // TestLoadConfigurationEnvVariable tests loading the config. file for testing from an environment variable 97 func TestLoadConfigurationEnvVariable(t *testing.T) { 98 os.Clearenv() 99 100 mustSetEnv(t, "INSIGHTS_CONTENT_SERVICE_CONFIG_FILE", "tests/config") 101 102 mustLoadConfiguration(t, "foobar") 103 } 104 105 // TestLoadConfigurationEnvVariableNegative tests loading the config. file for testing from an environment variable 106 func TestLoadConfigurationEnvVariableNegative(t *testing.T) { 107 os.Clearenv() 108 109 mustSetEnv(t, "INSIGHTS_CONTENT_SERVICE_CONFIG_FILE", "does not exists") 110 111 err := conf.LoadConfiguration("foobar") 112 if err == nil { 113 t.Fatal("Error should be reported for non existing file") 114 } 115 } 116 117 // TestTryToLoadNonExistingConfig checks if non existing config file causes failure or not 118 func TestTryToLoadNonExistingConfig(t *testing.T) { 119 os.Clearenv() 120 err := conf.LoadConfiguration("foobar") 121 if err != nil { 122 t.Fatal(err) 123 } 124 } 125 126 // TestGetMetricsConfiguration checks if the metrics section is loaded properly 127 func TestGetMetricsConfiguration(t *testing.T) { 128 TestLoadConfiguration(t) 129 130 metricsCfg := conf.GetMetricsConfiguration() 131 assert.Equal(t, "contentservice", metricsCfg.Namespace) 132 } 133 134 // TestGetLoggingConfiguration checks if the logging section is loaded properly 135 func TestGetLoggingConfiguration(t *testing.T) { 136 TestLoadConfiguration(t) 137 138 loggingCfg := conf.GetLoggingConfiguration() 139 assert.True(t, loggingCfg.Debug) 140 } 141 142 // TestCheckIfFileExists tests the functionality of function checkIfFileExists 143 func TestCheckIfFileExists(t *testing.T) { 144 err := conf.CheckIfFileExists("") 145 if err == nil { 146 t.Fatal("File with empty name should not exists") 147 } 148 149 err = conf.CheckIfFileExists("config.toml") 150 if err != nil { 151 t.Fatal("File should exists:", err) 152 } 153 154 err = conf.CheckIfFileExists("\n") 155 if err == nil { 156 t.Fatal("File '' should not exist") 157 } 158 159 err = conf.CheckIfFileExists(".") 160 if err == nil { 161 t.Fatal("File '.' is a directory") 162 } 163 164 err = conf.CheckIfFileExists("..") 165 if err == nil { 166 t.Fatal("File '..' is a directory") 167 } 168 }