diff options
Diffstat (limited to 'src/go/collectors/go.d.plugin/modules/systemdunits/systemdunits_test.go')
-rw-r--r-- | src/go/collectors/go.d.plugin/modules/systemdunits/systemdunits_test.go | 891 |
1 files changed, 891 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/systemdunits/systemdunits_test.go b/src/go/collectors/go.d.plugin/modules/systemdunits/systemdunits_test.go new file mode 100644 index 0000000000..baa9ed46a1 --- /dev/null +++ b/src/go/collectors/go.d.plugin/modules/systemdunits/systemdunits_test.go @@ -0,0 +1,891 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +//go:build linux +// +build linux + +package systemdunits + +import ( + "context" + "errors" + "fmt" + "path/filepath" + "testing" + + "github.com/netdata/go.d.plugin/agent/module" + + "github.com/coreos/go-systemd/v22/dbus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNew(t *testing.T) { + assert.Implements(t, (*module.Module)(nil), New()) +} + +func TestSystemdUnits_Init(t *testing.T) { + tests := map[string]struct { + config Config + wantFail bool + }{ + "success on default config": { + config: New().Config, + }, + "success when 'include' option set": { + config: Config{ + Include: []string{"*"}, + }, + }, + "fails when 'include' option not set": { + wantFail: true, + config: Config{Include: []string{}}, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + systemd := New() + systemd.Config = test.config + + if test.wantFail { + assert.False(t, systemd.Init()) + } else { + assert.True(t, systemd.Init()) + } + }) + } +} + +func TestSystemdUnits_Check(t *testing.T) { + tests := map[string]struct { + prepare func() *SystemdUnits + wantFail bool + }{ + "success on systemd v230+": { + prepare: func() *SystemdUnits { + systemd := New() + systemd.Include = []string{"*"} + systemd.client = prepareOKClient(230) + return systemd + }, + }, + "success on systemd v230-": { + prepare: func() *SystemdUnits { + systemd := New() + systemd.Include = []string{"*"} + systemd.client = prepareOKClient(220) + return systemd + }, + }, + "fails when all unites are filtered": { + wantFail: true, + prepare: func() *SystemdUnits { + systemd := New() + systemd.Include = []string{"*.not_exists"} + systemd.client = prepareOKClient(230) + return systemd + }, + }, + "fails on error on connect": { + wantFail: true, + prepare: func() *SystemdUnits { + systemd := New() + systemd.client = prepareClientErrOnConnect() + return systemd + }, + }, + "fails on error on get manager property": { + wantFail: true, + prepare: func() *SystemdUnits { + systemd := New() + systemd.client = prepareClientErrOnGetManagerProperty() + return systemd + }, + }, + "fails on error on list units": { + wantFail: true, + prepare: func() *SystemdUnits { + systemd := New() + systemd.client = prepareClientErrOnListUnits() + return systemd + }, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + systemd := test.prepare() + require.True(t, systemd.Init()) + + if test.wantFail { + assert.False(t, systemd.Check()) + } else { + assert.True(t, systemd.Check()) + } + }) + } +} + +func TestSystemdUnits_Charts(t *testing.T) { + systemd := New() + require.True(t, systemd.Init()) + assert.NotNil(t, systemd.Charts()) +} + +func TestSystemdUnits_Cleanup(t *testing.T) { + systemd := New() + systemd.Include = []string{"*"} + client := prepareOKClient(230) + systemd.client = client + + require.True(t, systemd.Init()) + require.NotNil(t, systemd.Collect()) + conn := systemd.conn + systemd.Cleanup() + + assert.Nil(t, systemd.conn) + v, _ := conn.(*mockConn) + assert.True(t, v.closeCalled) +} + +func TestSystemdUnits_Collect(t *testing.T) { + tests := map[string]struct { + prepare func() *SystemdUnits + wantCollected map[string]int64 + }{ + "success on systemd v230+ on collecting all unit type": { + prepare: func() *SystemdUnits { + systemd := New() + systemd.Include = []string{"*"} + systemd.client = prepareOKClient(230) + return systemd + }, + wantCollected: map[string]int64{ + "unit_dbus_socket_state_activating": 0, + "unit_dbus_socket_state_active": 1, + "unit_dbus_socket_state_deactivating": 0, + "unit_dbus_socket_state_failed": 0, + "unit_dbus_socket_state_inactive": 0, + "unit_dev-disk-by-uuid-DE44-CEE0_device_state_activating": 0, + "unit_dev-disk-by-uuid-DE44-CEE0_device_state_active": 1, + "unit_dev-disk-by-uuid-DE44-CEE0_device_state_deactivating": 0, + "unit_dev-disk-by-uuid-DE44-CEE0_device_state_failed": 0, + "unit_dev-disk-by-uuid-DE44-CEE0_device_state_inactive": 0, + "unit_dev-nvme0n1_device_state_activating": 0, + "unit_dev-nvme0n1_device_state_active": 1, + "unit_dev-nvme0n1_device_state_deactivating": 0, + "unit_dev-nvme0n1_device_state_failed": 0, + "unit_dev-nvme0n1_device_state_inactive": 0, + "unit_docker_socket_state_activating": 0, + "unit_docker_socket_state_active": 1, + "unit_docker_socket_state_deactivating": 0, + "unit_docker_socket_state_failed": 0, + "unit_docker_socket_state_inactive": 0, + "unit_getty-pre_target_state_activating": 0, + "unit_getty-pre_target_state_active": 0, + "unit_getty-pre_target_state_deactivating": 0, + "unit_getty-pre_target_state_failed": 0, + "unit_getty-pre_target_state_inactive": 1, + "unit_init_scope_state_activating": 0, + "unit_init_scope_state_active": 1, + "unit_init_scope_state_deactivating": 0, + "unit_init_scope_state_failed": 0, + "unit_init_scope_state_inactive": 0, + "unit_logrotate_timer_state_activating": 0, + "unit_logrotate_timer_state_active": 1, + "unit_logrotate_timer_state_deactivating": 0, + "unit_logrotate_timer_state_failed": 0, + "unit_logrotate_timer_state_inactive": 0, + "unit_lvm2-lvmetad_socket_state_activating": 0, + "unit_lvm2-lvmetad_socket_state_active": 1, + "unit_lvm2-lvmetad_socket_state_deactivating": 0, + "unit_lvm2-lvmetad_socket_state_failed": 0, + "unit_lvm2-lvmetad_socket_state_inactive": 0, + "unit_lvm2-lvmpolld_socket_state_activating": 0, + "unit_lvm2-lvmpolld_socket_state_active": 1, + "unit_lvm2-lvmpolld_socket_state_deactivating": 0, + "unit_lvm2-lvmpolld_socket_state_failed": 0, + "unit_lvm2-lvmpolld_socket_state_inactive": 0, + "unit_man-db_timer_state_activating": 0, + "unit_man-db_timer_state_active": 1, + "unit_man-db_timer_state_deactivating": 0, + "unit_man-db_timer_state_failed": 0, + "unit_man-db_timer_state_inactive": 0, + "unit_org.cups.cupsd_path_state_activating": 0, + "unit_org.cups.cupsd_path_state_active": 1, + "unit_org.cups.cupsd_path_state_deactivating": 0, + "unit_org.cups.cupsd_path_state_failed": 0, + "unit_org.cups.cupsd_path_state_inactive": 0, + "unit_pamac-cleancache_timer_state_activating": 0, + "unit_pamac-cleancache_timer_state_active": 1, + "unit_pamac-cleancache_timer_state_deactivating": 0, + "unit_pamac-cleancache_timer_state_failed": 0, + "unit_pamac-cleancache_timer_state_inactive": 0, + "unit_pamac-mirrorlist_timer_state_activating": 0, + "unit_pamac-mirrorlist_timer_state_active": 1, + "unit_pamac-mirrorlist_timer_state_deactivating": 0, + "unit_pamac-mirrorlist_timer_state_failed": 0, + "unit_pamac-mirrorlist_timer_state_inactive": 0, + "unit_proc-sys-fs-binfmt_misc_automount_state_activating": 0, + "unit_proc-sys-fs-binfmt_misc_automount_state_active": 1, + "unit_proc-sys-fs-binfmt_misc_automount_state_deactivating": 0, + "unit_proc-sys-fs-binfmt_misc_automount_state_failed": 0, + "unit_proc-sys-fs-binfmt_misc_automount_state_inactive": 0, + "unit_remote-fs-pre_target_state_activating": 0, + "unit_remote-fs-pre_target_state_active": 0, + "unit_remote-fs-pre_target_state_deactivating": 0, + "unit_remote-fs-pre_target_state_failed": 0, + "unit_remote-fs-pre_target_state_inactive": 1, + "unit_rpc_pipefs_target_state_activating": 0, + "unit_rpc_pipefs_target_state_active": 0, + "unit_rpc_pipefs_target_state_deactivating": 0, + "unit_rpc_pipefs_target_state_failed": 0, + "unit_rpc_pipefs_target_state_inactive": 1, + "unit_run-user-1000-gvfs_mount_state_activating": 0, + "unit_run-user-1000-gvfs_mount_state_active": 1, + "unit_run-user-1000-gvfs_mount_state_deactivating": 0, + "unit_run-user-1000-gvfs_mount_state_failed": 0, + "unit_run-user-1000-gvfs_mount_state_inactive": 0, + "unit_run-user-1000_mount_state_activating": 0, + "unit_run-user-1000_mount_state_active": 1, + "unit_run-user-1000_mount_state_deactivating": 0, + "unit_run-user-1000_mount_state_failed": 0, + "unit_run-user-1000_mount_state_inactive": 0, + "unit_session-1_scope_state_activating": 0, + "unit_session-1_scope_state_active": 1, + "unit_session-1_scope_state_deactivating": 0, + "unit_session-1_scope_state_failed": 0, + "unit_session-1_scope_state_inactive": 0, + "unit_session-2_scope_state_activating": 0, + "unit_session-2_scope_state_active": 1, + "unit_session-2_scope_state_deactivating": 0, + "unit_session-2_scope_state_failed": 0, + "unit_session-2_scope_state_inactive": 0, + "unit_session-3_scope_state_activating": 0, + "unit_session-3_scope_state_active": 1, + "unit_session-3_scope_state_deactivating": 0, + "unit_session-3_scope_state_failed": 0, + "unit_session-3_scope_state_inactive": 0, + "unit_session-6_scope_state_activating": 0, + "unit_session-6_scope_state_active": 1, + "unit_session-6_scope_state_deactivating": 0, + "unit_session-6_scope_state_failed": 0, + "unit_session-6_scope_state_inactive": 0, + "unit_shadow_timer_state_activating": 0, + "unit_shadow_timer_state_active": 1, + "unit_shadow_timer_state_deactivating": 0, + "unit_shadow_timer_state_failed": 0, + "unit_shadow_timer_state_inactive": 0, + "unit_sound_target_state_activating": 0, + "unit_sound_target_state_active": 1, + "unit_sound_target_state_deactivating": 0, + "unit_sound_target_state_failed": 0, + "unit_sound_target_state_inactive": 0, + "unit_sys-devices-virtual-net-loopback1_device_state_activating": 0, + "unit_sys-devices-virtual-net-loopback1_device_state_active": 1, + "unit_sys-devices-virtual-net-loopback1_device_state_deactivating": 0, + "unit_sys-devices-virtual-net-loopback1_device_state_failed": 0, + "unit_sys-devices-virtual-net-loopback1_device_state_inactive": 0, + "unit_sys-module-fuse_device_state_activating": 0, + "unit_sys-module-fuse_device_state_active": 1, + "unit_sys-module-fuse_device_state_deactivating": 0, + "unit_sys-module-fuse_device_state_failed": 0, + "unit_sys-module-fuse_device_state_inactive": 0, + "unit_sysinit_target_state_activating": 0, + "unit_sysinit_target_state_active": 1, + "unit_sysinit_target_state_deactivating": 0, + "unit_sysinit_target_state_failed": 0, + "unit_sysinit_target_state_inactive": 0, + "unit_system-getty_slice_state_activating": 0, + "unit_system-getty_slice_state_active": 1, + "unit_system-getty_slice_state_deactivating": 0, + "unit_system-getty_slice_state_failed": 0, + "unit_system-getty_slice_state_inactive": 0, + "unit_system-netctl_slice_state_activating": 0, + "unit_system-netctl_slice_state_active": 1, + "unit_system-netctl_slice_state_deactivating": 0, + "unit_system-netctl_slice_state_failed": 0, + "unit_system-netctl_slice_state_inactive": 0, + "unit_system-systemd-fsck_slice_state_activating": 0, + "unit_system-systemd-fsck_slice_state_active": 1, + "unit_system-systemd-fsck_slice_state_deactivating": 0, + "unit_system-systemd-fsck_slice_state_failed": 0, + "unit_system-systemd-fsck_slice_state_inactive": 0, + "unit_system_slice_state_activating": 0, + "unit_system_slice_state_active": 1, + "unit_system_slice_state_deactivating": 0, + "unit_system_slice_state_failed": 0, + "unit_system_slice_state_inactive": 0, + "unit_systemd-ask-password-console_path_state_activating": 0, + "unit_systemd-ask-password-console_path_state_active": 1, + "unit_systemd-ask-password-console_path_state_deactivating": 0, + "unit_systemd-ask-password-console_path_state_failed": 0, + "unit_systemd-ask-password-console_path_state_inactive": 0, + "unit_systemd-ask-password-wall_path_state_activating": 0, + "unit_systemd-ask-password-wall_path_state_active": 1, + "unit_systemd-ask-password-wall_path_state_deactivating": 0, + "unit_systemd-ask-password-wall_path_state_failed": 0, + "unit_systemd-ask-password-wall_path_state_inactive": 0, + "unit_systemd-ask-password-wall_service_state_activating": 0, + "unit_systemd-ask-password-wall_service_state_active": 0, + "unit_systemd-ask-password-wall_service_state_deactivating": 0, + "unit_systemd-ask-password-wall_service_state_failed": 0, + "unit_systemd-ask-password-wall_service_state_inactive": 1, + "unit_systemd-fsck-root_service_state_activating": 0, + "unit_systemd-fsck-root_service_state_active": 0, + "unit_systemd-fsck-root_service_state_deactivating": 0, + "unit_systemd-fsck-root_service_state_failed": 0, + "unit_systemd-fsck-root_service_state_inactive": 1, + "unit_systemd-udevd-kernel_socket_state_activating": 0, + "unit_systemd-udevd-kernel_socket_state_active": 1, + "unit_systemd-udevd-kernel_socket_state_deactivating": 0, + "unit_systemd-udevd-kernel_socket_state_failed": 0, + "unit_systemd-udevd-kernel_socket_state_inactive": 0, + "unit_tmp_mount_state_activating": 0, + "unit_tmp_mount_state_active": 1, + "unit_tmp_mount_state_deactivating": 0, + "unit_tmp_mount_state_failed": 0, + "unit_tmp_mount_state_inactive": 0, + "unit_user-runtime-dir@1000_service_state_activating": 0, + "unit_user-runtime-dir@1000_service_state_active": 1, + "unit_user-runtime-dir@1000_service_state_deactivating": 0, + "unit_user-runtime-dir@1000_service_state_failed": 0, + "unit_user-runtime-dir@1000_service_state_inactive": 0, + "unit_user@1000_service_state_activating": 0, + "unit_user@1000_service_state_active": 1, + "unit_user@1000_service_state_deactivating": 0, + "unit_user@1000_service_state_failed": 0, + "unit_user@1000_service_state_inactive": 0, + "unit_user_slice_state_activating": 0, + "unit_user_slice_state_active": 1, + "unit_user_slice_state_deactivating": 0, + "unit_user_slice_state_failed": 0, + "unit_user_slice_state_inactive": 0, + "unit_var-lib-nfs-rpc_pipefs_mount_state_activating": 0, + "unit_var-lib-nfs-rpc_pipefs_mount_state_active": 0, + "unit_var-lib-nfs-rpc_pipefs_mount_state_deactivating": 0, + "unit_var-lib-nfs-rpc_pipefs_mount_state_failed": 0, + "unit_var-lib-nfs-rpc_pipefs_mount_state_inactive": 1, + }, + }, + "success on systemd v230- on collecting all unit types": { + prepare: func() *SystemdUnits { + systemd := New() + systemd.Include = []string{"*"} + systemd.client = prepareOKClient(220) + return systemd + }, + wantCollected: map[string]int64{ + "unit_dbus_socket_state_activating": 0, + "unit_dbus_socket_state_active": 1, + "unit_dbus_socket_state_deactivating": 0, + "unit_dbus_socket_state_failed": 0, + "unit_dbus_socket_state_inactive": 0, + "unit_dev-disk-by-uuid-DE44-CEE0_device_state_activating": 0, + "unit_dev-disk-by-uuid-DE44-CEE0_device_state_active": 1, + "unit_dev-disk-by-uuid-DE44-CEE0_device_state_deactivating": 0, + "unit_dev-disk-by-uuid-DE44-CEE0_device_state_failed": 0, + "unit_dev-disk-by-uuid-DE44-CEE0_device_state_inactive": 0, + "unit_dev-nvme0n1_device_state_activating": 0, + "unit_dev-nvme0n1_device_state_active": 1, + "unit_dev-nvme0n1_device_state_deactivating": 0, + "unit_dev-nvme0n1_device_state_failed": 0, + "unit_dev-nvme0n1_device_state_inactive": 0, + "unit_docker_socket_state_activating": 0, + "unit_docker_socket_state_active": 1, + "unit_docker_socket_state_deactivating": 0, + "unit_docker_socket_state_failed": 0, + "unit_docker_socket_state_inactive": 0, + "unit_getty-pre_target_state_activating": 0, + "unit_getty-pre_target_state_active": 0, + "unit_getty-pre_target_state_deactivating": 0, + "unit_getty-pre_target_state_failed": 0, + "unit_getty-pre_target_state_inactive": 1, + "unit_init_scope_state_activating": 0, + "unit_init_scope_state_active": 1, + "unit_init_scope_state_deactivating": 0, + "unit_init_scope_state_failed": 0, + "unit_init_scope_state_inactive": 0, + "unit_logrotate_timer_state_activating": 0, + "unit_logrotate_timer_state_active": 1, + "unit_logrotate_timer_state_deactivating": 0, + "unit_logrotate_timer_state_failed": 0, + "unit_logrotate_timer_state_inactive": 0, + "unit_lvm2-lvmetad_socket_state_activating": 0, + "unit_lvm2-lvmetad_socket_state_active": 1, + "unit_lvm2-lvmetad_socket_state_deactivating": 0, + "unit_lvm2-lvmetad_socket_state_failed": 0, + "unit_lvm2-lvmetad_socket_state_inactive": 0, + "unit_lvm2-lvmpolld_socket_state_activating": 0, + "unit_lvm2-lvmpolld_socket_state_active": 1, + "unit_lvm2-lvmpolld_socket_state_deactivating": 0, + "unit_lvm2-lvmpolld_socket_state_failed": 0, + "unit_lvm2-lvmpolld_socket_state_inactive": 0, + "unit_man-db_timer_state_activating": 0, + "unit_man-db_timer_state_active": 1, + "unit_man-db_timer_state_deactivating": 0, + "unit_man-db_timer_state_failed": 0, + "unit_man-db_timer_state_inactive": 0, + "unit_org.cups.cupsd_path_state_activating": 0, + "unit_org.cups.cupsd_path_state_active": 1, + "unit_org.cups.cupsd_path_state_deactivating": 0, + "unit_org.cups.cupsd_path_state_failed": 0, + "unit_org.cups.cupsd_path_state_inactive": 0, + "unit_pamac-cleancache_timer_state_activating": 0, + "unit_pamac-cleancache_timer_state_active": 1, + "unit_pamac-cleancache_timer_state_deactivating": 0, + "unit_pamac-cleancache_timer_state_failed": 0, + "unit_pamac-cleancache_timer_state_inactive": 0, + "unit_pamac-mirrorlist_timer_state_activating": 0, + "unit_pamac-mirrorlist_timer_state_active": 1, + "unit_pamac-mirrorlist_timer_state_deactivating": 0, + "unit_pamac-mirrorlist_timer_state_failed": 0, + "unit_pamac-mirrorlist_timer_state_inactive": 0, + "unit_proc-sys-fs-binfmt_misc_automount_state_activating": 0, + "unit_proc-sys-fs-binfmt_misc_automount_state_active": 1, + "unit_proc-sys-fs-binfmt_misc_automount_state_deactivating": 0, + "unit_proc-sys-fs-binfmt_misc_automount_state_failed": 0, + "unit_proc-sys-fs-binfmt_misc_automount_state_inactive": 0, + "unit_remote-fs-pre_target_state_activating": 0, + "unit_remote-fs-pre_target_state_active": 0, + "unit_remote-fs-pre_target_state_deactivating": 0, + "unit_remote-fs-pre_target_state_failed": 0, + "unit_remote-fs-pre_target_state_inactive": 1, + "unit_rpc_pipefs_target_state_activating": 0, + "unit_rpc_pipefs_target_state_active": 0, + "unit_rpc_pipefs_target_state_deactivating": 0, + "unit_rpc_pipefs_target_state_failed": 0, + "unit_rpc_pipefs_target_state_inactive": 1, + "unit_run-user-1000-gvfs_mount_state_activating": 0, + "unit_run-user-1000-gvfs_mount_state_active": 1, + "unit_run-user-1000-gvfs_mount_state_deactivating": 0, + "unit_run-user-1000-gvfs_mount_state_failed": 0, + "unit_run-user-1000-gvfs_mount_state_inactive": 0, + "unit_run-user-1000_mount_state_activating": 0, + "unit_run-user-1000_mount_state_active": 1, + "unit_run-user-1000_mount_state_deactivating": 0, + "unit_run-user-1000_mount_state_failed": 0, + "unit_run-user-1000_mount_state_inactive": 0, + "unit_session-1_scope_state_activating": 0, + "unit_session-1_scope_state_active": 1, + "unit_session-1_scope_state_deactivating": 0, + "unit_session-1_scope_state_failed": 0, + "unit_session-1_scope_state_inactive": 0, + "unit_session-2_scope_state_activating": 0, + "unit_session-2_scope_state_active": 1, + "unit_session-2_scope_state_deactivating": 0, + "unit_session-2_scope_state_failed": 0, + "unit_session-2_scope_state_inactive": 0, + "unit_session-3_scope_state_activating": 0, + "unit_session-3_scope_state_active": 1, + "unit_session-3_scope_state_deactivating": 0, + "unit_session-3_scope_state_failed": 0, + "unit_session-3_scope_state_inactive": 0, + "unit_session-6_scope_state_activating": 0, + "unit_session-6_scope_state_active": 1, + "unit_session-6_scope_state_deactivating": 0, + "unit_session-6_scope_state_failed": 0, + "unit_session-6_scope_state_inactive": 0, + "unit_shadow_timer_state_activating": 0, + "unit_shadow_timer_state_active": 1, + "unit_shadow_timer_state_deactivating": 0, + "unit_shadow_timer_state_failed": 0, + "unit_shadow_timer_state_inactive": 0, + "unit_sound_target_state_activating": 0, + "unit_sound_target_state_active": 1, + "unit_sound_target_state_deactivating": 0, + "unit_sound_target_state_failed": 0, + "unit_sound_target_state_inactive": 0, + "unit_sys-devices-virtual-net-loopback1_device_state_activating": 0, + "unit_sys-devices-virtual-net-loopback1_device_state_active": 1, + "unit_sys-devices-virtual-net-loopback1_device_state_deactivating": 0, + "unit_sys-devices-virtual-net-loopback1_device_state_failed": 0, + "unit_sys-devices-virtual-net-loopback1_device_state_inactive": 0, + "unit_sys-module-fuse_device_state_activating": 0, + "unit_sys-module-fuse_device_state_active": 1, + "unit_sys-module-fuse_device_state_deactivating": 0, + "unit_sys-module-fuse_device_state_failed": 0, + "unit_sys-module-fuse_device_state_inactive": 0, + "unit_sysinit_target_state_activating": 0, + "unit_sysinit_target_state_active": 1, + "unit_sysinit_target_state_deactivating": 0, + "unit_sysinit_target_state_failed": 0, + "unit_sysinit_target_state_inactive": 0, + "unit_system-getty_slice_state_activating": 0, + "unit_system-getty_slice_state_active": 1, + "unit_system-getty_slice_state_deactivating": 0, + "unit_system-getty_slice_state_failed": 0, + "unit_system-getty_slice_state_inactive": 0, + "unit_system-netctl_slice_state_activating": 0, + "unit_system-netctl_slice_state_active": 1, + "unit_system-netctl_slice_state_deactivating": 0, + "unit_system-netctl_slice_state_failed": 0, + "unit_system-netctl_slice_state_inactive": 0, + "unit_system-systemd-fsck_slice_state_activating": 0, + "unit_system-systemd-fsck_slice_state_active": 1, + "unit_system-systemd-fsck_slice_state_deactivating": 0, + "unit_system-systemd-fsck_slice_state_failed": 0, + "unit_system-systemd-fsck_slice_state_inactive": 0, + "unit_system_slice_state_activating": 0, + "unit_system_slice_state_active": 1, + "unit_system_slice_state_deactivating": 0, + "unit_system_slice_state_failed": 0, + "unit_system_slice_state_inactive": 0, + "unit_systemd-ask-password-console_path_state_activating": 0, + "unit_systemd-ask-password-console_path_state_active": 1, + "unit_systemd-ask-password-console_path_state_deactivating": 0, + "unit_systemd-ask-password-console_path_state_failed": 0, + "unit_systemd-ask-password-console_path_state_inactive": 0, + "unit_systemd-ask-password-wall_path_state_activating": 0, + "unit_systemd-ask-password-wall_path_state_active": 1, + "unit_systemd-ask-password-wall_path_state_deactivating": 0, + "unit_systemd-ask-password-wall_path_state_failed": 0, + "unit_systemd-ask-password-wall_path_state_inactive": 0, + "unit_systemd-ask-password-wall_service_state_activating": 0, + "unit_systemd-ask-password-wall_service_state_active": 0, + "unit_systemd-ask-password-wall_service_state_deactivating": 0, + "unit_systemd-ask-password-wall_service_state_failed": 0, + "unit_systemd-ask-password-wall_service_state_inactive": 1, + "unit_systemd-fsck-root_service_state_activating": 0, + "unit_systemd-fsck-root_service_state_active": 0, + "unit_systemd-fsck-root_service_state_deactivating": 0, + "unit_systemd-fsck-root_service_state_failed": 0, + "unit_systemd-fsck-root_service_state_inactive": 1, + "unit_systemd-udevd-kernel_socket_state_activating": 0, + "unit_systemd-udevd-kernel_socket_state_active": 1, + "unit_systemd-udevd-kernel_socket_state_deactivating": 0, + "unit_systemd-udevd-kernel_socket_state_failed": 0, + "unit_systemd-udevd-kernel_socket_state_inactive": 0, + "unit_tmp_mount_state_activating": 0, + "unit_tmp_mount_state_active": 1, + "unit_tmp_mount_state_deactivating": 0, + "unit_tmp_mount_state_failed": 0, + "unit_tmp_mount_state_inactive": 0, + "unit_user-runtime-dir@1000_service_state_activating": 0, + "unit_user-runtime-dir@1000_service_state_active": 1, + "unit_user-runtime-dir@1000_service_state_deactivating": 0, + "unit_user-runtime-dir@1000_service_state_failed": 0, + "unit_user-runtime-dir@1000_service_state_inactive": 0, + "unit_user@1000_service_state_activating": 0, + "unit_user@1000_service_state_active": 1, + "unit_user@1000_service_state_deactivating": 0, + "unit_user@1000_service_state_failed": 0, + "unit_user@1000_service_state_inactive": 0, + "unit_user_slice_state_activating": 0, + "unit_user_slice_state_active": 1, + "unit_user_slice_state_deactivating": 0, + "unit_user_slice_state_failed": 0, + "unit_user_slice_state_inactive": 0, + "unit_var-lib-nfs-rpc_pipefs_mount_state_activating": 0, + "unit_var-lib-nfs-rpc_pipefs_mount_state_active": 0, + "unit_var-lib-nfs-rpc_pipefs_mount_state_deactivating": 0, + "unit_var-lib-nfs-rpc_pipefs_mount_state_failed": 0, + "unit_var-lib-nfs-rpc_pipefs_mount_state_inactive": 1, + }, + }, + "success on systemd v230+ on collecting only 'service' unit type": { + prepare: func() *SystemdUnits { + systemd := New() + systemd.Include = []string{"*.service"} + systemd.client = prepareOKClient(230) + return systemd + }, + wantCollected: map[string]int64{ + "unit_systemd-ask-password-wall_service_state_activating": 0, + "unit_systemd-ask-password-wall_service_state_active": 0, + "unit_systemd-ask-password-wall_service_state_deactivating": 0, + "unit_systemd-ask-password-wall_service_state_failed": 0, + "unit_systemd-ask-password-wall_service_state_inactive": 1, + "unit_systemd-fsck-root_service_state_activating": 0, + "unit_systemd-fsck-root_service_state_active": 0, + "unit_systemd-fsck-root_service_state_deactivating": 0, + "unit_systemd-fsck-root_service_state_failed": 0, + "unit_systemd-fsck-root_service_state_inactive": 1, + "unit_user-runtime-dir@1000_service_state_activating": 0, + "unit_user-runtime-dir@1000_service_state_active": 1, + "unit_user-runtime-dir@1000_service_state_deactivating": 0, + "unit_user-runtime-dir@1000_service_state_failed": 0, + "unit_user-runtime-dir@1000_service_state_inactive": 0, + "unit_user@1000_service_state_activating": 0, + "unit_user@1000_service_state_active": 1, + "unit_user@1000_service_state_deactivating": 0, + "unit_user@1000_service_state_failed": 0, + "unit_user@1000_service_state_inactive": 0, + }, + }, + "success on systemd v230- on collecting only 'service' unit type": { + prepare: func() *SystemdUnits { + systemd := New() + systemd.Include = []string{"*.service"} + systemd.client = prepareOKClient(220) + return systemd + }, + wantCollected: map[string]int64{ + "unit_systemd-ask-password-wall_service_state_activating": 0, + "unit_systemd-ask-password-wall_service_state_active": 0, + "unit_systemd-ask-password-wall_service_state_deactivating": 0, + "unit_systemd-ask-password-wall_service_state_failed": 0, + "unit_systemd-ask-password-wall_service_state_inactive": 1, + "unit_systemd-fsck-root_service_state_activating": 0, + "unit_systemd-fsck-root_service_state_active": 0, + "unit_systemd-fsck-root_service_state_deactivating": 0, + "unit_systemd-fsck-root_service_state_failed": 0, + "unit_systemd-fsck-root_service_state_inactive": 1, + "unit_user-runtime-dir@1000_service_state_activating": 0, + "unit_user-runtime-dir@1000_service_state_active": 1, + "unit_user-runtime-dir@1000_service_state_deactivating": 0, + "unit_user-runtime-dir@1000_service_state_failed": 0, + "unit_user-runtime-dir@1000_service_state_inactive": 0, + "unit_user@1000_service_state_activating": 0, + "unit_user@1000_service_state_active": 1, + "unit_user@1000_service_state_deactivating": 0, + "unit_user@1000_service_state_failed": 0, + "unit_user@1000_service_state_inactive": 0, + }, + }, + "fails when all unites are filtered": { + prepare: func() *SystemdUnits { + systemd := New() + systemd.Include = []string{"*.not_exists"} + systemd.client = prepareOKClient(230) + return systemd + }, + wantCollected: nil, + }, + "fails on error on connect": { + prepare: func() *SystemdUnits { + systemd := New() + systemd.client = prepareClientErrOnConnect() + return systemd + }, + wantCollected: nil, + }, + "fails on error on get manager property": { + prepare: func() *SystemdUnits { + systemd := New() + systemd.client = prepareClientErrOnGetManagerProperty() + return systemd + }, + wantCollected: nil, + }, + "fails on error on list units": { + prepare: func() *SystemdUnits { + systemd := New() + systemd.client = prepareClientErrOnListUnits() + return systemd + }, + wantCollected: nil, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + systemd := test.prepare() + require.True(t, systemd.Init()) + + var collected map[string]int64 + + for i := 0; i < 10; i++ { + collected = systemd.Collect() + } + + assert.Equal(t, test.wantCollected, collected) + if len(test.wantCollected) > 0 { + ensureCollectedHasAllChartsDimsVarsIDs(t, systemd, collected) + } + }) + } +} + +func TestSystemdUnits_connectionReuse(t *testing.T) { + systemd := New() + systemd.Include = []string{"*"} + client := prepareOKClient(230) + systemd.client = client + require.True(t, systemd.Init()) + + var collected map[string]int64 + for i := 0; i < 10; i++ { + col |