nosql_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package nosql
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/assert"
  7. "github.com/smallstep/certificates/db"
  8. "github.com/smallstep/nosql"
  9. )
  10. func TestNew(t *testing.T) {
  11. type test struct {
  12. db nosql.DB
  13. err error
  14. }
  15. var tests = map[string]test{
  16. "fail/db.CreateTable-error": {
  17. db: &db.MockNoSQLDB{
  18. MCreateTable: func(bucket []byte) error {
  19. assert.Equals(t, string(bucket), string(accountTable))
  20. return errors.New("force")
  21. },
  22. },
  23. err: errors.Errorf("error creating table %s: force", string(accountTable)),
  24. },
  25. "ok": {
  26. db: &db.MockNoSQLDB{
  27. MCreateTable: func(bucket []byte) error {
  28. return nil
  29. },
  30. },
  31. },
  32. }
  33. for name, tc := range tests {
  34. t.Run(name, func(t *testing.T) {
  35. if _, err := New(tc.db); err != nil {
  36. if assert.NotNil(t, tc.err) {
  37. assert.HasPrefix(t, err.Error(), tc.err.Error())
  38. }
  39. } else {
  40. assert.Nil(t, tc.err)
  41. }
  42. })
  43. }
  44. }
  45. type errorThrower string
  46. func (et errorThrower) MarshalJSON() ([]byte, error) {
  47. return nil, errors.New("force")
  48. }
  49. func TestDB_save(t *testing.T) {
  50. type test struct {
  51. db nosql.DB
  52. nu interface{}
  53. old interface{}
  54. err error
  55. }
  56. var tests = map[string]test{
  57. "fail/error-marshaling-new": {
  58. nu: errorThrower("foo"),
  59. err: errors.New("error marshaling acme type: challenge"),
  60. },
  61. "fail/error-marshaling-old": {
  62. nu: "new",
  63. old: errorThrower("foo"),
  64. err: errors.New("error marshaling acme type: challenge"),
  65. },
  66. "fail/db.CmpAndSwap-error": {
  67. nu: "new",
  68. old: "old",
  69. db: &db.MockNoSQLDB{
  70. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  71. assert.Equals(t, bucket, challengeTable)
  72. assert.Equals(t, string(key), "id")
  73. assert.Equals(t, string(old), "\"old\"")
  74. assert.Equals(t, string(nu), "\"new\"")
  75. return nil, false, errors.New("force")
  76. },
  77. },
  78. err: errors.New("error saving acme challenge: force"),
  79. },
  80. "fail/db.CmpAndSwap-false-marshaling-old": {
  81. nu: "new",
  82. old: "old",
  83. db: &db.MockNoSQLDB{
  84. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  85. assert.Equals(t, bucket, challengeTable)
  86. assert.Equals(t, string(key), "id")
  87. assert.Equals(t, string(old), "\"old\"")
  88. assert.Equals(t, string(nu), "\"new\"")
  89. return nil, false, nil
  90. },
  91. },
  92. err: errors.New("error saving acme challenge; changed since last read"),
  93. },
  94. "ok": {
  95. nu: "new",
  96. old: "old",
  97. db: &db.MockNoSQLDB{
  98. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  99. assert.Equals(t, bucket, challengeTable)
  100. assert.Equals(t, string(key), "id")
  101. assert.Equals(t, string(old), "\"old\"")
  102. assert.Equals(t, string(nu), "\"new\"")
  103. return nu, true, nil
  104. },
  105. },
  106. },
  107. "ok/nils": {
  108. nu: nil,
  109. old: nil,
  110. db: &db.MockNoSQLDB{
  111. MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
  112. assert.Equals(t, bucket, challengeTable)
  113. assert.Equals(t, string(key), "id")
  114. assert.Equals(t, old, nil)
  115. assert.Equals(t, nu, nil)
  116. return nu, true, nil
  117. },
  118. },
  119. },
  120. }
  121. for name, tc := range tests {
  122. t.Run(name, func(t *testing.T) {
  123. d := &DB{db: tc.db}
  124. if err := d.save(context.Background(), "id", tc.nu, tc.old, "challenge", challengeTable); err != nil {
  125. if assert.NotNil(t, tc.err) {
  126. assert.HasPrefix(t, err.Error(), tc.err.Error())
  127. }
  128. } else {
  129. assert.Nil(t, tc.err)
  130. }
  131. })
  132. }
  133. }