main_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "reflect"
  17. "testing"
  18. "k8s.io/component-base/metrics"
  19. )
  20. const fakeFilename = "testdata/metric.go"
  21. func TestSkipMetrics(t *testing.T) {
  22. for _, test := range []struct {
  23. testName string
  24. src string
  25. }{
  26. {
  27. testName: "Skip alpha metric with local variable",
  28. src: `
  29. package test
  30. import "k8s.io/component-base/metrics"
  31. var name = "metric"
  32. var _ = metrics.NewCounter(
  33. &metrics.CounterOpts{
  34. Name: name,
  35. StabilityLevel: metrics.ALPHA,
  36. },
  37. )
  38. `},
  39. {
  40. testName: "Skip alpha metric created via function call",
  41. src: `
  42. package test
  43. import "k8s.io/component-base/metrics"
  44. func getName() string {
  45. return "metric"
  46. }
  47. var _ = metrics.NewCounter(
  48. &metrics.CounterOpts{
  49. Name: getName(),
  50. StabilityLevel: metrics.ALPHA,
  51. },
  52. )
  53. `},
  54. {
  55. testName: "Skip metric without stability set",
  56. src: `
  57. package test
  58. import "k8s.io/component-base/metrics"
  59. var _ = metrics.NewCounter(
  60. &metrics.CounterOpts{
  61. Name: "metric",
  62. },
  63. )
  64. `},
  65. {
  66. testName: "Skip functions of similar signature (not imported from framework path) with import rename",
  67. src: `
  68. package test
  69. import metrics "k8s.io/fake/path"
  70. var _ = metrics.NewCounter(
  71. &metrics.CounterOpts{
  72. StabilityLevel: metrics.STABLE,
  73. },
  74. )
  75. `},
  76. {
  77. testName: "Skip functions of similar signature (not imported from framework path)",
  78. src: `
  79. package test
  80. import "k8s.io/fake/path/metrics"
  81. var _ = metrics.NewCounter(
  82. &metrics.CounterOpts{
  83. StabilityLevel: metrics.STABLE,
  84. },
  85. )
  86. `},
  87. {
  88. testName: "Skip . package import of non metric framework",
  89. src: `
  90. package test
  91. import . "k8s.io/fake/path"
  92. var _ = NewCounter(
  93. &CounterOpts{
  94. StabilityLevel: STABLE,
  95. },
  96. )
  97. `},
  98. } {
  99. t.Run(test.testName, func(t *testing.T) {
  100. metrics, errors := searchFileForStableMetrics(fakeFilename, test.src)
  101. if len(metrics) != 0 {
  102. t.Errorf("Didn't expect any stable metrics found, got: %d", len(metrics))
  103. }
  104. if len(errors) != 0 {
  105. t.Errorf("Didn't expect any errors found, got: %s", errors)
  106. }
  107. })
  108. }
  109. }
  110. func TestStableMetric(t *testing.T) {
  111. for _, test := range []struct {
  112. testName string
  113. src string
  114. metric metric
  115. }{
  116. {
  117. testName: "Counter",
  118. metric: metric{
  119. Name: "metric",
  120. Namespace: "namespace",
  121. Subsystem: "subsystem",
  122. StabilityLevel: "STABLE",
  123. DeprecatedVersion: "1.16",
  124. Help: "help",
  125. Type: counterMetricType,
  126. },
  127. src: `
  128. package test
  129. import "k8s.io/component-base/metrics"
  130. var _ = metrics.NewCounter(
  131. &metrics.CounterOpts{
  132. Name: "metric",
  133. Subsystem: "subsystem",
  134. Namespace: "namespace",
  135. Help: "help",
  136. DeprecatedVersion: "1.16",
  137. StabilityLevel: metrics.STABLE,
  138. },
  139. )
  140. `},
  141. {
  142. testName: "CounterVec",
  143. metric: metric{
  144. Name: "metric",
  145. Namespace: "namespace",
  146. Subsystem: "subsystem",
  147. Labels: []string{"label-1"},
  148. StabilityLevel: "STABLE",
  149. DeprecatedVersion: "1.16",
  150. Help: "help",
  151. Type: counterMetricType,
  152. },
  153. src: `
  154. package test
  155. import "k8s.io/component-base/metrics"
  156. var _ = metrics.NewCounterVec(
  157. &metrics.CounterOpts{
  158. Name: "metric",
  159. Namespace: "namespace",
  160. Subsystem: "subsystem",
  161. Help: "help",
  162. DeprecatedVersion: "1.16",
  163. StabilityLevel: metrics.STABLE,
  164. },
  165. []string{"label-1"},
  166. )
  167. `},
  168. {
  169. testName: "Gauge",
  170. metric: metric{
  171. Name: "gauge",
  172. Namespace: "namespace",
  173. Subsystem: "subsystem",
  174. StabilityLevel: "STABLE",
  175. DeprecatedVersion: "1.16",
  176. Help: "help",
  177. Type: gaugeMetricType,
  178. },
  179. src: `
  180. package test
  181. import "k8s.io/component-base/metrics"
  182. var _ = metrics.NewGauge(
  183. &metrics.GaugeOpts{
  184. Name: "gauge",
  185. Namespace: "namespace",
  186. Subsystem: "subsystem",
  187. Help: "help",
  188. DeprecatedVersion: "1.16",
  189. StabilityLevel: metrics.STABLE,
  190. },
  191. )
  192. `},
  193. {
  194. testName: "GaugeVec",
  195. metric: metric{
  196. Name: "gauge",
  197. Namespace: "namespace",
  198. Subsystem: "subsystem",
  199. StabilityLevel: "STABLE",
  200. DeprecatedVersion: "1.16",
  201. Help: "help",
  202. Type: gaugeMetricType,
  203. Labels: []string{"label-1", "label-2"},
  204. },
  205. src: `
  206. package test
  207. import "k8s.io/component-base/metrics"
  208. var _ = metrics.NewGaugeVec(
  209. &metrics.GaugeOpts{
  210. Name: "gauge",
  211. Namespace: "namespace",
  212. Subsystem: "subsystem",
  213. Help: "help",
  214. DeprecatedVersion: "1.16",
  215. StabilityLevel: metrics.STABLE,
  216. },
  217. []string{"label-2", "label-1"},
  218. )
  219. `},
  220. {
  221. testName: "Histogram",
  222. metric: metric{
  223. Name: "histogram",
  224. Namespace: "namespace",
  225. Subsystem: "subsystem",
  226. DeprecatedVersion: "1.16",
  227. StabilityLevel: "STABLE",
  228. Buckets: []float64{0.001, 0.01, 0.1, 1, 10, 100},
  229. Help: "help",
  230. Type: histogramMetricType,
  231. },
  232. src: `
  233. package test
  234. import "k8s.io/component-base/metrics"
  235. var _ = metrics.NewHistogram(
  236. &metrics.HistogramOpts{
  237. Name: "histogram",
  238. Namespace: "namespace",
  239. Subsystem: "subsystem",
  240. StabilityLevel: metrics.STABLE,
  241. Help: "help",
  242. DeprecatedVersion: "1.16",
  243. Buckets: []float64{0.001, 0.01, 0.1, 1, 10, 100},
  244. },
  245. )
  246. `},
  247. {
  248. testName: "HistogramVec",
  249. metric: metric{
  250. Name: "histogram",
  251. Namespace: "namespace",
  252. Subsystem: "subsystem",
  253. DeprecatedVersion: "1.16",
  254. StabilityLevel: "STABLE",
  255. Buckets: []float64{0.001, 0.01, 0.1, 1, 10, 100},
  256. Help: "help",
  257. Type: histogramMetricType,
  258. Labels: []string{"label-1", "label-2"},
  259. },
  260. src: `
  261. package test
  262. import "k8s.io/component-base/metrics"
  263. var _ = metrics.NewHistogramVec(
  264. &metrics.HistogramOpts{
  265. Name: "histogram",
  266. Namespace: "namespace",
  267. Subsystem: "subsystem",
  268. StabilityLevel: metrics.STABLE,
  269. Help: "help",
  270. DeprecatedVersion: "1.16",
  271. Buckets: []float64{0.001, 0.01, 0.1, 1, 10, 100},
  272. },
  273. []string{"label-2", "label-1"},
  274. )
  275. `},
  276. {
  277. testName: "Custom import",
  278. metric: metric{
  279. Name: "metric",
  280. StabilityLevel: "STABLE",
  281. Type: counterMetricType,
  282. },
  283. src: `
  284. package test
  285. import custom "k8s.io/component-base/metrics"
  286. var _ = custom.NewCounter(
  287. &custom.CounterOpts{
  288. Name: "metric",
  289. StabilityLevel: custom.STABLE,
  290. },
  291. )
  292. `},
  293. {
  294. testName: "Const",
  295. metric: metric{
  296. Name: "metric",
  297. StabilityLevel: "STABLE",
  298. Type: counterMetricType,
  299. },
  300. src: `
  301. package test
  302. import "k8s.io/component-base/metrics"
  303. const name = "metric"
  304. var _ = metrics.NewCounter(
  305. &metrics.CounterOpts{
  306. Name: name,
  307. StabilityLevel: metrics.STABLE,
  308. },
  309. )
  310. `},
  311. {
  312. testName: "Variable",
  313. metric: metric{
  314. Name: "metric",
  315. StabilityLevel: "STABLE",
  316. Type: counterMetricType,
  317. },
  318. src: `
  319. package test
  320. import "k8s.io/component-base/metrics"
  321. var name = "metric"
  322. var _ = metrics.NewCounter(
  323. &metrics.CounterOpts{
  324. Name: name,
  325. StabilityLevel: metrics.STABLE,
  326. },
  327. )
  328. `},
  329. {
  330. testName: "Multiple consts in block",
  331. metric: metric{
  332. Name: "metric",
  333. StabilityLevel: "STABLE",
  334. Type: counterMetricType,
  335. },
  336. src: `
  337. package test
  338. import "k8s.io/component-base/metrics"
  339. const (
  340. unrelated1 = "unrelated1"
  341. name = "metric"
  342. unrelated2 = "unrelated2"
  343. )
  344. var _ = metrics.NewCounter(
  345. &metrics.CounterOpts{
  346. Name: name,
  347. StabilityLevel: metrics.STABLE,
  348. },
  349. )
  350. `},
  351. {
  352. testName: "Multiple variables in Block",
  353. metric: metric{
  354. Name: "metric",
  355. StabilityLevel: "STABLE",
  356. Type: counterMetricType,
  357. },
  358. src: `
  359. package test
  360. import "k8s.io/component-base/metrics"
  361. var (
  362. unrelated1 = "unrelated1"
  363. name = "metric"
  364. _ = metrics.NewCounter(
  365. &metrics.CounterOpts{
  366. Name: name,
  367. StabilityLevel: metrics.STABLE,
  368. },
  369. )
  370. )
  371. `},
  372. {
  373. testName: "Histogram with linear buckets",
  374. metric: metric{
  375. Name: "histogram",
  376. StabilityLevel: "STABLE",
  377. Buckets: metrics.LinearBuckets(1, 1, 3),
  378. Type: histogramMetricType,
  379. },
  380. src: `
  381. package test
  382. import "k8s.io/component-base/metrics"
  383. var _ = metrics.NewHistogram(
  384. &metrics.HistogramOpts{
  385. Name: "histogram",
  386. StabilityLevel: metrics.STABLE,
  387. Buckets: metrics.LinearBuckets(1, 1, 3),
  388. },
  389. )
  390. `},
  391. {
  392. testName: "Histogram with exponential buckets",
  393. metric: metric{
  394. Name: "histogram",
  395. StabilityLevel: "STABLE",
  396. Buckets: metrics.ExponentialBuckets(1, 2, 3),
  397. Type: histogramMetricType,
  398. },
  399. src: `
  400. package test
  401. import "k8s.io/component-base/metrics"
  402. var _ = metrics.NewHistogram(
  403. &metrics.HistogramOpts{
  404. Name: "histogram",
  405. StabilityLevel: metrics.STABLE,
  406. Buckets: metrics.ExponentialBuckets(1, 2, 3),
  407. },
  408. )
  409. `},
  410. {
  411. testName: "Histogram with default buckets",
  412. metric: metric{
  413. Name: "histogram",
  414. StabilityLevel: "STABLE",
  415. Buckets: metrics.DefBuckets,
  416. Type: histogramMetricType,
  417. },
  418. src: `
  419. package test
  420. import "k8s.io/component-base/metrics"
  421. var _ = metrics.NewHistogram(
  422. &metrics.HistogramOpts{
  423. Name: "histogram",
  424. StabilityLevel: metrics.STABLE,
  425. Buckets: metrics.DefBuckets,
  426. },
  427. )
  428. `},
  429. } {
  430. t.Run(test.testName, func(t *testing.T) {
  431. metrics, errors := searchFileForStableMetrics(fakeFilename, test.src)
  432. if len(errors) != 0 {
  433. t.Errorf("Unexpected errors: %s", errors)
  434. }
  435. if len(metrics) != 1 {
  436. t.Fatalf("Unexpected number of metrics: got %d, want 1", len(metrics))
  437. }
  438. if test.metric.Labels == nil {
  439. test.metric.Labels = []string{}
  440. }
  441. if !reflect.DeepEqual(metrics[0], test.metric) {
  442. t.Errorf("metric:\ngot %v\nwant %v", metrics[0], test.metric)
  443. }
  444. })
  445. }
  446. }
  447. func TestIncorrectStableMetricDeclarations(t *testing.T) {
  448. for _, test := range []struct {
  449. testName string
  450. src string
  451. err error
  452. }{
  453. {
  454. testName: "Fail on stable summary metric (Summary is DEPRECATED)",
  455. err: fmt.Errorf("testdata/metric.go:4:9: Stable summary metric is not supported"),
  456. src: `
  457. package test
  458. import "k8s.io/component-base/metrics"
  459. var _ = metrics.NewSummary(
  460. &metrics.SummaryOpts{
  461. StabilityLevel: metrics.STABLE,
  462. },
  463. )
  464. `},
  465. {
  466. testName: "Fail on stable metric with attribute set to unknown variable",
  467. err: fmt.Errorf("testdata/metric.go:6:4: Metric attribute was not correctly set. Please use only global consts in same file"),
  468. src: `
  469. package test
  470. import "k8s.io/component-base/metrics"
  471. var _ = metrics.NewCounter(
  472. &metrics.CounterOpts{
  473. Name: unknownVariable,
  474. StabilityLevel: metrics.STABLE,
  475. },
  476. )
  477. `},
  478. {
  479. testName: "Fail on stable metric with attribute set to local function return",
  480. err: fmt.Errorf("testdata/metric.go:9:4: Non string attribute it not supported"),
  481. src: `
  482. package test
  483. import "k8s.io/component-base/metrics"
  484. func getName() string {
  485. return "metric"
  486. }
  487. var _ = metrics.NewCounter(
  488. &metrics.CounterOpts{
  489. Name: getName(),
  490. StabilityLevel: metrics.STABLE,
  491. },
  492. )
  493. `},
  494. {
  495. testName: "Fail on stable metric with attribute set to imported function return",
  496. err: fmt.Errorf("testdata/metric.go:7:4: Non string attribute it not supported"),
  497. src: `
  498. package test
  499. import "k8s.io/component-base/metrics"
  500. import "k8s.io/kubernetes/utils"
  501. var _ = metrics.NewCounter(
  502. &metrics.CounterOpts{
  503. Name: utils.getMetricName(),
  504. StabilityLevel: metrics.STABLE,
  505. },
  506. )
  507. `},
  508. {
  509. testName: "Fail on metric with stability set to function return",
  510. err: fmt.Errorf("testdata/metric.go:9:20: StabilityLevel should be passed STABLE, ALPHA or removed"),
  511. src: `
  512. package test
  513. import "k8s.io/component-base/metrics"
  514. func getMetricStability() metrics.StabilityLevel {
  515. return metrics.STABLE
  516. }
  517. var _ = metrics.NewCounter(
  518. &metrics.CounterOpts{
  519. StabilityLevel: getMetricsStability(),
  520. },
  521. )
  522. `},
  523. {
  524. testName: "error for passing stability as string",
  525. err: fmt.Errorf("testdata/metric.go:6:20: StabilityLevel should be passed STABLE, ALPHA or removed"),
  526. src: `
  527. package test
  528. import "k8s.io/component-base/metrics"
  529. var _ = metrics.NewCounter(
  530. &metrics.CounterOpts{
  531. StabilityLevel: "stable",
  532. },
  533. )
  534. `},
  535. {
  536. testName: "error for passing stability as unknown const",
  537. err: fmt.Errorf("testdata/metric.go:6:20: StabilityLevel should be passed STABLE, ALPHA or removed"),
  538. src: `
  539. package test
  540. import "k8s.io/component-base/metrics"
  541. var _ = metrics.NewCounter(
  542. &metrics.CounterOpts{
  543. StabilityLevel: metrics.UNKNOWN,
  544. },
  545. )
  546. `},
  547. {
  548. testName: "error for passing stability as variable",
  549. err: fmt.Errorf("testdata/metric.go:7:20: StabilityLevel should be passed STABLE, ALPHA or removed"),
  550. src: `
  551. package test
  552. import "k8s.io/component-base/metrics"
  553. var stable = metrics.STABLE
  554. var _ = metrics.NewCounter(
  555. &metrics.CounterOpts{
  556. StabilityLevel: stable,
  557. },
  558. )
  559. `},
  560. {
  561. testName: "error for stable metric created via function call",
  562. err: fmt.Errorf("testdata/metric.go:6:10: Opts for STABLE metric was not directly passed to new metric function"),
  563. src: `
  564. package test
  565. import "k8s.io/component-base/metrics"
  566. var _ = metrics.NewCounter(getStableCounterOpts())
  567. func getStableCounterOpts() *metrics.CounterOpts {
  568. return &metrics.CounterOpts{
  569. StabilityLevel: metrics.STABLE,
  570. }
  571. }
  572. `},
  573. {
  574. testName: "error . package import of metric framework",
  575. err: fmt.Errorf(`testdata/metric.go:3:8: Importing using "." is not supported`),
  576. src: `
  577. package test
  578. import . "k8s.io/component-base/metrics"
  579. var _ = NewCounter(
  580. &CounterOpts{
  581. StabilityLevel: STABLE,
  582. },
  583. )
  584. `},
  585. {
  586. testName: "error stable metric opts passed to local function",
  587. err: fmt.Errorf("testdata/metric.go:4:9: Opts for STABLE metric was not directly passed to new metric function"),
  588. src: `
  589. package test
  590. import "k8s.io/component-base/metrics"
  591. var _ = RegisterMetric(
  592. &metrics.CounterOpts{
  593. StabilityLevel: metrics.STABLE,
  594. },
  595. )
  596. `},
  597. {
  598. testName: "error stable metric opts passed to imported function",
  599. err: fmt.Errorf("testdata/metric.go:4:9: Opts for STABLE metric was not directly passed to new metric function"),
  600. src: `
  601. package test
  602. import "k8s.io/component-base/metrics"
  603. var _ = test.RegisterMetric(
  604. &metrics.CounterOpts{
  605. StabilityLevel: metrics.STABLE,
  606. },
  607. )
  608. `},
  609. {
  610. testName: "error stable metric opts passed to imported function",
  611. err: fmt.Errorf("testdata/metric.go:6:4: Positional arguments are not supported"),
  612. src: `
  613. package test
  614. import "k8s.io/component-base/metrics"
  615. var _ = metrics.NewCounter(
  616. &metrics.CounterOpts{
  617. "counter",
  618. },
  619. )
  620. `},
  621. {
  622. testName: "error stable historgram with unknown prometheus bucket variable",
  623. err: fmt.Errorf("testdata/metric.go:9:13: Buckets should be set to list of floats, result from function call of prometheus.LinearBuckets or prometheus.ExponentialBuckets"),
  624. src: `
  625. package test
  626. import "k8s.io/component-base/metrics"
  627. import "github.com/prometheus/client_golang/prometheus"
  628. var _ = metrics.NewHistogram(
  629. &metrics.HistogramOpts{
  630. Name: "histogram",
  631. StabilityLevel: metrics.STABLE,
  632. Buckets: prometheus.FakeBuckets,
  633. },
  634. )
  635. `},
  636. {
  637. testName: "error stable historgram with unknown bucket variable",
  638. err: fmt.Errorf("testdata/metric.go:9:13: Buckets should be set to list of floats, result from function call of prometheus.LinearBuckets or prometheus.ExponentialBuckets"),
  639. src: `
  640. package test
  641. import "k8s.io/component-base/metrics"
  642. var buckets = []float64{1, 2, 3}
  643. var _ = metrics.NewHistogram(
  644. &metrics.HistogramOpts{
  645. Name: "histogram",
  646. StabilityLevel: metrics.STABLE,
  647. Buckets: buckets,
  648. },
  649. )
  650. `},
  651. {
  652. testName: "error stable historgram with unknown bucket variable from unknown library",
  653. err: fmt.Errorf("testdata/metric.go:9:13: Buckets should be set to list of floats, result from function call of prometheus.LinearBuckets or prometheus.ExponentialBuckets"),
  654. src: `
  655. package test
  656. import "k8s.io/component-base/metrics"
  657. import "github.com/fake_prometheus/prometheus"
  658. var _ = metrics.NewHistogram(
  659. &metrics.HistogramOpts{
  660. Name: "histogram",
  661. StabilityLevel: metrics.STABLE,
  662. Buckets: prometheus.DefBuckets,
  663. },
  664. )
  665. `},
  666. } {
  667. t.Run(test.testName, func(t *testing.T) {
  668. _, errors := searchFileForStableMetrics(fakeFilename, test.src)
  669. if len(errors) != 1 {
  670. t.Fatalf("Unexpected number of errors, got %d, want 1", len(errors))
  671. }
  672. if !reflect.DeepEqual(errors[0], test.err) {
  673. t.Errorf("error:\ngot %v\nwant %v", errors[0], test.err)
  674. }
  675. })
  676. }
  677. }