Skip to content

Commit e62a620

Browse files
authored
Merge pull request #531 from Icinga/bugfix/tests-dont-update-some-attrs
Tests: don't update some attributes via API in to work with recent icinga2 changes
2 parents a93da02 + 5cbaac5 commit e62a620

File tree

3 files changed

+66
-28
lines changed

3 files changed

+66
-28
lines changed

tests/go.mod

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
module github.com/icinga/icingadb/tests
22

3-
go 1.16
3+
go 1.18
44

55
require (
6-
github.com/containerd/containerd v1.5.6 // indirect
76
github.com/go-redis/redis/v8 v8.11.4
87
github.com/go-sql-driver/mysql v1.6.0
98
github.com/goccy/go-yaml v1.9.5
@@ -13,9 +12,39 @@ require (
1312
github.com/lib/pq v1.10.5
1413
github.com/stretchr/testify v1.7.0
1514
go.uber.org/zap v1.21.0
15+
golang.org/x/exp v0.0.0-20221012112151-59b0eab1532e
16+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
17+
)
18+
19+
require (
20+
github.com/Icinga/go-libs v0.0.0-20220420130327-ef58ad52edd8 // indirect
21+
github.com/Microsoft/go-winio v0.5.0 // indirect
22+
github.com/benbjohnson/clock v1.1.0 // indirect
23+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
24+
github.com/containerd/containerd v1.5.6 // indirect
25+
github.com/davecgh/go-spew v1.1.1 // indirect
26+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
27+
github.com/docker/distribution v2.7.1+incompatible // indirect
28+
github.com/docker/docker v20.10.8+incompatible // indirect
29+
github.com/docker/go-connections v0.4.0 // indirect
30+
github.com/docker/go-units v0.4.0 // indirect
31+
github.com/fatih/color v1.10.0 // indirect
32+
github.com/gogo/protobuf v1.3.2 // indirect
33+
github.com/golang/protobuf v1.5.2 // indirect
34+
github.com/mattn/go-colorable v0.1.8 // indirect
35+
github.com/mattn/go-isatty v0.0.12 // indirect
36+
github.com/opencontainers/go-digest v1.0.0 // indirect
37+
github.com/opencontainers/image-spec v1.0.1 // indirect
38+
github.com/pkg/errors v0.9.1 // indirect
39+
github.com/pmezard/go-difflib v1.0.0 // indirect
40+
github.com/sirupsen/logrus v1.8.1 // indirect
41+
go.uber.org/atomic v1.9.0 // indirect
42+
go.uber.org/multierr v1.7.0 // indirect
1643
golang.org/x/net v0.0.0-20211020060615-d418f374d309 // indirect
17-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
18-
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359 // indirect
44+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
45+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
1946
google.golang.org/genproto v0.0.0-20211027162914-98a5263abeca // indirect
2047
google.golang.org/grpc v1.41.0 // indirect
48+
google.golang.org/protobuf v1.27.1 // indirect
49+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
2150
)

tests/go.sum

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd/go.mod h1:2oa8n
9494
github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0BsqsP2LwDJ9aOkm/6J86V6lyAXCoQWGw3K50=
9595
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
9696
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
97-
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
9897
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
9998
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
10099
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
@@ -362,8 +361,8 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
362361
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
363362
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
364363
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
365-
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
366364
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
365+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
367366
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
368367
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
369368
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
@@ -406,8 +405,6 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
406405
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
407406
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
408407
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
409-
github.com/icinga/icinga-testing v0.0.0-20220513144038-4aef51dce82a h1:d0pnssSfJwMsEDB1CIQSkQ3XACVhVIzkVgcirMfLfkM=
410-
github.com/icinga/icinga-testing v0.0.0-20220513144038-4aef51dce82a/go.mod h1:ZP0pyqhmrRwwQ6FpAfz7UZMgmH7i3vOjEOm9JcFwOw0=
411408
github.com/icinga/icinga-testing v0.0.0-20220516144008-9600081b7a69 h1:M5KN3s3TuHpGPnP78h5cFogtQrywapFIaYfvohQHc7I=
412409
github.com/icinga/icinga-testing v0.0.0-20220516144008-9600081b7a69/go.mod h1:ZP0pyqhmrRwwQ6FpAfz7UZMgmH7i3vOjEOm9JcFwOw0=
413410
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
@@ -711,6 +708,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
711708
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
712709
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
713710
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
711+
golang.org/x/exp v0.0.0-20221012112151-59b0eab1532e h1:/SJUJZl3kz7J5GzAx5lgaKvqKGd4OfzshwDMr6YJCC4=
712+
golang.org/x/exp v0.0.0-20221012112151-59b0eab1532e/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
714713
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
715714
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
716715
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -789,8 +788,9 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
789788
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
790789
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
791790
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
792-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
793791
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
792+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
793+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
794794
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
795795
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
796796
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -858,8 +858,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
858858
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
859859
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
860860
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
861-
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359 h1:2B5p2L5IfGiD7+b9BOoRMC6DgObAVZV+Fsp050NqXik=
862-
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
861+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
862+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
863863
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
864864
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
865865
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

tests/object_sync_test.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/stretchr/testify/assert"
1616
"github.com/stretchr/testify/require"
1717
"go.uber.org/zap"
18+
"golang.org/x/exp/slices"
1819
"io"
1920
"reflect"
2021
"sort"
@@ -328,7 +329,7 @@ func TestObjectSync(t *testing.T) {
328329
t.Parallel()
329330

330331
client.CreateObject(t, "services", *service.HostName+"!"+service.Name, map[string]interface{}{
331-
"attrs": makeIcinga2ApiAttributes(service),
332+
"attrs": makeIcinga2ApiAttributes(service, false),
332333
})
333334

334335
eventually.Assert(t, func(t require.TestingT) {
@@ -387,7 +388,7 @@ func TestObjectSync(t *testing.T) {
387388
}, 20*time.Second, 1*time.Second, "service with name=%q should exist in database", service.Name)
388389

389390
client.UpdateObject(t, "services", *service.HostName+"!"+service.Name, map[string]interface{}{
390-
"attrs": makeIcinga2ApiAttributes(service),
391+
"attrs": makeIcinga2ApiAttributes(service, true),
391392
})
392393

393394
eventually.Assert(t, func(t require.TestingT) {
@@ -426,7 +427,7 @@ func TestObjectSync(t *testing.T) {
426427
t.Parallel()
427428

428429
client.CreateObject(t, "users", user.Name, map[string]interface{}{
429-
"attrs": makeIcinga2ApiAttributes(user),
430+
"attrs": makeIcinga2ApiAttributes(user, false),
430431
})
431432

432433
eventually.Assert(t, func(t require.TestingT) {
@@ -465,7 +466,7 @@ func TestObjectSync(t *testing.T) {
465466

466467
t.Run(user.VariantInfoString(), func(t *testing.T) {
467468
client.UpdateObject(t, "users", userName, map[string]interface{}{
468-
"attrs": makeIcinga2ApiAttributes(user),
469+
"attrs": makeIcinga2ApiAttributes(user, true),
469470
})
470471

471472
eventually.Assert(t, func(t require.TestingT) {
@@ -488,7 +489,7 @@ func TestObjectSync(t *testing.T) {
488489
t.Parallel()
489490

490491
client.CreateObject(t, "notifications", notification.fullName(), map[string]interface{}{
491-
"attrs": makeIcinga2ApiAttributes(notification),
492+
"attrs": makeIcinga2ApiAttributes(notification, false),
492493
})
493494

494495
eventually.Assert(t, func(t require.TestingT) {
@@ -520,7 +521,7 @@ func TestObjectSync(t *testing.T) {
520521
}
521522

522523
client.CreateObject(t, "notifications", baseNotification.fullName(), map[string]interface{}{
523-
"attrs": makeIcinga2ApiAttributes(baseNotification),
524+
"attrs": makeIcinga2ApiAttributes(baseNotification, false),
524525
})
525526

526527
require.Eventuallyf(t, func() bool {
@@ -597,7 +598,7 @@ func TestObjectSync(t *testing.T) {
597598

598599
t.Run(notification.VariantInfoString(), func(t *testing.T) {
599600
client.UpdateObject(t, "notifications", notification.fullName(), map[string]interface{}{
600-
"attrs": makeIcinga2ApiAttributes(notification),
601+
"attrs": makeIcinga2ApiAttributes(notification, true),
601602
})
602603

603604
eventually.Assert(t, func(t require.TestingT) {
@@ -743,7 +744,7 @@ func makeTestSyncHosts(t *testing.T) []Host {
743744
type Service struct {
744745
Name string ` icingadb:"name"`
745746
DisplayName string `icinga2:"display_name" icingadb:"display_name"`
746-
HostName *string `icinga2:"host_name" icingadb:"host.name"`
747+
HostName *string `icinga2:"host_name,nomodify" icingadb:"host.name"`
747748
CheckCommandName string `icinga2:"check_command" icingadb:"checkcommand_name"`
748749
MaxCheckAttempts float64 `icinga2:"max_check_attempts" icingadb:"max_check_attempts"`
749750
CheckPeriodName string `icinga2:"check_period" icingadb:"check_timeperiod_name"`
@@ -889,8 +890,8 @@ func makeTestUsers(t *testing.T) []User {
889890

890891
type Notification struct {
891892
Name string ` icingadb:"name"`
892-
HostName *string `icinga2:"host_name" icingadb:"host.name"`
893-
ServiceName *string `icinga2:"service_name" icingadb:"service.name"`
893+
HostName *string `icinga2:"host_name,nomodify" icingadb:"host.name"`
894+
ServiceName *string `icinga2:"service_name,nomodify" icingadb:"service.name"`
894895
Command string `icinga2:"command" icingadb:"notificationcommand.name"`
895896
Times map[string]string `icinga2:"times"`
896897
Interval int `icinga2:"interval" icingadb:"notification_interval"`
@@ -1061,7 +1062,9 @@ func writeIcinga2ConfigObject(w io.Writer, obj interface{}) error {
10611062
}
10621063

10631064
for fieldIndex := 0; fieldIndex < typ.NumField(); fieldIndex++ {
1064-
if attr := typ.Field(fieldIndex).Tag.Get("icinga2"); attr != "" {
1065+
tag := typ.Field(fieldIndex).Tag.Get("icinga2")
1066+
attr := strings.Split(tag, ",")[0]
1067+
if attr != "" {
10651068
if v := o.Field(fieldIndex).Interface(); v != nil {
10661069
_, err := fmt.Fprintf(w, "\t%s = %s\n", attr, value.ToIcinga2Config(v))
10671070
if err != nil {
@@ -1076,17 +1079,23 @@ func writeIcinga2ConfigObject(w io.Writer, obj interface{}) error {
10761079
}
10771080

10781081
// makeIcinga2ApiAttributes generates a map that can be JSON marshaled and passed to the icinga2 API
1079-
// based on the type of obj and its field having icinga2 struct tags.
1080-
func makeIcinga2ApiAttributes(obj interface{}) map[string]interface{} {
1082+
// based on the type of obj and its field having icinga2 struct tags. Fields that are marked as "nomodify"
1083+
// (for example `icinga2:"host_name,nomodify"`) are omitted if the modify parameter is set to true.
1084+
func makeIcinga2ApiAttributes(obj interface{}, modify bool) map[string]interface{} {
10811085
attrs := make(map[string]interface{})
10821086

10831087
o := reflect.ValueOf(obj)
10841088
typ := o.Type()
10851089
for fieldIndex := 0; fieldIndex < typ.NumField(); fieldIndex++ {
1086-
if attr := typ.Field(fieldIndex).Tag.Get("icinga2"); attr != "" {
1087-
if val := o.Field(fieldIndex).Interface(); val != nil {
1088-
attrs[attr] = value.ToIcinga2Api(val)
1089-
}
1090+
tag := typ.Field(fieldIndex).Tag.Get("icinga2")
1091+
parts := strings.Split(tag, ",")
1092+
attr := parts[0]
1093+
flags := parts[1:]
1094+
if attr == "" || (modify && slices.Contains(flags, "nomodify")) {
1095+
continue
1096+
}
1097+
if val := o.Field(fieldIndex).Interface(); val != nil {
1098+
attrs[attr] = value.ToIcinga2Api(val)
10901099
}
10911100
}
10921101

0 commit comments

Comments
 (0)