loader_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 clientcmd
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path"
  19. "path/filepath"
  20. "reflect"
  21. "strings"
  22. "testing"
  23. "github.com/ghodss/yaml"
  24. clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
  25. clientcmdlatest "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest"
  26. "k8s.io/kubernetes/pkg/runtime"
  27. )
  28. var (
  29. testConfigAlfa = clientcmdapi.Config{
  30. AuthInfos: map[string]*clientcmdapi.AuthInfo{
  31. "red-user": {Token: "red-token"}},
  32. Clusters: map[string]*clientcmdapi.Cluster{
  33. "cow-cluster": {Server: "http://cow.org:8080"}},
  34. Contexts: map[string]*clientcmdapi.Context{
  35. "federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}},
  36. }
  37. testConfigBravo = clientcmdapi.Config{
  38. AuthInfos: map[string]*clientcmdapi.AuthInfo{
  39. "black-user": {Token: "black-token"}},
  40. Clusters: map[string]*clientcmdapi.Cluster{
  41. "pig-cluster": {Server: "http://pig.org:8080"}},
  42. Contexts: map[string]*clientcmdapi.Context{
  43. "queen-anne-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}},
  44. }
  45. testConfigCharlie = clientcmdapi.Config{
  46. AuthInfos: map[string]*clientcmdapi.AuthInfo{
  47. "green-user": {Token: "green-token"}},
  48. Clusters: map[string]*clientcmdapi.Cluster{
  49. "horse-cluster": {Server: "http://horse.org:8080"}},
  50. Contexts: map[string]*clientcmdapi.Context{
  51. "shaker-context": {AuthInfo: "green-user", Cluster: "horse-cluster", Namespace: "chisel-ns"}},
  52. }
  53. testConfigDelta = clientcmdapi.Config{
  54. AuthInfos: map[string]*clientcmdapi.AuthInfo{
  55. "blue-user": {Token: "blue-token"}},
  56. Clusters: map[string]*clientcmdapi.Cluster{
  57. "chicken-cluster": {Server: "http://chicken.org:8080"}},
  58. Contexts: map[string]*clientcmdapi.Context{
  59. "gothic-context": {AuthInfo: "blue-user", Cluster: "chicken-cluster", Namespace: "plane-ns"}},
  60. }
  61. testConfigConflictAlfa = clientcmdapi.Config{
  62. AuthInfos: map[string]*clientcmdapi.AuthInfo{
  63. "red-user": {Token: "a-different-red-token"},
  64. "yellow-user": {Token: "yellow-token"}},
  65. Clusters: map[string]*clientcmdapi.Cluster{
  66. "cow-cluster": {Server: "http://a-different-cow.org:8080", InsecureSkipTLSVerify: true},
  67. "donkey-cluster": {Server: "http://donkey.org:8080", InsecureSkipTLSVerify: true}},
  68. CurrentContext: "federal-context",
  69. }
  70. )
  71. func TestNonExistentCommandLineFile(t *testing.T) {
  72. loadingRules := ClientConfigLoadingRules{
  73. ExplicitPath: "bogus_file",
  74. }
  75. _, err := loadingRules.Load()
  76. if err == nil {
  77. t.Fatalf("Expected error for missing command-line file, got none")
  78. }
  79. if !strings.Contains(err.Error(), "bogus_file") {
  80. t.Fatalf("Expected error about 'bogus_file', got %s", err.Error())
  81. }
  82. }
  83. func TestToleratingMissingFiles(t *testing.T) {
  84. loadingRules := ClientConfigLoadingRules{
  85. Precedence: []string{"bogus1", "bogus2", "bogus3"},
  86. }
  87. _, err := loadingRules.Load()
  88. if err != nil {
  89. t.Fatalf("Unexpected error: %v", err)
  90. }
  91. }
  92. func TestErrorReadingFile(t *testing.T) {
  93. commandLineFile, _ := ioutil.TempFile("", "")
  94. defer os.Remove(commandLineFile.Name())
  95. if err := ioutil.WriteFile(commandLineFile.Name(), []byte("bogus value"), 0644); err != nil {
  96. t.Fatalf("Error creating tempfile: %v", err)
  97. }
  98. loadingRules := ClientConfigLoadingRules{
  99. ExplicitPath: commandLineFile.Name(),
  100. }
  101. _, err := loadingRules.Load()
  102. if err == nil {
  103. t.Fatalf("Expected error for unloadable file, got none")
  104. }
  105. if !strings.Contains(err.Error(), commandLineFile.Name()) {
  106. t.Fatalf("Expected error about '%s', got %s", commandLineFile.Name(), err.Error())
  107. }
  108. }
  109. func TestErrorReadingNonFile(t *testing.T) {
  110. tmpdir, err := ioutil.TempDir("", "")
  111. if err != nil {
  112. t.Fatalf("Couldn't create tmpdir")
  113. }
  114. defer os.Remove(tmpdir)
  115. loadingRules := ClientConfigLoadingRules{
  116. ExplicitPath: tmpdir,
  117. }
  118. _, err = loadingRules.Load()
  119. if err == nil {
  120. t.Fatalf("Expected error for non-file, got none")
  121. }
  122. if !strings.Contains(err.Error(), tmpdir) {
  123. t.Fatalf("Expected error about '%s', got %s", tmpdir, err.Error())
  124. }
  125. }
  126. func TestConflictingCurrentContext(t *testing.T) {
  127. commandLineFile, _ := ioutil.TempFile("", "")
  128. defer os.Remove(commandLineFile.Name())
  129. envVarFile, _ := ioutil.TempFile("", "")
  130. defer os.Remove(envVarFile.Name())
  131. mockCommandLineConfig := clientcmdapi.Config{
  132. CurrentContext: "any-context-value",
  133. }
  134. mockEnvVarConfig := clientcmdapi.Config{
  135. CurrentContext: "a-different-context",
  136. }
  137. WriteToFile(mockCommandLineConfig, commandLineFile.Name())
  138. WriteToFile(mockEnvVarConfig, envVarFile.Name())
  139. loadingRules := ClientConfigLoadingRules{
  140. ExplicitPath: commandLineFile.Name(),
  141. Precedence: []string{envVarFile.Name()},
  142. }
  143. mergedConfig, err := loadingRules.Load()
  144. if err != nil {
  145. t.Errorf("Unexpected error: %v", err)
  146. }
  147. if mergedConfig.CurrentContext != mockCommandLineConfig.CurrentContext {
  148. t.Errorf("expected %v, got %v", mockCommandLineConfig.CurrentContext, mergedConfig.CurrentContext)
  149. }
  150. }
  151. func TestLoadingEmptyMaps(t *testing.T) {
  152. configFile, _ := ioutil.TempFile("", "")
  153. defer os.Remove(configFile.Name())
  154. mockConfig := clientcmdapi.Config{
  155. CurrentContext: "any-context-value",
  156. }
  157. WriteToFile(mockConfig, configFile.Name())
  158. config, err := LoadFromFile(configFile.Name())
  159. if err != nil {
  160. t.Errorf("Unexpected error: %v", err)
  161. }
  162. if config.Clusters == nil {
  163. t.Error("expected config.Clusters to be non-nil")
  164. }
  165. if config.AuthInfos == nil {
  166. t.Error("expected config.AuthInfos to be non-nil")
  167. }
  168. if config.Contexts == nil {
  169. t.Error("expected config.Contexts to be non-nil")
  170. }
  171. }
  172. func TestResolveRelativePaths(t *testing.T) {
  173. pathResolutionConfig1 := clientcmdapi.Config{
  174. AuthInfos: map[string]*clientcmdapi.AuthInfo{
  175. "relative-user-1": {ClientCertificate: "relative/client/cert", ClientKey: "../relative/client/key"},
  176. "absolute-user-1": {ClientCertificate: "/absolute/client/cert", ClientKey: "/absolute/client/key"},
  177. },
  178. Clusters: map[string]*clientcmdapi.Cluster{
  179. "relative-server-1": {CertificateAuthority: "../relative/ca"},
  180. "absolute-server-1": {CertificateAuthority: "/absolute/ca"},
  181. },
  182. }
  183. pathResolutionConfig2 := clientcmdapi.Config{
  184. AuthInfos: map[string]*clientcmdapi.AuthInfo{
  185. "relative-user-2": {ClientCertificate: "relative/client/cert2", ClientKey: "../relative/client/key2"},
  186. "absolute-user-2": {ClientCertificate: "/absolute/client/cert2", ClientKey: "/absolute/client/key2"},
  187. },
  188. Clusters: map[string]*clientcmdapi.Cluster{
  189. "relative-server-2": {CertificateAuthority: "../relative/ca2"},
  190. "absolute-server-2": {CertificateAuthority: "/absolute/ca2"},
  191. },
  192. }
  193. configDir1, _ := ioutil.TempDir("", "")
  194. configFile1 := path.Join(configDir1, ".kubeconfig")
  195. configDir1, _ = filepath.Abs(configDir1)
  196. defer os.Remove(configFile1)
  197. configDir2, _ := ioutil.TempDir("", "")
  198. configDir2, _ = ioutil.TempDir(configDir2, "")
  199. configFile2 := path.Join(configDir2, ".kubeconfig")
  200. configDir2, _ = filepath.Abs(configDir2)
  201. defer os.Remove(configFile2)
  202. WriteToFile(pathResolutionConfig1, configFile1)
  203. WriteToFile(pathResolutionConfig2, configFile2)
  204. loadingRules := ClientConfigLoadingRules{
  205. Precedence: []string{configFile1, configFile2},
  206. }
  207. mergedConfig, err := loadingRules.Load()
  208. if err != nil {
  209. t.Errorf("Unexpected error: %v", err)
  210. }
  211. foundClusterCount := 0
  212. for key, cluster := range mergedConfig.Clusters {
  213. if key == "relative-server-1" {
  214. foundClusterCount++
  215. matchStringArg(path.Join(configDir1, pathResolutionConfig1.Clusters["relative-server-1"].CertificateAuthority), cluster.CertificateAuthority, t)
  216. }
  217. if key == "relative-server-2" {
  218. foundClusterCount++
  219. matchStringArg(path.Join(configDir2, pathResolutionConfig2.Clusters["relative-server-2"].CertificateAuthority), cluster.CertificateAuthority, t)
  220. }
  221. if key == "absolute-server-1" {
  222. foundClusterCount++
  223. matchStringArg(pathResolutionConfig1.Clusters["absolute-server-1"].CertificateAuthority, cluster.CertificateAuthority, t)
  224. }
  225. if key == "absolute-server-2" {
  226. foundClusterCount++
  227. matchStringArg(pathResolutionConfig2.Clusters["absolute-server-2"].CertificateAuthority, cluster.CertificateAuthority, t)
  228. }
  229. }
  230. if foundClusterCount != 4 {
  231. t.Errorf("Expected 4 clusters, found %v: %v", foundClusterCount, mergedConfig.Clusters)
  232. }
  233. foundAuthInfoCount := 0
  234. for key, authInfo := range mergedConfig.AuthInfos {
  235. if key == "relative-user-1" {
  236. foundAuthInfoCount++
  237. matchStringArg(path.Join(configDir1, pathResolutionConfig1.AuthInfos["relative-user-1"].ClientCertificate), authInfo.ClientCertificate, t)
  238. matchStringArg(path.Join(configDir1, pathResolutionConfig1.AuthInfos["relative-user-1"].ClientKey), authInfo.ClientKey, t)
  239. }
  240. if key == "relative-user-2" {
  241. foundAuthInfoCount++
  242. matchStringArg(path.Join(configDir2, pathResolutionConfig2.AuthInfos["relative-user-2"].ClientCertificate), authInfo.ClientCertificate, t)
  243. matchStringArg(path.Join(configDir2, pathResolutionConfig2.AuthInfos["relative-user-2"].ClientKey), authInfo.ClientKey, t)
  244. }
  245. if key == "absolute-user-1" {
  246. foundAuthInfoCount++
  247. matchStringArg(pathResolutionConfig1.AuthInfos["absolute-user-1"].ClientCertificate, authInfo.ClientCertificate, t)
  248. matchStringArg(pathResolutionConfig1.AuthInfos["absolute-user-1"].ClientKey, authInfo.ClientKey, t)
  249. }
  250. if key == "absolute-user-2" {
  251. foundAuthInfoCount++
  252. matchStringArg(pathResolutionConfig2.AuthInfos["absolute-user-2"].ClientCertificate, authInfo.ClientCertificate, t)
  253. matchStringArg(pathResolutionConfig2.AuthInfos["absolute-user-2"].ClientKey, authInfo.ClientKey, t)
  254. }
  255. }
  256. if foundAuthInfoCount != 4 {
  257. t.Errorf("Expected 4 users, found %v: %v", foundAuthInfoCount, mergedConfig.AuthInfos)
  258. }
  259. }
  260. func TestMigratingFile(t *testing.T) {
  261. sourceFile, _ := ioutil.TempFile("", "")
  262. defer os.Remove(sourceFile.Name())
  263. destinationFile, _ := ioutil.TempFile("", "")
  264. // delete the file so that we'll write to it
  265. os.Remove(destinationFile.Name())
  266. WriteToFile(testConfigAlfa, sourceFile.Name())
  267. loadingRules := ClientConfigLoadingRules{
  268. MigrationRules: map[string]string{destinationFile.Name(): sourceFile.Name()},
  269. }
  270. if _, err := loadingRules.Load(); err != nil {
  271. t.Errorf("unexpected error %v", err)
  272. }
  273. // the load should have recreated this file
  274. defer os.Remove(destinationFile.Name())
  275. sourceContent, err := ioutil.ReadFile(sourceFile.Name())
  276. if err != nil {
  277. t.Errorf("unexpected error %v", err)
  278. }
  279. destinationContent, err := ioutil.ReadFile(destinationFile.Name())
  280. if err != nil {
  281. t.Errorf("unexpected error %v", err)
  282. }
  283. if !reflect.DeepEqual(sourceContent, destinationContent) {
  284. t.Errorf("source and destination do not match")
  285. }
  286. }
  287. func TestMigratingFileLeaveExistingFileAlone(t *testing.T) {
  288. sourceFile, _ := ioutil.TempFile("", "")
  289. defer os.Remove(sourceFile.Name())
  290. destinationFile, _ := ioutil.TempFile("", "")
  291. defer os.Remove(destinationFile.Name())
  292. WriteToFile(testConfigAlfa, sourceFile.Name())
  293. loadingRules := ClientConfigLoadingRules{
  294. MigrationRules: map[string]string{destinationFile.Name(): sourceFile.Name()},
  295. }
  296. if _, err := loadingRules.Load(); err != nil {
  297. t.Errorf("unexpected error %v", err)
  298. }
  299. destinationContent, err := ioutil.ReadFile(destinationFile.Name())
  300. if err != nil {
  301. t.Errorf("unexpected error %v", err)
  302. }
  303. if len(destinationContent) > 0 {
  304. t.Errorf("destination should not have been touched")
  305. }
  306. }
  307. func TestMigratingFileSourceMissingSkip(t *testing.T) {
  308. sourceFilename := "some-missing-file"
  309. destinationFile, _ := ioutil.TempFile("", "")
  310. // delete the file so that we'll write to it
  311. os.Remove(destinationFile.Name())
  312. loadingRules := ClientConfigLoadingRules{
  313. MigrationRules: map[string]string{destinationFile.Name(): sourceFilename},
  314. }
  315. if _, err := loadingRules.Load(); err != nil {
  316. t.Errorf("unexpected error %v", err)
  317. }
  318. if _, err := os.Stat(destinationFile.Name()); !os.IsNotExist(err) {
  319. t.Errorf("destination should not exist")
  320. }
  321. }
  322. func TestFileLocking(t *testing.T) {
  323. f, _ := ioutil.TempFile("", "")
  324. defer os.Remove(f.Name())
  325. err := lockFile(f.Name())
  326. if err != nil {
  327. t.Errorf("unexpected error while locking file: %v", err)
  328. }
  329. defer unlockFile(f.Name())
  330. err = lockFile(f.Name())
  331. if err == nil {
  332. t.Error("expected error while locking file.")
  333. }
  334. }
  335. func Example_noMergingOnExplicitPaths() {
  336. commandLineFile, _ := ioutil.TempFile("", "")
  337. defer os.Remove(commandLineFile.Name())
  338. envVarFile, _ := ioutil.TempFile("", "")
  339. defer os.Remove(envVarFile.Name())
  340. WriteToFile(testConfigAlfa, commandLineFile.Name())
  341. WriteToFile(testConfigConflictAlfa, envVarFile.Name())
  342. loadingRules := ClientConfigLoadingRules{
  343. ExplicitPath: commandLineFile.Name(),
  344. Precedence: []string{envVarFile.Name()},
  345. }
  346. mergedConfig, err := loadingRules.Load()
  347. json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig)
  348. if err != nil {
  349. fmt.Printf("Unexpected error: %v", err)
  350. }
  351. output, err := yaml.JSONToYAML(json)
  352. if err != nil {
  353. fmt.Printf("Unexpected error: %v", err)
  354. }
  355. fmt.Printf("%v", string(output))
  356. // Output:
  357. // apiVersion: v1
  358. // clusters:
  359. // - cluster:
  360. // server: http://cow.org:8080
  361. // name: cow-cluster
  362. // contexts:
  363. // - context:
  364. // cluster: cow-cluster
  365. // namespace: hammer-ns
  366. // user: red-user
  367. // name: federal-context
  368. // current-context: ""
  369. // kind: Config
  370. // preferences: {}
  371. // users:
  372. // - name: red-user
  373. // user:
  374. // token: red-token
  375. }
  376. func Example_mergingSomeWithConflict() {
  377. commandLineFile, _ := ioutil.TempFile("", "")
  378. defer os.Remove(commandLineFile.Name())
  379. envVarFile, _ := ioutil.TempFile("", "")
  380. defer os.Remove(envVarFile.Name())
  381. WriteToFile(testConfigAlfa, commandLineFile.Name())
  382. WriteToFile(testConfigConflictAlfa, envVarFile.Name())
  383. loadingRules := ClientConfigLoadingRules{
  384. Precedence: []string{commandLineFile.Name(), envVarFile.Name()},
  385. }
  386. mergedConfig, err := loadingRules.Load()
  387. json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig)
  388. if err != nil {
  389. fmt.Printf("Unexpected error: %v", err)
  390. }
  391. output, err := yaml.JSONToYAML(json)
  392. if err != nil {
  393. fmt.Printf("Unexpected error: %v", err)
  394. }
  395. fmt.Printf("%v", string(output))
  396. // Output:
  397. // apiVersion: v1
  398. // clusters:
  399. // - cluster:
  400. // server: http://cow.org:8080
  401. // name: cow-cluster
  402. // - cluster:
  403. // insecure-skip-tls-verify: true
  404. // server: http://donkey.org:8080
  405. // name: donkey-cluster
  406. // contexts:
  407. // - context:
  408. // cluster: cow-cluster
  409. // namespace: hammer-ns
  410. // user: red-user
  411. // name: federal-context
  412. // current-context: federal-context
  413. // kind: Config
  414. // preferences: {}
  415. // users:
  416. // - name: red-user
  417. // user:
  418. // token: red-token
  419. // - name: yellow-user
  420. // user:
  421. // token: yellow-token
  422. }
  423. func Example_mergingEverythingNoConflicts() {
  424. commandLineFile, _ := ioutil.TempFile("", "")
  425. defer os.Remove(commandLineFile.Name())
  426. envVarFile, _ := ioutil.TempFile("", "")
  427. defer os.Remove(envVarFile.Name())
  428. currentDirFile, _ := ioutil.TempFile("", "")
  429. defer os.Remove(currentDirFile.Name())
  430. homeDirFile, _ := ioutil.TempFile("", "")
  431. defer os.Remove(homeDirFile.Name())
  432. WriteToFile(testConfigAlfa, commandLineFile.Name())
  433. WriteToFile(testConfigBravo, envVarFile.Name())
  434. WriteToFile(testConfigCharlie, currentDirFile.Name())
  435. WriteToFile(testConfigDelta, homeDirFile.Name())
  436. loadingRules := ClientConfigLoadingRules{
  437. Precedence: []string{commandLineFile.Name(), envVarFile.Name(), currentDirFile.Name(), homeDirFile.Name()},
  438. }
  439. mergedConfig, err := loadingRules.Load()
  440. json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig)
  441. if err != nil {
  442. fmt.Printf("Unexpected error: %v", err)
  443. }
  444. output, err := yaml.JSONToYAML(json)
  445. if err != nil {
  446. fmt.Printf("Unexpected error: %v", err)
  447. }
  448. fmt.Printf("%v", string(output))
  449. // Output:
  450. // apiVersion: v1
  451. // clusters:
  452. // - cluster:
  453. // server: http://chicken.org:8080
  454. // name: chicken-cluster
  455. // - cluster:
  456. // server: http://cow.org:8080
  457. // name: cow-cluster
  458. // - cluster:
  459. // server: http://horse.org:8080
  460. // name: horse-cluster
  461. // - cluster:
  462. // server: http://pig.org:8080
  463. // name: pig-cluster
  464. // contexts:
  465. // - context:
  466. // cluster: cow-cluster
  467. // namespace: hammer-ns
  468. // user: red-user
  469. // name: federal-context
  470. // - context:
  471. // cluster: chicken-cluster
  472. // namespace: plane-ns
  473. // user: blue-user
  474. // name: gothic-context
  475. // - context:
  476. // cluster: pig-cluster
  477. // namespace: saw-ns
  478. // user: black-user
  479. // name: queen-anne-context
  480. // - context:
  481. // cluster: horse-cluster
  482. // namespace: chisel-ns
  483. // user: green-user
  484. // name: shaker-context
  485. // current-context: ""
  486. // kind: Config
  487. // preferences: {}
  488. // users:
  489. // - name: black-user
  490. // user:
  491. // token: black-token
  492. // - name: blue-user
  493. // user:
  494. // token: blue-token
  495. // - name: green-user
  496. // user:
  497. // token: green-token
  498. // - name: red-user
  499. // user:
  500. // token: red-token
  501. }