-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup(miniooni): rename kvstore2 to engine (#1436)
Noticed when preparing the SplinterCon demo. We should have the same name for the engine state directory for miniooni and ooniprobe. Otherwise, we need to tell people about this difference. Extracted from: #1423 Closes: ooni/probe#2646
- Loading branch information
1 parent
86bea50
commit fdce52c
Showing
3 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Package kvstore2dir migrates $OONI_HOME/kvstore2 to $OONI_HOME/engine. This ensures | ||
// that miniooni and ooniprobe use the same directory name for the engine state. | ||
package kvstore2dir | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type statBuf interface { | ||
IsDir() bool | ||
} | ||
|
||
func simplifiedStat(path string) (statBuf, error) { | ||
return os.Stat(path) | ||
} | ||
|
||
var ( | ||
osStat = simplifiedStat | ||
osRename = os.Rename | ||
) | ||
|
||
// Move moves $OONI_HOME/kvstore2 to $OONI_HOME/engine, if possible. | ||
func Move(dir string) error { | ||
kvstore2dir := filepath.Join(dir, "kvstore2") | ||
if stat, err := osStat(kvstore2dir); err != nil || !stat.IsDir() { | ||
return nil | ||
} | ||
enginedir := filepath.Join(dir, "engine") | ||
if _, err := osStat(enginedir); err == nil { | ||
return nil | ||
} | ||
return osRename(kvstore2dir, enginedir) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package kvstore2dir | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
type booleanStatBuf bool | ||
|
||
var _ statBuf = booleanStatBuf(true) | ||
|
||
// IsDir implements statBuf. | ||
func (v booleanStatBuf) IsDir() bool { | ||
return bool(v) | ||
} | ||
|
||
func TestMove(t *testing.T) { | ||
// testcase is a test case implemented by this function | ||
type testcase struct { | ||
name string | ||
osStat func(name string) (statBuf, error) | ||
osRename func(oldpath string, newpath string) error | ||
expect error | ||
} | ||
|
||
cases := []testcase{{ | ||
name: "when we cannot stat kvstore2", | ||
osStat: func(name string) (statBuf, error) { | ||
return nil, io.EOF | ||
}, | ||
osRename: func(oldpath string, newpath string) error { | ||
panic("should not be called") | ||
}, | ||
expect: nil, | ||
}, { | ||
name: "when kvstore2 is not a directory", | ||
osStat: func(name string) (statBuf, error) { | ||
return booleanStatBuf(false), nil | ||
}, | ||
osRename: func(oldpath string, newpath string) error { | ||
panic("should not be called") | ||
}, | ||
expect: nil, | ||
}, { | ||
name: "when we can find kvstore2 as a dir and engine", | ||
osStat: func(name string) (statBuf, error) { | ||
if name == filepath.Join("xo", "kvstore2") { | ||
return booleanStatBuf(true), nil | ||
} | ||
return booleanStatBuf(true), nil | ||
}, | ||
osRename: func(oldpath string, newpath string) error { | ||
panic("should not be called") | ||
}, | ||
expect: nil, | ||
}, { | ||
name: "when we can find kvstore2 as a dir without engine", | ||
osStat: func(name string) (statBuf, error) { | ||
if name == filepath.Join("xo", "kvstore2") { | ||
return booleanStatBuf(true), nil | ||
} | ||
return nil, io.EOF | ||
}, | ||
osRename: func(oldpath string, newpath string) error { | ||
return nil | ||
}, | ||
expect: nil, | ||
}} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
// override and restore functions | ||
osStat = tc.osStat | ||
osRename = tc.osRename | ||
defer func() { | ||
osStat = simplifiedStat | ||
osRename = os.Rename | ||
}() | ||
|
||
// invoke Move | ||
err := Move("xo") | ||
|
||
// check the result | ||
if !errors.Is(err, tc.expect) { | ||
t.Fatal("expected", tc.expect, "got", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSimplifiedStat(t *testing.T) { | ||
buf, err := simplifiedStat("kvstore2dir.go") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if buf.IsDir() { | ||
t.Fatal("expected not dir") | ||
} | ||
} |