@@ -3,24 +3,32 @@ package integration
3
3
import (
4
4
"fmt"
5
5
"io"
6
+ "io/ioutil"
6
7
"net/http"
7
8
"os"
8
9
"os/exec"
10
+ "strconv"
11
+ "strings"
9
12
"testing"
13
+ "time"
10
14
)
11
15
12
16
func TestIntegration (t * testing.T ) {
13
17
const port = "8771"
18
+ const dummyBinaryName = "prcexpintdum"
19
+ const dummyDescripiveName = "iamdummy"
14
20
run ("go" , "build" , "-o" , "exporter" , "github.com/setlog/process_exporter/cmd/exporter" )
15
21
defer os .Remove ("exporter" )
16
- run ("go" , "build" , "-o" , "proc_exporter_integration_dummy" , "github.com/setlog/process_exporter/test/dummy" )
17
- defer os .Remove ("proc_exporter_integration_dummy" )
18
- cmd := exec .Command ("exporter" , "-port" , port , "-binary" , "proc_exporter_integration_dummy" )
22
+ run ("go" , "build" , "-o" , dummyBinaryName , "github.com/setlog/process_exporter/test/dummy" )
23
+ defer os .Remove (dummyBinaryName )
24
+ cmd := exec .Command ("./ exporter" , "-port" , port , "-binary" , dummyBinaryName )
19
25
err := cmd .Start ()
20
26
if err != nil {
21
27
panic (err )
22
28
}
23
- cmdDummy := exec .Command ("proc_exporter_integration_dummy" , "-name" , "iamdummy" )
29
+ defer cmd .Process .Signal (os .Interrupt )
30
+
31
+ cmdDummy := exec .Command ("./" + dummyBinaryName , "-name" , dummyDescripiveName )
24
32
dummyReader , err := cmdDummy .StdoutPipe ()
25
33
if err != nil {
26
34
panic (err )
@@ -29,26 +37,53 @@ func TestIntegration(t *testing.T) {
29
37
if err != nil {
30
38
panic (err )
31
39
}
40
+ defer dummyWriter .Close ()
32
41
err = cmdDummy .Start ()
33
42
if err != nil {
34
43
panic (err )
35
44
}
36
- b := make ( []byte , 1 , 1 )
45
+ b := []byte { 0 }
37
46
_ , err = io .ReadAtLeast (dummyReader , b , 1 )
38
47
if err != nil {
39
48
panic (err )
40
49
}
41
- resp , err := http .Get ("http://localhost:" + port )
50
+ if b [0 ] != 0x2A {
51
+ panic (fmt .Sprintf ("Byte was %x. Expected 0x2A" , b [0 ]))
52
+ }
53
+ time .Sleep (time .Second )
54
+
55
+ resp , err := http .Get ("http://localhost:" + port + "/metrics" )
42
56
if err != nil {
43
57
panic (err )
44
58
}
59
+ defer resp .Body .Close ()
60
+ dummyWriter .Close ()
45
61
if resp .StatusCode != http .StatusOK {
46
62
panic (fmt .Sprintf ("got code %d when %d expected" , resp .StatusCode , http .StatusOK ))
47
63
}
48
- err = dummyWriter . Close ( )
64
+ contents , err := ioutil . ReadAll ( resp . Body )
49
65
if err != nil {
50
66
panic (err )
51
67
}
68
+ contentsString := string (contents )
69
+ lines := strings .Split (contentsString , "\n " )
70
+ gotLine := false
71
+ for _ , line := range lines {
72
+ prefix := fmt .Sprintf ("mine_disk_write_bytes{bin=\" %s\" ,name=\" %s\" ,pid=\" %d\" } " , dummyBinaryName , dummyDescripiveName , cmdDummy .Process .Pid )
73
+ if strings .HasPrefix (line , prefix ) {
74
+ writeBytes , err := strconv .Atoi (line [len (prefix ):])
75
+ if err != nil {
76
+ panic (fmt .Sprintf ("failed to parse write_bytes from line %s: %v" , line , err ))
77
+ }
78
+ if writeBytes < 1024 || writeBytes > 8192 {
79
+ panic (fmt .Sprintf ("exporter reported %d bytes written. Expected something in the range [1024;8192]." , writeBytes ))
80
+ }
81
+ gotLine = true
82
+ }
83
+ }
84
+ if ! gotLine {
85
+ panic (fmt .Sprintf ("exporter did not report io byte write count" ))
86
+ }
52
87
err = cmdDummy .Wait ()
53
88
if err != nil {
54
89
panic (err )
0 commit comments