aboutsummaryrefslogtreecommitdiff
path: root/test/external_test.go
blob: f5737e17b6ebaa69fbb3ef7b15f3d75a6ceb1e67 (plain) (blame)
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
package test

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
	"testing"
)

// Go get external example plugin, compile it into CoreDNS
// and check if it is really there, but running coredns -plugins.

// Dangerous test as it messes with your git tree, maybe use tag?
func testExternalPluginCompile(t *testing.T) {
	if err := addExamplePlugin(); err != nil {
		t.Fatal(err)
	}
	defer run(t, gitReset)

	if _, err := run(t, goGet); err != nil {
		t.Fatal(err)
	}

	if _, err := run(t, goGen); err != nil {
		t.Fatal(err)
	}

	if _, err := run(t, goBuild); err != nil {
		t.Fatal(err)
	}

	out, err := run(t, coredns)
	if err != nil {
		t.Fatal(err)
	}

	if !strings.Contains(string(out), "dns.example") {
		t.Fatal("Plugin dns.example should be there")
	}
}

func run(t *testing.T, c *exec.Cmd) ([]byte, error) {
	c.Dir = ".."
	out, err := c.Output()
	if err != nil {
		return nil, fmt.Errorf("Run: failed to run %s %s: %q", c.Args[0], c.Args[1], err)
	}
	return out, nil

}

func addExamplePlugin() error {
	f, err := os.OpenFile("../plugin.cfg", os.O_APPEND|os.O_WRONLY, os.ModeAppend)
	if err != nil {
		return err
	}
	defer f.Close()

	_, err = f.WriteString(example)
	return err
}

var (
	goBuild  = exec.Command("go", "build")
	goGen    = exec.Command("go", "generate")
	goGet    = exec.Command("go", "get", "github.com/coredns/example")
	gitReset = exec.Command("git", "checkout", "core/*")
	coredns  = exec.Command("./coredns", "-plugins")
)

const example = "1001:example:github.com/coredns/example"