source_format_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sumologicexporter
  4. import (
  5. "regexp"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func getTestSourceFormat(template string) sourceFormat {
  10. r := regexp.MustCompile(sourceRegex)
  11. return newSourceFormat(r, template)
  12. }
  13. func TestNewSourceFormat(t *testing.T) {
  14. expected := sourceFormat{
  15. matches: []string{
  16. "test",
  17. },
  18. template: "%s/test",
  19. }
  20. r := regexp.MustCompile(sourceRegex)
  21. s := newSourceFormat(r, "%{test}/test")
  22. assert.Equal(t, expected, s)
  23. }
  24. func TestNewSourceFormats(t *testing.T) {
  25. expected := sourceFormats{
  26. host: sourceFormat{
  27. matches: []string{
  28. "namespace",
  29. },
  30. template: "ns/%s",
  31. },
  32. name: sourceFormat{
  33. matches: []string{
  34. "pod",
  35. },
  36. template: "name/%s",
  37. },
  38. category: sourceFormat{
  39. matches: []string{
  40. "cluster",
  41. },
  42. template: "category/%s",
  43. },
  44. }
  45. cfg := &Config{
  46. SourceName: "name/%{pod}",
  47. SourceHost: "ns/%{namespace}",
  48. SourceCategory: "category/%{cluster}",
  49. }
  50. s := newSourceFormats(cfg)
  51. assert.Equal(t, expected, s)
  52. }
  53. func TestFormat(t *testing.T) {
  54. f := fieldsFromMap(map[string]string{
  55. "key_1": "value_1",
  56. "key_2.subkey": "value_2",
  57. })
  58. s := getTestSourceFormat("%{key_1}/%{key_2.subkey}")
  59. expected := "value_1/value_2"
  60. result := s.format(f)
  61. assert.Equal(t, expected, result)
  62. }
  63. func TestFormatNonExistingKey(t *testing.T) {
  64. f := fieldsFromMap(map[string]string{"key_2": "value_2"})
  65. s := getTestSourceFormat("%{key_1}/%{key_2}")
  66. expected := "/value_2"
  67. result := s.format(f)
  68. assert.Equal(t, expected, result)
  69. }
  70. func TestIsSet(t *testing.T) {
  71. s := getTestSourceFormat("%{key_1}/%{key_2}")
  72. assert.True(t, s.isSet())
  73. }
  74. func TestIsNotSet(t *testing.T) {
  75. s := getTestSourceFormat("")
  76. assert.False(t, s.isSet())
  77. }