commit 717a9e7823638c994c6620883fcee2ef98da294b
parent dca9442c1f7da187028aaaf66f2ebf7c34d6a863
Author: Oliver Lowe <o@olowe.co>
Date: Wed, 2 Feb 2022 15:41:21 +1100
Keep test data in version control
While here also don't log messages in testing unless there's an error.
Diffstat:
6 files changed, 230 insertions(+), 24 deletions(-)
diff --git a/host_test.go b/host_test.go
@@ -26,29 +26,27 @@ func TestHostMarshal(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- t.Log(string(p))
got := make(map[string]interface{})
if err := json.Unmarshal(p, &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, got) {
- t.Fail()
+ t.Error("want", want, "got", got)
}
- t.Log("want", want, "got", got)
}
func TestHostUnmarshal(t *testing.T) {
want := Host{
- Name: "example.com",
+ Name: "VuS9jZ8u.example.org",
Address: "",
- Groups: []string{"example"},
+ Groups: []string{},
State: HostDown,
StateType: StateSoft,
CheckCommand: "hostalive",
- DisplayName: "example.com",
+ DisplayName: "VuS9jZ8u.example.org",
Acknowledgement: false,
}
- f, err := os.Open("testdata/hosts.json")
+ f, err := os.Open("testdata/objects/hosts/VuS9jZ8u.example.org")
if err != nil {
t.Fatal(err)
}
@@ -57,16 +55,8 @@ func TestHostUnmarshal(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- var got Host
- for _, r := range resp.Results {
- h := r.(Host)
- if h.Name == "example.com" {
- got = h
- break
- }
- }
+ got := resp.Results[0].(Host)
if !reflect.DeepEqual(want, got) {
- t.Fail()
+ t.Errorf("want %+v, got %+v", want, got)
}
- t.Logf("want %+v, got %+v", want, got)
}
diff --git a/service_test.go b/service_test.go
@@ -8,7 +8,7 @@ import (
)
func TestServiceUnmarshal(t *testing.T) {
- f, err := os.Open("testdata/services.json")
+ f, err := os.Open("testdata/objects/services/9p.io!http")
if err != nil {
t.Fatal(err)
}
@@ -25,7 +25,7 @@ func TestServiceUnmarshal(t *testing.T) {
CheckCommand: "http",
DisplayName: "http",
LastCheckResult: CheckResult{
- Output: "HTTP OK: HTTP/1.1 200 OK - 1714 bytes in 0.703 second response time ",
+ Output: "HTTP OK: HTTP/1.1 200 OK - 1714 bytes in 1.083 second response time ",
},
}
var got Service
@@ -35,9 +35,8 @@ func TestServiceUnmarshal(t *testing.T) {
}
}
if !reflect.DeepEqual(want, got) {
- t.Fail()
+ t.Errorf("want %+v, got %+v", want, got)
}
- t.Logf("want %+v, got %+v", want, got)
}
func TestServiceMarshal(t *testing.T) {
@@ -59,7 +58,6 @@ func TestServiceMarshal(t *testing.T) {
}
got := string(b)
if want != got {
- t.Fail()
+ t.Error("want", want, "got", got)
}
- t.Log("want", want, "got", got)
}
diff --git a/testdata/objects/hosts/VuS9jZ8u.example.org b/testdata/objects/hosts/VuS9jZ8u.example.org
@@ -0,0 +1,91 @@
+{
+ "results": [
+ {
+ "attrs": {
+ "__name": "VuS9jZ8u.example.org",
+ "acknowledgement": 0,
+ "acknowledgement_expiry": 0,
+ "acknowledgement_last_change": 0,
+ "action_url": "",
+ "active": true,
+ "address": "",
+ "address6": "",
+ "check_attempt": 1,
+ "check_command": "hostalive",
+ "check_interval": 300,
+ "check_period": "",
+ "check_timeout": null,
+ "command_endpoint": "",
+ "display_name": "VuS9jZ8u.example.org",
+ "downtime_depth": 0,
+ "enable_active_checks": true,
+ "enable_event_handler": true,
+ "enable_flapping": false,
+ "enable_notifications": true,
+ "enable_passive_checks": true,
+ "enable_perfdata": true,
+ "event_command": "",
+ "executions": null,
+ "flapping": false,
+ "flapping_current": 0,
+ "flapping_ignore_states": null,
+ "flapping_last_change": 0,
+ "flapping_threshold": 0,
+ "flapping_threshold_high": 30,
+ "flapping_threshold_low": 25,
+ "force_next_check": false,
+ "force_next_notification": false,
+ "groups": [],
+ "ha_mode": 0,
+ "handled": false,
+ "icon_image": "",
+ "icon_image_alt": "",
+ "last_check": -1,
+ "last_check_result": null,
+ "last_hard_state": 1,
+ "last_hard_state_change": 1641607863.315727,
+ "last_reachable": true,
+ "last_state": 1,
+ "last_state_change": 1641607863.315727,
+ "last_state_down": 0,
+ "last_state_type": 0,
+ "last_state_unreachable": 0,
+ "last_state_up": 0,
+ "max_check_attempts": 3,
+ "name": "VuS9jZ8u.example.org",
+ "next_check": 1641614556.854738,
+ "next_update": 1641614856.854738,
+ "notes": "",
+ "notes_url": "",
+ "original_attributes": null,
+ "package": "_api",
+ "paused": false,
+ "previous_state_change": 1641607863.315727,
+ "problem": false,
+ "retry_interval": 60,
+ "severity": 16,
+ "source_location": {
+ "first_column": 0,
+ "first_line": 1,
+ "last_column": 33,
+ "last_line": 1,
+ "path": "/var/lib/icinga2/api/packages/_api/19602f4d-849c-4589-a9fe-cc1c71790f3c/conf.d/hosts/VuS9jZ8u.example.org.conf"
+ },
+ "state": 1,
+ "state_type": 0,
+ "templates": [
+ "VuS9jZ8u.example.org"
+ ],
+ "type": "Host",
+ "vars": null,
+ "version": 1641608345.032706,
+ "volatile": false,
+ "zone": "alpine.olowe.co"
+ },
+ "joins": {},
+ "meta": {},
+ "name": "VuS9jZ8u.example.org",
+ "type": "Host"
+ }
+ ]
+}
diff --git a/testdata/objects/services/9p.io!http b/testdata/objects/services/9p.io!http
@@ -0,0 +1,127 @@
+{
+ "results": [
+ {
+ "attrs": {
+ "__name": "9p.io!http",
+ "acknowledgement": 0,
+ "acknowledgement_expiry": 0,
+ "acknowledgement_last_change": 0,
+ "action_url": "",
+ "active": true,
+ "check_attempt": 1,
+ "check_command": "http",
+ "check_interval": 60,
+ "check_period": "",
+ "check_timeout": null,
+ "command_endpoint": "",
+ "display_name": "http",
+ "downtime_depth": 0,
+ "enable_active_checks": true,
+ "enable_event_handler": true,
+ "enable_flapping": false,
+ "enable_notifications": true,
+ "enable_passive_checks": true,
+ "enable_perfdata": true,
+ "event_command": "",
+ "executions": null,
+ "flapping": false,
+ "flapping_current": 0,
+ "flapping_ignore_states": null,
+ "flapping_last_change": 1642495283.418991,
+ "flapping_threshold": 0,
+ "flapping_threshold_high": 30,
+ "flapping_threshold_low": 25,
+ "force_next_check": false,
+ "force_next_notification": false,
+ "groups": [],
+ "ha_mode": 0,
+ "handled": true,
+ "host_name": "9p.io",
+ "icon_image": "",
+ "icon_image_alt": "",
+ "last_check": 1642496528.415804,
+ "last_check_result": {
+ "active": true,
+ "check_source": "alpine.olowe.co",
+ "command": [
+ "/usr/lib/monitoring-plugins/check_http",
+ "-I",
+ "9p.io"
+ ],
+ "execution_end": 1642496528.415702,
+ "execution_start": 1642496527.310383,
+ "exit_status": 0,
+ "output": "HTTP OK: HTTP/1.1 200 OK - 1714 bytes in 1.083 second response time ",
+ "performance_data": [
+ "time=1.082636s;;;0.000000;10.000000",
+ "size=1714B;;;0"
+ ],
+ "schedule_end": 1642496528.415804,
+ "schedule_start": 1642496527.016395,
+ "scheduling_source": "alpine.olowe.co",
+ "state": 0,
+ "ttl": 0,
+ "type": "CheckResult",
+ "vars_after": {
+ "attempt": 1,
+ "reachable": false,
+ "state": 0,
+ "state_type": 1
+ },
+ "vars_before": {
+ "attempt": 1,
+ "reachable": false,
+ "state": 0,
+ "state_type": 1
+ }
+ },
+ "last_hard_state": 0,
+ "last_hard_state_change": 1642494988.994212,
+ "last_reachable": false,
+ "last_state": 0,
+ "last_state_change": 1642494988.994212,
+ "last_state_critical": 1642494810.489733,
+ "last_state_ok": 1642496528.415702,
+ "last_state_type": 1,
+ "last_state_unknown": 1642494869.390854,
+ "last_state_unreachable": 1642496528.415702,
+ "last_state_warning": 1642494987.196548,
+ "max_check_attempts": 5,
+ "name": "http",
+ "next_check": 1642496587.835849,
+ "next_update": 1642496650.634463,
+ "notes": "",
+ "notes_url": "",
+ "original_attributes": null,
+ "package": "_etc",
+ "paused": false,
+ "previous_state_change": 1642494988.994212,
+ "problem": false,
+ "retry_interval": 30,
+ "severity": 0,
+ "source_location": {
+ "first_column": 1,
+ "first_line": 55,
+ "last_column": 20,
+ "last_line": 55,
+ "path": "/etc/icinga2/conf.d/ye.conf"
+ },
+ "state": 0,
+ "state_type": 1,
+ "templates": [
+ "http",
+ "generic-service"
+ ],
+ "type": "Service",
+ "vars": null,
+ "version": 0,
+ "volatile": false,
+ "zone": ""
+ },
+ "joins": {},
+ "meta": {},
+ "name": "9p.io!http",
+ "type": "Service"
+ }
+ ]
+}
diff --git a/testdata/users.json b/testdata/objects/users/test
diff --git a/user_test.go b/user_test.go
@@ -9,7 +9,7 @@ import (
func TestUser(t *testing.T) {
want := User{Name: "test", Email: "test@example.com", Groups: []string{}}
- f, err := os.Open("testdata/users.json")
+ f, err := os.Open("testdata/objects/users/test")
if err != nil {
t.Fatal(err)
}