-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluginator_common_test.go
123 lines (106 loc) · 2.56 KB
/
pluginator_common_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
// Copyright Piero de Salvia.
// All Rights Reserved
package pluginator
import (
"errors"
"flag"
"io"
"io/ioutil"
"os"
"testing"
)
var (
testDataDir string
runConsulTests = flag.Bool("consul", false, "whether to run tests requiring a consul instance running at localhost:8500")
)
func init() {
goPath := os.Getenv("GOPATH")
testDataDir = goPath + "/src/github.com/pierods/pluginator/testdata"
}
func TestMain(m *testing.M) {
flag.Parse()
retCode := m.Run()
os.Exit(retCode)
}
func copyTestFile(dst, src string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
if stats, _ := os.Stat(dst); stats != nil {
return errors.New(dst + " already exists")
}
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Sync()
}
func deleteTestFile(fileName string) error {
if _, err := os.Stat(fileName); os.IsNotExist(err) {
return errors.New(fileName + " does not exist")
}
return os.Remove(fileName)
}
func updateTestFile(fileName, content string) error {
if _, err := os.Stat(fileName); os.IsNotExist(err) {
return errors.New(fileName + " does not exist")
}
return ioutil.WriteFile(fileName, []byte(content), 700)
}
func readTestFile(fileName string) (string, error) {
if _, err := os.Stat(fileName); os.IsNotExist(err) {
return "", errors.New(fileName + " does not exist")
}
content, err := ioutil.ReadFile(fileName)
if err != nil {
return "", err
}
return string(content), nil
}
type EventSubscriber struct {
ScanDone chan bool
RemoveDone chan bool
UpdateDone chan bool
AddDone chan bool
AddedName string
AddedLib *PluginContent
RemovedName string
RemovedLib *PluginContent
ScannedPlugins map[string]*PluginContent
UpdatedName string
UpdatedLib *PluginContent
}
func (e *EventSubscriber) ScanSubscriber(pluginNamesAndLibs map[string]*PluginContent) {
e.ScannedPlugins = pluginNamesAndLibs
go func() {
e.ScanDone <- true
}()
}
func (e *EventSubscriber) RemoveSubscriber(pluginName string, pluginLib *PluginContent) {
e.RemovedName = pluginName
e.RemovedLib = pluginLib
go func() {
e.RemoveDone <- true
}()
}
func (e *EventSubscriber) UpdateSubscriber(pluginName string, pluginLib *PluginContent) {
e.UpdatedName = pluginName
e.UpdatedLib = pluginLib
go func() {
e.UpdateDone <- true
}()
}
func (e *EventSubscriber) AddSubscriber(pluginName string, pluginLib *PluginContent) {
e.AddedName = pluginName
e.AddedLib = pluginLib
go func() {
e.AddDone <- true
}()
}