version_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2015 CoreOS, Inc.
  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{"snapshot/", "conf", "log"}, DataDir0_4},
  30. {[]string{"weird"}, DataDirUnknown},
  31. {[]string{"snap/", "wal/"}, DataDirUnknown},
  32. }
  33. for i, tt := range tests {
  34. p := mustMakeDir(t, tt.names...)
  35. ver, err := DetectDataDir(p)
  36. if ver != tt.wver {
  37. t.Errorf("#%d: version = %s, want %s", i, ver, tt.wver)
  38. }
  39. if err != nil {
  40. t.Errorf("#%d: err = %s, want nil", i, err)
  41. }
  42. os.RemoveAll(p)
  43. }
  44. }
  45. // mustMakeDir builds the directory that contains files with the given
  46. // names. If the name ends with '/', it is created as a directory.
  47. func mustMakeDir(t *testing.T, names ...string) string {
  48. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. for _, n := range names {
  53. if strings.HasSuffix(n, "/") {
  54. if err := os.MkdirAll(path.Join(p, n), 0700); err != nil {
  55. t.Fatal(err)
  56. }
  57. } else {
  58. if _, err := os.Create(path.Join(p, n)); err != nil {
  59. t.Fatal(err)
  60. }
  61. }
  62. }
  63. return p
  64. }