Skip to content

Commit 7eef028

Browse files
committed
Add gomega ID matcher for gophercloud objects
1 parent 6e20028 commit 7eef028

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

test/e2e/suites/e2e/e2e_suite_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ var _ = SynchronizedAfterSuite(func() {
8686
}, func() {
8787
endServers, err := shared.DumpOpenStackServers(e2eCtx, servers.ListOpts{})
8888
Expect(err).NotTo(HaveOccurred())
89-
Expect(endServers).To(Equal(initialServers))
89+
Expect(endServers).To(ConsistOfIDs(initialServers))
9090
endNetworks, err := shared.DumpOpenStackNetworks(e2eCtx, networks.ListOpts{})
9191
Expect(err).NotTo(HaveOccurred())
92-
Expect(endNetworks).To(Equal(initialNetworks))
92+
Expect(endNetworks).To(ConsistOfIDs(initialNetworks))
9393
endSecurityGroups, err := shared.DumpOpenStackSecurityGroups(e2eCtx, groups.ListOpts{})
9494
Expect(err).NotTo(HaveOccurred())
95-
Expect(endSecurityGroups).To(Equal(initialSecurityGroups))
95+
Expect(endSecurityGroups).To(ConsistOfIDs(initialSecurityGroups))
9696
endLoadBalancers, err := shared.DumpOpenStackLoadBalancers(e2eCtx, loadbalancers.ListOpts{})
9797
Expect(err).NotTo(HaveOccurred())
98-
Expect(endLoadBalancers).To(Equal(initialLoadBalancers))
98+
Expect(endLoadBalancers).To(ConsistOfIDs(initialLoadBalancers))
9999
endVolumes, err := shared.DumpOpenStackVolumes(e2eCtx, volumes.ListOpts{})
100100
Expect(err).NotTo(HaveOccurred())
101-
Expect(endVolumes).To(Equal(initialVolumes))
101+
Expect(endVolumes).To(ConsistOfIDs(initialVolumes))
102102

103103
shared.Node1AfterSuite(e2eCtx)
104104
})

test/e2e/suites/e2e/idmatcher_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package e2e
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
7+
"github.com/onsi/gomega"
8+
"github.com/onsi/gomega/format"
9+
"github.com/onsi/gomega/gstruct"
10+
"github.com/onsi/gomega/types"
11+
)
12+
13+
type idMatcher struct {
14+
expected interface{}
15+
}
16+
17+
var (
18+
_ types.GomegaMatcher = &idMatcher{}
19+
_ format.GomegaStringer = &idMatcher{}
20+
)
21+
22+
func (m *idMatcher) Match(actual interface{}) (bool, error) {
23+
v := reflect.ValueOf(m.expected)
24+
id := v.FieldByName("ID").String()
25+
26+
matcher := &gstruct.FieldsMatcher{
27+
Fields: gstruct.Fields{
28+
"ID": gomega.Equal(id),
29+
},
30+
IgnoreExtras: true,
31+
}
32+
33+
return matcher.Match(actual)
34+
}
35+
36+
func (m *idMatcher) FailureMessage(actual interface{}) string {
37+
return fmt.Sprintf("Expected:\n%s\nto have the same ID as:\n%s", format.Object(actual, 1), format.Object(m.expected, 1))
38+
}
39+
40+
func (m *idMatcher) NegatedFailureMessage(actual interface{}) string {
41+
return fmt.Sprintf("Expected:\n%s\nnot to have the same ID as:\n%s", format.Object(actual, 1), format.Object(m.expected, 1))
42+
}
43+
44+
func (m *idMatcher) GomegaString() string {
45+
return fmt.Sprintf("ID match for:\n%s", format.Object(m.expected, 1))
46+
}
47+
48+
func IDOf(expected interface{}) types.GomegaMatcher {
49+
return &idMatcher{expected: expected}
50+
}
51+
52+
func ConsistOfIDs[T any](expected []T) types.GomegaMatcher {
53+
matchers := make([]types.GomegaMatcher, len(expected))
54+
for i := range expected {
55+
matchers[i] = IDOf(expected[i])
56+
}
57+
58+
return gomega.ConsistOf(matchers)
59+
}

0 commit comments

Comments
 (0)