File: raw_codec_test.go

package info (click to toggle)
gitlab-agent 16.1.3-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 6,324 kB
  • sloc: makefile: 175; sh: 52; ruby: 3
file content (57 lines) | stat: -rw-r--r-- 1,841 bytes parent folder | download | duplicates (2)
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
package grpctool

import (
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/grpctool/test"
	"google.golang.org/grpc/encoding"
	"google.golang.org/protobuf/testing/protocmp"
)

var (
	_ encoding.Codec = RawCodec{}
	_ encoding.Codec = RawCodecWithProtoFallback{}
)

func TestRawCodec_Roundtrip(t *testing.T) {
	input := &RawFrame{Data: []byte{1, 2, 3, 4, 5}}
	serialized, err := RawCodec{}.Marshal(input)
	require.NoError(t, err)
	output := &RawFrame{}
	err = RawCodec{}.Unmarshal(serialized, output)
	require.NoError(t, err)
	assert.Equal(t, input, output)
}

func TestRawCodec_BadType(t *testing.T) {
	serialized, err := RawCodec{}.Marshal(&test.Request{})
	require.EqualError(t, err, "RawCodec.Marshal(): unexpected source message type: *test.Request")
	assert.Empty(t, serialized)

	output := &test.Request{}
	err = RawCodec{}.Unmarshal([]byte{1, 2, 3, 4, 5}, output)
	require.EqualError(t, err, "RawCodec.Unmarshal(): unexpected target message type: *test.Request")
}

func TestRawCodecWithProtoFallback_RoundtripRaw(t *testing.T) {
	input := &RawFrame{Data: []byte{1, 2, 3, 4, 5}}
	serialized, err := RawCodecWithProtoFallback{}.Marshal(input)
	require.NoError(t, err)
	output := &RawFrame{}
	err = RawCodecWithProtoFallback{}.Unmarshal(serialized, output)
	require.NoError(t, err)
	assert.Equal(t, input, output)
}

func TestRawCodecWithProtoFallback_RoundtripNonRaw(t *testing.T) {
	input := &test.Request{S1: "bla"}
	serialized, err := RawCodecWithProtoFallback{}.Marshal(input)
	require.NoError(t, err)
	output := &test.Request{}
	err = RawCodecWithProtoFallback{}.Unmarshal(serialized, output)
	require.NoError(t, err)
	assert.Empty(t, cmp.Diff(input, output, protocmp.Transform()))
}