| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | package setting | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-06-20 22:15:21 +08:00
										 |  |  | 	"bufio" | 
					
						
							| 
									
										
										
										
											2015-02-12 20:31:41 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2019-04-25 14:29:07 +08:00
										 |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2019-06-20 22:15:21 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 17:04:48 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-12 13:27:47 +08:00
										 |  |  | 	"gopkg.in/ini.v1" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 	. "github.com/smartystreets/goconvey/convey" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-12 13:27:47 +08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	windows = "windows" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | func TestLoadingSettings(t *testing.T) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Convey("Testing loading settings from ini file", t, func() { | 
					
						
							| 
									
										
										
										
											2015-09-11 14:58:45 +08:00
										 |  |  | 		skipStaticRootValidation = true | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Given the default ini files", func() { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 			cfg := NewCfg() | 
					
						
							|  |  |  | 			err := cfg.Load(&CommandLineArgs{HomePath: "../../"}) | 
					
						
							| 
									
										
										
										
											2015-09-11 14:58:45 +08:00
										 |  |  | 			So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 20:31:41 +08:00
										 |  |  | 			So(AdminUser, ShouldEqual, "admin") | 
					
						
							| 
									
										
										
										
											2018-09-04 19:42:55 +08:00
										 |  |  | 			So(cfg.RendererCallbackUrl, ShouldEqual, "http://localhost:3000/") | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2015-02-12 20:31:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 22:15:21 +08:00
										 |  |  | 		Convey("default.ini should have no semi-colon commented entries", func() { | 
					
						
							|  |  |  | 			file, err := os.Open("../../conf/defaults.ini") | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Errorf("failed to load defaults.ini file: %v", err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			defer file.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			scanner := bufio.NewScanner(file) | 
					
						
							|  |  |  | 			for scanner.Scan() { | 
					
						
							|  |  |  | 				// This only catches values commented out with ";" and will not catch those that are commented out with "#".
 | 
					
						
							|  |  |  | 				if strings.HasPrefix(scanner.Text(), ";") { | 
					
						
							|  |  |  | 					t.Errorf("entries in defaults.ini must not be commented or environment variables will not work: %v", scanner.Text()) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 20:31:41 +08:00
										 |  |  | 		Convey("Should be able to override via environment variables", func() { | 
					
						
							|  |  |  | 			os.Setenv("GF_SECURITY_ADMIN_USER", "superduper") | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 			err := cfg.Load(&CommandLineArgs{HomePath: "../../"}) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2015-02-12 20:31:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			So(AdminUser, ShouldEqual, "superduper") | 
					
						
							| 
									
										
										
										
											2018-10-12 13:55:36 +08:00
										 |  |  | 			So(cfg.DataPath, ShouldEqual, filepath.Join(HomePath, "data")) | 
					
						
							|  |  |  | 			So(cfg.LogsPath, ShouldEqual, filepath.Join(cfg.DataPath, "log")) | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-29 00:37:59 +08:00
										 |  |  | 		Convey("Should replace password when defined in environment", func() { | 
					
						
							|  |  |  | 			os.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret") | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 			err := cfg.Load(&CommandLineArgs{HomePath: "../../"}) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-06-29 00:37:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			So(appliedEnvOverrides, ShouldContain, "GF_SECURITY_ADMIN_PASSWORD=*********") | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 01:09:49 +08:00
										 |  |  | 		Convey("Should return an error when url is invalid", func() { | 
					
						
							|  |  |  | 			os.Setenv("GF_DATABASE_URL", "postgres.%31://grafana:secret@postgres:5432/grafana") | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			cfg := NewCfg() | 
					
						
							|  |  |  | 			err := cfg.Load(&CommandLineArgs{HomePath: "../../"}) | 
					
						
							| 
									
										
										
										
											2018-03-29 00:03:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			So(err, ShouldNotBeNil) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-29 00:37:59 +08:00
										 |  |  | 		Convey("Should replace password in URL when url environment is defined", func() { | 
					
						
							|  |  |  | 			os.Setenv("GF_DATABASE_URL", "mysql://user:secret@localhost:3306/database") | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 			err := cfg.Load(&CommandLineArgs{HomePath: "../../"}) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-06-29 00:37:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			So(appliedEnvOverrides, ShouldContain, "GF_DATABASE_URL=mysql://user:-redacted-@localhost:3306/database") | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 		Convey("Should get property map from command line args array", func() { | 
					
						
							|  |  |  | 			props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			So(len(props), ShouldEqual, 2) | 
					
						
							|  |  |  | 			So(props["test"], ShouldEqual, "value") | 
					
						
							|  |  |  | 			So(props["map.test"], ShouldEqual, "1") | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Should be able to override via command line", func() { | 
					
						
							| 
									
										
										
										
											2019-06-12 13:27:47 +08:00
										 |  |  | 			if runtime.GOOS == windows { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 				cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				err := cfg.Load(&CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					HomePath: "../../", | 
					
						
							|  |  |  | 					Args:     []string{`cfg:paths.data=c:\tmp\data`, `cfg:paths.logs=c:\tmp\logs`}, | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-10-12 13:55:36 +08:00
										 |  |  | 				So(cfg.DataPath, ShouldEqual, `c:\tmp\data`) | 
					
						
							|  |  |  | 				So(cfg.LogsPath, ShouldEqual, `c:\tmp\logs`) | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 				cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				err := cfg.Load(&CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					HomePath: "../../", | 
					
						
							|  |  |  | 					Args:     []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"}, | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 13:55:36 +08:00
										 |  |  | 				So(cfg.DataPath, ShouldEqual, "/tmp/data") | 
					
						
							|  |  |  | 				So(cfg.LogsPath, ShouldEqual, "/tmp/logs") | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Should be able to override defaults via command line", func() { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 			err := cfg.Load(&CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2015-04-12 15:15:49 +08:00
										 |  |  | 				HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2015-04-10 16:58:32 +08:00
										 |  |  | 				Args: []string{ | 
					
						
							|  |  |  | 					"cfg:default.server.domain=test2", | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2018-09-17 16:09:49 +08:00
										 |  |  | 				Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 			So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-10 16:58:32 +08:00
										 |  |  | 			So(Domain, ShouldEqual, "test2") | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 20:20:34 +08:00
										 |  |  | 		Convey("Defaults can be overridden in specified config file", func() { | 
					
						
							| 
									
										
										
										
											2019-06-12 13:27:47 +08:00
										 |  |  | 			if runtime.GOOS == windows { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 				cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				err := cfg.Load(&CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2018-09-17 16:09:49 +08:00
										 |  |  | 					Config:   filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"), | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					Args:     []string{`cfg:default.paths.data=c:\tmp\data`}, | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 13:55:36 +08:00
										 |  |  | 				So(cfg.DataPath, ShouldEqual, `c:\tmp\override`) | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 				cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				err := cfg.Load(&CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2018-09-17 16:09:49 +08:00
										 |  |  | 					Config:   filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					Args:     []string{"cfg:default.paths.data=/tmp/data"}, | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 13:55:36 +08:00
										 |  |  | 				So(cfg.DataPath, ShouldEqual, "/tmp/override") | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Command line overrides specified config file", func() { | 
					
						
							| 
									
										
										
										
											2019-06-12 13:27:47 +08:00
										 |  |  | 			if runtime.GOOS == windows { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 				cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				err := cfg.Load(&CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2018-09-17 23:56:52 +08:00
										 |  |  | 					Config:   filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"), | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					Args:     []string{`cfg:paths.data=c:\tmp\data`}, | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 13:55:36 +08:00
										 |  |  | 				So(cfg.DataPath, ShouldEqual, `c:\tmp\data`) | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 				cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				err := cfg.Load(&CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2018-09-17 16:09:49 +08:00
										 |  |  | 					Config:   filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					Args:     []string{"cfg:paths.data=/tmp/data"}, | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 13:55:36 +08:00
										 |  |  | 				So(cfg.DataPath, ShouldEqual, "/tmp/data") | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Can use environment variables in config values", func() { | 
					
						
							| 
									
										
										
										
											2019-06-12 13:27:47 +08:00
										 |  |  | 			if runtime.GOOS == windows { | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 				os.Setenv("GF_DATA_PATH", `c:\tmp\env_override`) | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 				cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				err := cfg.Load(&CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					HomePath: "../../", | 
					
						
							|  |  |  | 					Args:     []string{"cfg:paths.data=${GF_DATA_PATH}"}, | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 13:55:36 +08:00
										 |  |  | 				So(cfg.DataPath, ShouldEqual, `c:\tmp\env_override`) | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				os.Setenv("GF_DATA_PATH", "/tmp/env_override") | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 				cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				err := cfg.Load(&CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 					HomePath: "../../", | 
					
						
							|  |  |  | 					Args:     []string{"cfg:paths.data=${GF_DATA_PATH}"}, | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 13:55:36 +08:00
										 |  |  | 				So(cfg.DataPath, ShouldEqual, "/tmp/env_override") | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-02-12 20:31:41 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 20:20:34 +08:00
										 |  |  | 		Convey("instance_name default to hostname even if hostname env is empty", func() { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 			err := cfg.Load(&CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2016-06-02 20:32:17 +08:00
										 |  |  | 				HomePath: "../../", | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 			So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-06-02 20:32:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			hostname, _ := os.Hostname() | 
					
						
							|  |  |  | 			So(InstanceName, ShouldEqual, hostname) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 19:42:55 +08:00
										 |  |  | 		Convey("Reading callback_url should add trailing slash", func() { | 
					
						
							|  |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 			err := cfg.Load(&CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2018-09-04 19:42:55 +08:00
										 |  |  | 				HomePath: "../../", | 
					
						
							|  |  |  | 				Args:     []string{"cfg:rendering.callback_url=http://myserver/renderer"}, | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2019-10-22 22:17:49 +08:00
										 |  |  | 			So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-09-04 19:42:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			So(cfg.RendererCallbackUrl, ShouldEqual, "http://myserver/renderer/") | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Only sync_ttl should return the value sync_ttl", func() { | 
					
						
							|  |  |  | 			cfg := NewCfg() | 
					
						
							|  |  |  | 			err := cfg.Load(&CommandLineArgs{ | 
					
						
							|  |  |  | 				HomePath: "../../", | 
					
						
							|  |  |  | 				Args:     []string{"cfg:auth.proxy.sync_ttl=2"}, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			So(AuthProxySyncTtl, ShouldEqual, 2) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Only ldap_sync_ttl should return the value ldap_sync_ttl", func() { | 
					
						
							|  |  |  | 			cfg := NewCfg() | 
					
						
							|  |  |  | 			err := cfg.Load(&CommandLineArgs{ | 
					
						
							|  |  |  | 				HomePath: "../../", | 
					
						
							|  |  |  | 				Args:     []string{"cfg:auth.proxy.ldap_sync_ttl=5"}, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			So(AuthProxySyncTtl, ShouldEqual, 5) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("ldap_sync should override ldap_sync_ttl that is default value", func() { | 
					
						
							|  |  |  | 			cfg := NewCfg() | 
					
						
							|  |  |  | 			err := cfg.Load(&CommandLineArgs{ | 
					
						
							|  |  |  | 				HomePath: "../../", | 
					
						
							|  |  |  | 				Args:     []string{"cfg:auth.proxy.sync_ttl=5"}, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			So(AuthProxySyncTtl, ShouldEqual, 5) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("ldap_sync should not override ldap_sync_ttl that is different from default value", func() { | 
					
						
							|  |  |  | 			cfg := NewCfg() | 
					
						
							|  |  |  | 			err := cfg.Load(&CommandLineArgs{ | 
					
						
							|  |  |  | 				HomePath: "../../", | 
					
						
							|  |  |  | 				Args:     []string{"cfg:auth.proxy.ldap_sync_ttl=12", "cfg:auth.proxy.sync_ttl=5"}, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			So(AuthProxySyncTtl, ShouldEqual, 12) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2019-04-25 14:29:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Convey("Test reading string values from .ini file", t, func() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		iniFile, err := ini.Load(path.Join(HomePath, "pkg/setting/testdata/invalid.ini")) | 
					
						
							|  |  |  | 		So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("If key is found - should return value from ini file", func() { | 
					
						
							|  |  |  | 			value, err := valueAsString(iniFile.Section("server"), "alt_url", "") | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 			So(value, ShouldEqual, "https://grafana.com/") | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("If key is not found - should return default value", func() { | 
					
						
							|  |  |  | 			value, err := valueAsString(iniFile.Section("server"), "extra_url", "default_url_val") | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 			So(value, ShouldEqual, "default_url_val") | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("In case of panic - should return user-friendly error", func() { | 
					
						
							|  |  |  | 			value, err := valueAsString(iniFile.Section("server"), "root_url", "") | 
					
						
							|  |  |  | 			So(err.Error(), ShouldEqual, "Invalid value for key 'root_url' in configuration file") | 
					
						
							|  |  |  | 			So(value, ShouldEqual, "") | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-11 17:04:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestParseAppUrlAndSubUrl(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		rootURL           string | 
					
						
							|  |  |  | 		expectedAppURL    string | 
					
						
							|  |  |  | 		expectedAppSubURL string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{rootURL: "http://localhost:3000/", expectedAppURL: "http://localhost:3000/"}, | 
					
						
							|  |  |  | 		{rootURL: "http://localhost:3000", expectedAppURL: "http://localhost:3000/"}, | 
					
						
							|  |  |  | 		{rootURL: "http://localhost:3000/grafana", expectedAppURL: "http://localhost:3000/grafana/", expectedAppSubURL: "/grafana"}, | 
					
						
							|  |  |  | 		{rootURL: "http://localhost:3000/grafana/", expectedAppURL: "http://localhost:3000/grafana/", expectedAppSubURL: "/grafana"}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tc := range testCases { | 
					
						
							|  |  |  | 		f := ini.Empty() | 
					
						
							|  |  |  | 		s, err := f.NewSection("server") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = s.NewKey("root_url", tc.rootURL) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		appURL, appSubURL, err := parseAppUrlAndSubUrl(s) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		require.Equal(t, tc.expectedAppURL, appURL) | 
					
						
							|  |  |  | 		require.Equal(t, tc.expectedAppSubURL, appSubURL) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |