version_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2015 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package version
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path"
  19. "strings"
  20. "testing"
  21. )
  22. func TestDetectDataDir(t *testing.T) {
  23. tests := []struct {
  24. names []string
  25. wver DataDirVersion
  26. }{
  27. {[]string{"member/", "member/wal/", "member/wal/1", "member/snap/"}, DataDir2_0_1},
  28. {[]string{"snap/", "wal/", "wal/1"}, DataDir2_0},
  29. {[]string{"weird"}, DataDirUnknown},
  30. {[]string{"snap/", "wal/"}, DataDirUnknown},
  31. }
  32. for i, tt := range tests {
  33. p := mustMakeDir(t, tt.names...)
  34. ver, err := DetectDataDir(p)
  35. if ver != tt.wver {
  36. t.Errorf("#%d: version = %s, want %s", i, ver, tt.wver)
  37. }
  38. if err != nil {
  39. t.Errorf("#%d: err = %s, want nil", i, err)
  40. }
  41. os.RemoveAll(p)
  42. }
  43. }
  44. // mustMakeDir builds the directory that contains files with the given
  45. // names. If the name ends with '/', it is created as a directory.
  46. func mustMakeDir(t *testing.T, names ...string) string {
  47. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. for _, n := range names {
  52. if strings.HasSuffix(n, "/") {
  53. if err := os.MkdirAll(path.Join(p, n), 0700); err != nil {
  54. t.Fatal(err)
  55. }
  56. } else {
  57. if _, err := os.Create(path.Join(p, n)); err != nil {
  58. t.Fatal(err)
  59. }
  60. }
  61. }
  62. return p
  63. }