config_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 manager
  11. import (
  12. "errors"
  13. "fmt"
  14. "io/ioutil"
  15. "os"
  16. "strings"
  17. "testing"
  18. "devt.de/krotik/common/datautil"
  19. )
  20. const invalidFileName = "**" + string(0x0)
  21. func TestDefaultStateInfo(t *testing.T) {
  22. _, err := NewDefaultStateInfo(invalidFileName)
  23. if !strings.HasPrefix(err.Error(),
  24. "ClusterError: Cluster configuration error (Cannot create state info file") {
  25. t.Error("It should not be possible to create a state info with an invalid filename:", err)
  26. return
  27. }
  28. // Store some data
  29. fc, err := NewDefaultStateInfo("test_conf.cfg")
  30. defer func() {
  31. if err := os.RemoveAll("test_conf.cfg"); err != nil {
  32. t.Error(err)
  33. }
  34. }()
  35. // Store list of strings
  36. testList := make([]string, 5)
  37. testList[3] = "456"
  38. testList[4] = "111"
  39. fc.Put("data", testList)
  40. // Write to disk
  41. fc.Flush()
  42. if len(fc.Map()) != len(fc.(*DefaultStateInfo).Data) {
  43. t.Error("Unexpected result")
  44. return
  45. }
  46. // Load data again
  47. fc2, err := NewDefaultStateInfo("test_conf.cfg")
  48. // Check we can get the data back
  49. m, ok := fc2.Get("data")
  50. if !ok || fmt.Sprint(m) != fmt.Sprint(fc.(*DefaultStateInfo).Data["data"]) {
  51. t.Error("Should get back what is stored")
  52. return
  53. }
  54. pm, _ := datautil.NewPersistentMap(invalidFileName)
  55. fc2.(*DefaultStateInfo).PersistentMap = pm
  56. if err := fc2.Flush(); !strings.HasPrefix(err.Error(),
  57. "ClusterError: Cluster configuration error (Cannot persist state info") {
  58. t.Error("Unexpected error:", err)
  59. return
  60. }
  61. ioutil.WriteFile("test_conf.cfg", []byte{0x00, 0x00}, 0660)
  62. _, err = NewDefaultStateInfo("test_conf.cfg")
  63. if !strings.HasPrefix(err.Error(),
  64. "ClusterError: Cluster configuration error (Cannot load state info file test_conf.cfg") {
  65. t.Error(err)
  66. }
  67. }
  68. func TestMemStateInfo(t *testing.T) {
  69. msi := NewMemStateInfo()
  70. // Store list of strings
  71. testList := make([]string, 5)
  72. testList[3] = "456"
  73. testList[4] = "111"
  74. msi.Put("data", testList)
  75. // NOP
  76. msi.Flush()
  77. // Check we can get the data back
  78. m, ok := msi.Get("data")
  79. if !ok || fmt.Sprint(m) != fmt.Sprint(msi.(*MemStateInfo).data["data"]) {
  80. t.Error("Should get back what is stored")
  81. return
  82. }
  83. if len(msi.Map()) != len(msi.(*MemStateInfo).data) {
  84. t.Error("Unexpected result")
  85. return
  86. }
  87. }
  88. func TestErrors(t *testing.T) {
  89. // Test cluster error
  90. err := &Error{errors.New("test"), ""}
  91. if err.Error() != "ClusterError: test" {
  92. t.Error("Unexpected result:", err.Error())
  93. return
  94. }
  95. err = &Error{errors.New("test"), "testdetail"}
  96. if err.Error() != "ClusterError: test (testdetail)" {
  97. t.Error("Unexpected result:", err.Error())
  98. return
  99. }
  100. }