metadata_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 db
  15. import (
  16. "context"
  17. "reflect"
  18. "testing"
  19. "github.com/stretchr/testify/mock"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. )
  22. func TestIsMetadataKeySystemAdmin(t *testing.T) {
  23. tests := []struct {
  24. name string
  25. key string
  26. want bool
  27. }{
  28. {
  29. name: "__sys_key is system admin key",
  30. key: "__sys_key",
  31. want: true,
  32. },
  33. {
  34. name: "__sys is not system admin key",
  35. key: "__sys",
  36. want: false,
  37. },
  38. }
  39. for _, tt := range tests {
  40. t.Run(tt.name, func(t *testing.T) {
  41. if got := isMetadataKeySystemAdmin(tt.key); got != tt.want {
  42. t.Errorf("IsMetadataKeySystemAdmin() = %v, want %v", got, tt.want)
  43. }
  44. })
  45. }
  46. }
  47. func TestIsMetadataKeySysTag(t *testing.T) {
  48. tests := []struct {
  49. name string
  50. key string
  51. want bool
  52. }{
  53. {
  54. name: "__qemu_version is sys tag key",
  55. key: "__qemu_version",
  56. want: true,
  57. },
  58. {
  59. name: "_sys is not sys tag key",
  60. key: "_sys",
  61. want: false,
  62. },
  63. }
  64. for _, tt := range tests {
  65. t.Run(tt.name, func(t *testing.T) {
  66. if got := isMetadataKeySysTag(tt.key); got != tt.want {
  67. t.Errorf("IsMetadataKeySysTag() = %v, want %v", got, tt.want)
  68. }
  69. })
  70. }
  71. }
  72. func TestIsMetadataKeyVisiable(t *testing.T) {
  73. tests := []struct {
  74. name string
  75. key string
  76. want bool
  77. }{
  78. {
  79. name: "__qemu_version should not visiable",
  80. key: "__qemu_version",
  81. want: false,
  82. },
  83. {
  84. name: "__sys_key should not visiable",
  85. key: "__sys_key",
  86. want: false,
  87. },
  88. {
  89. name: "key1 should visiable",
  90. key: "key1",
  91. want: true,
  92. },
  93. }
  94. for _, tt := range tests {
  95. t.Run(tt.name, func(t *testing.T) {
  96. if got := IsMetadataKeyVisible(tt.key); got != tt.want {
  97. t.Errorf("IsMetadataKeyVisiable() = %v, want %v", got, tt.want)
  98. }
  99. })
  100. }
  101. }
  102. type MockMetadataModelManager struct {
  103. mock.Mock
  104. SStandaloneResourceBaseManager
  105. }
  106. type MockMetadataModel struct {
  107. mock.Mock
  108. SStandaloneResourceBase
  109. }
  110. func (m *MockMetadataModel) GetAllMetadata(ctx context.Context, userCred mcclient.TokenCredential) (map[string]string, error) {
  111. args := m.Called(userCred)
  112. return args.Get(0).(map[string]string), args.Error(1)
  113. }
  114. func (m *MockMetadataModelManager) GetMetadataHiddenKeys() []string {
  115. args := m.Called()
  116. return args.Get(0).([]string)
  117. }
  118. func TestGetVisiableMetadata(t *testing.T) {
  119. testManager := new(MockMetadataModelManager)
  120. testObj := new(MockMetadataModel)
  121. testObj.On("GetAllMetadata", nil).Return(
  122. map[string]string{
  123. "__os_profile__": "{\"disk_driver\":\"scsi\",\"fs_format\":\"ext4\",\"hypervisor\":\"kvm\",\"net_driver\":\"virtio\",\"os_type\":\"Linux\"}",
  124. "login_account": "root",
  125. "os_arch": "x86_64",
  126. "os_distribution": "CentOS",
  127. },
  128. nil,
  129. )
  130. testManager.On("GetMetadataHiddenKeys").Return([]string{"login_account"})
  131. testObj.SetModelManager(testManager, testObj)
  132. tests := []struct {
  133. name string
  134. model IStandaloneModel
  135. want map[string]string
  136. wantErr bool
  137. }{
  138. {
  139. name: "exclude sys tag and customize hide keys",
  140. model: testObj,
  141. want: map[string]string{
  142. "os_arch": "x86_64",
  143. "os_distribution": "CentOS",
  144. },
  145. wantErr: false,
  146. },
  147. }
  148. for _, tt := range tests {
  149. t.Run(tt.name, func(t *testing.T) {
  150. got, err := GetVisibleMetadata(nil, tt.model, nil)
  151. if (err != nil) != tt.wantErr {
  152. t.Errorf("GetVisiableMetadata() error = %v, wantErr %v", err, tt.wantErr)
  153. return
  154. }
  155. if !reflect.DeepEqual(got, tt.want) {
  156. t.Errorf("GetVisiableMetadata() = %v, want %v", got, tt.want)
  157. }
  158. })
  159. }
  160. }