github.com/netdata/go.d.plugin@v0.58.1/modules/mongodb/mongodb_test.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package mongo
     4  
     5  import (
     6  	"encoding/json"
     7  	"errors"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/netdata/go.d.plugin/pkg/matcher"
    16  )
    17  
    18  var (
    19  	dataV6MongodServerStatus, _ = os.ReadFile("testdata/v6.0.3/mongod-serverStatus.json")
    20  	dataV6MongosServerStatus, _ = os.ReadFile("testdata/v6.0.3/mongos-serverStatus.json")
    21  	dataV6DbStats, _            = os.ReadFile("testdata/v6.0.3/dbStats.json")
    22  	dataV6ReplSetGetStatus, _   = os.ReadFile("testdata/v6.0.3/replSetGetStatus.json")
    23  )
    24  
    25  func Test_testDataIsValid(t *testing.T) {
    26  	for name, data := range map[string][]byte{
    27  		"dataV6MongodServerStatus": dataV6MongodServerStatus,
    28  		"dataV6MongosServerStatus": dataV6MongosServerStatus,
    29  		"dataV6DbStats":            dataV6DbStats,
    30  		"dataV6ReplSetGetStatus":   dataV6ReplSetGetStatus,
    31  	} {
    32  		require.NotNilf(t, data, name)
    33  	}
    34  }
    35  
    36  func TestMongo_Init(t *testing.T) {
    37  	tests := map[string]struct {
    38  		config   Config
    39  		wantFail bool
    40  	}{
    41  		"success on default config": {
    42  			wantFail: false,
    43  			config:   New().Config,
    44  		},
    45  		"fails on unset 'address'": {
    46  			wantFail: true,
    47  			config: Config{
    48  				URI: "",
    49  			},
    50  		},
    51  		"fails on invalid database selector": {
    52  			wantFail: true,
    53  			config: Config{
    54  				URI: "mongodb://localhost:27017",
    55  				Databases: matcher.SimpleExpr{
    56  					Includes: []string{"!@#"},
    57  				},
    58  			},
    59  		},
    60  	}
    61  
    62  	for name, test := range tests {
    63  		t.Run(name, func(t *testing.T) {
    64  			mongo := New()
    65  			mongo.Config = test.config
    66  
    67  			if test.wantFail {
    68  				assert.False(t, mongo.Init())
    69  			} else {
    70  				assert.True(t, mongo.Init())
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestMongo_Charts(t *testing.T) {
    77  	assert.NotNil(t, New().Charts())
    78  }
    79  
    80  func TestMongo_Cleanup(t *testing.T) {
    81  	tests := map[string]struct {
    82  		prepare   func(t *testing.T) *Mongo
    83  		wantClose bool
    84  	}{
    85  		"client not initialized": {
    86  			wantClose: false,
    87  			prepare: func(t *testing.T) *Mongo {
    88  				return New()
    89  			},
    90  		},
    91  		"client initialized": {
    92  			wantClose: true,
    93  			prepare: func(t *testing.T) *Mongo {
    94  				mongo := New()
    95  				mongo.conn = caseMongod()
    96  				_ = mongo.conn.initClient("", 0)
    97  
    98  				return mongo
    99  			},
   100  		},
   101  	}
   102  
   103  	for name, test := range tests {
   104  		t.Run(name, func(t *testing.T) {
   105  			mongo := test.prepare(t)
   106  
   107  			require.NotPanics(t, mongo.Cleanup)
   108  			if test.wantClose {
   109  				mock, ok := mongo.conn.(*mockMongoClient)
   110  				require.True(t, ok)
   111  				assert.True(t, mock.closeCalled)
   112  			}
   113  		})
   114  	}
   115  }
   116  
   117  func TestMongo_Check(t *testing.T) {
   118  	tests := map[string]struct {
   119  		prepare  func() *mockMongoClient
   120  		wantFail bool
   121  	}{
   122  		"success on Mongod (v6)": {
   123  			wantFail: false,
   124  			prepare:  caseMongod,
   125  		},
   126  		"success on Mongod Replicas Set(v6)": {
   127  			wantFail: false,
   128  			prepare:  caseMongodReplicaSet,
   129  		},
   130  		"success on Mongos (v6)": {
   131  			wantFail: false,
   132  			prepare:  caseMongos,
   133  		},
   134  	}
   135  
   136  	for name, test := range tests {
   137  		t.Run(name, func(t *testing.T) {
   138  			mongo := prepareMongo()
   139  			defer mongo.Cleanup()
   140  			mongo.conn = test.prepare()
   141  
   142  			require.True(t, mongo.Init())
   143  
   144  			if test.wantFail {
   145  				assert.False(t, mongo.Check())
   146  			} else {
   147  				assert.True(t, mongo.Check())
   148  			}
   149  		})
   150  	}
   151  }
   152  
   153  func TestMongo_Collect(t *testing.T) {
   154  	tests := map[string]struct {
   155  		prepare       func() *mockMongoClient
   156  		wantCollected map[string]int64
   157  	}{
   158  		"success on Mongod (v6)": {
   159  			prepare: caseMongod,
   160  			wantCollected: map[string]int64{
   161  				"asserts_msg":                                              0,
   162  				"asserts_regular":                                          0,
   163  				"asserts_rollovers":                                        0,
   164  				"asserts_tripwire":                                         0,
   165  				"asserts_user":                                             246,
   166  				"asserts_warning":                                          0,
   167  				"connections_active":                                       7,
   168  				"connections_available":                                    838841,
   169  				"connections_awaiting_topology_changes":                    5,
   170  				"connections_current":                                      19,
   171  				"connections_exhaust_hello":                                2,
   172  				"connections_exhaust_is_master":                            1,
   173  				"connections_threaded":                                     19,
   174  				"connections_total_created":                                77,
   175  				"database_admin_collections":                               3,
   176  				"database_admin_data_size":                                 796,
   177  				"database_admin_documents":                                 5,
   178  				"database_admin_index_size":                                81920,
   179  				"database_admin_indexes":                                   4,
   180  				"database_admin_storage_size":                              61440,
   181  				"database_admin_views":                                     0,
   182  				"database_config_collections":                              3,
   183  				"database_config_data_size":                                796,
   184  				"database_config_documents":                                5,
   185  				"database_config_index_size":                               81920,
   186  				"database_config_indexes":                                  4,
   187  				"database_config_storage_size":                             61440,
   188  				"database_config_views":                                    0,
   189  				"database_local_collections":                               3,
   190  				"database_local_data_size":                                 796,
   191  				"database_local_documents":                                 5,
   192  				"database_local_index_size":                                81920,
   193  				"database_local_indexes":                                   4,
   194  				"database_local_storage_size":                              61440,
   195  				"database_local_views":                                     0,
   196  				"extra_info_page_faults":                                   0,
   197  				"global_lock_active_clients_readers":                       0,
   198  				"global_lock_active_clients_writers":                       0,
   199  				"global_lock_current_queue_readers":                        0,
   200  				"global_lock_current_queue_writers":                        0,
   201  				"locks_collection_acquire_exclusive":                       6,
   202  				"locks_collection_acquire_intent_exclusive":                172523,
   203  				"locks_collection_acquire_intent_shared":                   336370,
   204  				"locks_collection_acquire_shared":                          0,
   205  				"locks_database_acquire_exclusive":                         3,
   206  				"locks_database_acquire_intent_exclusive":                  172539,
   207  				"locks_database_acquire_intent_shared":                     50971,
   208  				"locks_database_acquire_shared":                            0,
   209  				"locks_global_acquire_exclusive":                           6,
   210  				"locks_global_acquire_intent_exclusive":                    174228,
   211  				"locks_global_acquire_intent_shared":                       437905,
   212  				"locks_global_acquire_shared":                              0,
   213  				"locks_mutex_acquire_exclusive":                            0,
   214  				"locks_mutex_acquire_intent_exclusive":                     0,
   215  				"locks_mutex_acquire_intent_shared":                        245077,
   216  				"locks_mutex_acquire_shared":                               0,
   217  				"locks_oplog_acquire_exclusive":                            0,
   218  				"locks_oplog_acquire_intent_exclusive":                     1,
   219  				"locks_oplog_acquire_intent_shared":                        16788,
   220  				"locks_oplog_acquire_shared":                               0,
   221  				"memory_resident":                                          193986560,
   222  				"memory_virtual":                                           3023044608,
   223  				"metrics_cursor_lifespan_greater_than_or_equal_10_minutes": 0,
   224  				"metrics_cursor_lifespan_less_than_10_minutes":             0,
   225  				"metrics_cursor_lifespan_less_than_15_seconds":             0,
   226  				"metrics_cursor_lifespan_less_than_1_minute":               0,
   227  				"metrics_cursor_lifespan_less_than_1_second":               0,
   228  				"metrics_cursor_lifespan_less_than_30_seconds":             0,
   229  				"metrics_cursor_lifespan_less_than_5_seconds":              0,
   230  				"metrics_cursor_open_no_timeout":                           0,
   231  				"metrics_cursor_open_total":                                1,
   232  				"metrics_cursor_timed_out":                                 0,
   233  				"metrics_cursor_total_opened":                              1,
   234  				"metrics_document_deleted":                                 7,
   235  				"metrics_document_inserted":                                0,
   236  				"metrics_document_returned":                                1699,
   237  				"metrics_document_updated":                                 52,
   238  				"metrics_query_executor_scanned":                           61,
   239  				"metrics_query_executor_scanned_objects":                   1760,
   240  				"network_bytes_in":                                         38851356,
   241  				"network_bytes_out":                                        706335836,
   242  				"network_requests":                                         130530,
   243  				"network_slow_dns_operations":                              0,
   244  				"network_slow_ssl_operations":                              0,
   245  				"operations_command":                                       125531,
   246  				"operations_delete":                                        7,
   247  				"operations_getmore":                                       5110,
   248  				"operations_insert":                                        0,
   249  				"operations_latencies_commands_latency":                    46432082,
   250  				"operations_latencies_commands_ops":                        125412,
   251  				"operations_latencies_reads_latency":                       1009868,
   252  				"operations_latencies_reads_ops":                           5111,
   253  				"operations_latencies_writes_latency":                      0,
   254  				"operations_latencies_writes_ops":                          0,
   255  				"operations_query":                                         76,
   256  				"operations_update":                                        59,
   257  				"tcmalloc_aggressive_memory_decommit":                      0,
   258  				"tcmalloc_central_cache_free_bytes":                        406680,
   259  				"tcmalloc_current_total_thread_cache_bytes":                2490832,
   260  				"tcmalloc_generic_current_allocated_bytes":                 109050648,
   261  				"tcmalloc_generic_heap_size":                               127213568,
   262  				"tcmalloc_max_total_thread_cache_bytes":                    1073741824,
   263  				"tcmalloc_pageheap_commit_count":                           376,
   264  				"tcmalloc_pageheap_committed_bytes":                        127086592,
   265  				"tcmalloc_pageheap_decommit_count":                         122,
   266  				"tcmalloc_pageheap_free_bytes":                             13959168,
   267  				"tcmalloc_pageheap_reserve_count":                          60,
   268  				"tcmalloc_pageheap_scavenge_bytes":                         0,
   269  				"tcmalloc_pageheap_total_commit_bytes":                     229060608,
   270  				"tcmalloc_pageheap_total_decommit_bytes":                   101974016,
   271  				"tcmalloc_pageheap_total_reserve_bytes":                    127213568,
   272  				"tcmalloc_pageheap_unmapped_bytes":                         126976,
   273  				"tcmalloc_spinlock_total_delay_ns":                         33426251,
   274  				"tcmalloc_thread_cache_free_bytes":                         2490832,
   275  				"tcmalloc_total_free_bytes":                                4076776,
   276  				"tcmalloc_transfer_cache_free_bytes":                       1179264,
   277  				"txn_active":                                               0,
   278  				"txn_inactive":                                             0,
   279  				"txn_open":                                                 0,
   280  				"txn_prepared":                                             0,
   281  				"txn_total_aborted":                                        0,
   282  				"txn_total_committed":                                      0,
   283  				"txn_total_prepared":                                       0,
   284  				"txn_total_started":                                        0,
   285  				"wiredtiger_cache_currently_in_cache_bytes":                814375,
   286  				"wiredtiger_cache_maximum_configured_bytes":                7854882816,
   287  				"wiredtiger_cache_modified_evicted_pages":                  0,
   288  				"wiredtiger_cache_read_into_cache_pages":                   108,
   289  				"wiredtiger_cache_tracked_dirty_in_the_cache_bytes":        456446,
   290  				"wiredtiger_cache_unmodified_evicted_pages":                0,
   291  				"wiredtiger_cache_written_from_cache_pages":                3177,
   292  				"wiredtiger_concurrent_txn_read_available":                 128,
   293  				"wiredtiger_concurrent_txn_read_out":                       0,
   294  				"wiredtiger_concurrent_txn_write_available":                128,
   295  				"wiredtiger_concurrent_txn_write_out":                      0,
   296  			},
   297  		},
   298  		"success on Mongod Replica Set (v6)": {
   299  			prepare: caseMongodReplicaSet,
   300  			wantCollected: map[string]int64{
   301  				"asserts_msg":                                                0,
   302  				"asserts_regular":                                            0,
   303  				"asserts_rollovers":                                          0,
   304  				"asserts_tripwire":                                           0,
   305  				"asserts_user":                                               246,
   306  				"asserts_warning":                                            0,
   307  				"connections_active":                                         7,
   308  				"connections_available":                                      838841,
   309  				"connections_awaiting_topology_changes":                      5,
   310  				"connections_current":                                        19,
   311  				"connections_exhaust_hello":                                  2,
   312  				"connections_exhaust_is_master":                              1,
   313  				"connections_threaded":                                       19,
   314  				"connections_total_created":                                  77,
   315  				"database_admin_collections":                                 3,
   316  				"database_admin_data_size":                                   796,
   317  				"database_admin_documents":                                   5,
   318  				"database_admin_index_size":                                  81920,
   319  				"database_admin_indexes":                                     4,
   320  				"database_admin_storage_size":                                61440,
   321  				"database_admin_views":                                       0,
   322  				"database_config_collections":                                3,
   323  				"database_config_data_size":                                  796,
   324  				"database_config_documents":                                  5,
   325  				"database_config_index_size":                                 81920,
   326  				"database_config_indexes":                                    4,
   327  				"database_config_storage_size":                               61440,
   328  				"database_config_views":                                      0,
   329  				"database_local_collections":                                 3,
   330  				"database_local_data_size":                                   796,
   331  				"database_local_documents":                                   5,
   332  				"database_local_index_size":                                  81920,
   333  				"database_local_indexes":                                     4,
   334  				"database_local_storage_size":                                61440,
   335  				"database_local_views":                                       0,
   336  				"extra_info_page_faults":                                     0,
   337  				"global_lock_active_clients_readers":                         0,
   338  				"global_lock_active_clients_writers":                         0,
   339  				"global_lock_current_queue_readers":                          0,
   340  				"global_lock_current_queue_writers":                          0,
   341  				"locks_collection_acquire_exclusive":                         6,
   342  				"locks_collection_acquire_intent_exclusive":                  172523,
   343  				"locks_collection_acquire_intent_shared":                     336370,
   344  				"locks_collection_acquire_shared":                            0,
   345  				"locks_database_acquire_exclusive":                           3,
   346  				"locks_database_acquire_intent_exclusive":                    172539,
   347  				"locks_database_acquire_intent_shared":                       50971,
   348  				"locks_database_acquire_shared":                              0,
   349  				"locks_global_acquire_exclusive":                             6,
   350  				"locks_global_acquire_intent_exclusive":                      174228,
   351  				"locks_global_acquire_intent_shared":                         437905,
   352  				"locks_global_acquire_shared":                                0,
   353  				"locks_mutex_acquire_exclusive":                              0,
   354  				"locks_mutex_acquire_intent_exclusive":                       0,
   355  				"locks_mutex_acquire_intent_shared":                          245077,
   356  				"locks_mutex_acquire_shared":                                 0,
   357  				"locks_oplog_acquire_exclusive":                              0,
   358  				"locks_oplog_acquire_intent_exclusive":                       1,
   359  				"locks_oplog_acquire_intent_shared":                          16788,
   360  				"locks_oplog_acquire_shared":                                 0,
   361  				"memory_resident":                                            193986560,
   362  				"memory_virtual":                                             3023044608,
   363  				"metrics_cursor_lifespan_greater_than_or_equal_10_minutes":   0,
   364  				"metrics_cursor_lifespan_less_than_10_minutes":               0,
   365  				"metrics_cursor_lifespan_less_than_15_seconds":               0,
   366  				"metrics_cursor_lifespan_less_than_1_minute":                 0,
   367  				"metrics_cursor_lifespan_less_than_1_second":                 0,
   368  				"metrics_cursor_lifespan_less_than_30_seconds":               0,
   369  				"metrics_cursor_lifespan_less_than_5_seconds":                0,
   370  				"metrics_cursor_open_no_timeout":                             0,
   371  				"metrics_cursor_open_total":                                  1,
   372  				"metrics_cursor_timed_out":                                   0,
   373  				"metrics_cursor_total_opened":                                1,
   374  				"metrics_document_deleted":                                   7,
   375  				"metrics_document_inserted":                                  0,
   376  				"metrics_document_returned":                                  1699,
   377  				"metrics_document_updated":                                   52,
   378  				"metrics_query_executor_scanned":                             61,
   379  				"metrics_query_executor_scanned_objects":                     1760,
   380  				"network_bytes_in":                                           38851356,
   381  				"network_bytes_out":                                          706335836,
   382  				"network_requests":                                           130530,
   383  				"network_slow_dns_operations":                                0,
   384  				"network_slow_ssl_operations":                                0,
   385  				"operations_command":                                         125531,
   386  				"operations_delete":                                          7,
   387  				"operations_getmore":                                         5110,
   388  				"operations_insert":                                          0,
   389  				"operations_latencies_commands_latency":                      46432082,
   390  				"operations_latencies_commands_ops":                          125412,
   391  				"operations_latencies_reads_latency":                         1009868,
   392  				"operations_latencies_reads_ops":                             5111,
   393  				"operations_latencies_writes_latency":                        0,
   394  				"operations_latencies_writes_ops":                            0,
   395  				"operations_query":                                           76,
   396  				"operations_update":                                          59,
   397  				"repl_set_member_mongodb-primary:27017_health_status_down":   0,
   398  				"repl_set_member_mongodb-primary:27017_health_status_up":     1,
   399  				"repl_set_member_mongodb-primary:27017_replication_lag":      4572,
   400  				"repl_set_member_mongodb-primary:27017_state_arbiter":        0,
   401  				"repl_set_member_mongodb-primary:27017_state_down":           0,
   402  				"repl_set_member_mongodb-primary:27017_state_primary":        1,
   403  				"repl_set_member_mongodb-primary:27017_state_recovering":     0,
   404  				"repl_set_member_mongodb-primary:27017_state_removed":        0,
   405  				"repl_set_member_mongodb-primary:27017_state_rollback":       0,
   406  				"repl_set_member_mongodb-primary:27017_state_secondary":      0,
   407  				"repl_set_member_mongodb-primary:27017_state_startup":        0,
   408  				"repl_set_member_mongodb-primary:27017_state_startup2":       0,
   409  				"repl_set_member_mongodb-primary:27017_state_unknown":        0,
   410  				"repl_set_member_mongodb-secondary:27017_health_status_down": 0,
   411  				"repl_set_member_mongodb-secondary:27017_health_status_up":   1,
   412  				"repl_set_member_mongodb-secondary:27017_heartbeat_latency":  1359,
   413  				"repl_set_member_mongodb-secondary:27017_ping_rtt":           0,
   414  				"repl_set_member_mongodb-secondary:27017_replication_lag":    4572,
   415  				"repl_set_member_mongodb-secondary:27017_state_arbiter":      0,
   416  				"repl_set_member_mongodb-secondary:27017_state_down":         0,
   417  				"repl_set_member_mongodb-secondary:27017_state_primary":      0,
   418  				"repl_set_member_mongodb-secondary:27017_state_recovering":   0,
   419  				"repl_set_member_mongodb-secondary:27017_state_removed":      0,
   420  				"repl_set_member_mongodb-secondary:27017_state_rollback":     0,
   421  				"repl_set_member_mongodb-secondary:27017_state_secondary":    1,
   422  				"repl_set_member_mongodb-secondary:27017_state_startup":      0,
   423  				"repl_set_member_mongodb-secondary:27017_state_startup2":     0,
   424  				"repl_set_member_mongodb-secondary:27017_state_unknown":      0,
   425  				"repl_set_member_mongodb-secondary:27017_uptime":             192370,
   426  				"tcmalloc_aggressive_memory_decommit":                        0,
   427  				"tcmalloc_central_cache_free_bytes":                          406680,
   428  				"tcmalloc_current_total_thread_cache_bytes":                  2490832,
   429  				"tcmalloc_generic_current_allocated_bytes":                   109050648,
   430  				"tcmalloc_generic_heap_size":                                 127213568,
   431  				"tcmalloc_max_total_thread_cache_bytes":                      1073741824,
   432  				"tcmalloc_pageheap_commit_count":                             376,
   433  				"tcmalloc_pageheap_committed_bytes":                          127086592,
   434  				"tcmalloc_pageheap_decommit_count":                           122,
   435  				"tcmalloc_pageheap_free_bytes":                               13959168,
   436  				"tcmalloc_pageheap_reserve_count":                            60,
   437  				"tcmalloc_pageheap_scavenge_bytes":                           0,
   438  				"tcmalloc_pageheap_total_commit_bytes":                       229060608,
   439  				"tcmalloc_pageheap_total_decommit_bytes":                     101974016,
   440  				"tcmalloc_pageheap_total_reserve_bytes":                      127213568,
   441  				"tcmalloc_pageheap_unmapped_bytes":                           126976,
   442  				"tcmalloc_spinlock_total_delay_ns":                           33426251,
   443  				"tcmalloc_thread_cache_free_bytes":                           2490832,
   444  				"tcmalloc_total_free_bytes":                                  4076776,
   445  				"tcmalloc_transfer_cache_free_bytes":                         1179264,
   446  				"txn_active":                                                 0,
   447  				"txn_inactive":                                               0,
   448  				"txn_open":                                                   0,
   449  				"txn_prepared":                                               0,
   450  				"txn_total_aborted":                                          0,
   451  				"txn_total_committed":                                        0,
   452  				"txn_total_prepared":                                         0,
   453  				"txn_total_started":                                          0,
   454  				"wiredtiger_cache_currently_in_cache_bytes":                  814375,
   455  				"wiredtiger_cache_maximum_configured_bytes":                  7854882816,
   456  				"wiredtiger_cache_modified_evicted_pages":                    0,
   457  				"wiredtiger_cache_read_into_cache_pages":                     108,
   458  				"wiredtiger_cache_tracked_dirty_in_the_cache_bytes":          456446,
   459  				"wiredtiger_cache_unmodified_evicted_pages":                  0,
   460  				"wiredtiger_cache_written_from_cache_pages":                  3177,
   461  				"wiredtiger_concurrent_txn_read_available":                   128,
   462  				"wiredtiger_concurrent_txn_read_out":                         0,
   463  				"wiredtiger_concurrent_txn_write_available":                  128,
   464  				"wiredtiger_concurrent_txn_write_out":                        0,
   465  			},
   466  		},
   467  		"success on Mongos (v6)": {
   468  			prepare: caseMongos,
   469  			wantCollected: map[string]int64{
   470  				"asserts_msg":                                                    0,
   471  				"asserts_regular":                                                0,
   472  				"asserts_rollovers":                                              0,
   473  				"asserts_tripwire":                                               0,
   474  				"asserts_user":                                                   352,
   475  				"asserts_warning":                                                0,
   476  				"connections_active":                                             5,
   477  				"connections_available":                                          838842,
   478  				"connections_awaiting_topology_changes":                          4,
   479  				"connections_current":                                            18,
   480  				"connections_exhaust_hello":                                      3,
   481  				"connections_exhaust_is_master":                                  0,
   482  				"connections_threaded":                                           18,
   483  				"connections_total_created":                                      89,
   484  				"database_admin_collections":                                     3,
   485  				"database_admin_data_size":                                       796,
   486  				"database_admin_documents":                                       5,
   487  				"database_admin_index_size":                                      81920,
   488  				"database_admin_indexes":                                         4,
   489  				"database_admin_storage_size":                                    61440,
   490  				"database_admin_views":                                           0,
   491  				"database_config_collections":                                    3,
   492  				"database_config_data_size":                                      796,
   493  				"database_config_documents":                                      5,
   494  				"database_config_index_size":                                     81920,
   495  				"database_config_indexes":                                        4,
   496  				"database_config_storage_size":                                   61440,
   497  				"database_config_views":                                          0,
   498  				"database_local_collections":                                     3,
   499  				"database_local_data_size":                                       796,
   500  				"database_local_documents":                                       5,
   501  				"database_local_index_size":                                      81920,
   502  				"database_local_indexes":                                         4,
   503  				"database_local_storage_size":                                    61440,
   504  				"database_local_views":                                           0,
   505  				"extra_info_page_faults":                                         526,
   506  				"memory_resident":                                                84934656,
   507  				"memory_virtual":                                                 2596274176,
   508  				"metrics_document_deleted":                                       0,
   509  				"metrics_document_inserted":                                      0,
   510  				"metrics_document_returned":                                      0,
   511  				"metrics_document_updated":                                       0,
   512  				"metrics_query_executor_scanned":                                 0,
   513  				"metrics_query_executor_scanned_objects":                         0,
   514  				"network_bytes_in":                                               57943348,
   515  				"network_bytes_out":                                              247343709,
   516  				"network_requests":                                               227310,
   517  				"network_slow_dns_operations":                                    0,
   518  				"network_slow_ssl_operations":                                    0,
   519  				"operations_command":                                             227283,
   520  				"operations_delete":                                              0,
   521  				"operations_getmore":                                             0,
   522  				"operations_insert":                                              0,
   523  				"operations_query":                                               10,
   524  				"operations_update":                                              0,
   525  				"shard_collections_partitioned":                                  1,
   526  				"shard_collections_unpartitioned":                                1,
   527  				"shard_databases_partitioned":                                    1,
   528  				"shard_databases_unpartitioned":                                  1,
   529  				"shard_id_shard0_chunks":                                         1,
   530  				"shard_id_shard1_chunks":                                         1,
   531  				"shard_nodes_aware":                                              1,
   532  				"shard_nodes_unaware":                                            1,
   533  				"tcmalloc_aggressive_memory_decommit":                            0,
   534  				"tcmalloc_central_cache_free_bytes":                              736960,
   535  				"tcmalloc_current_total_thread_cache_bytes":                      1638104,
   536  				"tcmalloc_generic_current_allocated_bytes":                       13519784,
   537  				"tcmalloc_generic_heap_size":                                     24576000,
   538  				"tcmalloc_max_total_thread_cache_bytes":                          1042284544,
   539  				"tcmalloc_pageheap_commit_count":                                 480,
   540  				"tcmalloc_pageheap_committed_bytes":                              24518656,
   541  				"tcmalloc_pageheap_decommit_count":                               127,
   542  				"tcmalloc_pageheap_free_bytes":                                   5697536,
   543  				"tcmalloc_pageheap_reserve_count":                                15,
   544  				"tcmalloc_pageheap_scavenge_bytes":                               0,
   545  				"tcmalloc_pageheap_total_commit_bytes":                           84799488,
   546  				"tcmalloc_pageheap_total_decommit_bytes":                         60280832,
   547  				"tcmalloc_pageheap_total_reserve_bytes":                          24576000,
   548  				"tcmalloc_pageheap_unmapped_bytes":                               57344,
   549  				"tcmalloc_spinlock_total_delay_ns":                               96785212,
   550  				"tcmalloc_thread_cache_free_bytes":                               1638104,
   551  				"tcmalloc_total_free_bytes":                                      5301336,
   552  				"tcmalloc_transfer_cache_free_bytes":                             2926272,
   553  				"txn_active":                                                     0,
   554  				"txn_commit_types_no_shards_initiated":                           0,
   555  				"txn_commit_types_no_shards_successful":                          0,
   556  				"txn_commit_types_no_shards_successful_duration_micros":          0,
   557  				"txn_commit_types_no_shards_unsuccessful":                        0,
   558  				"txn_commit_types_read_only_initiated":                           0,
   559  				"txn_commit_types_read_only_successful":                          0,
   560  				"txn_commit_types_read_only_successful_duration_micros":          0,
   561  				"txn_commit_types_read_only_unsuccessful":                        0,
   562  				"txn_commit_types_recover_with_token_initiated":                  0,
   563  				"txn_commit_types_recover_with_token_successful":                 0,
   564  				"txn_commit_types_recover_with_token_successful_duration_micros": 0,
   565  				"txn_commit_types_recover_with_token_unsuccessful":               0,
   566  				"txn_commit_types_single_shard_initiated":                        0,
   567  				"txn_commit_types_single_shard_successful":                       0,
   568  				"txn_commit_types_single_shard_successful_duration_micros":       0,
   569  				"txn_commit_types_single_shard_unsuccessful":                     0,
   570  				"txn_commit_types_single_write_shard_initiated":                  0,
   571  				"txn_commit_types_single_write_shard_successful":                 0,
   572  				"txn_commit_types_single_write_shard_successful_duration_micros": 0,
   573  				"txn_commit_types_single_write_shard_unsuccessful":               0,
   574  				"txn_commit_types_two_phase_commit_initiated":                    0,
   575  				"txn_commit_types_two_phase_commit_successful":                   0,
   576  				"txn_commit_types_two_phase_commit_successful_duration_micros":   0,
   577  				"txn_commit_types_two_phase_commit_unsuccessful":                 0,
   578  				"txn_inactive":                                                   0,
   579  				"txn_open":                                                       0,
   580  				"txn_total_aborted":                                              0,
   581  				"txn_total_committed":                                            0,
   582  				"txn_total_started":                                              0,
   583  			},
   584  		},
   585  	}
   586  
   587  	for name, test := range tests {
   588  		t.Run(name, func(t *testing.T) {
   589  			mongo := prepareMongo()
   590  			defer mongo.Cleanup()
   591  			mongo.conn = test.prepare()
   592  
   593  			require.True(t, mongo.Init())
   594  
   595  			mx := mongo.Collect()
   596  
   597  			assert.Equal(t, test.wantCollected, mx)
   598  		})
   599  	}
   600  }
   601  
   602  func prepareMongo() *Mongo {
   603  	m := New()
   604  	m.Databases = matcher.SimpleExpr{Includes: []string{"* *"}}
   605  	return m
   606  }
   607  
   608  func caseMongodReplicaSet() *mockMongoClient {
   609  	return &mockMongoClient{replicaSet: true}
   610  }
   611  
   612  func caseMongod() *mockMongoClient {
   613  	return &mockMongoClient{}
   614  }
   615  
   616  func caseMongos() *mockMongoClient {
   617  	return &mockMongoClient{mongos: true}
   618  }
   619  
   620  type mockMongoClient struct {
   621  	replicaSet                        bool
   622  	mongos                            bool
   623  	errOnServerStatus                 bool
   624  	errOnListDatabaseNames            bool
   625  	errOnDbStats                      bool
   626  	errOnReplSetGetStatus             bool
   627  	errOnShardNodes                   bool
   628  	errOnShardDatabasesPartitioning   bool
   629  	errOnShardCollectionsPartitioning bool
   630  	errOnShardChunks                  bool
   631  	errOnInitClient                   bool
   632  	clientInited                      bool
   633  	closeCalled                       bool
   634  }
   635  
   636  func (m *mockMongoClient) serverStatus() (*documentServerStatus, error) {
   637  	if !m.clientInited {
   638  		return nil, errors.New("mock.serverStatus() error: mongo client not inited")
   639  	}
   640  	if m.errOnServerStatus {
   641  		return nil, errors.New("mock.serverStatus() error")
   642  	}
   643  
   644  	data := dataV6MongodServerStatus
   645  	if m.mongos {
   646  		data = dataV6MongosServerStatus
   647  	}
   648  
   649  	var s documentServerStatus
   650  	if err := json.Unmarshal(data, &s); err != nil {
   651  		return nil, err
   652  	}
   653  
   654  	return &s, nil
   655  }
   656  
   657  func (m *mockMongoClient) listDatabaseNames() ([]string, error) {
   658  	if !m.clientInited {
   659  		return nil, errors.New("mock.listDatabaseNames() error: mongo client not inited")
   660  	}
   661  	if m.errOnListDatabaseNames {
   662  		return nil, errors.New("mock.listDatabaseNames() error")
   663  	}
   664  	return []string{"admin", "config", "local"}, nil
   665  }
   666  
   667  func (m *mockMongoClient) dbStats(_ string) (*documentDBStats, error) {
   668  	if !m.clientInited {
   669  		return nil, errors.New("mock.dbStats() error: mongo client not inited")
   670  	}
   671  	if m.errOnDbStats {
   672  		return nil, errors.New("mock.dbStats() error")
   673  	}
   674  
   675  	var s documentDBStats
   676  	if err := json.Unmarshal(dataV6DbStats, &s); err != nil {
   677  		return nil, err
   678  	}
   679  
   680  	return &s, nil
   681  }
   682  
   683  func (m *mockMongoClient) isReplicaSet() bool {
   684  	return m.replicaSet
   685  }
   686  
   687  func (m *mockMongoClient) isMongos() bool {
   688  	return m.mongos
   689  }
   690  
   691  func (m *mockMongoClient) replSetGetStatus() (*documentReplSetStatus, error) {
   692  	if !m.clientInited {
   693  		return nil, errors.New("mock.replSetGetStatus() error: mongo client not inited")
   694  	}
   695  	if m.mongos {
   696  		return nil, errors.New("mock.replSetGetStatus() error: shouldn't be called for mongos")
   697  	}
   698  	if !m.replicaSet {
   699  		return nil, errors.New("mock.replSetGetStatus() error: should be called for replica set")
   700  	}
   701  	if m.errOnReplSetGetStatus {
   702  		return nil, errors.New("mock.replSetGetStatus() error")
   703  	}
   704  
   705  	var s documentReplSetStatus
   706  	if err := json.Unmarshal(dataV6ReplSetGetStatus, &s); err != nil {
   707  		return nil, err
   708  	}
   709  
   710  	return &s, nil
   711  }
   712  
   713  func (m *mockMongoClient) shardNodes() (*documentShardNodesResult, error) {
   714  	if !m.clientInited {
   715  		return nil, errors.New("mock.shardNodes() error: mongo client not inited")
   716  	}
   717  	if m.replicaSet {
   718  		return nil, errors.New("mock.replSetGetStatus() error: shouldn't be called for replica set")
   719  	}
   720  	if !m.mongos {
   721  		return nil, errors.New("mock.shardNodes() error: should be called for mongos")
   722  	}
   723  	if m.errOnShardNodes {
   724  		return nil, errors.New("mock.shardNodes() error")
   725  	}
   726  
   727  	return &documentShardNodesResult{
   728  		ShardAware:   1,
   729  		ShardUnaware: 1,
   730  	}, nil
   731  }
   732  
   733  func (m *mockMongoClient) shardDatabasesPartitioning() (*documentPartitionedResult, error) {
   734  	if !m.clientInited {
   735  		return nil, errors.New("mock.shardDatabasesPartitioning() error: mongo client not inited")
   736  	}
   737  	if m.replicaSet {
   738  		return nil, errors.New("mock.shardDatabasesPartitioning() error: shouldn't be called for replica set")
   739  	}
   740  	if !m.mongos {
   741  		return nil, errors.New("mock.shardDatabasesPartitioning() error: should be called for mongos")
   742  	}
   743  	if m.errOnShardDatabasesPartitioning {
   744  		return nil, errors.New("mock.shardDatabasesPartitioning() error")
   745  	}
   746  
   747  	return &documentPartitionedResult{
   748  		Partitioned:   1,
   749  		UnPartitioned: 1,
   750  	}, nil
   751  }
   752  
   753  func (m *mockMongoClient) shardCollectionsPartitioning() (*documentPartitionedResult, error) {
   754  	if !m.clientInited {
   755  		return nil, errors.New("mock.shardCollectionsPartitioning() error: mongo client not inited")
   756  	}
   757  	if m.replicaSet {
   758  		return nil, errors.New("mock.shardCollectionsPartitioning() error: shouldn't be called for replica set")
   759  	}
   760  	if !m.mongos {
   761  		return nil, errors.New("mock.shardCollectionsPartitioning() error: should be called for mongos")
   762  	}
   763  	if m.errOnShardCollectionsPartitioning {
   764  		return nil, errors.New("mock.shardCollectionsPartitioning() error")
   765  	}
   766  
   767  	return &documentPartitionedResult{
   768  		Partitioned:   1,
   769  		UnPartitioned: 1,
   770  	}, nil
   771  }
   772  
   773  func (m *mockMongoClient) shardChunks() (map[string]int64, error) {
   774  	if !m.clientInited {
   775  		return nil, errors.New("mock.shardChunks() error: mongo client not inited")
   776  	}
   777  	if m.replicaSet {
   778  		return nil, errors.New("mock.shardChunks() error: shouldn't be called for replica set")
   779  	}
   780  	if !m.mongos {
   781  		return nil, errors.New("mock.shardChunks() error: should be called for mongos")
   782  	}
   783  	if m.errOnShardChunks {
   784  		return nil, errors.New("mock.shardChunks() error")
   785  	}
   786  
   787  	return map[string]int64{
   788  		"shard0": 1,
   789  		"shard1": 1,
   790  	}, nil
   791  }
   792  
   793  func (m *mockMongoClient) initClient(_ string, _ time.Duration) error {
   794  	if m.errOnInitClient {
   795  		return errors.New("mock.initClient() error")
   796  	}
   797  	m.clientInited = true
   798  	return nil
   799  }
   800  
   801  func (m *mockMongoClient) close() error {
   802  	if m.clientInited {
   803  		m.closeCalled = true
   804  	}
   805  	return nil
   806  }