summaryrefslogtreecommitdiff
path: root/flow-cleaner_test.go
blob: 0c01c1cd0e64cdbb980753aa7446e1df7d14f822 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main

import (
	"bufio"
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"io"
	"os"
	"testing"
	"time"
)

func TestCleaningFromJSON(t *testing.T) {
	cfg := &Config{
		Limit:      0,
		Interval:   "5min",
		Epsilon:    0,
		DataSource: "json",

		DBConn:     "",
		DBName:     "test",
		CleanTable: "test_clean",
		DBUser:     "flowcleaner",
		DBPass:     "nil",
	}

	testProcessFromStdin(t, cfg)
	time.Sleep(15 * time.Second)
	controlDB(t, cfg)
}

func testProcessFromStdin(t *testing.T, cfg *Config) {
	stdin := make(chan []byte)

	go func() {
		for i := 0; i < 3; i++ {
			fakeStdin(t, stdin)
			if i < 2 {
				time.Sleep(3 * time.Minute)
			}
		}
		close(stdin)
	}()

	rDatChan := parseRawData(stdin, cfg)
	err := cleanFromStdin(rDatChan, cfg)
	if err != nil {
		t.Fatal(err)
	}
}
func fakeStdin(t *testing.T, wr chan<- []byte) {
	file, err := os.Open("testdata/jsoninput")
	if err != nil {
		t.Fatal(err)
	}
	defer file.Close()

	reader := bufio.NewReader(file)

	for {
		line, err := reader.ReadBytes('\n')
		if err == io.EOF {
			break
		} else if err != nil {
			t.Fatal(err)
		} else {
			wr <- line
		}
	}
}

func controlDB(t *testing.T, cfg *Config) {
	db, err := sql.Open("mysql", cfg.DBUser+":"+cfg.DBPass+"@"+cfg.DBConn+"/"+cfg.DBName)
	if err != nil {
		t.Fatal("Failed to connect to db:", err)
	}
	defer db.Close()

	rows, err := db.Query("SELECT * FROM " + cfg.CleanTable)
	if err != nil {
		t.Fatal("Failed to select cleaned data:", err)
	}
	defer rows.Close()

	numRows := 0
	for rows.Next() {
		numRows++
	}
	if numRows != 14 {
		t.Fatal("Wrong number of rows found in db")
	}

	check1, err := db.Query("SELECT occurences, volume FROM " + cfg.CleanTable + " ORDER BY occurences DESC")
	if err != nil {
		t.Fatal("Failed to select occurences and volume:", err)
	}
	defer check1.Close()
	var oc int
	var vol string

	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 36 && vol == "0-199" {
		t.Fatal("Failed oc 36, vol 0-199. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 20 && vol == "200-299" {
		t.Fatal("Failed oc 20, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 18 && vol == "200-299" {
		t.Fatal("Failed oc 18, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 14 && vol == "200-299" {
		t.Fatal("Failed oc 14, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 12 && vol == "200-299" {
		t.Fatal("Failed oc 12, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 10 && vol == "200-299" {
		t.Fatal("Failed oc 10, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 8 && vol == "200-299" {
		t.Fatal("Failed oc 8, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 7 && vol == "200-299" {
		t.Fatal("Failed oc 7, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 6 && vol == "200-299" {
		t.Fatal("Failed oc 6, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 4 && vol == "200-299" {
		t.Fatal("Failed oc 4, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 2 && vol == "200-299" {
		t.Fatal("Failed oc 2, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 1 && vol == "200-299" {
		t.Fatal("Failed oc 1, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 0 && vol == "200-299" {
		t.Fatal("Failed oc 0, vol 200-299. Actually was:", oc, vol)
	}
	check1.Next()
	check1.Scan(&oc, &vol)
	if oc != 0 && vol == "200-299" {
		t.Fatal("Failed oc 0, vol 200-299. Actually was:", oc, vol)
	}
}