kubeconfig_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package kubernetes_test
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/porter-dev/porter/internal/kubernetes"
  6. "github.com/porter-dev/porter/internal/models"
  7. "gopkg.in/yaml.v2"
  8. )
  9. type KubeConfigTest struct {
  10. msg string
  11. raw []byte
  12. allowedClusters []string
  13. expected []models.ClusterConfig
  14. }
  15. var MissingFieldsTest = []KubeConfigTest{
  16. KubeConfigTest{
  17. msg: "no fields at all",
  18. raw: []byte(""),
  19. allowedClusters: []string{},
  20. expected: []models.ClusterConfig{},
  21. },
  22. KubeConfigTest{
  23. msg: "no contexts to join",
  24. raw: []byte(noContexts),
  25. allowedClusters: []string{},
  26. expected: []models.ClusterConfig{},
  27. },
  28. KubeConfigTest{
  29. msg: "no clusters to join",
  30. raw: []byte(noClusters),
  31. allowedClusters: []string{},
  32. expected: []models.ClusterConfig{},
  33. },
  34. KubeConfigTest{
  35. msg: "no users to join",
  36. raw: []byte(noUsers),
  37. allowedClusters: []string{},
  38. expected: []models.ClusterConfig{},
  39. },
  40. KubeConfigTest{
  41. msg: "no cluster contexts to join",
  42. raw: []byte(noContextClusters),
  43. allowedClusters: []string{},
  44. expected: []models.ClusterConfig{},
  45. },
  46. KubeConfigTest{
  47. msg: "no cluster users to join",
  48. raw: []byte(noContextUsers),
  49. allowedClusters: []string{},
  50. expected: []models.ClusterConfig{},
  51. },
  52. }
  53. func TestToClusterConfigsMissingFields(t *testing.T) {
  54. for _, c := range MissingFieldsTest {
  55. // take raw and decode
  56. conf := kubernetes.KubeConfig{}
  57. err := yaml.Unmarshal(c.raw, &conf)
  58. if err != nil {
  59. t.Errorf("Testing: %s, Error: %v\n", c.msg, err)
  60. }
  61. res := conf.ToAllowedClusterConfigs(c.allowedClusters)
  62. isEqual := reflect.DeepEqual(c.expected, res)
  63. if !isEqual {
  64. t.Errorf("Testing: %s, Expected: %v, Got: %v\n", c.msg, c.expected, res)
  65. }
  66. }
  67. }
  68. var NoAllowedClustersTests = []KubeConfigTest{
  69. KubeConfigTest{
  70. msg: "basic test",
  71. raw: []byte(basic),
  72. allowedClusters: []string{},
  73. expected: []models.ClusterConfig{},
  74. },
  75. }
  76. func TestToClusterConfigsNoAllowedClusters(t *testing.T) {
  77. for _, c := range NoAllowedClustersTests {
  78. // take raw and decode
  79. conf := kubernetes.KubeConfig{}
  80. err := yaml.Unmarshal(c.raw, &conf)
  81. if err != nil {
  82. t.Errorf("Testing: %s, Error: %v\n", c.msg, err)
  83. }
  84. res := conf.ToAllowedClusterConfigs(c.allowedClusters)
  85. isEqual := reflect.DeepEqual(c.expected, res)
  86. if !isEqual {
  87. t.Errorf("Testing: %s, Expected: %v, Got: %v\n", c.msg, c.expected, res)
  88. }
  89. }
  90. }
  91. var BasicClustersTests = []KubeConfigTest{
  92. KubeConfigTest{
  93. msg: "basic test",
  94. raw: []byte(basic),
  95. allowedClusters: []string{"cluster-test"},
  96. expected: []models.ClusterConfig{
  97. models.ClusterConfig{
  98. Name: "cluster-test",
  99. Server: "https://localhost",
  100. Context: "context-test",
  101. User: "test-admin",
  102. },
  103. },
  104. },
  105. }
  106. func TestToClusterConfigsBasic(t *testing.T) {
  107. for _, c := range BasicClustersTests {
  108. // take raw and decode
  109. conf := kubernetes.KubeConfig{}
  110. err := yaml.Unmarshal(c.raw, &conf)
  111. if err != nil {
  112. t.Errorf("Testing: %s, Error: %v\n", c.msg, err)
  113. }
  114. res := conf.ToAllowedClusterConfigs(c.allowedClusters)
  115. isEqual := reflect.DeepEqual(c.expected, res)
  116. if !isEqual {
  117. t.Errorf("Testing: %s, Expected: %v, Got: %v\n", c.msg, c.expected, res)
  118. }
  119. }
  120. }
  121. const noContexts string = `
  122. apiVersion: v1
  123. kind: Config
  124. preferences: {}
  125. clusters:
  126. - cluster:
  127. server: https://localhost
  128. name: porter-test-1
  129. current-context: default
  130. users:
  131. - name: test-admin
  132. user:
  133. `
  134. const noClusters string = `
  135. apiVersion: v1
  136. kind: Config
  137. preferences: {}
  138. current-context: default
  139. contexts:
  140. - context:
  141. cluster: porter-test-1
  142. user: test-admin
  143. name: context-test
  144. users:
  145. - name: test-admin
  146. user:
  147. `
  148. const noUsers string = `
  149. apiVersion: v1
  150. kind: Config
  151. preferences: {}
  152. current-context: default
  153. clusters:
  154. - cluster:
  155. server: https://localhost
  156. name: porter-test-1
  157. contexts:
  158. - context:
  159. cluster: porter-test-1
  160. user: test-admin
  161. name: context-test
  162. `
  163. const noContextClusters string = `
  164. apiVersion: v1
  165. kind: Config
  166. preferences: {}
  167. current-context: default
  168. clusters:
  169. - cluster:
  170. server: https://localhost
  171. name: porter-test-1
  172. contexts:
  173. - context:
  174. # cluster: porter-test-1
  175. user: test-admin
  176. name: context-test
  177. users:
  178. - name: test-admin
  179. user:
  180. `
  181. const noContextUsers string = `
  182. apiVersion: v1
  183. kind: Config
  184. preferences: {}
  185. current-context: default
  186. clusters:
  187. - cluster:
  188. server: https://localhost
  189. name: porter-test-1
  190. contexts:
  191. - context:
  192. cluster: porter-test-1
  193. # user: test-admin
  194. name: context-test
  195. users:
  196. - name: test-admin
  197. user:
  198. `
  199. const basic string = `
  200. apiVersion: v1
  201. kind: Config
  202. preferences: {}
  203. current-context: default
  204. clusters:
  205. - cluster:
  206. server: https://localhost
  207. name: cluster-test
  208. contexts:
  209. - context:
  210. cluster: cluster-test
  211. user: test-admin
  212. name: context-test
  213. users:
  214. - name: test-admin
  215. `