helper_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cmdline
  15. import (
  16. "testing"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/apis/compute"
  19. )
  20. func TestFetchServerConfigsByJSON(t *testing.T) {
  21. type args struct {
  22. obj jsonutils.JSONObject
  23. }
  24. tests := []struct {
  25. name string
  26. args args
  27. want *compute.ServerConfigs
  28. wantErr bool
  29. }{
  30. {
  31. name: "all configs",
  32. args: args{jsonutils.Marshal(map[string]string{
  33. "disk.0": "1g:centos",
  34. "net.0": "192.168.222.3:inf0",
  35. "net.1": "[random]",
  36. "schedtag.0": "ssd:require",
  37. "schedtag.1": "container:exclude",
  38. "isolated_device.0": "vendor=NVIDIA:GeForce GTX 1050 Ti",
  39. "baremetal_disk_config.0": "raid0:[1,2]",
  40. })},
  41. want: &compute.ServerConfigs{
  42. Disks: []*compute.DiskConfig{
  43. {
  44. Index: 0,
  45. SizeMb: 1024,
  46. ImageId: "centos",
  47. },
  48. },
  49. Networks: []*compute.NetworkConfig{
  50. {
  51. Network: "inf0",
  52. Address: "192.168.222.3",
  53. },
  54. {
  55. Index: 1,
  56. Exit: false,
  57. },
  58. },
  59. Schedtags: []*compute.SchedtagConfig{
  60. {
  61. Id: "ssd",
  62. Strategy: "require",
  63. },
  64. {
  65. Id: "container",
  66. Strategy: "exclude",
  67. },
  68. },
  69. IsolatedDevices: []*compute.IsolatedDeviceConfig{
  70. {
  71. Vendor: "NVIDIA",
  72. Model: "GeForce GTX 1050 Ti",
  73. },
  74. },
  75. BaremetalDiskConfigs: []*compute.BaremetalDiskConfig{
  76. {
  77. Type: "hybrid",
  78. Conf: "raid0",
  79. Range: []int64{1, 2},
  80. },
  81. },
  82. },
  83. wantErr: false,
  84. },
  85. }
  86. for _, tt := range tests {
  87. t.Run(tt.name, func(t *testing.T) {
  88. got, err := FetchServerConfigsByJSON(tt.args.obj)
  89. if (err != nil) != tt.wantErr {
  90. t.Errorf("wantErr %v, error = %v", tt.wantErr, err)
  91. return
  92. }
  93. gotJ := jsonutils.Marshal(got)
  94. wantJ := jsonutils.Marshal(tt.want)
  95. if !gotJ.Equals(wantJ) {
  96. t.Errorf("got:\n%s\nwant:\n%s", gotJ, wantJ)
  97. }
  98. })
  99. }
  100. }