2020-02-03 18:35:52 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ipn
|
|
|
|
|
|
|
|
import (
|
2021-09-24 20:46:08 +01:00
|
|
|
"path/filepath"
|
2020-02-03 18:35:52 +00:00
|
|
|
"testing"
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 03:59:54 +01:00
|
|
|
|
|
|
|
"tailscale.com/tstest"
|
2020-02-03 18:35:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func testStoreSemantics(t *testing.T, store StateStore) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
// if true, data is data to write. If false, data is expected
|
|
|
|
// output of read.
|
|
|
|
write bool
|
|
|
|
id StateKey
|
|
|
|
data string
|
|
|
|
// If write=false, true if we expect a not-exist error.
|
|
|
|
notExists bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
id: "foo",
|
|
|
|
notExists: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
write: true,
|
|
|
|
id: "foo",
|
|
|
|
data: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "foo",
|
|
|
|
data: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "baz",
|
|
|
|
notExists: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
write: true,
|
|
|
|
id: "baz",
|
|
|
|
data: "quux",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "foo",
|
|
|
|
data: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "baz",
|
|
|
|
data: "quux",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
if test.write {
|
|
|
|
if err := store.WriteState(test.id, []byte(test.data)); err != nil {
|
|
|
|
t.Errorf("writing %q to %q: %v", test.data, test.id, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bs, err := store.ReadState(test.id)
|
|
|
|
if err != nil {
|
|
|
|
if test.notExists && err == ErrStateNotExist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Errorf("reading %q: %v", test.id, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if string(bs) != test.data {
|
|
|
|
t.Errorf("reading %q: got %q, want %q", test.id, string(bs), test.data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryStore(t *testing.T) {
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 03:59:54 +01:00
|
|
|
tstest.PanicOnLog()
|
|
|
|
|
2020-02-03 18:35:52 +00:00
|
|
|
store := &MemoryStore{}
|
|
|
|
testStoreSemantics(t, store)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileStore(t *testing.T) {
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 03:59:54 +01:00
|
|
|
tstest.PanicOnLog()
|
|
|
|
|
2021-09-24 20:46:08 +01:00
|
|
|
dir := t.TempDir()
|
|
|
|
path := filepath.Join(dir, "test-file-store.conf")
|
2020-02-03 18:35:52 +00:00
|
|
|
|
|
|
|
store, err := NewFileStore(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating file store failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testStoreSemantics(t, store)
|
|
|
|
|
|
|
|
// Build a brand new file store and check that both IDs written
|
|
|
|
// above are still there.
|
|
|
|
store, err = NewFileStore(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating second file store failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[StateKey]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "quux",
|
|
|
|
}
|
2021-09-24 20:46:08 +01:00
|
|
|
for key, want := range expected {
|
|
|
|
bs, err := store.ReadState(key)
|
2020-02-03 18:35:52 +00:00
|
|
|
if err != nil {
|
2021-09-24 20:46:08 +01:00
|
|
|
t.Errorf("reading %q (2nd store): %v", key, err)
|
|
|
|
continue
|
2020-02-03 18:35:52 +00:00
|
|
|
}
|
|
|
|
if string(bs) != want {
|
2021-09-24 20:46:08 +01:00
|
|
|
t.Errorf("reading %q (2nd store): got %q, want %q", key, bs, want)
|
2020-02-03 18:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|