TUN-528: Move cloudflared into a separate repo

This commit is contained in:
Areg Harutyunyan
2018-05-01 18:45:06 -05:00
parent e8c621a648
commit d06fc520c7
4726 changed files with 1763680 additions and 0 deletions

20
vendor/github.com/mholt/caddy/onevent/hook/config.go generated vendored Normal file
View File

@@ -0,0 +1,20 @@
package hook
import (
"github.com/mholt/caddy"
)
// Config describes how Hook should be configured and used.
type Config struct {
ID string
Event caddy.EventName
Command string
Args []string
}
// SupportedEvents is a map of supported events.
var SupportedEvents = map[string]caddy.EventName{
"startup": caddy.InstanceStartupEvent,
"shutdown": caddy.ShutdownEvent,
"certrenew": caddy.CertRenewEvent,
}

41
vendor/github.com/mholt/caddy/onevent/hook/hook.go generated vendored Normal file
View File

@@ -0,0 +1,41 @@
package hook
import (
"log"
"os"
"os/exec"
"strings"
"github.com/mholt/caddy"
)
// Hook executes a command.
func (cfg *Config) Hook(event caddy.EventName, info interface{}) error {
if event != cfg.Event {
return nil
}
nonblock := false
if len(cfg.Args) >= 1 && cfg.Args[len(cfg.Args)-1] == "&" {
// Run command in background; non-blocking
nonblock = true
cfg.Args = cfg.Args[:len(cfg.Args)-1]
}
// Execute command.
cmd := exec.Command(cfg.Command, cfg.Args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if nonblock {
log.Printf("[INFO] Nonblocking Command \"%s %s\" with ID %s", cfg.Command, strings.Join(cfg.Args, " "), cfg.ID)
return cmd.Start()
}
log.Printf("[INFO] Blocking Command \"%s %s\" with ID %s", cfg.Command, strings.Join(cfg.Args, " "), cfg.ID)
err := cmd.Run()
if err != nil {
return err
}
return nil
}

View 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)
}
})
}
}