wal: add DetectVersion test

This commit is contained in:
Yicheng Qin 2015-01-08 21:54:05 -08:00
parent f08d1090d0
commit 50c179ec1c
2 changed files with 82 additions and 3 deletions

View File

@ -37,11 +37,11 @@ const (
)
func DetectVersion(dirpath string) (WalVersion, error) {
if _, err := os.Stat(dirpath); os.IsNotExist(err) {
return WALNotExist, nil
}
names, err := fileutil.ReadDir(dirpath)
if err != nil {
if os.IsNotExist(err) {
err = nil
}
// Error reading the directory
return WALNotExist, err
}

79
wal/util_test.go Normal file
View File

@ -0,0 +1,79 @@
/*
Copyright 2014 CoreOS, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package wal
import (
"io/ioutil"
"os"
"path"
"strings"
"testing"
)
func TestDetectVersion(t *testing.T) {
tests := []struct {
names []string
wver WalVersion
}{
{[]string{}, WALNotExist},
{[]string{"snap/", "wal/", "wal/1"}, WALv0_5},
{[]string{"snapshot/", "conf", "log"}, WALv0_4},
{[]string{"weird"}, WALUnknown},
{[]string{"snap/", "wal/"}, WALUnknown},
}
for i, tt := range tests {
p := mustMakeDir(t, tt.names...)
ver, err := DetectVersion(p)
if ver != tt.wver {
t.Errorf("#%d: version = %s, want %s", i, ver, tt.wver)
}
if err != nil {
t.Errorf("#%d: err = %s, want nil", i, err)
}
os.RemoveAll(p)
}
// detect on non-exist directory
v, err := DetectVersion(path.Join(os.TempDir(), "waltest", "not-exist"))
if v != WALNotExist {
t.Errorf("#non-exist: version = %s, want %s", v, WALNotExist)
}
if err != nil {
t.Errorf("#non-exist: err = %s, want %s", v, WALNotExist)
}
}
// mustMakeDir builds the directory that contains files with the given
// names. If the name ends with '/', it is created as a directory.
func mustMakeDir(t *testing.T, names ...string) string {
p, err := ioutil.TempDir(os.TempDir(), "waltest")
if err != nil {
t.Fatal(err)
}
for _, n := range names {
if strings.HasSuffix(n, "/") {
if err := os.MkdirAll(path.Join(p, n), 0700); err != nil {
t.Fatal(err)
}
} else {
if _, err := os.Create(path.Join(p, n)); err != nil {
t.Fatal(err)
}
}
}
return p
}