-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
138 lines (120 loc) · 3.06 KB
/
integration_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
// +build integration
package main
import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path"
"strconv"
"syscall"
"testing"
"time"
"github.com/pp2p/paranoid/libpfs/commands"
"github.com/pp2p/paranoid/logger"
"github.com/pp2p/pfsd/globals"
)
func createTestDir(t *testing.T, name string) {
os.RemoveAll(path.Join(os.TempDir(), name))
err := os.Mkdir(path.Join(os.TempDir(), name), 0777)
if err != nil {
if os.IsExist(err) {
cmd := exec.Command("fusermount", "-z", "-u", path.Join(os.TempDir(), name))
cmd.Run()
os.RemoveAll(path.Join(os.TempDir(), name))
err = os.Mkdir(path.Join(os.TempDir(), name), 0777)
if err != nil {
t.Fatal("Error creating directory", err)
}
} else {
t.Fatal("Error creating directory", err)
}
}
}
func removeTestDir(name string) {
time.Sleep(1 * time.Second)
os.RemoveAll(path.Join(os.TempDir(), name))
}
func TestKillSignal(t *testing.T) {
createTestDir(t, "testksMountpoint")
defer removeTestDir("testksMountpoint")
createTestDir(t, "testksDirectory")
defer removeTestDir("testksDirectory")
commands.Log = logger.New("pfsdintegration", "pfsdintegration", os.DevNull)
discovery := exec.Command("discovery-server", "--port=10102", "-state=false")
err := discovery.Start()
if err != nil {
t.Fatal(err)
}
defer func() {
err := discovery.Process.Kill()
if err != nil {
t.Error("Failed to kill discovery server,", err)
}
}()
_, err = commands.InitCommand(path.Join(os.TempDir(), "testksDirectory"))
if err != nil {
t.Fatal(err)
}
attributes := &globals.FileSystemAttributes{
Encrypted: true,
KeyGenerated: false,
NetworkOff: false,
}
attributesJSON, err := json.Marshal(attributes)
if err != nil {
t.Fatal("unable to save file system attributes to file:", err)
}
err = ioutil.WriteFile(path.Join(os.TempDir(), "testksDirectory", "meta", "attributes"), attributesJSON, 0600)
if err != nil {
t.Fatal("unable to save file system attributes to file:", err)
}
pfsd := exec.Command(
"pfsd",
path.Join(os.TempDir(), "testksDirectory"),
path.Join(os.TempDir(), "testksMountpoint"),
"localhost",
"10102",
"testPool",
"",
)
pfsd.Stderr = os.Stderr
err = pfsd.Start()
if err != nil {
t.Fatal(err)
}
defer pfsd.Process.Kill()
defer func() {
cmd := exec.Command("fusermount", "-z", "-u", path.Join(os.TempDir(), "testksMountPoint"))
cmd.Run()
}()
time.Sleep(10 * time.Second)
pidPath := path.Join(os.TempDir(), "testksDirectory", "meta", "pfsd.pid")
if _, err := os.Stat(pidPath); err == nil {
pidByte, err := ioutil.ReadFile(pidPath)
if err != nil {
t.Fatal("Can't read pid file", err)
}
pid, err := strconv.Atoi(string(pidByte))
if err != nil {
t.Fatal("Incorrect pid information", err)
}
err = syscall.Kill(pid, syscall.SIGTERM)
if err != nil {
t.Fatal("Can not kill PFSD,", err)
}
done := make(chan bool, 1)
go func() {
pfsd.Wait()
done <- true
}()
select {
case <-time.After(10 * time.Second):
t.Fatal("pfsd did not finish within 10 seconds")
case <-done:
break
}
} else {
t.Fatal("Could not read pid file:", err)
}
}