status_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "encoding/json"
  16. "regexp"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. const (
  21. // this JSON was generated from `ceph status -f json`, using Ceph Luminous 12.1.3
  22. CephStatusResponseRaw = `{"fsid":"613975f3-3025-4802-9de1-a2280b950e75","health":{"checks":{"OSD_DOWN":{"severity":"HEALTH_WARN","summary":{"message":"1 osds down"}},"OSD_HOST_DOWN":{"severity":"HEALTH_WARN","summary":{"message":"1 host (1 osds) down"}},"PG_AVAILABILITY":{"severity":"HEALTH_WARN","summary":{"message":"Reduced data availability: 101 pgs stale"}},"POOL_APP_NOT_ENABLED":{"severity":"HEALTH_WARN","summary":{"message":"application not enabled on 1 pool(s)"}}},"status":"HEALTH_WARN","overall_status":"HEALTH_WARN"},"election_epoch":12,"quorum":[0,1,2],"quorum_names":["rook-ceph-mon0","rook-ceph-mon2","rook-ceph-mon1"],"monmap":{"epoch":3,"fsid":"613975f3-3025-4802-9de1-a2280b950e75","modified":"2017-08-11 20:13:02.075679","created":"2017-08-11 20:12:35.314510","features":{"persistent":["kraken","luminous"],"optional":[]},"mons":[{"rank":0,"name":"rook-ceph-mon0","addr":"10.3.0.45:6789/0","public_addr":"10.3.0.45:6789/0"},{"rank":1,"name":"rook-ceph-mon2","addr":"10.3.0.249:6789/0","public_addr":"10.3.0.249:6789/0"},{"rank":2,"name":"rook-ceph-mon1","addr":"10.3.0.252:6789/0","public_addr":"10.3.0.252:6789/0"}]},"osdmap":{"epoch":17,"num_osds":2,"num_up_osds":1,"num_in_osds":2,"full":false,"nearfull":true,"num_remapped_pgs":0},"pgmap":{"pgs_by_state":[{"state_name":"stale+active+clean","count":101},{"state_name":"active+clean","count":99}],"num_pgs":200,"num_pools":2,"num_objects":243,"data_bytes":976793635,"bytes_used":13611479040,"bytes_avail":19825307648,"bytes_total":33436786688},"fsmap":{"epoch":1,"by_rank":[]},"mgrmap":{"epoch":3,"active_gid":14111,"active_name":"rook-ceph-mgr0","active_addr":"10.2.73.6:6800/9","available":true,"standbys":[],"modules":["restful","status"],"available_modules":["dashboard","prometheus","restful","status","zabbix"]},"servicemap":{"epoch":1,"modified":"0.000000","services":{}}}`
  23. )
  24. var (
  25. // this JSON was generated from `ceph status -f json`, using Ceph Nautilus 14.2.1
  26. // It was chopped to only show what the tests are looking for
  27. statusFakeRaw = []byte(`{
  28. "fsmap": {
  29. "epoch": 13,
  30. "id": 1,
  31. "up": 1,
  32. "in": 1,
  33. "max": 1,
  34. "by_rank": [
  35. {
  36. "filesystem_id": 1,
  37. "rank": 0,
  38. "name": "myfs-b",
  39. "status": "up:active",
  40. "gid": 14716
  41. }
  42. ],
  43. "up:standby": 1
  44. }
  45. }`)
  46. )
  47. func TestStatusMarshal(t *testing.T) {
  48. var status CephStatus
  49. err := json.Unmarshal([]byte(CephStatusResponseRaw), &status)
  50. assert.Nil(t, err)
  51. // verify some health fields
  52. assert.Equal(t, "HEALTH_WARN", status.Health.Status)
  53. assert.Equal(t, 4, len(status.Health.Checks))
  54. assert.Equal(t, "HEALTH_WARN", status.Health.Checks["OSD_DOWN"].Severity)
  55. assert.Equal(t, "1 osds down", status.Health.Checks["OSD_DOWN"].Summary.Message)
  56. assert.Equal(t, "HEALTH_WARN", status.Health.Checks["OSD_HOST_DOWN"].Severity)
  57. assert.Equal(t, "1 host (1 osds) down", status.Health.Checks["OSD_HOST_DOWN"].Summary.Message)
  58. // verify some Mon map fields
  59. assert.Equal(t, 3, status.MonMap.Epoch)
  60. assert.Equal(t, "rook-ceph-mon0", status.MonMap.Mons[0].Name)
  61. assert.Equal(t, "10.3.0.45:6789/0", status.MonMap.Mons[0].Address)
  62. // verify some OSD map fields
  63. assert.Equal(t, 2, status.OsdMap.NumOsd)
  64. assert.Equal(t, 1, status.OsdMap.NumUpOsd)
  65. assert.False(t, status.OsdMap.Full)
  66. assert.True(t, status.OsdMap.NearFull)
  67. // verify some PG map fields
  68. assert.Equal(t, 200, status.PgMap.NumPgs)
  69. assert.Equal(t, uint64(13611479040), status.PgMap.UsedBytes)
  70. assert.Equal(t, 101, status.PgMap.PgsByState[0].Count)
  71. assert.Equal(t, "stale+active+clean", status.PgMap.PgsByState[0].StateName)
  72. }
  73. func TestIsClusterClean(t *testing.T) {
  74. status := CephStatus{
  75. PgMap: PgMap{
  76. PgsByState: []PgStateEntry{
  77. {StateName: activeClean, Count: 3},
  78. },
  79. NumPgs: 10,
  80. },
  81. }
  82. // not a clean cluster with PGs not adding up
  83. _, clean := isClusterClean(status, defaultPgHealthyRegexCompiled)
  84. assert.False(t, clean)
  85. // clean cluster
  86. status.PgMap.PgsByState = append(status.PgMap.PgsByState,
  87. PgStateEntry{StateName: activeCleanScrubbing, Count: 5})
  88. status.PgMap.PgsByState = append(status.PgMap.PgsByState,
  89. PgStateEntry{StateName: activeCleanScrubbingDeep, Count: 2})
  90. _, clean = isClusterClean(status, defaultPgHealthyRegexCompiled)
  91. assert.True(t, clean)
  92. // not a clean cluster with PGs in a bad state
  93. status.PgMap.PgsByState[0].StateName = "notclean"
  94. _, clean = isClusterClean(status, defaultPgHealthyRegexCompiled)
  95. assert.False(t, clean)
  96. // clean cluster if the regex is satisfied
  97. re := regexp.MustCompile("notclean")
  98. _, clean = isClusterClean(status, re)
  99. assert.False(t, clean)
  100. }
  101. func TestGetMDSRank(t *testing.T) {
  102. var statusFake CephStatus
  103. err := json.Unmarshal(statusFakeRaw, &statusFake)
  104. assert.NoError(t, err)
  105. mdsRankFake, err := getMDSRank(statusFake, "myfs-b")
  106. assert.Nil(t, err)
  107. assert.Equal(t, 0, mdsRankFake)
  108. }
  109. func TestIsCephHealthy(t *testing.T) {
  110. var statusFake CephStatus
  111. err := json.Unmarshal(statusFakeRaw, &statusFake)
  112. assert.NoError(t, err)
  113. statusFake.Health.Status = "HEALTH_WARN"
  114. s := isCephHealthy(statusFake)
  115. assert.True(t, s)
  116. statusFake.Health.Status = "HEALTH_OK"
  117. s = isCephHealthy(statusFake)
  118. assert.True(t, s)
  119. statusFake.Health.Status = "HEALTH_ERR"
  120. s = isCephHealthy(statusFake)
  121. assert.False(t, s)
  122. }