forked from pshima/consul-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acceptance_test.go
161 lines (132 loc) · 3.78 KB
/
acceptance_test.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"reflect"
"testing"
"time"
randomdata "github.com/Pallinder/go-randomdata"
consulapi "github.com/hashicorp/consul/api"
"github.com/pshima/consul-snapshot/backup"
"github.com/pshima/consul-snapshot/config"
"github.com/pshima/consul-snapshot/consul"
"github.com/pshima/consul-snapshot/restore"
)
type Seeder struct {
Data []*consulapi.KVPair
}
type LocalDevConsul struct {
Command string
CommandArgs []string
CommandOut []string
}
func scan(s *bufio.Scanner, c *LocalDevConsul) {
for s.Scan() {
c.CommandOut = append(c.CommandOut, s.Text())
}
}
func (c *LocalDevConsul) Run() {
cmd := exec.Command(c.Command, c.CommandArgs...)
c.CommandOut = append(c.CommandOut, "Preparing to start...")
reader, err := cmd.StdoutPipe()
if err != nil {
log.Print("Unable to create stdout pipe for command")
}
scanner := bufio.NewScanner(reader)
go scan(scanner, c)
err = cmd.Start()
if err != nil {
log.Printf("Unable to start: %v", err)
}
err = cmd.Wait()
if err != nil {
log.Printf("Unable to wait for command: %v", err)
}
err = cmd.Process.Kill()
if err != nil {
log.Printf("Unable to kill command: %v", err)
}
}
func putKey(c *consulapi.Client, k string, v string, s *Seeder) error {
writeOpt := &consulapi.WriteOptions{}
var err error
writeKey := &consulapi.KVPair{Key: k, Value: []byte(v)}
s.Data = append(s.Data, writeKey)
_, err = c.KV().Put(writeKey, writeOpt)
if err != nil {
return fmt.Errorf("Failed to write test key: %v", err)
}
return nil
}
func checkKey(c *consulapi.Client, kv *consulapi.KVPair) error {
queryOpt := &consulapi.QueryOptions{}
keyCheck, _, err := c.KV().Get(kv.Key, queryOpt)
if err != nil || keyCheck == nil {
return fmt.Errorf("Failed to get key: %v", err)
}
reflecttest := reflect.DeepEqual(keyCheck.Value, kv.Value)
if reflecttest != true {
return fmt.Errorf("Key %v did not match\n\tExpected: %v\n\tGot: %v", kv.Key, string(kv.Value), string(keyCheck.Value))
}
return nil
}
func TestAcceptance(t *testing.T) {
var err error
conf := config.ParseConfig(true)
if conf.Acceptance == false {
t.Skip("Skipping acceptance test, Set ACCEPTANCE_TEST=1 to run")
}
command := "consul"
commandargs := []string{"agent", "-dev", "-bind=127.0.0.1"}
cmd := &LocalDevConsul{Command: command, CommandArgs: commandargs}
go cmd.Run()
// 5-10 seconds is about the time it takes for consul to wake up
time.Sleep(5 * time.Second)
c := consul.Client()
seedData := &Seeder{}
t.Log("Adding random seed data to consul kv")
for i := 1; i < 500; i++ {
// more entropy is needed here.
randname := fmt.Sprintf("%s.%s", randomdata.SillyName(), randomdata.SillyName())
err = putKey(c, randname, randomdata.Address(), seedData)
if err != nil {
t.Errorf("Failed putting test data to kv: %v", err)
}
err = putKey(c, randomdata.IpV4Address(), randomdata.Paragraph(), seedData)
if err != nil {
t.Errorf("Failed putting test data to kv: %v", err)
}
}
t.Log("Starting Backup")
backup.Runner("test", true)
_, err = c.KV().DeleteTree("", nil)
if err != nil {
for _, i := range cmd.CommandOut {
log.Printf("CONSUL: %v", i)
}
t.Errorf("Unable to clear consul kv store after backup; %v", err)
}
restore.Runner("/tmp/acceptancetest.tar.gz")
for _, kv := range seedData.Data {
//log.Printf("SEED: %v | %v", kv.Key, string(kv.Value))
err := checkKey(c, kv)
if err != nil {
for _, i := range cmd.CommandOut {
log.Printf("CONSUL: %v", i)
}
t.Errorf("Key Failure: %v", err)
}
}
// Remove the staging path
err = os.RemoveAll("/tmp/acceptancetest")
if err != nil {
log.Printf("Unable to remove temporary backup file: %v", err)
}
err = os.RemoveAll("/tmp/acceptancetest.tar.gz")
if err != nil {
log.Printf("Unable to remove temporary backup file: %v", err)
}
}