Browse Source

Initialise config immediately

Add tests
master
Chris Smith 5 years ago
parent
commit
696f3af4f8
4 changed files with 117 additions and 2 deletions
  1. 31
    0
      config_test.go
  2. 1
    2
      dotege.go
  3. 55
    0
      dotege_test.go
  4. 30
    0
      lego_test.go

+ 31
- 0
config_test.go View File

@@ -0,0 +1,31 @@
1
+package main
2
+
3
+import (
4
+	"reflect"
5
+	"testing"
6
+)
7
+
8
+func Test_splitList(t *testing.T) {
9
+	type args struct {
10
+		input string
11
+	}
12
+	tests := []struct {
13
+		name string
14
+		args args
15
+		want []string
16
+	}{
17
+		{"empty string", args{""}, []string{}},
18
+		{"single item", args{"test"}, []string{"test"}},
19
+		{"spaces", args{"test1 test2 test3"}, []string{"test1", "test2", "test3"}},
20
+		{"commas", args{"test1,test2,test3"}, []string{"test1", "test2", "test3"}},
21
+		{"gaps", args{"test1   test2  test3 "}, []string{"test1", "test2", "test3"}},
22
+		{"mixed", args{"  test1, test2 test3,,,"}, []string{"test1", "test2", "test3"}},
23
+	}
24
+	for _, tt := range tests {
25
+		t.Run(tt.name, func(t *testing.T) {
26
+			if got := splitList(tt.args.input); !reflect.DeepEqual(got, tt.want) {
27
+				t.Errorf("splitList() = %v, want %v", got, tt.want)
28
+			}
29
+		})
30
+	}
31
+}

+ 1
- 2
dotege.go View File

@@ -36,8 +36,8 @@ type Hostname struct {
36 36
 var (
37 37
 	logger             *zap.SugaredLogger
38 38
 	certificateManager *CertificateManager
39
-	config             *Config
40 39
 	dockerClient       *client.Client
40
+	config             = createConfig()
41 41
 	containers         = make(map[string]*Container)
42 42
 )
43 43
 
@@ -88,7 +88,6 @@ func main() {
88 88
 	logger.Info("Dotege is starting")
89 89
 
90 90
 	doneChan := monitorSignals()
91
-	createConfig()
92 91
 
93 92
 	var err error
94 93
 	dockerStopChan := make(chan struct{})

+ 55
- 0
dotege_test.go View File

@@ -0,0 +1,55 @@
1
+package main
2
+
3
+import (
4
+	"reflect"
5
+	"testing"
6
+)
7
+
8
+func Test_wildcardMatches(t *testing.T) {
9
+	type args struct {
10
+		wildcard string
11
+		domain   string
12
+	}
13
+	tests := []struct {
14
+		name string
15
+		args args
16
+		want bool
17
+	}{
18
+		{"non-matching", args{"example.com", "example.org"}, false},
19
+		{"same level domain", args{"example.com", "example.com"}, false},
20
+		{"single level sub domain", args{"example.com", "foo.example.com"}, true},
21
+		{"multi level sub domain", args{"example.com", "bar.foo.example.com"}, false},
22
+	}
23
+	for _, tt := range tests {
24
+		t.Run(tt.name, func(t *testing.T) {
25
+			if got := wildcardMatches(tt.args.wildcard, tt.args.domain); got != tt.want {
26
+				t.Errorf("wildcardMatches() = %v, want %v", got, tt.want)
27
+			}
28
+		})
29
+	}
30
+}
31
+
32
+func Test_applyWildcards(t *testing.T) {
33
+	type args struct {
34
+		domains   []string
35
+		wildcards []string
36
+	}
37
+	tests := []struct {
38
+		name string
39
+		args args
40
+		want []string
41
+	}{
42
+		{"no wildcards", args{[]string{"example.com", "example.org"}, []string{}}, []string{"example.com", "example.org"}},
43
+		{"non-matching wildcards", args{[]string{"example.com", "example.org"}, []string{"example.net"}}, []string{"example.com", "example.org"}},
44
+		{"single match", args{[]string{"foo.example.com", "example.org"}, []string{"example.com"}}, []string{"*.example.com", "example.org"}},
45
+		{"multiple matches", args{[]string{"foo.example.com", "example.org", "bar.example.com"}, []string{"example.com"}}, []string{"*.example.com", "example.org"}},
46
+		{"multiple wildcards", args{[]string{"foo.example.com", "baz.example.org", "bar.example.com"}, []string{"example.com", "example.org"}}, []string{"*.example.com", "*.example.org"}},
47
+	}
48
+	for _, tt := range tests {
49
+		t.Run(tt.name, func(t *testing.T) {
50
+			if got := applyWildcards(tt.args.domains, tt.args.wildcards); !reflect.DeepEqual(got, tt.want) {
51
+				t.Errorf("applyWildcards() = %v, want %v", got, tt.want)
52
+			}
53
+		})
54
+	}
55
+}

+ 30
- 0
lego_test.go View File

@@ -0,0 +1,30 @@
1
+package main
2
+
3
+import "testing"
4
+
5
+func Test_domainsMatch(t *testing.T) {
6
+	type args struct {
7
+		domains1 []string
8
+		domains2 []string
9
+	}
10
+	tests := []struct {
11
+		name string
12
+		args args
13
+		want bool
14
+	}{
15
+		{"separate single domains", args{[]string{"example.com"}, []string{"example.org"}}, false},
16
+		{"matching single domains", args{[]string{"example.com"}, []string{"example.com"}}, true},
17
+		{"matching subject missing sans", args{[]string{"example.com", "example.org"}, []string{"example.com"}}, false},
18
+		{"matching subject extra sans", args{[]string{"example.com"}, []string{"example.com", "example.org"}}, false},
19
+		{"matching subject different sans", args{[]string{"example.com", "example.org"}, []string{"example.com", "example.net"}}, false},
20
+		{"mismatched subject and san", args{[]string{"example.org", "example.com"}, []string{"example.com", "example.org"}}, false},
21
+		{"reordered sans", args{[]string{"example.org", "example.com", "example.net"}, []string{"example.org", "example.net", "example.com"}}, true},
22
+	}
23
+	for _, tt := range tests {
24
+		t.Run(tt.name, func(t *testing.T) {
25
+			if got := domainsMatch(tt.args.domains1, tt.args.domains2); got != tt.want {
26
+				t.Errorf("domainsMatch() = %v, want %v", got, tt.want)
27
+			}
28
+		})
29
+	}
30
+}

Loading…
Cancel
Save