diskgraphstorage_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package graphstorage
  11. import (
  12. "errors"
  13. "flag"
  14. "fmt"
  15. "os"
  16. "testing"
  17. "devt.de/krotik/common/datautil"
  18. "devt.de/krotik/common/fileutil"
  19. "devt.de/krotik/eliasdb/storage"
  20. )
  21. const diskGraphStorageTestDBDir = "diskgraphstoragetest1"
  22. const diskGraphStorageTestDBDir2 = "diskgraphstoragetest2"
  23. var dbdirs = []string{diskGraphStorageTestDBDir, diskGraphStorageTestDBDir2}
  24. const invalidFileName = "**" + string(0x0)
  25. // Main function for all tests in this package
  26. func TestMain(m *testing.M) {
  27. flag.Parse()
  28. for _, dbdir := range dbdirs {
  29. if res, _ := fileutil.PathExists(dbdir); res {
  30. if err := os.RemoveAll(dbdir); err != nil {
  31. fmt.Print("Could not remove test directory:", err.Error())
  32. }
  33. }
  34. }
  35. // Run the tests
  36. res := m.Run()
  37. // Teardown
  38. for _, dbdir := range dbdirs {
  39. if res, _ := fileutil.PathExists(dbdir); res {
  40. if err := os.RemoveAll(dbdir); err != nil {
  41. fmt.Print("Could not remove test directory:", err.Error())
  42. }
  43. }
  44. }
  45. os.Exit(res)
  46. }
  47. func TestDiskGraphStorage(t *testing.T) {
  48. dgsnew, err := NewDiskGraphStorage(diskGraphStorageTestDBDir, false)
  49. if err != nil {
  50. t.Error(err)
  51. return
  52. }
  53. if res := dgsnew.Name(); res != diskGraphStorageTestDBDir {
  54. t.Error("Unexpected name:", res)
  55. return
  56. }
  57. // Check that the storage directory exists
  58. if res, _ := fileutil.PathExists(diskGraphStorageTestDBDir); !res {
  59. t.Error("Storage directory does not exist")
  60. return
  61. }
  62. if res, _ := fileutil.PathExists(diskGraphStorageTestDBDir + "/" + FilenameNameDB); !res {
  63. t.Error("Name DB does not exist")
  64. return
  65. }
  66. // Get a storage file
  67. sm1 := dgsnew.StorageManager("store1.nodes", true)
  68. if sm1 == nil {
  69. t.Error("Unexpected result")
  70. return
  71. }
  72. if res, _ := fileutil.PathExists(diskGraphStorageTestDBDir + "/store1.nodes.db.0"); !res {
  73. t.Error("Storage file does not exist")
  74. return
  75. }
  76. sm2 := dgsnew.StorageManager("store2.nodes", false)
  77. if res, _ := fileutil.PathExists(diskGraphStorageTestDBDir + "/store2.nodes.db.0"); res {
  78. t.Error("Storage file should not have been created")
  79. return
  80. }
  81. if sm2 != nil {
  82. t.Error("Unexpected result")
  83. return
  84. }
  85. m := dgsnew.MainDB()
  86. m["test1"] = "test1value"
  87. dgsnew.FlushMain()
  88. dgsnew.RollbackMain()
  89. if err := dgsnew.FlushAll(); err != nil {
  90. t.Error("Unexpected error return:", err)
  91. }
  92. if err := dgsnew.Close(); err != nil {
  93. t.Error(err)
  94. return
  95. }
  96. // Open the storage again to make sure we can load it
  97. dgs, err := NewDiskGraphStorage(diskGraphStorageTestDBDir, false)
  98. if err != nil {
  99. t.Error(err)
  100. return
  101. }
  102. if res := dgs.MainDB()["test1"]; res != "test1value" {
  103. t.Error("Unexpected value in mainDB value:", res)
  104. return
  105. }
  106. // Check readonly mode
  107. dgs.(*DiskGraphStorage).readonly = true
  108. if err := dgs.RollbackMain(); err.Error() != "GraphError: Failed write to readonly storage (Cannot rollback main db)" {
  109. t.Error("Unexpected error return:", err)
  110. }
  111. if err := dgs.FlushMain(); err.Error() != "GraphError: Failed write to readonly storage (Cannot flush main db)" {
  112. t.Error("Unexpected error return:", err)
  113. }
  114. if err := dgs.FlushAll(); err != nil {
  115. t.Error("Unexpected error return:", err)
  116. }
  117. if err := dgs.Close(); err != nil {
  118. t.Error(err)
  119. return
  120. }
  121. }
  122. func TestDiskGraphStorageErrors(t *testing.T) {
  123. _, err := NewDiskGraphStorage(invalidFileName, false)
  124. if err == nil {
  125. t.Error("Unexpected new disk graph storage result")
  126. return
  127. }
  128. // Test names map error case
  129. old := FilenameNameDB
  130. FilenameNameDB = invalidFileName
  131. _, err = NewDiskGraphStorage(diskGraphStorageTestDBDir2, false)
  132. if err == nil {
  133. t.Error("Unexpected new disk graph storage result")
  134. FilenameNameDB = old
  135. return
  136. }
  137. _, err = NewDiskGraphStorage(diskGraphStorageTestDBDir2, false)
  138. if err == nil {
  139. t.Error("Unexpected new disk graph storage result")
  140. FilenameNameDB = old
  141. return
  142. }
  143. FilenameNameDB = old
  144. dgs := &DiskGraphStorage{invalidFileName, false, nil,
  145. make(map[string]storage.Manager)}
  146. pm, _ := datautil.NewPersistentStringMap(invalidFileName)
  147. dgs.mainDB = pm
  148. msm := storage.NewMemoryStorageManager("test")
  149. dgs.storagemanagers["test"] = msm
  150. storage.MsmRetFlush = errors.New("TestError")
  151. storage.MsmRetClose = errors.New("TestError")
  152. if err := dgs.RollbackMain(); err == nil {
  153. t.Error("Unexpected flush result")
  154. return
  155. }
  156. if err := dgs.FlushMain(); err == nil {
  157. t.Error("Unexpected flush result")
  158. return
  159. }
  160. if err := dgs.FlushAll(); err == nil {
  161. t.Error("Unexpected flush all result")
  162. return
  163. }
  164. if err := dgs.Close(); err == nil {
  165. t.Error("Unexpected close result")
  166. return
  167. }
  168. }