You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.3 KiB

  1. /*
  2. * Minio Cloud Storage, (C) 2016 Minio, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package cmd
  17. import (
  18. "crypto/x509"
  19. "crypto/x509/pkix"
  20. "fmt"
  21. "strings"
  22. "testing"
  23. "time"
  24. )
  25. // Tests if we generate storage info.
  26. func TestStorageInfoMsg(t *testing.T) {
  27. infoStorage := StorageInfo{
  28. Total: 1024 * 1024 * 1024 * 10,
  29. Free: 1024 * 1024 * 1024 * 2,
  30. Backend: struct {
  31. Type BackendType
  32. OnlineDisks int
  33. OfflineDisks int
  34. ReadQuorum int
  35. WriteQuorum int
  36. }{XL, 7, 1, 4, 5},
  37. }
  38. if msg := getStorageInfoMsg(infoStorage); !strings.Contains(msg, "2.0 GiB Free, 10 GiB Total") || !strings.Contains(msg, "7 Online, 1 Offline") {
  39. t.Fatal("Unexpected storage info message, found:", msg)
  40. }
  41. }
  42. // Tests if certificate expiry warning will be printed
  43. func TestCertificateExpiryInfo(t *testing.T) {
  44. // given
  45. var expiredDate = time.Now().Add(time.Hour * 24 * (30 - 1)) // 29 days.
  46. var fakeCerts = []*x509.Certificate{
  47. {
  48. NotAfter: expiredDate,
  49. Subject: pkix.Name{
  50. CommonName: "Test cert",
  51. },
  52. },
  53. }
  54. expectedMsg := colorBlue("\nCertificate expiry info:\n") +
  55. colorBold(fmt.Sprintf("#1 Test cert will expire on %s\n", expiredDate))
  56. // When
  57. msg := getCertificateChainMsg(fakeCerts)
  58. // Then
  59. if msg != expectedMsg {
  60. t.Fatalf("Expected message was: %s, got: %s", expectedMsg, msg)
  61. }
  62. }
  63. // Tests if certificate expiry warning will not be printed if certificate not expired
  64. func TestCertificateNotExpired(t *testing.T) {
  65. // given
  66. var expiredDate = time.Now().Add(time.Hour * 24 * (30 + 1)) // 31 days.
  67. var fakeCerts = []*x509.Certificate{
  68. {
  69. NotAfter: expiredDate,
  70. Subject: pkix.Name{
  71. CommonName: "Test cert",
  72. },
  73. },
  74. }
  75. // when
  76. msg := getCertificateChainMsg(fakeCerts)
  77. // then
  78. if msg != "" {
  79. t.Fatalf("Expected empty message was: %s", msg)
  80. }
  81. }