|
| 1 | +package mariadb |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/devops-works/slowql/query" |
| 8 | + "github.com/devops-works/slowql/server" |
| 9 | +) |
| 10 | + |
| 11 | +// parseTime is a helper function that allow us to cast a string into a time.Time |
| 12 | +// value in the tests |
| 13 | +func parseTime(t string) time.Time { |
| 14 | + time, _ := time.Parse("060102 15:04:05", t) |
| 15 | + return time |
| 16 | +} |
| 17 | +func TestDatabase_parseMariaDBHeader(t *testing.T) { |
| 18 | + type args struct { |
| 19 | + line string |
| 20 | + } |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + args args |
| 24 | + refQuery query.Query |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "time", |
| 28 | + args: args{ |
| 29 | + line: "# Time: 210323 11:31:57", |
| 30 | + }, |
| 31 | + refQuery: query.Query{ |
| 32 | + Time: parseTime("210323 11:31:57"), |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "user, host", |
| 37 | + args: args{ |
| 38 | + line: "# User@Host: hugo[hugo] @ [172.18.0.3]", |
| 39 | + }, |
| 40 | + refQuery: query.Query{ |
| 41 | + User: "hugo", |
| 42 | + Host: "172.18.0.3", |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "id, schema", // QC_Hit is not parsed yet |
| 47 | + args: args{ |
| 48 | + line: "# Thread_id: 12794 Schema: QC_hit: No", |
| 49 | + }, |
| 50 | + refQuery: query.Query{ |
| 51 | + ID: 12794, |
| 52 | + Schema: "", |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "query time, lock time, rows sent, rows examined", |
| 57 | + args: args{ |
| 58 | + line: "# Query_time: 0.000035 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0", |
| 59 | + }, |
| 60 | + refQuery: query.Query{ |
| 61 | + QueryTime: 0.000035, |
| 62 | + LockTime: 0.000000, |
| 63 | + RowsSent: 0, |
| 64 | + RowsExamined: 0, |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "rows affected, bytes sent", |
| 69 | + args: args{ |
| 70 | + line: "# Rows_affected: 0 Bytes_sent: 11", |
| 71 | + }, |
| 72 | + refQuery: query.Query{ |
| 73 | + RowsAffected: 0, |
| 74 | + BytesSent: 11, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | + for _, tt := range tests { |
| 79 | + db := New(nil) |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + q := query.Query{} |
| 82 | + db.parseMariaDBHeader(tt.args.line, &q) |
| 83 | + if q != tt.refQuery { |
| 84 | + t.Errorf("got = %v, want %v", q, tt.refQuery) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestDatabase_ParseServerMeta(t *testing.T) { |
| 91 | + tests := []struct { |
| 92 | + name string |
| 93 | + lines []string |
| 94 | + refSrv server.Server |
| 95 | + }{ |
| 96 | + { |
| 97 | + name: "parsable", |
| 98 | + lines: []string{ |
| 99 | + "/opt/bitnami/mariadb/sbin/mysqld, Version: 10.5.9-MariaDB (Source distribution). started with:", |
| 100 | + "Tcp port: 3306 Unix socket: /opt/bitnami/mariadb/tmp/mysql.sock", |
| 101 | + "Time Id Command Argument", |
| 102 | + }, |
| 103 | + refSrv: server.Server{ |
| 104 | + Binary: "/opt/bitnami/mariadb/sbin/mysqld", |
| 105 | + Port: 3306, |
| 106 | + Socket: "/opt/bitnami/mariadb/tmp/mysql.sock", |
| 107 | + Version: "10.5.9-MariaDB", |
| 108 | + VersionShort: "10.5.9", |
| 109 | + VersionDescription: "Source distribution", |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "unparsable", |
| 114 | + lines: []string{"Version: 8.0.23 (MySQL Community Server - GPL). started with:", |
| 115 | + "Tcp port: 3306", |
| 116 | + "Time Id Command Argument"}, |
| 117 | + refSrv: server.Server{ |
| 118 | + Binary: "unable to parse line", |
| 119 | + Port: 0, |
| 120 | + Socket: "unable to parse line", |
| 121 | + Version: "unable to parse line", |
| 122 | + VersionShort: "unable to parse line", |
| 123 | + VersionDescription: "unable to parse line", |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + for _, tt := range tests { |
| 128 | + lines := make(chan []string, 2) |
| 129 | + t.Run(tt.name, func(t *testing.T) { |
| 130 | + db := New(nil) |
| 131 | + lines <- tt.lines |
| 132 | + db.ParseServerMeta(lines) |
| 133 | + if db.srv != tt.refSrv { |
| 134 | + t.Errorf("got = %v, want = %v", db.srv, tt.refSrv) |
| 135 | + } |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestDatabase_ParseBlocs(t *testing.T) { |
| 141 | + tests := []struct { |
| 142 | + name string |
| 143 | + bloc []string |
| 144 | + refQuery query.Query |
| 145 | + }{ |
| 146 | + { |
| 147 | + name: "testing", |
| 148 | + bloc: []string{ |
| 149 | + "# Time: 210323 11:31:57", |
| 150 | + "# User@Host: hugo[hugo] @ [172.18.0.3]", |
| 151 | + "# Thread_id: 12794 Schema: QC_hit: No", |
| 152 | + "# Query_time: 0.000035 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0", |
| 153 | + "# Rows_affected: 0 Bytes_sent: 11", |
| 154 | + "SET timestamp=1616499117;", |
| 155 | + "SET NAMES utf8mb4;", |
| 156 | + }, |
| 157 | + refQuery: query.Query{ |
| 158 | + Time: parseTime("210323 11:31:57"), |
| 159 | + User: "hugo", |
| 160 | + Host: "172.18.0.3", |
| 161 | + ID: 12794, |
| 162 | + Schema: "", |
| 163 | + QueryTime: 0.000035, |
| 164 | + LockTime: 0.000000, |
| 165 | + RowsSent: 0, |
| 166 | + RowsExamined: 0, |
| 167 | + RowsAffected: 0, |
| 168 | + BytesSent: 11, |
| 169 | + Query: "SET timestamp=1616499117;SET NAMES utf8mb4;", |
| 170 | + }, |
| 171 | + }, |
| 172 | + } |
| 173 | + for _, tt := range tests { |
| 174 | + rawBlocs := make(chan []string, 10) |
| 175 | + qc := make(chan query.Query) |
| 176 | + db := New(qc) |
| 177 | + t.Run(tt.name, func(t *testing.T) { |
| 178 | + rawBlocs <- tt.bloc |
| 179 | + go db.ParseBlocs(rawBlocs) |
| 180 | + q := <-db.WaitingList |
| 181 | + if q != tt.refQuery { |
| 182 | + t.Errorf("got = %v, want = %v", q, tt.refQuery) |
| 183 | + } |
| 184 | + }) |
| 185 | + } |
| 186 | +} |
0 commit comments