thirdparty_controller_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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 master
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/api"
  17. expapi "k8s.io/kubernetes/pkg/apis/extensions"
  18. "k8s.io/kubernetes/pkg/registry/thirdpartyresourcedata"
  19. "k8s.io/kubernetes/pkg/util/sets"
  20. )
  21. type FakeAPIInterface struct {
  22. removed []string
  23. installed []*expapi.ThirdPartyResource
  24. apis []string
  25. t *testing.T
  26. }
  27. func (f *FakeAPIInterface) RemoveThirdPartyResource(path string) error {
  28. f.removed = append(f.removed, path)
  29. return nil
  30. }
  31. func (f *FakeAPIInterface) InstallThirdPartyResource(rsrc *expapi.ThirdPartyResource) error {
  32. f.installed = append(f.installed, rsrc)
  33. _, group, _ := thirdpartyresourcedata.ExtractApiGroupAndKind(rsrc)
  34. f.apis = append(f.apis, makeThirdPartyPath(group))
  35. return nil
  36. }
  37. func (f *FakeAPIInterface) HasThirdPartyResource(rsrc *expapi.ThirdPartyResource) (bool, error) {
  38. if f.apis == nil {
  39. return false, nil
  40. }
  41. _, group, _ := thirdpartyresourcedata.ExtractApiGroupAndKind(rsrc)
  42. path := makeThirdPartyPath(group)
  43. for _, api := range f.apis {
  44. if api == path {
  45. return true, nil
  46. }
  47. }
  48. return false, nil
  49. }
  50. func (f *FakeAPIInterface) ListThirdPartyResources() []string {
  51. return f.apis
  52. }
  53. func TestSyncAPIs(t *testing.T) {
  54. resourcesNamed := func(names ...string) []expapi.ThirdPartyResource {
  55. result := []expapi.ThirdPartyResource{}
  56. for _, name := range names {
  57. result = append(result, expapi.ThirdPartyResource{ObjectMeta: api.ObjectMeta{Name: name}})
  58. }
  59. return result
  60. }
  61. tests := []struct {
  62. list *expapi.ThirdPartyResourceList
  63. apis []string
  64. expectedInstalled []string
  65. expectedRemoved []string
  66. name string
  67. }{
  68. {
  69. list: &expapi.ThirdPartyResourceList{
  70. Items: resourcesNamed("foo.example.com"),
  71. },
  72. expectedInstalled: []string{"foo.example.com"},
  73. name: "simple add",
  74. },
  75. {
  76. list: &expapi.ThirdPartyResourceList{
  77. Items: resourcesNamed("foo.example.com"),
  78. },
  79. apis: []string{
  80. "/apis/example.com",
  81. "/apis/example.com/v1",
  82. },
  83. name: "does nothing",
  84. },
  85. {
  86. list: &expapi.ThirdPartyResourceList{
  87. Items: resourcesNamed("foo.example.com"),
  88. },
  89. apis: []string{
  90. "/apis/example.com",
  91. "/apis/example.com/v1",
  92. "/apis/example.co",
  93. "/apis/example.co/v1",
  94. },
  95. name: "deletes substring API",
  96. expectedRemoved: []string{
  97. "/apis/example.co",
  98. "/apis/example.co/v1",
  99. },
  100. },
  101. {
  102. list: &expapi.ThirdPartyResourceList{
  103. Items: resourcesNamed("foo.example.com", "foo.company.com"),
  104. },
  105. apis: []string{
  106. "/apis/company.com",
  107. "/apis/company.com/v1",
  108. },
  109. expectedInstalled: []string{"foo.example.com"},
  110. name: "adds with existing",
  111. },
  112. {
  113. list: &expapi.ThirdPartyResourceList{
  114. Items: resourcesNamed("foo.example.com"),
  115. },
  116. apis: []string{
  117. "/apis/company.com",
  118. "/apis/company.com/v1",
  119. },
  120. expectedInstalled: []string{"foo.example.com"},
  121. expectedRemoved: []string{"/apis/company.com", "/apis/company.com/v1"},
  122. name: "removes with existing",
  123. },
  124. }
  125. for _, test := range tests {
  126. fake := FakeAPIInterface{
  127. apis: test.apis,
  128. t: t,
  129. }
  130. cntrl := ThirdPartyController{master: &fake}
  131. if err := cntrl.syncResourceList(test.list); err != nil {
  132. t.Errorf("[%s] unexpected error: %v", test.name, err)
  133. }
  134. if len(test.expectedInstalled) != len(fake.installed) {
  135. t.Errorf("[%s] unexpected installed APIs: %d, expected %d (%#v)", test.name, len(fake.installed), len(test.expectedInstalled), fake.installed[0])
  136. continue
  137. } else {
  138. names := sets.String{}
  139. for ix := range fake.installed {
  140. names.Insert(fake.installed[ix].Name)
  141. }
  142. for _, name := range test.expectedInstalled {
  143. if !names.Has(name) {
  144. t.Errorf("[%s] missing installed API: %s", test.name, name)
  145. }
  146. }
  147. }
  148. if len(test.expectedRemoved) != len(fake.removed) {
  149. t.Errorf("[%s] unexpected installed APIs: %d, expected %d", test.name, len(fake.removed), len(test.expectedRemoved))
  150. continue
  151. } else {
  152. names := sets.String{}
  153. names.Insert(fake.removed...)
  154. for _, name := range test.expectedRemoved {
  155. if !names.Has(name) {
  156. t.Errorf("[%s] missing removed API: %s (%s)", test.name, name, names)
  157. }
  158. }
  159. }
  160. }
  161. }