File: on_test.go

package info (click to toggle)
golang-github-coredns-caddy 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 360 kB
  • sloc: makefile: 2
file content (81 lines) | stat: -rw-r--r-- 2,560 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package onevent

import (
	"testing"

	"github.com/coredns/caddy"
	"github.com/coredns/caddy/onevent/hook"
)

func TestSetup(t *testing.T) {
	tests := []struct {
		name      string
		input     string
		shouldErr bool
	}{
		{name: "noInput", input: "on", shouldErr: true},
		{name: "nonExistent", input: "on xyz cmd arg", shouldErr: true},
		{name: "startup", input: "on startup cmd arg", shouldErr: false},
		{name: "shutdown", input: "on shutdown cmd arg &", shouldErr: false},
		{name: "certrenew", input: "on certrenew cmd arg", shouldErr: false},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			c := caddy.NewTestController("http", test.input)
			c.Key = test.name

			err := setup(c)

			if err == nil && test.shouldErr {
				t.Error("Test didn't error, but it should have")
			} else if err != nil && !test.shouldErr {
				t.Errorf("Test errored, but it shouldn't have; got '%v'", err)
			}
		})
	}
}

func TestCommandParse(t *testing.T) {
	tests := []struct {
		name      string
		input     string
		shouldErr bool
		config    hook.Config
	}{
		{name: "noInput", input: `on`, shouldErr: true},
		{name: "nonExistent", input: "on xyz cmd arg", shouldErr: true},
		{name: "startup", input: `on startup cmd arg1 arg2`, shouldErr: false, config: hook.Config{Event: caddy.InstanceStartupEvent, Command: "cmd", Args: []string{"arg1", "arg2"}}},
		{name: "shutdown", input: `on shutdown cmd arg1 arg2 &`, shouldErr: false, config: hook.Config{Event: caddy.ShutdownEvent, Command: "cmd", Args: []string{"arg1", "arg2", "&"}}},
		{name: "certrenew", input: `on certrenew cmd arg1 arg2`, shouldErr: false, config: hook.Config{Event: caddy.CertRenewEvent, Command: "cmd", Args: []string{"arg1", "arg2"}}},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			config, err := onParse(caddy.NewTestController("http", test.input))

			if err == nil && test.shouldErr {
				t.Error("Test didn't error, but it should have")
			} else if err != nil && !test.shouldErr {
				t.Errorf("Test errored, but it shouldn't have; got '%v'", err)
			}

			for _, cfg := range config {
				if cfg.Event != test.config.Event {
					t.Errorf("Expected event %s; got %s", test.config.Event, cfg.Event)
				}

				if cfg.Command != test.config.Command {
					t.Errorf("Expected command %s; got %s", test.config.Command, cfg.Command)
				}

				for i, arg := range cfg.Args {
					if arg != test.config.Args[i] {
						t.Errorf("Expected arg in position %d to be %s, got %s", i, test.config.Args[i], arg)
					}
				}

			}
		})
	}
}