Blob


1 package icinga
3 import (
4 "encoding/json"
5 "os"
6 "reflect"
7 "testing"
8 )
10 func TestServiceUnmarshal(t *testing.T) {
11 f, err := os.Open("testdata/services.json")
12 if err != nil {
13 t.Fatal(err)
14 }
15 defer f.Close()
16 resp, err := parseResponse(f)
17 if err != nil {
18 t.Fatal(err)
19 }
20 want := Service{
21 Name: "9p.io!http",
22 Groups: []string{},
23 State: ServiceOK,
24 StateType: StateHard,
25 CheckCommand: "http",
26 DisplayName: "http",
27 LastCheckResult: CheckResult{
28 Output: "HTTP OK: HTTP/1.1 200 OK - 1714 bytes in 0.703 second response time ",
29 },
30 }
31 var got Service
32 for _, r := range resp.Results {
33 if r.name() == "9p.io!http" {
34 got = r.(Service)
35 }
36 }
37 if !reflect.DeepEqual(want, got) {
38 t.Fail()
39 }
40 t.Logf("want %+v, got %+v", want, got)
41 }
43 func TestServiceMarshal(t *testing.T) {
44 want := `{"attrs":{"check_command":"http","display_name":"http"}}`
46 b, err := json.Marshal(Service{
47 Name: "9p.io!http",
48 Groups: []string{},
49 State: ServiceOK,
50 StateType: StateHard,
51 CheckCommand: "http",
52 DisplayName: "http",
53 LastCheckResult: CheckResult{
54 Output: "HTTP OK: HTTP/1.1 200 OK - 1714 bytes in 0.703 second response time ",
55 },
56 })
57 if err != nil {
58 t.Error(err)
59 }
60 got := string(b)
61 if want != got {
62 t.Fail()
63 }
64 t.Log("want", want, "got", got)
65 }