This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfilestore_test_suite.go
120 lines (103 loc) · 3.07 KB
/
filestore_test_suite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2019 Google LLC
//
// 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
//
// https://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 filestore
import (
"context"
"errors"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
log "github.com/golang/glog"
)
// suite contains store tests and the underlying FileStore implementation.
type suite struct {
builder func() FileStore
}
// RunTestSuite runs all generic store tests.
//
// The tests use the provided builder to instantiate a FileStore.
// The builder is expected to always return a valid Store.
func RunTestSuite(t *testing.T, builder func() FileStore) {
s := &suite{builder}
s.Run(t)
}
// Run runs Test* methods of the suite as subtests.
func (s *suite) Run(t *testing.T) {
sv := reflect.ValueOf(s)
st := reflect.TypeOf(s)
for i := 0; i < sv.NumMethod(); i++ {
n := st.Method(i).Name
if strings.HasPrefix(n, "Test") {
mv := sv.MethodByName(n)
mt := mv.Type()
if mt.NumIn() != 1 || !reflect.TypeOf(t).AssignableTo(mt.In(0)) {
log.Fatalf("Method %q of the test suite must have 1 argument of type *testing.T", n)
}
if mt.NumOut() != 0 {
log.Fatalf("Method %q of the test suite must have no return value", n)
}
m := mv.Interface().(func(t *testing.T))
t.Run(n, m)
}
}
}
var (
dir1 = "/location1/zone1/"
path1 = filepath.Join(dir1, "rulefile1")
rules1 = []byte("rule 1\nrule 2\nrule3")
path2 = filepath.Join(dir1, "rulefile2")
rules2 = []byte("rule 4\nrule 5\nrule6")
dir2 = "/location2/zone2/"
path3 = filepath.Join(dir2, "rulefile1")
rules3 = []byte("rule 7\nrule 8\nrule9")
dir3 = "/location3/zone3/"
path4 = filepath.Join(dir3, "rulefile1")
rules4 = []byte("rule 1\nrule 2\nrule3")
)
func (s *suite) TestAddRuleFile(t *testing.T) {
st := s.builder()
ctx := context.Background()
if err := st.AddRuleFile(ctx, path1, rules1); err != nil {
t.Error(err)
}
}
func (s *suite) TestGetRuleFile(t *testing.T) {
st := s.builder()
ctx := context.Background()
if err := st.AddRuleFile(ctx, path1, rules1); err != nil {
t.Error(err)
}
got, err := st.GetRuleFile(ctx, path1)
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(rules1, got); diff != "" {
t.Errorf("expectation mismatch:\n%s", diff)
}
}
func (s *suite) TestDeleteRuleFile(t *testing.T) {
st := s.builder()
ctx := context.Background()
if err := st.AddRuleFile(ctx, path1, rules1); err != nil {
t.Error(err)
}
if err := st.DeleteRuleFile(ctx, path1); err != nil {
t.Error(err)
}
if _, err := st.GetRuleFile(ctx, path1); err == nil {
t.Error(errors.New("returning a non-existent rule file should have raised an error"))
}
}