spec_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2018 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 v1
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. "k8s.io/apimachinery/pkg/util/yaml"
  20. )
  21. func TestClusterSpecMarshal(t *testing.T) {
  22. specYaml := []byte(`
  23. dataDirHostPath: /var/lib/rook
  24. mon:
  25. count: 5
  26. allowMultiplePerNode: false
  27. network:
  28. hostNetwork: true
  29. storage:
  30. useAllNodes: false
  31. useAllDevices: false
  32. deviceFilter: "^sd."
  33. devicePathFilter: "^/dev/disk/by-path/pci-.*"
  34. location: "region=us-west,datacenter=delmar"
  35. config:
  36. metadataDevice: "nvme01"
  37. databaseSizeMB: "1024"
  38. nodes:
  39. - name: "node2"
  40. deviceFilter: "^foo*"
  41. devicePathFilter: "^/dev/disk/by-id/.*foo.*"`)
  42. // convert the raw spec yaml into JSON
  43. rawJSON, err := yaml.ToJSON(specYaml)
  44. assert.Nil(t, err)
  45. fmt.Printf("rawJSON: %s\n", string(rawJSON))
  46. // unmarshal the JSON into a strongly typed storage spec object
  47. var clusterSpec ClusterSpec
  48. err = json.Unmarshal(rawJSON, &clusterSpec)
  49. assert.Nil(t, err)
  50. // the unmarshalled storage spec should equal the expected spec below
  51. useAllDevices := false
  52. expectedSpec := ClusterSpec{
  53. Mon: MonSpec{
  54. Count: 5,
  55. AllowMultiplePerNode: false,
  56. },
  57. DataDirHostPath: "/var/lib/rook",
  58. Network: NetworkSpec{
  59. HostNetwork: true,
  60. },
  61. Storage: StorageScopeSpec{
  62. UseAllNodes: false,
  63. Selection: Selection{
  64. UseAllDevices: &useAllDevices,
  65. DeviceFilter: "^sd.",
  66. DevicePathFilter: "^/dev/disk/by-path/pci-.*",
  67. },
  68. Config: map[string]string{
  69. "metadataDevice": "nvme01",
  70. "databaseSizeMB": "1024",
  71. },
  72. Nodes: []Node{
  73. {
  74. Name: "node2",
  75. Selection: Selection{
  76. DeviceFilter: "^foo*",
  77. DevicePathFilter: "^/dev/disk/by-id/.*foo.*",
  78. },
  79. },
  80. },
  81. },
  82. }
  83. assert.Equal(t, expectedSpec, clusterSpec)
  84. }