config_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "context"
  16. "path/filepath"
  17. "strings"
  18. "testing"
  19. "github.com/rook/rook/pkg/operator/k8sutil"
  20. "github.com/rook/rook/pkg/operator/test"
  21. v1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "github.com/go-ini/ini"
  24. "github.com/rook/rook/pkg/clusterd"
  25. cephver "github.com/rook/rook/pkg/operator/ceph/version"
  26. "github.com/stretchr/testify/assert"
  27. )
  28. func TestCreateDefaultCephConfig(t *testing.T) {
  29. clusterInfo := &ClusterInfo{
  30. FSID: "id",
  31. MonitorSecret: "monsecret",
  32. Namespace: "foo-cluster",
  33. Monitors: map[string]*MonInfo{
  34. "node0": {Name: "mon0", Endpoint: "10.0.0.1:6789"},
  35. "node1": {Name: "mon1", Endpoint: "10.0.0.2:6789"},
  36. },
  37. CephVersion: cephver.Quincy,
  38. }
  39. // start with INFO level logging
  40. context := &clusterd.Context{}
  41. cephConfig, err := CreateDefaultCephConfig(context, clusterInfo)
  42. if err != nil {
  43. t.Fatalf("failed to create default ceph config. %+v", err)
  44. }
  45. verifyConfig(t, cephConfig, clusterInfo, 0)
  46. cephConfig, err = CreateDefaultCephConfig(context, clusterInfo)
  47. if err != nil {
  48. t.Fatalf("failed to create default ceph config. %+v", err)
  49. }
  50. verifyConfig(t, cephConfig, clusterInfo, 10)
  51. }
  52. func TestGenerateConfigFile(t *testing.T) {
  53. ctx := context.TODO()
  54. // set up a temporary config directory that will be cleaned up after test
  55. configDir := t.TempDir()
  56. // create mocked cluster context and info
  57. clientset := test.New(t, 3)
  58. context := &clusterd.Context{
  59. ConfigDir: configDir,
  60. Clientset: clientset,
  61. }
  62. ns := "foo-cluster"
  63. data := make(map[string]string, 1)
  64. data["config"] = "[global]\n bluestore_min_alloc_size_hdd = 4096"
  65. cm := &v1.ConfigMap{
  66. ObjectMeta: metav1.ObjectMeta{
  67. Name: k8sutil.ConfigOverrideName,
  68. Namespace: ns,
  69. },
  70. Data: data,
  71. }
  72. _, err := clientset.CoreV1().ConfigMaps(ns).Create(ctx, cm, metav1.CreateOptions{})
  73. assert.NoError(t, err)
  74. clusterInfo := &ClusterInfo{
  75. FSID: "myfsid",
  76. MonitorSecret: "monsecret",
  77. Namespace: ns,
  78. Monitors: map[string]*MonInfo{
  79. "node0": {Name: "mon0", Endpoint: "10.0.0.1:6789"},
  80. },
  81. CephVersion: cephver.Quincy,
  82. CephCred: CephCred{Username: "admin", Secret: "mysecret"},
  83. Context: ctx,
  84. }
  85. isInitialized := clusterInfo.IsInitialized()
  86. assert.NoError(t, isInitialized)
  87. // generate the config file to disk now
  88. configFilePath, err := generateConfigFile(context, clusterInfo, configDir, filepath.Join(configDir, "mykeyring"), nil, nil)
  89. assert.Nil(t, err)
  90. assert.Equal(t, filepath.Join(configDir, "foo-cluster.config"), configFilePath)
  91. // verify some of the contents of written config file by loading it from disk
  92. actualConf, err := ini.Load(configFilePath)
  93. assert.Nil(t, err)
  94. verifyConfigValue(t, actualConf, "global", "fsid", clusterInfo.FSID)
  95. verifyConfigValue(t, actualConf, "global", "bluestore_min_alloc_size_hdd", "4096")
  96. }
  97. func verifyConfig(t *testing.T, cephConfig *CephConfig, cluster *ClusterInfo, loggingLevel int) {
  98. monMembers := make([]string, len(cluster.Monitors))
  99. i := 0
  100. for _, expectedMon := range cluster.Monitors {
  101. contained := false
  102. monMembers[i] = expectedMon.Name
  103. for _, actualMon := range strings.Split(cephConfig.MonMembers, " ") {
  104. if expectedMon.Name == actualMon {
  105. contained = true
  106. break
  107. }
  108. }
  109. assert.True(t, contained)
  110. }
  111. // Testing mon_host
  112. expectedMons := "[v2:10.0.0.1:3300,v1:10.0.0.1:6789],[v2:10.0.0.2:3300,v1:10.0.0.2:6789]"
  113. for _, expectedMon := range strings.Split(expectedMons, ",") {
  114. contained := false
  115. for _, actualMon := range strings.Split(cephConfig.MonHost, ",") {
  116. if expectedMon == actualMon {
  117. contained = true
  118. break
  119. }
  120. }
  121. assert.True(t, contained, "expectedMons: %+v, actualMons: %+v", expectedMons, cephConfig.MonHost)
  122. }
  123. }
  124. func verifyConfigValue(t *testing.T, actualConf *ini.File, section, key, expectedVal string) {
  125. s, err := actualConf.GetSection(section)
  126. if !assert.Nil(t, err) {
  127. return
  128. }
  129. k := s.Key(key)
  130. if !assert.NotNil(t, k) {
  131. return
  132. }
  133. actualVal := k.Value()
  134. assert.Equal(t, expectedVal, actualVal)
  135. }