Skip to content

Commit

Permalink
Add a very simple test.
Browse files Browse the repository at this point in the history
  • Loading branch information
cure committed Apr 26, 2021
1 parent 031c91a commit 37601f6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build:
dev: lint test build

test:
go test -coverprofile=coverage.out
@go test -coverprofile=coverage.out ./...

coverprofile_func:
go tool cover -func=coverage.out
Expand Down
18 changes: 13 additions & 5 deletions cmd/headscale/headscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,16 @@ var createPreAuthKeyCmd = &cobra.Command{
},
}

func main() {
func loadConfig(path string) {
viper.SetConfigName("config")
viper.AddConfigPath("/etc/headscale/")
viper.AddConfigPath("$HOME/.headscale")
viper.AddConfigPath(".")
if path == "" {
viper.AddConfigPath("/etc/headscale/")
viper.AddConfigPath("$HOME/.headscale")
viper.AddConfigPath(".")
} else {
// For testing
viper.AddConfigPath(path)
}
viper.AutomaticEnv()

viper.SetDefault("tls_letsencrypt_cache_dir", "/var/www/.cache")
Expand All @@ -279,6 +284,10 @@ func main() {
if !strings.HasPrefix(viper.GetString("server_url"), "http://") && !strings.HasPrefix(viper.GetString("server_url"), "https://") {
log.Fatalf("Fatal config error: server_url must start with https:// or http://")
}
}

func main() {
loadConfig("")

headscaleCmd.AddCommand(versionCmd)
headscaleCmd.AddCommand(serveCmd)
Expand All @@ -302,7 +311,6 @@ func main() {
fmt.Println(err)
os.Exit(-1)
}

}

func absPath(path string) string {
Expand Down
56 changes: 56 additions & 0 deletions cmd/headscale/headscale_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/spf13/viper"
"gopkg.in/check.v1"
)

func Test(t *testing.T) {
check.TestingT(t)
}

var _ = check.Suite(&Suite{})

type Suite struct{}

func (s *Suite) SetUpSuite(c *check.C) {
}

func (s *Suite) TearDownSuite(c *check.C) {

}

func (*Suite) TestConfigLoading(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.example"), filepath.Join(tmpDir, "config.json"))
if err != nil {
c.Fatal(err)
}

// Load config
loadConfig(tmpDir)

// 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("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_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")
}

0 comments on commit 37601f6

Please sign in to comment.