-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcsv_test.go
132 lines (105 loc) · 2.98 KB
/
csv_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
package main
import (
"encoding/csv"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewCSVPrinter(t *testing.T) {
dataFilename := "test_data.csv"
showTimestamp := true
showSourceAddress := true
cp, err := newCSVPrinter(dataFilename, &showTimestamp, &showSourceAddress)
assert.NoError(t, err)
assert.NotNil(t, cp)
assert.Equal(t, dataFilename, cp.probeFilename)
assert.Equal(t, dataFilename[:len(dataFilename)-4]+"_stats.csv", cp.statsFilename)
cp.cleanup()
os.Remove(dataFilename)
os.Remove(cp.statsFilename)
}
func TestWriteRecord(t *testing.T) {
dataFilename := "test_data.csv"
showTimestamp := false
showSourceAddress := true
cp, err := newCSVPrinter(dataFilename, &showTimestamp, &showSourceAddress)
assert.NoError(t, err)
assert.NotNil(t, cp)
record := []string{"Success", "hostname", "127.0.0.1", "80", "1", "10.123", "sourceAddr"}
err = cp.writeRecord(record)
assert.NoError(t, err)
// Verify the record is written
file, err := os.Open(dataFilename)
assert.NoError(t, err)
defer file.Close()
reader := csv.NewReader(file)
headers, err := reader.Read()
assert.NoError(t, err)
assert.Equal(t, []string{"Status", "Hostname", "IP", "Port", "TCP_Conn", "Latency(ms)", "Source Address"}, headers)
readRecord, err := reader.Read()
assert.NoError(t, err)
assert.Equal(t, record, readRecord)
// Cleanup
cp.cleanup()
os.Remove(dataFilename)
os.Remove(cp.statsFilename)
}
func TestWriteStatistics(t *testing.T) {
dataFilename := "test_data.csv"
showTimestamp := true
showSourceAddress := false
cp, err := newCSVPrinter(dataFilename, &showTimestamp, &showSourceAddress)
assert.NoError(t, err)
assert.NotNil(t, cp)
tcping := tcping{
totalSuccessfulProbes: 1,
totalUnsuccessfulProbes: 0,
lastSuccessfulProbe: time.Now(),
startTime: time.Now(),
}
cp.printStatistics(tcping)
statsFile, err := os.Open(cp.statsFilename)
assert.NoError(t, err)
defer statsFile.Close()
reader := csv.NewReader(statsFile)
headers, err := reader.Read()
assert.NoError(t, err)
assert.Equal(t, []string{"Metric", "Value"}, headers)
for {
record, err := reader.Read()
if err != nil {
break
}
assert.NotEmpty(t, record)
}
cp.cleanup()
os.Remove(dataFilename)
os.Remove(cp.statsFilename)
}
func TestCleanup(t *testing.T) {
dataFilename := "test_data.csv"
showTimestamp := true
showSourceAddress := false
cp, err := newCSVPrinter(dataFilename, &showTimestamp, &showSourceAddress)
assert.NoError(t, err)
assert.NotNil(t, cp)
// Call printStatistics to ensure the stats file is created
tcping := tcping{
totalSuccessfulProbes: 1,
totalUnsuccessfulProbes: 0,
lastSuccessfulProbe: time.Now(),
startTime: time.Now(),
}
cp.printStatistics(tcping)
// Perform cleanup
cp.cleanup()
// Verify files are closed and flushed
_, err = os.Stat(dataFilename)
assert.NoError(t, err)
_, err = os.Stat(cp.statsFilename)
assert.NoError(t, err)
// Cleanup files
os.Remove(dataFilename)
os.Remove(cp.statsFilename)
}