File: hooks_test.go

package info (click to toggle)
acmetool 0.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: sh: 349; makefile: 105
file content (84 lines) | stat: -rw-r--r-- 1,340 bytes parent folder | download | duplicates (3)
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
82
83
84
package hooks

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
)

const fileTpl = `#!/bin/sh
%s
[ -n "$ACME_STATE_DIR" ] || exit 1
echo NOTIFY-%d >> "$ACME_STATE_DIR/log"
while read line; do
  echo L-$line >> "$ACME_STATE_DIR/log"
done`

var answer = []string{
	`NOTIFY-0
L-a.b
L-c.d
L-e.f.g
NOTIFY-1
L-a.b
L-c.d
L-e.f.g
`,
	`NOTIFY-0
L-a.b
L-c.d
L-e.f.g
NOTIFY-3
L-a.b
L-c.d
L-e.f.g
`,
}

func TestNotify(t *testing.T) {
	dir, err := ioutil.TempDir("", "acme-notify-test")
	if err != nil {
		t.Fatal(err)
	}

	defer os.RemoveAll(dir)

	notify1 := filepath.Join(dir, "notify1")
	notify2 := filepath.Join(dir, "notify2")
	notifyDirs := []string{notify1, notify2}

	for i := 0; i < 2; i++ {
		err = Replace(notifyDirs, "alpha", fmt.Sprintf(fileTpl, "", i*2+0))
		if err != nil {
			t.Fatal(err)
		}

		err = Replace(notifyDirs, "beta", fmt.Sprintf(fileTpl, "#!acmetool-managed!#", i*2+1))
		if err != nil {
			t.Fatal(err)
		}

		os.Remove(filepath.Join(dir, "log"))

		ctx := &Context{
			HookDirs: notifyDirs,
			StateDir: dir,
		}
		err = NotifyLiveUpdated(ctx, []string{"a.b", "c.d", "e.f.g"})
		if err != nil {
			t.Fatal(err)
		}

		b, err := ioutil.ReadFile(filepath.Join(dir, "log"))
		if err != nil {
			t.Fatal(err)
		}

		s := string(b)
		if s != answer[i] {
			t.Fatalf("mismatch: %v != %v", s, answer[i])
		}
	}
}