From da9a07ddf3ec0e84dbb7d6b702192d111a983b44 Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Sat, 15 May 2021 14:36:13 +0200 Subject: [PATCH] Added tests for the sqlite-based config --- cmd/headscale/headscale_test.go | 41 +++++++++++++++++-- ...on.example => config.json.postgres.example | 3 +- config.json.sqlite.example | 13 ++++++ 3 files changed, 52 insertions(+), 5 deletions(-) rename config.json.example => config.json.postgres.example (85%) create mode 100644 config.json.sqlite.example diff --git a/cmd/headscale/headscale_test.go b/cmd/headscale/headscale_test.go index baa2a65..bc399e7 100644 --- a/cmd/headscale/headscale_test.go +++ b/cmd/headscale/headscale_test.go @@ -27,7 +27,7 @@ func (s *Suite) TearDownSuite(c *check.C) { } -func (*Suite) TestConfigLoading(c *check.C) { +func (*Suite) TestPostgresConfigLoading(c *check.C) { tmpDir, err := ioutil.TempDir("", "headscale") if err != nil { c.Fatal(err) @@ -40,7 +40,7 @@ func (*Suite) TestConfigLoading(c *check.C) { } // Symlink the example config file - err = os.Symlink(filepath.Clean(path+"/../../config.json.example"), filepath.Join(tmpDir, "config.json")) + err = os.Symlink(filepath.Clean(path+"/../../config.json.postgres.example"), filepath.Join(tmpDir, "config.json")) if err != nil { c.Fatal(err) } @@ -50,14 +50,47 @@ func (*Suite) TestConfigLoading(c *check.C) { c.Assert(err, check.IsNil) // Test that config file was interpreted correctly - c.Assert(viper.GetString("server_url"), check.Equals, "http://192.168.1.12:8000") + c.Assert(viper.GetString("server_url"), check.Equals, "http://127.0.0.1:8000") c.Assert(viper.GetString("listen_addr"), check.Equals, "0.0.0.0:8000") c.Assert(viper.GetString("derp_map_path"), check.Equals, "derp.yaml") + c.Assert(viper.GetString("db_type"), check.Equals, "postgres") c.Assert(viper.GetString("db_port"), check.Equals, "5432") c.Assert(viper.GetString("tls_letsencrypt_hostname"), check.Equals, "") c.Assert(viper.GetString("tls_letsencrypt_challenge_type"), check.Equals, "HTTP-01") } +func (*Suite) TestSqliteConfigLoading(c *check.C) { + tmpDir, err := ioutil.TempDir("", "headscale") + if err != nil { + c.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + path, err := os.Getwd() + if err != nil { + c.Fatal(err) + } + + // Symlink the example config file + err = os.Symlink(filepath.Clean(path+"/../../config.json.sqlite.example"), filepath.Join(tmpDir, "config.json")) + if err != nil { + c.Fatal(err) + } + + // Load example config, it should load without validation errors + err = loadConfig(tmpDir) + c.Assert(err, check.IsNil) + + // Test that config file was interpreted correctly + c.Assert(viper.GetString("server_url"), check.Equals, "http://127.0.0.1:8000") + c.Assert(viper.GetString("listen_addr"), check.Equals, "0.0.0.0:8000") + c.Assert(viper.GetString("derp_map_path"), check.Equals, "derp.yaml") + c.Assert(viper.GetString("db_type"), check.Equals, "sqlite3") + c.Assert(viper.GetString("db_path"), check.Equals, "./db.sqlite") + c.Assert(viper.GetString("tls_letsencrypt_hostname"), check.Equals, "") + c.Assert(viper.GetString("tls_letsencrypt_challenge_type"), check.Equals, "HTTP-01") +} + func writeConfig(c *check.C, tmpDir string, configYaml []byte) { // Populate a custom config file configFile := filepath.Join(tmpDir, "config.yaml") @@ -89,7 +122,7 @@ func (*Suite) TestTLSConfigValidation(c *check.C) { fmt.Println(tmp) // Check configuration validation errors (2) - configYaml = []byte("---\nserver_url: \"http://192.168.1.12:8000\"\ntls_letsencrypt_hostname: \"example.com\"\ntls_letsencrypt_challenge_type: \"TLS-ALPN-01\"") + configYaml = []byte("---\nserver_url: \"http://127.0.0.1:8000\"\ntls_letsencrypt_hostname: \"example.com\"\ntls_letsencrypt_challenge_type: \"TLS-ALPN-01\"") writeConfig(c, tmpDir, configYaml) err = loadConfig(tmpDir) c.Assert(err, check.NotNil) diff --git a/config.json.example b/config.json.postgres.example similarity index 85% rename from config.json.example rename to config.json.postgres.example index 5811dac..6436ec5 100644 --- a/config.json.example +++ b/config.json.postgres.example @@ -1,8 +1,9 @@ { - "server_url": "http://192.168.1.12:8000", + "server_url": "http://127.0.0.1:8000", "listen_addr": "0.0.0.0:8000", "private_key_path": "private.key", "derp_map_path": "derp.yaml", + "db_type": "postgres", "db_host": "localhost", "db_port": 5432, "db_name": "headscale", diff --git a/config.json.sqlite.example b/config.json.sqlite.example new file mode 100644 index 0000000..40bd85b --- /dev/null +++ b/config.json.sqlite.example @@ -0,0 +1,13 @@ +{ + "server_url": "http://127.0.0.1:8000", + "listen_addr": "0.0.0.0:8000", + "private_key_path": "private.key", + "derp_map_path": "derp.yaml", + "db_type": "sqlite3", + "db_path": "./db.sqlite", + "tls_letsencrypt_hostname": "", + "tls_letsencrypt_cache_dir": ".cache", + "tls_letsencrypt_challenge_type": "HTTP-01", + "tls_cert_path": "", + "tls_key_path": "" +}