PageRenderTime 1207ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/outputs/file/file_test.go

https://gitlab.com/ricardo.hernandez/telegraf
Go | 196 lines | 150 code | 42 blank | 4 comment | 7 complexity | 5984d9520f64d36a52c3c98fc1b5ed75 MD5 | raw file
  1. package file
  2. import (
  3. "bytes"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/influxdata/telegraf/internal"
  10. "github.com/influxdata/telegraf/plugins/serializers"
  11. "github.com/influxdata/telegraf/testutil"
  12. )
  13. const (
  14. expNewFile = "test1,tag1=value1 value=1 1257894000000000000\n"
  15. expExistFile = "cpu,cpu=cpu0 value=100 1455312810012459582\n" +
  16. "test1,tag1=value1 value=1 1257894000000000000\n"
  17. )
  18. func TestFileExistingFile(t *testing.T) {
  19. fh := createFile()
  20. s, _ := serializers.NewInfluxSerializer()
  21. f := File{
  22. Files: []string{fh.Name()},
  23. serializer: s,
  24. }
  25. err := f.Connect()
  26. assert.NoError(t, err)
  27. err = f.Write(testutil.MockMetrics())
  28. assert.NoError(t, err)
  29. validateFile(fh.Name(), expExistFile, t)
  30. err = f.Close()
  31. assert.NoError(t, err)
  32. }
  33. func TestFileNewFile(t *testing.T) {
  34. s, _ := serializers.NewInfluxSerializer()
  35. fh := tmpFile()
  36. f := File{
  37. Files: []string{fh},
  38. serializer: s,
  39. }
  40. err := f.Connect()
  41. assert.NoError(t, err)
  42. err = f.Write(testutil.MockMetrics())
  43. assert.NoError(t, err)
  44. validateFile(fh, expNewFile, t)
  45. err = f.Close()
  46. assert.NoError(t, err)
  47. }
  48. func TestFileExistingFiles(t *testing.T) {
  49. fh1 := createFile()
  50. fh2 := createFile()
  51. fh3 := createFile()
  52. s, _ := serializers.NewInfluxSerializer()
  53. f := File{
  54. Files: []string{fh1.Name(), fh2.Name(), fh3.Name()},
  55. serializer: s,
  56. }
  57. err := f.Connect()
  58. assert.NoError(t, err)
  59. err = f.Write(testutil.MockMetrics())
  60. assert.NoError(t, err)
  61. validateFile(fh1.Name(), expExistFile, t)
  62. validateFile(fh2.Name(), expExistFile, t)
  63. validateFile(fh3.Name(), expExistFile, t)
  64. err = f.Close()
  65. assert.NoError(t, err)
  66. }
  67. func TestFileNewFiles(t *testing.T) {
  68. s, _ := serializers.NewInfluxSerializer()
  69. fh1 := tmpFile()
  70. fh2 := tmpFile()
  71. fh3 := tmpFile()
  72. f := File{
  73. Files: []string{fh1, fh2, fh3},
  74. serializer: s,
  75. }
  76. err := f.Connect()
  77. assert.NoError(t, err)
  78. err = f.Write(testutil.MockMetrics())
  79. assert.NoError(t, err)
  80. validateFile(fh1, expNewFile, t)
  81. validateFile(fh2, expNewFile, t)
  82. validateFile(fh3, expNewFile, t)
  83. err = f.Close()
  84. assert.NoError(t, err)
  85. }
  86. func TestFileBoth(t *testing.T) {
  87. fh1 := createFile()
  88. fh2 := tmpFile()
  89. s, _ := serializers.NewInfluxSerializer()
  90. f := File{
  91. Files: []string{fh1.Name(), fh2},
  92. serializer: s,
  93. }
  94. err := f.Connect()
  95. assert.NoError(t, err)
  96. err = f.Write(testutil.MockMetrics())
  97. assert.NoError(t, err)
  98. validateFile(fh1.Name(), expExistFile, t)
  99. validateFile(fh2, expNewFile, t)
  100. err = f.Close()
  101. assert.NoError(t, err)
  102. }
  103. func TestFileStdout(t *testing.T) {
  104. // keep backup of the real stdout
  105. old := os.Stdout
  106. r, w, _ := os.Pipe()
  107. os.Stdout = w
  108. s, _ := serializers.NewInfluxSerializer()
  109. f := File{
  110. Files: []string{"stdout"},
  111. serializer: s,
  112. }
  113. err := f.Connect()
  114. assert.NoError(t, err)
  115. err = f.Write(testutil.MockMetrics())
  116. assert.NoError(t, err)
  117. err = f.Close()
  118. assert.NoError(t, err)
  119. outC := make(chan string)
  120. // copy the output in a separate goroutine so printing can't block indefinitely
  121. go func() {
  122. var buf bytes.Buffer
  123. io.Copy(&buf, r)
  124. outC <- buf.String()
  125. }()
  126. // back to normal state
  127. w.Close()
  128. // restoring the real stdout
  129. os.Stdout = old
  130. out := <-outC
  131. assert.Equal(t, expNewFile, out)
  132. }
  133. func createFile() *os.File {
  134. f, err := ioutil.TempFile("", "")
  135. if err != nil {
  136. panic(err)
  137. }
  138. f.WriteString("cpu,cpu=cpu0 value=100 1455312810012459582\n")
  139. return f
  140. }
  141. func tmpFile() string {
  142. d, err := ioutil.TempDir("", "")
  143. if err != nil {
  144. panic(err)
  145. }
  146. return d + internal.RandomString(10)
  147. }
  148. func validateFile(fname, expS string, t *testing.T) {
  149. buf, err := ioutil.ReadFile(fname)
  150. if err != nil {
  151. panic(err)
  152. }
  153. assert.Equal(t, expS, string(buf))
  154. }