github.com/kuoss/venti@v0.2.20/pkg/config/config_test.go (about) 1 package config 2 3 import ( 4 "os" 5 "os/exec" 6 "testing" 7 8 "github.com/kuoss/venti/pkg/model" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func init() { 13 err := os.Chdir("../..") 14 if err != nil { 15 panic(err) 16 } 17 } 18 19 func TestLoad(t *testing.T) { 20 _ = exec.Command("cp", "docs/examples/datasources.dev1.yml", "etc/datasources.yml").Run() 21 defer func() { 22 os.RemoveAll("etc/datasources.yml") 23 }() 24 cfg, err := Load("Unknown") 25 assert.NoError(t, err) 26 assert.Equal(t, cfg.AppInfo.Version, "Unknown") 27 assert.Equal(t, []model.Datasource{ 28 {Type: model.DatasourceTypePrometheus, Name: "prometheus", URL: "http://localhost:9090"}, 29 {Type: model.DatasourceTypeLethe, Name: "lethe", URL: "http://localhost:6060"}, 30 }, cfg.DatasourceConfig.Datasources) 31 assert.Equal(t, model.UserConfig{EtcUsers: []model.EtcUser{ 32 {Username: "admin", Hash: "$2a$12$VcCDgh2NDk07JGN0rjGbM.Ad41qVR/YFJcgHp0UGns5JDymv..TOG", IsAdmin: true}, 33 }}, cfg.UserConfig) 34 } 35 36 func TestLoadGlobalConfigFile(t *testing.T) { 37 testCases := []struct { 38 file string 39 want model.GlobalConfig 40 wantError string 41 }{ 42 { 43 "", 44 model.GlobalConfig{LogLevel: ""}, 45 "error on ReadFile: open : no such file or directory", 46 }, 47 { 48 "etc/datasources.yml", 49 model.GlobalConfig{LogLevel: ""}, 50 //"error on UnmarshalStrict: yaml: unmarshal errors:\n line 1: field datasources not found in type model.GlobalConfig", 51 "error on ReadFile: open etc/datasources.yml: no such file or directory", 52 }, 53 { 54 "etc/venti.yml", 55 model.GlobalConfig{GinMode: "release", LogLevel: "info"}, 56 "", 57 }, 58 } 59 for _, tc := range testCases { 60 t.Run("", func(t *testing.T) { 61 got, err := loadGlobalConfigFile(tc.file) 62 if tc.wantError == "" { 63 assert.NoError(t, err) 64 } else { 65 assert.EqualError(t, err, tc.wantError) 66 } 67 assert.Equal(t, tc.want, got) 68 }) 69 } 70 71 } 72 73 func TestLoadDatasourceConfigFile(t *testing.T) { 74 testCases := []struct { 75 file string 76 want *model.DatasourceConfig 77 wantError string 78 }{ 79 { 80 "", 81 nil, 82 "error on ReadFile: open : no such file or directory", 83 }, 84 { 85 "docs/examples/datasources.dev1.yml", 86 &model.DatasourceConfig{ 87 QueryTimeout: 30000000000, 88 Datasources: []model.Datasource{ 89 {Type: "prometheus", Name: "prometheus", URL: "http://localhost:9090"}, 90 {Type: "lethe", Name: "lethe", URL: "http://localhost:6060"}, 91 }, 92 Discovery: model.Discovery{AnnotationKey: "kuoss.org/datasource-type"}, 93 }, 94 "", 95 }, 96 { 97 "docs/examples/datasources.dev2.yml", 98 &model.DatasourceConfig{ 99 QueryTimeout: 30000000000, 100 Datasources: []model.Datasource{ 101 {Type: "prometheus", Name: "prometheus1", URL: "http://vs-prometheus-server"}, 102 {Type: "prometheus", Name: "prometheus2", URL: "http://vs-prometheus-server"}, 103 {Type: "lethe", Name: "lethe", URL: "http://vs-lethe"}, 104 }, 105 Discovery: model.Discovery{AnnotationKey: "kuoss.org/datasource-type"}, 106 }, 107 "", 108 }, 109 } 110 for _, tc := range testCases { 111 t.Run("", func(t *testing.T) { 112 got, err := loadDatasourceConfigFile(tc.file) 113 if tc.wantError == "" { 114 assert.NoError(t, err) 115 } else { 116 assert.EqualError(t, err, tc.wantError) 117 } 118 assert.Equal(t, tc.want, got) 119 }) 120 } 121 } 122 123 func TestLoadUserConfigFile(t *testing.T) { 124 testCases := []struct { 125 file string 126 want *model.UserConfig 127 wantError string 128 }{ 129 { 130 "", 131 nil, 132 "error on ReadFile: open : no such file or directory", 133 }, 134 { 135 "etc/users.yml", 136 &model.UserConfig{EtcUsers: []model.EtcUser{ 137 {Username: "admin", Hash: "$2a$12$VcCDgh2NDk07JGN0rjGbM.Ad41qVR/YFJcgHp0UGns5JDymv..TOG", IsAdmin: true}, 138 }}, 139 "", 140 }, 141 } 142 for _, tc := range testCases { 143 t.Run("", func(t *testing.T) { 144 got, err := loadUserConfigFile(tc.file) 145 if tc.wantError == "" { 146 assert.NoError(t, err) 147 } else { 148 assert.EqualError(t, err, tc.wantError) 149 } 150 assert.Equal(t, tc.want, got) 151 }) 152 } 153 } 154 155 func TestLoadAlertingConfigFile(t *testing.T) { 156 testCases := []struct { 157 file string 158 want model.AlertingConfig 159 wantError string 160 }{ 161 { 162 "", 163 model.AlertingConfig{}, 164 "error on ReadFile: open : no such file or directory", 165 }, 166 { 167 "etc/alerting.yml", 168 model.AlertingConfig{ 169 EvaluationInterval: 5000000000, 170 AlertRelabelConfigs: nil, 171 AlertmanagerConfigs: model.AlertmanagerConfigs{ 172 {StaticConfig: []*model.TargetGroup{ 173 {Targets: []string{"http://vs-alertmanager:9093"}}, 174 }}, 175 }, 176 GlobalLabels: map[string]string{"venti": "development"}, 177 }, 178 "", 179 }, 180 } 181 for _, tc := range testCases { 182 t.Run("", func(t *testing.T) { 183 got, err := loadAlertingConfigFile(tc.file) 184 if tc.wantError == "" { 185 assert.NoError(t, err) 186 } else { 187 assert.EqualError(t, err, tc.wantError) 188 } 189 assert.Equal(t, tc.want, got) 190 }) 191 } 192 }