Skip to content

Commit

Permalink
write protobuf in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu committed May 31, 2023
1 parent b2369bd commit cbaebfa
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 19 deletions.
10 changes: 5 additions & 5 deletions pkg/roles/dns/api_records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestAPIRecordsGet(t *testing.T) {
types.KeyZones,
zone,
).String(),
tests.MustJSON(dns.ZoneContext{}),
tests.MustPB(&types.Zone{}),
)
inst.KV().Put(
ctx,
Expand All @@ -35,7 +35,7 @@ func TestAPIRecordsGet(t *testing.T) {
"test",
"A",
).String(),
tests.MustJSON(types.Record{
tests.MustPB(&types.Record{
Data: "192.0.2.1",
}),
)
Expand All @@ -61,7 +61,7 @@ func TestAPIRecordsPut(t *testing.T) {
types.KeyZones,
name,
).String(),
tests.MustJSON(dns.ZoneContext{}),
tests.MustPB(&types.Zone{}),
)
assert.NoError(t, role.APIRecordsPut().Interact(ctx, dns.APIRecordsPutInput{
Zone: name,
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestAPIRecordsDelete(t *testing.T) {
types.KeyZones,
zone,
).String(),
tests.MustJSON(dns.ZoneContext{}),
tests.MustPB(&types.Zone{}),
)
inst.KV().Put(
ctx,
Expand All @@ -111,7 +111,7 @@ func TestAPIRecordsDelete(t *testing.T) {
"test",
"A",
).String(),
tests.MustJSON(types.Record{
tests.MustPB(&types.Record{
Data: "192.0.2.1",
}),
)
Expand Down
4 changes: 2 additions & 2 deletions pkg/roles/dns/api_zones_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAPIZonesGet(t *testing.T) {
types.KeyZones,
"test.",
).String(),
tests.MustJSON(dns.ZoneContext{}),
tests.MustPB(&types.Zone{}),
)

var output dns.APIZonesGetOutput
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestAPIZonesDelete(t *testing.T) {
types.KeyZones,
name,
).String(),
tests.MustJSON(dns.ZoneContext{}),
tests.MustPB(&types.Zone{}),
)

assert.NoError(t, role.APIZonesDelete().Interact(ctx, dns.APIZonesDeleteInput{
Expand Down
2 changes: 1 addition & 1 deletion pkg/roles/dns/handler_coredns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestRoleDNSHandlerCoreDNS(t *testing.T) {
types.KeyZones,
".",
).String(),
tests.MustJSON(types.Zone{
tests.MustPB(&types.Zone{
HandlerConfigs: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
Expand Down
12 changes: 6 additions & 6 deletions pkg/roles/dns/handler_etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestRoleDNS_Etcd(t *testing.T) {
types.KeyZones,
".",
).String(),
tests.MustJSON(types.Zone{
tests.MustPB(&types.Zone{
HandlerConfigs: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
Expand All @@ -44,7 +44,7 @@ func TestRoleDNS_Etcd(t *testing.T) {
types.DNSRecordTypeA,
"0",
).String(),
tests.MustJSON(types.Record{
tests.MustPB(&types.Record{
Data: "10.1.2.3",
}),
)
Expand All @@ -70,7 +70,7 @@ func TestRoleDNS_Etcd_Wildcard(t *testing.T) {
types.KeyZones,
".",
).String(),
tests.MustJSON(types.Zone{
tests.MustPB(&types.Zone{
HandlerConfigs: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
Expand All @@ -90,7 +90,7 @@ func TestRoleDNS_Etcd_Wildcard(t *testing.T) {
types.DNSRecordTypeA,
"0",
).String(),
tests.MustJSON(types.Record{
tests.MustPB(&types.Record{
Data: "10.1.2.3",
}),
)
Expand All @@ -116,7 +116,7 @@ func TestRoleDNS_Etcd_WildcardNested(t *testing.T) {
types.KeyZones,
".",
).String(),
tests.MustJSON(types.Zone{
tests.MustPB(&types.Zone{
HandlerConfigs: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
Expand All @@ -136,7 +136,7 @@ func TestRoleDNS_Etcd_WildcardNested(t *testing.T) {
types.DNSRecordTypeA,
"0",
).String(),
tests.MustJSON(types.Record{
tests.MustPB(&types.Record{
Data: "10.1.2.3",
}),
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/roles/dns/handler_forward_blocky_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestRoleDNS_BlockyForwarder(t *testing.T) {
types.KeyZones,
".",
).String(),
tests.MustJSON(types.Zone{
tests.MustPB(&types.Zone{
HandlerConfigs: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
Expand Down
6 changes: 3 additions & 3 deletions pkg/roles/dns/handler_forward_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestRoleDNS_IPForwarder_v4(t *testing.T) {
types.KeyZones,
".",
).String(),
tests.MustJSON(types.Zone{
tests.MustPB(&types.Zone{
HandlerConfigs: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestRoleDNS_IPForwarder_v4_Cache(t *testing.T) {
types.KeyZones,
".",
).String(),
tests.MustJSON(types.Zone{
tests.MustPB(&types.Zone{
HandlerConfigs: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestRoleDNS_IPForwarder_v6(t *testing.T) {
types.KeyZones,
".",
).String(),
tests.MustJSON(types.Zone{
tests.MustPB(&types.Zone{
HandlerConfigs: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
Expand Down
2 changes: 1 addition & 1 deletion pkg/roles/dns/zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestRoleDNSZoneFind(t *testing.T) {
types.KeyZones,
".",
).String(),
tests.MustJSON(types.Zone{
tests.MustPB(&types.Zone{
HandlerConfigs: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
Expand Down
10 changes: 10 additions & 0 deletions pkg/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
clientv3 "go.etcd.io/etcd/client/v3"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)

func MustJSON(in interface{}) string {
Expand All @@ -26,6 +28,14 @@ func MustJSON(in interface{}) string {
return string(j)
}

func MustPB(m protoreflect.ProtoMessage) string {
raw, err := proto.Marshal(m)
if err != nil {
panic(err)
}
return string(raw)
}

func Context() context.Context {
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) //nolint
return sentry.StartTransaction(ctx, "test").Context()
Expand Down

0 comments on commit cbaebfa

Please sign in to comment.