mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-29 16:30:22 +00:00
TUN-528: Move cloudflared into a separate repo
This commit is contained in:
61
vendor/github.com/mholt/caddy/onevent/hook/hook_test.go
generated
vendored
Normal file
61
vendor/github.com/mholt/caddy/onevent/hook/hook_test.go
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
package hook
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
|
||||
func TestHook(t *testing.T) {
|
||||
tempDirPath := os.TempDir()
|
||||
|
||||
testDir := filepath.Join(tempDirPath, "temp_dir_for_testing_command")
|
||||
defer func() {
|
||||
// clean up after non-blocking startup function quits
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
os.RemoveAll(testDir)
|
||||
}()
|
||||
osSenitiveTestDir := filepath.FromSlash(testDir)
|
||||
os.RemoveAll(osSenitiveTestDir) // start with a clean slate
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
event caddy.EventName
|
||||
command string
|
||||
args []string
|
||||
shouldErr bool
|
||||
shouldRemoveErr bool
|
||||
}{
|
||||
{name: "blocking", event: caddy.InstanceStartupEvent, command: "mkdir", args: []string{osSenitiveTestDir}, shouldErr: false, shouldRemoveErr: false},
|
||||
{name: "nonBlocking", event: caddy.ShutdownEvent, command: "mkdir", args: []string{osSenitiveTestDir, "&"}, shouldErr: false, shouldRemoveErr: true},
|
||||
{name: "nonBlocking2", event: caddy.ShutdownEvent, command: "echo", args: []string{"&"}, shouldErr: false, shouldRemoveErr: true},
|
||||
{name: "nonExistent", event: caddy.CertRenewEvent, command: strconv.Itoa(int(time.Now().UnixNano())), shouldErr: true, shouldRemoveErr: true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
cfg := new(Config)
|
||||
cfg.ID = uuid.New().String()
|
||||
cfg.Event = test.event
|
||||
cfg.Command = test.command
|
||||
cfg.Args = test.args
|
||||
|
||||
err := cfg.Hook(test.event, nil)
|
||||
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)
|
||||
}
|
||||
|
||||
err = os.Remove(osSenitiveTestDir)
|
||||
if err != nil && !test.shouldRemoveErr {
|
||||
t.Errorf("Test received an error of:\n%v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user