postupgrade_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2017 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 upgrade
  14. import (
  15. "os"
  16. "path/filepath"
  17. "strings"
  18. "testing"
  19. "github.com/pkg/errors"
  20. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  21. testutil "k8s.io/kubernetes/cmd/kubeadm/test"
  22. )
  23. func TestMoveFiles(t *testing.T) {
  24. tmpdir := testutil.SetupTempDir(t)
  25. defer os.RemoveAll(tmpdir)
  26. os.Chmod(tmpdir, 0766)
  27. certPath := filepath.Join(tmpdir, constants.APIServerCertName)
  28. certFile, err := os.OpenFile(certPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
  29. if err != nil {
  30. t.Fatalf("Failed to create cert file %s: %v", certPath, err)
  31. }
  32. defer certFile.Close()
  33. keyPath := filepath.Join(tmpdir, constants.APIServerKeyName)
  34. keyFile, err := os.OpenFile(keyPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
  35. if err != nil {
  36. t.Fatalf("Failed to create key file %s: %v", keyPath, err)
  37. }
  38. defer keyFile.Close()
  39. subDir := filepath.Join(tmpdir, "expired")
  40. if err := os.Mkdir(subDir, 0766); err != nil {
  41. t.Fatalf("Failed to create backup directory %s: %v", subDir, err)
  42. }
  43. filesToMove := map[string]string{
  44. filepath.Join(tmpdir, constants.APIServerCertName): filepath.Join(subDir, constants.APIServerCertName),
  45. filepath.Join(tmpdir, constants.APIServerKeyName): filepath.Join(subDir, constants.APIServerKeyName),
  46. }
  47. if err := moveFiles(filesToMove); err != nil {
  48. t.Fatalf("Failed to move files %v: %v", filesToMove, err)
  49. }
  50. }
  51. func TestRollbackFiles(t *testing.T) {
  52. tmpdir := testutil.SetupTempDir(t)
  53. defer os.RemoveAll(tmpdir)
  54. os.Chmod(tmpdir, 0766)
  55. subDir := filepath.Join(tmpdir, "expired")
  56. if err := os.Mkdir(subDir, 0766); err != nil {
  57. t.Fatalf("Failed to create backup directory %s: %v", subDir, err)
  58. }
  59. certPath := filepath.Join(subDir, constants.APIServerCertName)
  60. certFile, err := os.OpenFile(certPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
  61. if err != nil {
  62. t.Fatalf("Failed to create cert file %s: %v", certPath, err)
  63. }
  64. defer certFile.Close()
  65. keyPath := filepath.Join(subDir, constants.APIServerKeyName)
  66. keyFile, err := os.OpenFile(keyPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
  67. if err != nil {
  68. t.Fatalf("Failed to create key file %s: %v", keyPath, err)
  69. }
  70. defer keyFile.Close()
  71. filesToRollBack := map[string]string{
  72. filepath.Join(subDir, constants.APIServerCertName): filepath.Join(tmpdir, constants.APIServerCertName),
  73. filepath.Join(subDir, constants.APIServerKeyName): filepath.Join(tmpdir, constants.APIServerKeyName),
  74. }
  75. errString := "there are files need roll back"
  76. originalErr := errors.New(errString)
  77. err = rollbackFiles(filesToRollBack, originalErr)
  78. if err == nil {
  79. t.Fatalf("Expected error contains %q, got nil", errString)
  80. }
  81. if !strings.Contains(err.Error(), errString) {
  82. t.Fatalf("Expected error contains %q, got %v", errString, err)
  83. }
  84. }