-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector_test.go
125 lines (101 loc) · 2.56 KB
/
collector_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
package sumologic
import (
"flag"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
var (
integration = flag.Bool("integration", false, "run integration tests")
liveSumoClient *Sumologic
)
func TestMain(m *testing.M) {
flag.Parse()
var err error
if *integration {
fmt.Println("Running integration tests")
liveSumoClient, err = NewDefaultSumologic()
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
result := m.Run()
os.Exit(result)
}
func TestCollectors(t *testing.T) {
var sumoClient *Sumologic
if *integration {
sumoClient = liveSumoClient
} else {
_, sumoClient = Stub("stubs/collectors.json")
}
collectors, err := sumoClient.Collectors(0, 10)
if err != nil {
t.Fatalf("Failed to retrieve collectors: %s", err)
}
assert.Equal(t, 10, len(collectors))
for _, v := range collectors {
assert.NotNil(t, v.ID)
}
}
func getCollector(t *testing.T, id int) {
var sumoClient *Sumologic
if *integration {
sumoClient = liveSumoClient
} else {
_, sumoClient = Stub("stubs/collector.json")
}
collector, err := sumoClient.Collector(id)
if err != nil {
t.Fatalf("Failed to retrieve collector: %s", err)
}
if *integration {
assert.NotNil(t, collector.Name)
assert.NotNil(t, collector.Description)
} else {
assert.Equal(t, "Test Collector", collector.Name)
assert.Equal(t, "A Test Collector description", collector.Description)
}
assert.Equal(t, id, collector.ID)
assert.Equal(t, true, collector.Alive)
assert.Equal(t, "Hosted", collector.CollectorType)
}
func TestCreateGetDeleteCollector(t *testing.T) {
createdCollector := createCollector(t)
getCollector(t, createdCollector.ID)
deleteCollector(t, createdCollector.ID)
}
func createCollector(t *testing.T) *Collector {
var sumoClient *Sumologic
if *integration {
sumoClient = liveSumoClient
} else {
_, sumoClient = Stub("stubs/collector.json")
}
newCollector := Collector{
CollectorType: "Hosted",
Name: "Test Collector",
Description: "A Test Collector description",
Category: "HTTP Collection",
}
createdCollector, err := sumoClient.CreateCollector(newCollector)
if err != nil || createdCollector == nil {
t.Fatalf("Failed to create collector: %s", err)
}
assert.Equal(t, newCollector.Name, createdCollector.Name)
return createdCollector
}
func deleteCollector(t *testing.T, id int) {
var sumoClient *Sumologic
if *integration {
sumoClient = liveSumoClient
} else {
_, sumoClient = Stub("stubs/nil-response.json")
}
err := sumoClient.DeleteCollector(id)
if err != nil {
t.Fatalf("Failed to delete collector: %s", err)
}
}