-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testutils: Add script-based test utilities
Add testutils.CommandBuilder for creating commands for testcript. This allows implementing tests as scripts, which becomes useful when tests perform multiple steps on tables and need to verify the output each step. Signed-off-by: Jussi Maki <[email protected]>
- Loading branch information
Showing
5 changed files
with
214 additions
and
1 deletion.
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
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,121 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Cilium | ||
|
||
package testutils | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"slices" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/cilium/statedb" | ||
"github.com/rogpeppe/go-internal/testscript" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type Cmd = func(ts *testscript.TestScript, neg bool, args []string) | ||
|
||
func ShowTableCmd[Obj statedb.TableWritable](db *statedb.DB, tbl statedb.Table[Obj]) Cmd { | ||
var zero Obj | ||
return func(ts *testscript.TestScript, neg bool, args []string) { | ||
var buf bytes.Buffer | ||
w := tabwriter.NewWriter(&buf, 5, 4, 3, ' ', 0) | ||
fmt.Fprintf(w, "%s\n", strings.Join(zero.TableHeader(), "\t")) | ||
for obj := range tbl.All(db.ReadTxn()) { | ||
fmt.Fprintf(w, "%s\n", strings.Join(obj.TableRow(), "\t")) | ||
} | ||
w.Flush() | ||
ts.Logf("%s", buf.String()) | ||
} | ||
} | ||
|
||
func CompareTableCmd[Obj statedb.TableWritable](db *statedb.DB, tbl statedb.Table[Obj]) Cmd { | ||
var zero Obj | ||
return func(ts *testscript.TestScript, neg bool, args []string) { | ||
if len(args) != 1 { | ||
ts.Fatalf("usage: cmp file") | ||
} | ||
|
||
data := ts.ReadFile(args[0]) | ||
lines := strings.Split(data, "\n") | ||
if len(lines) < 1 { | ||
ts.Fatalf("input too short") | ||
} | ||
|
||
header := zero.TableHeader() | ||
columnNames := strings.Split(lines[0], "\t") | ||
columns := make([]int, 0, len(header)) | ||
loop: | ||
for _, name := range columnNames { | ||
for i, name2 := range header { | ||
if strings.EqualFold(name, name2) { | ||
columns = append(columns, i) | ||
continue loop | ||
} | ||
} | ||
ts.Fatalf("column %q not part of %v", name, header) | ||
} | ||
lines = lines[1:] | ||
for obj := range tbl.All(db.ReadTxn()) { | ||
row := obj.TableRow() | ||
row = takeColumns(row, columns) | ||
line := strings.Split(lines[0], "\t") | ||
lines = lines[1:] | ||
if !slices.Equal(row, line) { | ||
ts.Fatalf("mismatch: expected %#v, got %#v", line, row) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func takeColumns[T any](xs []T, idxs []int) []T { | ||
// Assuming idxs is sorted. | ||
for i, idx := range idxs { | ||
xs[i] = xs[idx] | ||
} | ||
return xs[:len(idxs)] | ||
} | ||
|
||
func InsertCmd[Obj any](db *statedb.DB, tbl statedb.RWTable[Obj]) Cmd { | ||
return func(ts *testscript.TestScript, neg bool, args []string) { | ||
if len(args) == 0 { | ||
ts.Fatalf("usage: insert path...") | ||
} | ||
wtxn := db.WriteTxn(tbl) | ||
defer wtxn.Commit() | ||
for _, arg := range args { | ||
data := ts.ReadFile(arg) | ||
var obj Obj | ||
if err := yaml.Unmarshal([]byte(data), &obj); err != nil { | ||
ts.Fatalf("Unmarshal(%s): %s", arg, err) | ||
} | ||
_, _, err := tbl.Insert(wtxn, obj) | ||
if err != nil { | ||
ts.Fatalf("Insert(%s): %s", arg, err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func DeleteCmd[Obj any](db *statedb.DB, tbl statedb.RWTable[Obj]) Cmd { | ||
return func(ts *testscript.TestScript, neg bool, args []string) { | ||
if len(args) == 0 { | ||
ts.Fatalf("usage: delete path...") | ||
} | ||
wtxn := db.WriteTxn(tbl) | ||
defer wtxn.Commit() | ||
for _, arg := range args { | ||
data := ts.ReadFile(arg) | ||
var obj Obj | ||
if err := yaml.Unmarshal([]byte(data), &obj); err != nil { | ||
ts.Fatalf("Unmarshal(%s): %s", arg, err) | ||
} | ||
_, _, err := tbl.Delete(wtxn, obj) | ||
if err != nil { | ||
ts.Fatalf("Delete(%s): %s", arg, err) | ||
} | ||
} | ||
} | ||
} |
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,63 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Cilium | ||
|
||
package testutils_test | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cilium/statedb" | ||
"github.com/cilium/statedb/index" | ||
"github.com/cilium/statedb/testutils" | ||
"github.com/rogpeppe/go-internal/testscript" | ||
) | ||
|
||
type object struct { | ||
ID uint64 | ||
Name string | ||
Tags []string | ||
} | ||
|
||
func (o object) TableHeader() []string { | ||
return []string{"ID", "Name", "Tags"} | ||
} | ||
|
||
func (o object) TableRow() []string { | ||
return []string{ | ||
strconv.FormatUint(o.ID, 10), | ||
o.Name, | ||
strings.Join(o.Tags, ", "), | ||
} | ||
} | ||
|
||
var idIdx = statedb.Index[object, uint64]{ | ||
Name: "id", | ||
FromObject: func(obj object) index.KeySet { | ||
return index.NewKeySet(index.Uint64(obj.ID)) | ||
}, | ||
FromKey: index.Uint64, | ||
Unique: true, | ||
} | ||
|
||
func TestScriptCommands(t *testing.T) { | ||
db := statedb.New() | ||
tbl, err := statedb.NewTable("test", idIdx) | ||
if err != nil { | ||
t.Fatalf("NewTable: %s", err) | ||
} | ||
if err := db.RegisterTable(tbl); err != nil { | ||
t.Fatalf("RegisterTable: %s", err) | ||
} | ||
|
||
testscript.Run(t, testscript.Params{ | ||
Dir: "testdata", | ||
Cmds: map[string]testutils.Cmd{ | ||
"cmp_objects": testutils.CompareTableCmd(db, tbl), | ||
"show_objects": testutils.ShowTableCmd(db, tbl), | ||
"insert_object": testutils.InsertCmd(db, tbl), | ||
"delete_object": testutils.DeleteCmd(db, tbl), | ||
}, | ||
}) | ||
} |
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,25 @@ | ||
insert_object example.yaml | ||
show_objects | ||
cmp_objects example.table | ||
cmp_objects example_only_ids.table | ||
|
||
delete_object example.yaml | ||
cmp_objects empty.table | ||
|
||
-- example.yaml -- | ||
id: 123 | ||
name: quux | ||
tags: | ||
- foo | ||
- bar | ||
|
||
-- example.table -- | ||
ID Name Tags | ||
123 quux foo, bar | ||
|
||
-- example_only_ids.table -- | ||
ID | ||
123 | ||
|
||
-- empty.table -- | ||
ID |