| 1 |
package statement |
| 2 |
|
| 3 |
import ( |
| 4 |
|
| 5 |
"reflect" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/influxdata/influxdb/stress/v2/statement" |
| 11 |
) |
| 12 |
|
| 13 |
func newParserFromString(s string) *Parser { |
| 14 |
f := strings.NewReader(s) |
| 15 |
p := NewParser(f) |
| 16 |
|
| 17 |
return p |
| 18 |
} |
| 19 |
|
| 20 |
func TestParser_ParseStatement(t *testing.T) { |
| 21 |
var tests = []struct { |
| 22 |
skip bool |
| 23 |
s string |
| 24 |
stmt statement.Statement |
| 25 |
err string |
| 26 |
}{ |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
{ |
| 31 |
s: "QUERY basicCount\nSELECT count(%f) FROM cpu\nDO 100", |
| 32 |
stmt: &statement.QueryStatement{Name: "basicCount", TemplateString: "SELECT count(%v) FROM cpu", Args: []string{"%f"}, Count: 100}, |
| 33 |
}, |
| 34 |
|
| 35 |
{ |
| 36 |
s: "QUERY basicCount\nSELECT count(%f) FROM %m\nDO 100", |
| 37 |
stmt: &statement.QueryStatement{Name: "basicCount", TemplateString: "SELECT count(%v) FROM %v", Args: []string{"%f", "%m"}, Count: 100}, |
| 38 |
}, |
| 39 |
|
| 40 |
{ |
| 41 |
skip: true, |
| 42 |
s: "QUERY\nSELECT count(%f) FROM %m\nDO 100", |
| 43 |
err: "Missing Name", |
| 44 |
}, |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
{ |
| 49 |
s: "INSERT mockCpu\ncpu,\nhost=[us-west|us-east|eu-north],server_id=[str rand(7) 1000]\nbusy=[int rand(1000) 100],free=[float rand(10) 0]\n100000 10s", |
| 50 |
stmt: &statement.InsertStatement{ |
| 51 |
Name: "mockCpu", |
| 52 |
TemplateString: "cpu,host=%v,server_id=%v busy=%v,free=%v %v", |
| 53 |
TagCount: 2, |
| 54 |
Templates: []*statement.Template{ |
| 55 |
&statement.Template{ |
| 56 |
Tags: []string{"us-west", "us-east", "eu-north"}, |
| 57 |
}, |
| 58 |
&statement.Template{ |
| 59 |
Function: &statement.Function{Type: "str", Fn: "rand", Argument: 7, Count: 1000}, |
| 60 |
}, |
| 61 |
&statement.Template{ |
| 62 |
Function: &statement.Function{Type: "int", Fn: "rand", Argument: 1000, Count: 100}, |
| 63 |
}, |
| 64 |
&statement.Template{ |
| 65 |
Function: &statement.Function{Type: "float", Fn: "rand", Argument: 10, Count: 0}, |
| 66 |
}, |
| 67 |
}, |
| 68 |
Timestamp: &statement.Timestamp{ |
| 69 |
Count: 100000, |
| 70 |
Duration: time.Duration(10 * time.Second), |
| 71 |
}, |
| 72 |
}, |
| 73 |
}, |
| 74 |
|
| 75 |
{ |
| 76 |
s: "INSERT mockCpu\ncpu,host=[us-west|us-east|eu-north],server_id=[str rand(7) 1000]\nbusy=[int rand(1000) 100],free=[float rand(10) 0]\n100000 10s", |
| 77 |
stmt: &statement.InsertStatement{ |
| 78 |
Name: "mockCpu", |
| 79 |
TemplateString: "cpu,host=%v,server_id=%v busy=%v,free=%v %v", |
| 80 |
TagCount: 2, |
| 81 |
Templates: []*statement.Template{ |
| 82 |
&statement.Template{ |
| 83 |
Tags: []string{"us-west", "us-east", "eu-north"}, |
| 84 |
}, |
| 85 |
&statement.Template{ |
| 86 |
Function: &statement.Function{Type: "str", Fn: "rand", Argument: 7, Count: 1000}, |
| 87 |
}, |
| 88 |
&statement.Template{ |
| 89 |
Function: &statement.Function{Type: "int", Fn: "rand", Argument: 1000, Count: 100}, |
| 90 |
}, |
| 91 |
&statement.Template{ |
| 92 |
Function: &statement.Function{Type: "float", Fn: "rand", Argument: 10, Count: 0}, |
| 93 |
}, |
| 94 |
}, |
| 95 |
Timestamp: &statement.Timestamp{ |
| 96 |
Count: 100000, |
| 97 |
Duration: time.Duration(10 * time.Second), |
| 98 |
}, |
| 99 |
}, |
| 100 |
}, |
| 101 |
|
| 102 |
{ |
| 103 |
s: "INSERT mockCpu\n[str rand(1000) 10],\nhost=[us-west|us-east|eu-north],server_id=[str rand(7) 1000],other=x\nbusy=[int rand(1000) 100],free=[float rand(10) 0]\n100000 10s", |
| 104 |
stmt: &statement.InsertStatement{ |
| 105 |
Name: "mockCpu", |
| 106 |
TemplateString: "%v,host=%v,server_id=%v,other=x busy=%v,free=%v %v", |
| 107 |
TagCount: 3, |
| 108 |
Templates: []*statement.Template{ |
| 109 |
&statement.Template{ |
| 110 |
Function: &statement.Function{Type: "str", Fn: "rand", Argument: 1000, Count: 10}, |
| 111 |
}, |
| 112 |
&statement.Template{ |
| 113 |
Tags: []string{"us-west", "us-east", "eu-north"}, |
| 114 |
}, |
| 115 |
&statement.Template{ |
| 116 |
Function: &statement.Function{Type: "str", Fn: "rand", Argument: 7, Count: 1000}, |
| 117 |
}, |
| 118 |
&statement.Template{ |
| 119 |
Function: &statement.Function{Type: "int", Fn: "rand", Argument: 1000, Count: 100}, |
| 120 |
}, |
| 121 |
&statement.Template{ |
| 122 |
Function: &statement.Function{Type: "float", Fn: "rand", Argument: 10, Count: 0}, |
| 123 |
}, |
| 124 |
}, |
| 125 |
Timestamp: &statement.Timestamp{ |
| 126 |
Count: 100000, |
| 127 |
Duration: time.Duration(10 * time.Second), |
| 128 |
}, |
| 129 |
}, |
| 130 |
}, |
| 131 |
|
| 132 |
{ |
| 133 |
skip: true, |
| 134 |
s: "INSERT\ncpu,\nhost=[us-west|us-east|eu-north],server_id=[str rand(7) 1000]\nbusy=[int rand(1000) 100],free=[float rand(10) 0]\n100000 10s", |
| 135 |
err: `found ",", expected WS`, |
| 136 |
}, |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
{ |
| 141 |
s: `EXEC other_script`, |
| 142 |
stmt: &statement.ExecStatement{Script: "other_script"}, |
| 143 |
}, |
| 144 |
|
| 145 |
{ |
| 146 |
skip: true, |
| 147 |
s: `EXEC other_script.sh`, |
| 148 |
stmt: &statement.ExecStatement{Script: "other_script.sh"}, |
| 149 |
}, |
| 150 |
|
| 151 |
{ |
| 152 |
skip: true, |
| 153 |
s: `EXEC ../other_script.sh`, |
| 154 |
stmt: &statement.ExecStatement{Script: "../other_script.sh"}, |
| 155 |
}, |
| 156 |
|
| 157 |
{ |
| 158 |
skip: true, |
| 159 |
s: `EXEC /path/to/some/other_script.sh`, |
| 160 |
stmt: &statement.ExecStatement{Script: "/path/to/some/other_script.sh"}, |
| 161 |
}, |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
{ |
| 166 |
skip: true, |
| 167 |
s: "GO INSERT mockCpu\ncpu,\nhost=[us-west|us-east|eu-north],server_id=[str rand(7) 1000]\nbusy=[int rand(1000) 100],free=[float rand(10) 0]\n100000 10s", |
| 168 |
stmt: &statement.GoStatement{ |
| 169 |
Statement: &statement.InsertStatement{ |
| 170 |
Name: "mockCpu", |
| 171 |
TemplateString: "cpu,host=%v,server_id=%v busy=%v,free=%v %v", |
| 172 |
Templates: []*statement.Template{ |
| 173 |
&statement.Template{ |
| 174 |
Tags: []string{"us-west", "us-east", "eu-north"}, |
| 175 |
}, |
| 176 |
&statement.Template{ |
| 177 |
Function: &statement.Function{Type: "str", Fn: "rand", Argument: 7, Count: 1000}, |
| 178 |
}, |
| 179 |
&statement.Template{ |
| 180 |
Function: &statement.Function{Type: "int", Fn: "rand", Argument: 1000, Count: 100}, |
| 181 |
}, |
| 182 |
&statement.Template{ |
| 183 |
Function: &statement.Function{Type: "float", Fn: "rand", Argument: 10, Count: 0}, |
| 184 |
}, |
| 185 |
}, |
| 186 |
Timestamp: &statement.Timestamp{ |
| 187 |
Count: 100000, |
| 188 |
Duration: time.Duration(10 * time.Second), |
| 189 |
}, |
| 190 |
}, |
| 191 |
}, |
| 192 |
}, |
| 193 |
|
| 194 |
{ |
| 195 |
skip: true, |
| 196 |
s: "GO QUERY basicCount\nSELECT count(free) FROM cpu\nDO 100", |
| 197 |
stmt: &statement.GoStatement{ |
| 198 |
Statement: &statement.QueryStatement{Name: "basicCount", TemplateString: "SELECT count(free) FROM cpu", Count: 100}, |
| 199 |
}, |
| 200 |
}, |
| 201 |
|
| 202 |
{ |
| 203 |
skip: true, |
| 204 |
s: `GO EXEC other_script`, |
| 205 |
stmt: &statement.GoStatement{ |
| 206 |
Statement: &statement.ExecStatement{Script: "other_script"}, |
| 207 |
}, |
| 208 |
}, |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
{ |
| 213 |
s: `SET database [stress]`, |
| 214 |
stmt: &statement.SetStatement{Var: "database", Value: "stress"}, |
| 215 |
}, |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
{ |
| 220 |
s: `Wait`, |
| 221 |
stmt: &statement.WaitStatement{}, |
| 222 |
}, |
| 223 |
} |
| 224 |
|
| 225 |
for _, tst := range tests { |
| 226 |
|
| 227 |
if tst.skip { |
| 228 |
continue |
| 229 |
} |
| 230 |
|
| 231 |
stmt, err := newParserFromString(tst.s).Parse() |
| 232 |
tst.stmt.SetID("x") |
| 233 |
|
| 234 |
if err != nil && err.Error() != tst.err { |
| 235 |
t.Errorf("REAL ERROR: %v\nExpected ERROR: %v\n", err, tst.err) |
| 236 |
} else if err != nil && tst.err == err.Error() { |
| 237 |
t.Errorf("REAL ERROR: %v\nExpected ERROR: %v\n", err, tst.err) |
| 238 |
} else if stmt.SetID("x"); !reflect.DeepEqual(stmt, tst.stmt) { |
| 239 |
t.Errorf("Expected\n%#v\n%#v", tst.stmt, stmt) |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
} |