token_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright 2016 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 kubeadm
  14. import (
  15. "flag"
  16. "os"
  17. "path"
  18. "path/filepath"
  19. "regexp"
  20. "testing"
  21. )
  22. const (
  23. TokenExpectedRegex = "^\\S{6}\\.\\S{16}\n$"
  24. )
  25. var kubeadmPathFlag = flag.String("kubeadm-path", filepath.Join(os.Getenv("KUBE_ROOT"), "cluster/kubeadm.sh"), "Location of kubeadm")
  26. func getKubeadmPath() string {
  27. kubeadmPath := *kubeadmPathFlag // TEST_SRCDIR is provided by Bazel.
  28. if srcDir := os.Getenv("TEST_SRCDIR"); srcDir != "" {
  29. kubeadmPath = path.Join(srcDir, os.Getenv("TEST_WORKSPACE"), kubeadmPath)
  30. }
  31. return kubeadmPath
  32. }
  33. var kubeadmCmdSkip = flag.Bool("kubeadm-cmd-skip", false, "Skip kubeadm cmd tests")
  34. func TestCmdTokenGenerate(t *testing.T) {
  35. kubeadmPath := getKubeadmPath()
  36. if *kubeadmCmdSkip {
  37. t.Log("kubeadm cmd tests being skipped")
  38. t.Skip()
  39. }
  40. stdout, _, _, err := RunCmd(kubeadmPath, "token", "generate")
  41. if err != nil {
  42. t.Fatalf("'kubeadm token generate' exited uncleanly: %v", err)
  43. }
  44. matched, err := regexp.MatchString(TokenExpectedRegex, stdout)
  45. if err != nil {
  46. t.Fatalf("encountered an error while trying to match 'kubeadm token generate' stdout: %v", err)
  47. }
  48. if !matched {
  49. t.Errorf("'kubeadm token generate' stdout did not match expected regex; wanted: [%q], got: [%s]", TokenExpectedRegex, stdout)
  50. }
  51. }
  52. func TestCmdTokenGenerateTypoError(t *testing.T) {
  53. /*
  54. Since we expect users to do things like this:
  55. $ TOKEN=$(kubeadm token generate)
  56. we want to make sure that if they have a typo in their command, we exit
  57. with a non-zero status code after showing the command's usage, so that
  58. the usage itself isn't captured as a token without the user noticing.
  59. */
  60. if *kubeadmCmdSkip {
  61. t.Log("kubeadm cmd tests being skipped")
  62. t.Skip()
  63. }
  64. kubeadmPath := getKubeadmPath()
  65. _, _, _, err := RunCmd(kubeadmPath, "token", "genorate") // subtle typo
  66. if err == nil {
  67. t.Error("'kubeadm token genorate' (a deliberate typo) exited without an error when we expected non-zero exit status")
  68. }
  69. }
  70. func TestCmdTokenDelete(t *testing.T) {
  71. if *kubeadmCmdSkip {
  72. t.Log("kubeadm cmd tests being skipped")
  73. t.Skip()
  74. }
  75. var tests = []struct {
  76. name string
  77. args string
  78. expected bool
  79. }{
  80. {"no token provided", "", false},
  81. {"invalid token", "foobar", false},
  82. }
  83. kubeadmPath := getKubeadmPath()
  84. for _, rt := range tests {
  85. t.Run(rt.name, func(t *testing.T) {
  86. _, _, _, actual := RunCmd(kubeadmPath, "token", "delete", rt.args)
  87. if (actual == nil) != rt.expected {
  88. t.Errorf(
  89. "failed CmdTokenDelete running 'kubeadm token %s' with an error: %v\n\texpected: %t\n\t actual: %t",
  90. rt.args,
  91. actual,
  92. rt.expected,
  93. (actual == nil),
  94. )
  95. }
  96. kubeadmReset()
  97. })
  98. }
  99. }