env_config_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package binarylog
  19. import (
  20. "fmt"
  21. "testing"
  22. )
  23. // This tests that when multiple configs are specified, all methods loggers will
  24. // be set correctly. Correctness of each logger is covered by other unit tests.
  25. func TestNewLoggerFromConfigString(t *testing.T) {
  26. const (
  27. s1 = "s1"
  28. m1 = "m1"
  29. m2 = "m2"
  30. fullM1 = s1 + "/" + m1
  31. fullM2 = s1 + "/" + m2
  32. )
  33. c := fmt.Sprintf("*{h:1;m:2},%s{h},%s{m},%s{h;m}", s1+"/*", fullM1, fullM2)
  34. l := NewLoggerFromConfigString(c).(*logger)
  35. if l.all.hdr != 1 || l.all.msg != 2 {
  36. t.Errorf("l.all = %#v, want headerLen: 1, messageLen: 2", l.all)
  37. }
  38. if ml, ok := l.services[s1]; ok {
  39. if ml.hdr != maxUInt || ml.msg != 0 {
  40. t.Errorf("want maxUInt header, 0 message, got header: %v, message: %v", ml.hdr, ml.msg)
  41. }
  42. } else {
  43. t.Errorf("service/* is not set")
  44. }
  45. if ml, ok := l.methods[fullM1]; ok {
  46. if ml.hdr != 0 || ml.msg != maxUInt {
  47. t.Errorf("want 0 header, maxUInt message, got header: %v, message: %v", ml.hdr, ml.msg)
  48. }
  49. } else {
  50. t.Errorf("service/method{h} is not set")
  51. }
  52. if ml, ok := l.methods[fullM2]; ok {
  53. if ml.hdr != maxUInt || ml.msg != maxUInt {
  54. t.Errorf("want maxUInt header, maxUInt message, got header: %v, message: %v", ml.hdr, ml.msg)
  55. }
  56. } else {
  57. t.Errorf("service/method{h;m} is not set")
  58. }
  59. }
  60. func TestNewLoggerFromConfigStringInvalid(t *testing.T) {
  61. testCases := []string{
  62. "",
  63. "*{}",
  64. "s/m,*{}",
  65. "s/m,s/m{a}",
  66. // Duplciate rules.
  67. "s/m,-s/m",
  68. "-s/m,s/m",
  69. "s/m,s/m",
  70. "s/m,s/m{h:1;m:1}",
  71. "s/m{h:1;m:1},s/m",
  72. "-s/m,-s/m",
  73. "s/*,s/*{h:1;m:1}",
  74. "*,*{h:1;m:1}",
  75. }
  76. for _, tc := range testCases {
  77. l := NewLoggerFromConfigString(tc)
  78. if l != nil {
  79. t.Errorf("With config %q, want logger %v, got %v", tc, nil, l)
  80. }
  81. }
  82. }
  83. func TestParseMethodConfigAndSuffix(t *testing.T) {
  84. testCases := []struct {
  85. in, service, method, suffix string
  86. }{
  87. {
  88. in: "p.s/m",
  89. service: "p.s", method: "m", suffix: "",
  90. },
  91. {
  92. in: "p.s/m{h,m}",
  93. service: "p.s", method: "m", suffix: "{h,m}",
  94. },
  95. {
  96. in: "p.s/*",
  97. service: "p.s", method: "*", suffix: "",
  98. },
  99. {
  100. in: "p.s/*{h,m}",
  101. service: "p.s", method: "*", suffix: "{h,m}",
  102. },
  103. // invalid suffix will be detected by another function.
  104. {
  105. in: "p.s/m{invalidsuffix}",
  106. service: "p.s", method: "m", suffix: "{invalidsuffix}",
  107. },
  108. {
  109. in: "p.s/*{invalidsuffix}",
  110. service: "p.s", method: "*", suffix: "{invalidsuffix}",
  111. },
  112. {
  113. in: "s/m*",
  114. service: "s", method: "m", suffix: "*",
  115. },
  116. {
  117. in: "s/*m",
  118. service: "s", method: "*", suffix: "m",
  119. },
  120. {
  121. in: "s/**",
  122. service: "s", method: "*", suffix: "*",
  123. },
  124. }
  125. for _, tc := range testCases {
  126. t.Logf("testing parseMethodConfigAndSuffix(%q)", tc.in)
  127. s, m, suffix, err := parseMethodConfigAndSuffix(tc.in)
  128. if err != nil {
  129. t.Errorf("returned error %v, want nil", err)
  130. continue
  131. }
  132. if s != tc.service {
  133. t.Errorf("service = %q, want %q", s, tc.service)
  134. }
  135. if m != tc.method {
  136. t.Errorf("method = %q, want %q", m, tc.method)
  137. }
  138. if suffix != tc.suffix {
  139. t.Errorf("suffix = %q, want %q", suffix, tc.suffix)
  140. }
  141. }
  142. }
  143. func TestParseMethodConfigAndSuffixInvalid(t *testing.T) {
  144. testCases := []string{
  145. "*/m",
  146. "*/m{}",
  147. }
  148. for _, tc := range testCases {
  149. s, m, suffix, err := parseMethodConfigAndSuffix(tc)
  150. if err == nil {
  151. t.Errorf("Parsing %q got nil error with %q, %q, %q, want non-nil error", tc, s, m, suffix)
  152. }
  153. }
  154. }
  155. func TestParseHeaderMessageLengthConfig(t *testing.T) {
  156. testCases := []struct {
  157. in string
  158. hdr, msg uint64
  159. }{
  160. {
  161. in: "",
  162. hdr: maxUInt, msg: maxUInt,
  163. },
  164. {
  165. in: "{h}",
  166. hdr: maxUInt, msg: 0,
  167. },
  168. {
  169. in: "{h:314}",
  170. hdr: 314, msg: 0,
  171. },
  172. {
  173. in: "{m}",
  174. hdr: 0, msg: maxUInt,
  175. },
  176. {
  177. in: "{m:213}",
  178. hdr: 0, msg: 213,
  179. },
  180. {
  181. in: "{h;m}",
  182. hdr: maxUInt, msg: maxUInt,
  183. },
  184. {
  185. in: "{h:314;m}",
  186. hdr: 314, msg: maxUInt,
  187. },
  188. {
  189. in: "{h;m:213}",
  190. hdr: maxUInt, msg: 213,
  191. },
  192. {
  193. in: "{h:314;m:213}",
  194. hdr: 314, msg: 213,
  195. },
  196. }
  197. for _, tc := range testCases {
  198. t.Logf("testing parseHeaderMessageLengthConfig(%q)", tc.in)
  199. hdr, msg, err := parseHeaderMessageLengthConfig(tc.in)
  200. if err != nil {
  201. t.Errorf("returned error %v, want nil", err)
  202. continue
  203. }
  204. if hdr != tc.hdr {
  205. t.Errorf("header length = %v, want %v", hdr, tc.hdr)
  206. }
  207. if msg != tc.msg {
  208. t.Errorf("message length = %v, want %v", msg, tc.msg)
  209. }
  210. }
  211. }
  212. func TestParseHeaderMessageLengthConfigInvalid(t *testing.T) {
  213. testCases := []string{
  214. "{}",
  215. "{h;a}",
  216. "{h;m;b}",
  217. }
  218. for _, tc := range testCases {
  219. _, _, err := parseHeaderMessageLengthConfig(tc)
  220. if err == nil {
  221. t.Errorf("Parsing %q got nil error, want non-nil error", tc)
  222. }
  223. }
  224. }
  225. func TestFillMethodLoggerWithConfigStringBlacklist(t *testing.T) {
  226. testCases := []string{
  227. "p.s/m",
  228. "service/method",
  229. }
  230. for _, tc := range testCases {
  231. c := "-" + tc
  232. t.Logf("testing fillMethodLoggerWithConfigString(%q)", c)
  233. l := newEmptyLogger()
  234. if err := l.fillMethodLoggerWithConfigString(c); err != nil {
  235. t.Errorf("returned err %v, want nil", err)
  236. continue
  237. }
  238. _, ok := l.blacklist[tc]
  239. if !ok {
  240. t.Errorf("blacklist[%q] is not set", tc)
  241. }
  242. }
  243. }
  244. func TestFillMethodLoggerWithConfigStringGlobal(t *testing.T) {
  245. testCases := []struct {
  246. in string
  247. hdr, msg uint64
  248. }{
  249. {
  250. in: "",
  251. hdr: maxUInt, msg: maxUInt,
  252. },
  253. {
  254. in: "{h}",
  255. hdr: maxUInt, msg: 0,
  256. },
  257. {
  258. in: "{h:314}",
  259. hdr: 314, msg: 0,
  260. },
  261. {
  262. in: "{m}",
  263. hdr: 0, msg: maxUInt,
  264. },
  265. {
  266. in: "{m:213}",
  267. hdr: 0, msg: 213,
  268. },
  269. {
  270. in: "{h;m}",
  271. hdr: maxUInt, msg: maxUInt,
  272. },
  273. {
  274. in: "{h:314;m}",
  275. hdr: 314, msg: maxUInt,
  276. },
  277. {
  278. in: "{h;m:213}",
  279. hdr: maxUInt, msg: 213,
  280. },
  281. {
  282. in: "{h:314;m:213}",
  283. hdr: 314, msg: 213,
  284. },
  285. }
  286. for _, tc := range testCases {
  287. c := "*" + tc.in
  288. t.Logf("testing fillMethodLoggerWithConfigString(%q)", c)
  289. l := newEmptyLogger()
  290. if err := l.fillMethodLoggerWithConfigString(c); err != nil {
  291. t.Errorf("returned err %v, want nil", err)
  292. continue
  293. }
  294. if l.all == nil {
  295. t.Errorf("l.all is not set")
  296. continue
  297. }
  298. if hdr := l.all.hdr; hdr != tc.hdr {
  299. t.Errorf("header length = %v, want %v", hdr, tc.hdr)
  300. }
  301. if msg := l.all.msg; msg != tc.msg {
  302. t.Errorf("message length = %v, want %v", msg, tc.msg)
  303. }
  304. }
  305. }
  306. func TestFillMethodLoggerWithConfigStringPerService(t *testing.T) {
  307. testCases := []struct {
  308. in string
  309. hdr, msg uint64
  310. }{
  311. {
  312. in: "",
  313. hdr: maxUInt, msg: maxUInt,
  314. },
  315. {
  316. in: "{h}",
  317. hdr: maxUInt, msg: 0,
  318. },
  319. {
  320. in: "{h:314}",
  321. hdr: 314, msg: 0,
  322. },
  323. {
  324. in: "{m}",
  325. hdr: 0, msg: maxUInt,
  326. },
  327. {
  328. in: "{m:213}",
  329. hdr: 0, msg: 213,
  330. },
  331. {
  332. in: "{h;m}",
  333. hdr: maxUInt, msg: maxUInt,
  334. },
  335. {
  336. in: "{h:314;m}",
  337. hdr: 314, msg: maxUInt,
  338. },
  339. {
  340. in: "{h;m:213}",
  341. hdr: maxUInt, msg: 213,
  342. },
  343. {
  344. in: "{h:314;m:213}",
  345. hdr: 314, msg: 213,
  346. },
  347. }
  348. const serviceName = "service"
  349. for _, tc := range testCases {
  350. c := serviceName + "/*" + tc.in
  351. t.Logf("testing fillMethodLoggerWithConfigString(%q)", c)
  352. l := newEmptyLogger()
  353. if err := l.fillMethodLoggerWithConfigString(c); err != nil {
  354. t.Errorf("returned err %v, want nil", err)
  355. continue
  356. }
  357. ml, ok := l.services[serviceName]
  358. if !ok {
  359. t.Errorf("l.service[%q] is not set", serviceName)
  360. continue
  361. }
  362. if hdr := ml.hdr; hdr != tc.hdr {
  363. t.Errorf("header length = %v, want %v", hdr, tc.hdr)
  364. }
  365. if msg := ml.msg; msg != tc.msg {
  366. t.Errorf("message length = %v, want %v", msg, tc.msg)
  367. }
  368. }
  369. }
  370. func TestFillMethodLoggerWithConfigStringPerMethod(t *testing.T) {
  371. testCases := []struct {
  372. in string
  373. hdr, msg uint64
  374. }{
  375. {
  376. in: "",
  377. hdr: maxUInt, msg: maxUInt,
  378. },
  379. {
  380. in: "{h}",
  381. hdr: maxUInt, msg: 0,
  382. },
  383. {
  384. in: "{h:314}",
  385. hdr: 314, msg: 0,
  386. },
  387. {
  388. in: "{m}",
  389. hdr: 0, msg: maxUInt,
  390. },
  391. {
  392. in: "{m:213}",
  393. hdr: 0, msg: 213,
  394. },
  395. {
  396. in: "{h;m}",
  397. hdr: maxUInt, msg: maxUInt,
  398. },
  399. {
  400. in: "{h:314;m}",
  401. hdr: 314, msg: maxUInt,
  402. },
  403. {
  404. in: "{h;m:213}",
  405. hdr: maxUInt, msg: 213,
  406. },
  407. {
  408. in: "{h:314;m:213}",
  409. hdr: 314, msg: 213,
  410. },
  411. }
  412. const (
  413. serviceName = "service"
  414. methodName = "method"
  415. fullMethodName = serviceName + "/" + methodName
  416. )
  417. for _, tc := range testCases {
  418. c := fullMethodName + tc.in
  419. t.Logf("testing fillMethodLoggerWithConfigString(%q)", c)
  420. l := newEmptyLogger()
  421. if err := l.fillMethodLoggerWithConfigString(c); err != nil {
  422. t.Errorf("returned err %v, want nil", err)
  423. continue
  424. }
  425. ml, ok := l.methods[fullMethodName]
  426. if !ok {
  427. t.Errorf("l.methods[%q] is not set", fullMethodName)
  428. continue
  429. }
  430. if hdr := ml.hdr; hdr != tc.hdr {
  431. t.Errorf("header length = %v, want %v", hdr, tc.hdr)
  432. }
  433. if msg := ml.msg; msg != tc.msg {
  434. t.Errorf("message length = %v, want %v", msg, tc.msg)
  435. }
  436. }
  437. }
  438. func TestFillMethodLoggerWithConfigStringInvalid(t *testing.T) {
  439. testCases := []string{
  440. "",
  441. "{}",
  442. "p.s/m{}",
  443. "p.s/m{a}",
  444. "p.s/m*",
  445. "p.s/**",
  446. "*/m",
  447. "-p.s/*",
  448. "-p.s/m{h}",
  449. }
  450. l := &logger{}
  451. for _, tc := range testCases {
  452. if err := l.fillMethodLoggerWithConfigString(tc); err == nil {
  453. t.Errorf("fillMethodLoggerWithConfigString(%q) returned nil error, want non-nil", tc)
  454. }
  455. }
  456. }