forked from newrelic/nri-flex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_test.go
108 lines (93 loc) · 2.67 KB
/
e2e_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
package scenarios
import (
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/newrelic/nri-flex/test/testbed"
"github.com/newrelic/nri-flex/test/testbed/scenarios/fixtures"
"github.com/stretchr/testify/assert"
)
func tmpFile(data string) (file *os.File, err error) {
file, err = ioutil.TempFile("", "")
if err != nil {
return
}
_, err = file.Write([]byte(data))
file.Close()
return
}
func TestCommandAPI(t *testing.T) {
for _, commandTest := range fixtures.CommandTests {
t.Run(commandTest.Name, func(t *testing.T) {
tmpConfig, err := tmpFile(commandTest.Config)
if err != nil {
t.Error(err)
}
defer os.Remove(tmpConfig.Name())
validator, err := testbed.NewIntegrationValidator(commandTest.ExpectedStdout, "")
if err != nil {
t.Error(err)
}
tc := testbed.NewTestCase(t, testbed.NewChildFlexRunner("/bin/nri-flex", tmpConfig.Name()), validator)
tc.RunTest()
})
}
}
func TestUrlAPI(t *testing.T) {
for _, apiTest := range fixtures.URLTests {
t.Run(apiTest.Name, func(t *testing.T) {
tmpConfig, err := tmpFile(apiTest.Config)
if err != nil {
t.Error(err)
}
defer os.Remove(tmpConfig.Name())
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "/"+apiTest.Endpoint) {
_, err := w.Write([]byte(apiTest.Payload))
assert.NoError(t, err)
} else {
_, err := w.Write([]byte{})
assert.NoError(t, err)
}
}))
l, _ := net.Listen("tcp", "127.0.0.1:"+apiTest.Port)
srv.Listener = l
srv.Start()
defer srv.Close()
validator, err := testbed.NewIntegrationValidator(apiTest.ExpectedStdout, "nothing")
if err != nil {
t.Error(err)
}
tc := testbed.NewTestCase(t, testbed.NewChildFlexRunner("/bin/nri-flex", tmpConfig.Name()), validator)
tc.RunTest()
})
}
}
func TestFileAPI(t *testing.T) {
for _, urlTest := range fixtures.FileTests {
t.Run(urlTest.Name, func(t *testing.T) {
tmpIntegrationFile, err := tmpFile(urlTest.FileContent)
if err != nil {
t.Error(err)
}
defer os.Remove(tmpIntegrationFile.Name())
// replace FILE_NAME string of the configuration for the temporal file path
dynamicConfig := strings.Replace(urlTest.Config, "FILE_PATH", tmpIntegrationFile.Name(), 1)
tmpConfig, err := tmpFile(dynamicConfig)
if err != nil {
t.Error(err)
}
defer os.Remove(tmpConfig.Name())
validator, err := testbed.NewIntegrationValidator(urlTest.ExpectedStdout, "")
if err != nil {
t.Error(err)
}
tc := testbed.NewTestCase(t, testbed.NewChildFlexRunner("/bin/nri-flex", tmpConfig.Name()), validator)
tc.RunTest()
})
}
}