Blob


1 package icinga
3 import (
4 "encoding/json"
5 "os"
6 "reflect"
7 "testing"
8 )
10 func TestHostMarshal(t *testing.T) {
11 s := `{"attrs":{"address":"192.0.2.1","address6":"2001:db8::","groups":["test"],"check_command":"dummy","display_name":"Example host"}}`
12 want := make(map[string]interface{})
13 if err := json.Unmarshal([]byte(s), &want); err != nil {
14 t.Fatal(err)
15 }
17 p, err := json.Marshal(Host{
18 Name: "example.com",
19 Address: "192.0.2.1",
20 Address6: "2001:db8::",
21 Groups: []string{"test"},
22 State: HostDown,
23 StateType: StateSoft,
24 CheckCommand: "dummy",
25 DisplayName: "Example host",
26 })
27 if err != nil {
28 t.Fatal(err)
29 }
30 got := make(map[string]interface{})
31 if err := json.Unmarshal(p, &got); err != nil {
32 t.Fatal(err)
33 }
35 if !reflect.DeepEqual(want, got) {
36 t.Fail()
37 }
38 t.Log("want", want, "got", got)
39 }
41 func TestHostUnmarshal(t *testing.T) {
42 want := Host{
43 Name: "example.com",
44 Address: "",
45 Groups: []string{"example"},
46 State: HostDown,
47 StateType: StateSoft,
48 CheckCommand: "hostalive",
49 DisplayName: "example.com",
50 Acknowledgement: false,
51 }
52 f, err := os.Open("testdata/hosts.json")
53 if err != nil {
54 t.Fatal(err)
55 }
56 defer f.Close()
57 resp, err := parseResponse(f)
58 if err != nil {
59 t.Fatal(err)
60 }
61 var got Host
62 for _, r := range resp.Results {
63 h := r.(Host)
64 if h.Name == "example.com" {
65 got = h
66 break
67 }
68 }
69 if !reflect.DeepEqual(want, got) {
70 t.Fail()
71 }
72 t.Logf("want %+v, got %+v", want, got)
73 }