tunneler_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2015 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 genericapiserver
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. "testing"
  19. "time"
  20. "k8s.io/kubernetes/pkg/util/clock"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. // TestSecondsSinceSync verifies that proper results are returned
  24. // when checking the time between syncs
  25. func TestSecondsSinceSync(t *testing.T) {
  26. tunneler := &SSHTunneler{}
  27. assert := assert.New(t)
  28. tunneler.lastSync = time.Date(2015, time.January, 1, 1, 1, 1, 1, time.UTC).Unix()
  29. // Nano Second. No difference.
  30. tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 1, 1, 2, time.UTC))
  31. assert.Equal(int64(0), tunneler.SecondsSinceSync())
  32. // Second
  33. tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 1, 2, 1, time.UTC))
  34. assert.Equal(int64(1), tunneler.SecondsSinceSync())
  35. // Minute
  36. tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 2, 1, 1, time.UTC))
  37. assert.Equal(int64(60), tunneler.SecondsSinceSync())
  38. // Hour
  39. tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 1, 2, 1, 1, 1, time.UTC))
  40. assert.Equal(int64(3600), tunneler.SecondsSinceSync())
  41. // Day
  42. tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 2, 1, 1, 1, 1, time.UTC))
  43. assert.Equal(int64(86400), tunneler.SecondsSinceSync())
  44. // Month
  45. tunneler.clock = clock.NewFakeClock(time.Date(2015, time.February, 1, 1, 1, 1, 1, time.UTC))
  46. assert.Equal(int64(2678400), tunneler.SecondsSinceSync())
  47. // Future Month. Should be -Month.
  48. tunneler.lastSync = time.Date(2015, time.February, 1, 1, 1, 1, 1, time.UTC).Unix()
  49. tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 1, 1, 1, time.UTC))
  50. assert.Equal(int64(-2678400), tunneler.SecondsSinceSync())
  51. }
  52. // generateTempFile creates a temporary file path
  53. func generateTempFilePath(prefix string) string {
  54. tmpPath, _ := filepath.Abs(fmt.Sprintf("%s/%s-%d", os.TempDir(), prefix, time.Now().Unix()))
  55. return tmpPath
  56. }
  57. // TestGenerateSSHKey verifies that SSH key generation does indeed
  58. // generate keys even with keys already exist.
  59. func TestGenerateSSHKey(t *testing.T) {
  60. assert := assert.New(t)
  61. privateKey := generateTempFilePath("private")
  62. publicKey := generateTempFilePath("public")
  63. // Make sure we have no test keys laying around
  64. os.Remove(privateKey)
  65. os.Remove(publicKey)
  66. // Pass case: Sunny day case
  67. err := generateSSHKey(privateKey, publicKey)
  68. assert.NoError(err, "generateSSHKey should not have retuend an error: %s", err)
  69. // Pass case: PrivateKey exists test case
  70. os.Remove(publicKey)
  71. err = generateSSHKey(privateKey, publicKey)
  72. assert.NoError(err, "generateSSHKey should not have retuend an error: %s", err)
  73. // Pass case: PublicKey exists test case
  74. os.Remove(privateKey)
  75. err = generateSSHKey(privateKey, publicKey)
  76. assert.NoError(err, "generateSSHKey should not have retuend an error: %s", err)
  77. // Make sure we have no test keys laying around
  78. os.Remove(privateKey)
  79. os.Remove(publicKey)
  80. // TODO: testing error cases where the file can not be removed?
  81. }