forked from prometheus/memcached_exporter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
128 lines (116 loc) · 3.26 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/grobie/gomemcache/memcache"
)
func TestAcceptance(t *testing.T) {
errc := make(chan error)
addr := "localhost:11211"
// MEMCACHED_PORT might be set by a linked memcached docker container.
if env := os.Getenv("MEMCACHED_PORT"); env != "" {
addr = strings.TrimPrefix(env, "tcp://")
}
ctx, cancel := context.WithCancel(context.Background())
exporter := exec.CommandContext(ctx, "./memcached_exporter", "--memcached.address", addr)
go func() {
if err := exporter.Run(); err != nil && errc != nil {
errc <- err
}
}()
defer cancel()
// Wait for the exporter to be up and running.
OUTER:
for {
timer := time.NewTimer(100 * time.Millisecond)
select {
case <-timer.C:
resp, err := http.Get("http://localhost:9150/")
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
break OUTER
}
}
case err := <-errc:
t.Fatal("error running the exporter:", err)
}
}
client, err := memcache.New(addr)
if err != nil {
t.Fatal(err)
}
if err := client.StatsReset(); err != nil {
t.Fatal(err)
}
item := &memcache.Item{Key: "foo", Value: []byte("bar")}
if err := client.Set(item); err != nil {
t.Fatal(err)
}
if err := client.Set(item); err != nil {
t.Fatal(err)
}
if _, err := client.Get("foo"); err != nil {
t.Fatal(err)
}
if _, err := client.Get("qux"); err != memcache.ErrCacheMiss {
t.Fatal(err)
}
last, err := client.Get("foo")
if err != nil {
t.Fatal(err)
}
last.Value = []byte("banana")
if err := client.CompareAndSwap(last); err != nil {
t.Fatal(err)
}
large := &memcache.Item{Key: "large", Value: bytes.Repeat([]byte("."), 130)}
if err := client.Set(large); err != nil {
t.Fatal(err)
}
resp, err := http.Get("http://localhost:9150/metrics")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
tests := []string{
// memcached_current_connections varies depending on memcached versions
// so it isn't practical to check for an exact value.
`memcached_current_connections `,
`memcached_up 1`,
`memcached_commands_total{command="get",status="hit"} 2`,
`memcached_commands_total{command="get",status="miss"} 1`,
`memcached_commands_total{command="set",status="hit"} 3`,
`memcached_commands_total{command="cas",status="hit"} 1`,
`memcached_current_bytes 262`,
`memcached_max_connections 1024`,
`memcached_current_items 2`,
`memcached_items_total 4`,
`memcached_slab_current_items{slab="1"} 1`,
`memcached_slab_current_items{slab="5"} 1`,
`memcached_slab_commands_total{command="set",slab="1",status="hit"} 2`,
`memcached_slab_commands_total{command="cas",slab="1",status="hit"} 1`,
`memcached_slab_commands_total{command="set",slab="5",status="hit"} 1`,
`memcached_slab_commands_total{command="cas",slab="5",status="hit"} 0`,
`memcached_slab_current_chunks{slab="1"} 10922`,
`memcached_slab_current_chunks{slab="5"} 4369`,
`memcached_slab_mem_requested_bytes{slab="1"} 68`,
`memcached_slab_mem_requested_bytes{slab="5"} 194`,
}
for _, test := range tests {
if !bytes.Contains(body, []byte(test)) {
t.Errorf("want metrics to include %q, have:\n%s", test, body)
}
}
}