File: grpc_version_test.go

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (193 lines) | stat: -rw-r--r-- 4,598 bytes parent folder | download | duplicates (3)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package server

import (
	"context"
	"testing"

	"github.com/stretchr/testify/require"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"

	"github.com/hashicorp/vagrant/internal/protocolversion"
	"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
)

func TestVersionUnaryInterceptor(t *testing.T) {
	f := versionUnaryInterceptor(&vagrant_server.VersionInfo{
		Api: &vagrant_server.VersionInfo_ProtocolVersion{
			Current: 10,
			Minimum: 2,
		},

		Entrypoint: &vagrant_server.VersionInfo_ProtocolVersion{
			Current: 10,
			Minimum: 5,
		},

		Version: "1.2.3",
	})

	t.Run("no headers", func(t *testing.T) {
		require := require.New(t)

		ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs())

		// Call
		called := false
		_, err := f(ctx, nil, &grpc.UnaryServerInfo{
			FullMethod: "/hashicorp.vagrant.Vagrant/Foo",
		}, func(
			ctx context.Context,
			req interface{},
		) (interface{}, error) {
			called = true
			return nil, nil
		})
		require.False(called)
		require.Error(err)
		require.Contains(err.Error(), "is not set")
	})

	t.Run("no headers on GetVersionInfo", func(t *testing.T) {
		require := require.New(t)

		ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs())

		// Call
		called := false
		_, err := f(ctx, nil, &grpc.UnaryServerInfo{
			FullMethod: "/hashicorp.vagrant.Vagrant/GetVersionInfo",
		}, func(
			ctx context.Context,
			req interface{},
		) (interface{}, error) {
			called = true
			return nil, nil
		})
		require.True(called)
		require.NoError(err)
	})

	t.Run("no headers on a different service", func(t *testing.T) {
		require := require.New(t)

		ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs())

		// Call
		called := false
		_, err := f(ctx, nil, &grpc.UnaryServerInfo{
			FullMethod: "/hashicorp.notvagrant.Vagrant/Foo",
		}, func(
			ctx context.Context,
			req interface{},
		) (interface{}, error) {
			called = true
			return nil, nil
		})
		require.True(called)
		require.NoError(err)
	})

	t.Run("valid API", func(t *testing.T) {
		require := require.New(t)

		ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs(
			protocolversion.HeaderClientApiProtocol, "4,7",
		))

		// Call
		var actual context.Context
		called := false
		_, err := f(ctx, nil, &grpc.UnaryServerInfo{
			FullMethod: "/hashicorp.vagrant.Vagrant/Foo",
		}, func(
			ctx context.Context,
			req interface{},
		) (interface{}, error) {
			called = true
			actual = ctx
			return nil, nil
		})
		require.True(called)
		require.NoError(err)

		// Check metadata
		require.Equal(uint32(7), protocolversion.FromContext(actual))
	})

	t.Run("invalid API", func(t *testing.T) {
		require := require.New(t)

		ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs(
			protocolversion.HeaderClientApiProtocol, "11,14",
			protocolversion.HeaderClientEntrypointProtocol, "4,7",
		))

		// Call
		called := false
		_, err := f(ctx, nil, &grpc.UnaryServerInfo{
			FullMethod: "/hashicorp.vagrant.Vagrant/Foo",
		}, func(
			ctx context.Context,
			req interface{},
		) (interface{}, error) {
			called = true
			return nil, nil
		})
		require.False(called)
		require.Error(err)
		require.Contains(err.Error(), "outdated")
	})

	t.Run("valid Entrypoint", func(t *testing.T) {
		require := require.New(t)

		ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs(
			protocolversion.HeaderClientApiProtocol, "4,7",
			protocolversion.HeaderClientEntrypointProtocol, "4,6",
		))

		// Call
		var actual context.Context
		called := false
		_, err := f(ctx, nil, &grpc.UnaryServerInfo{
			FullMethod: "/hashicorp.vagrant.Vagrant/EntrypointFoo",
		}, func(
			ctx context.Context,
			req interface{},
		) (interface{}, error) {
			called = true
			actual = ctx
			return nil, nil
		})
		require.True(called)
		require.NoError(err)

		// Check metadata
		require.Equal(uint32(6), protocolversion.FromContext(actual))
	})

	t.Run("invalid Entrypoint", func(t *testing.T) {
		require := require.New(t)

		ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs(
			protocolversion.HeaderClientApiProtocol, "4,7",
			protocolversion.HeaderClientEntrypointProtocol, "2,3",
		))

		// Call
		called := false
		_, err := f(ctx, nil, &grpc.UnaryServerInfo{
			FullMethod: "/hashicorp.vagrant.Vagrant/EntrypointFoo",
		}, func(
			ctx context.Context,
			req interface{},
		) (interface{}, error) {
			called = true
			return nil, nil
		})
		require.False(called)
		require.Error(err)
		require.Contains(err.Error(), "outdated")
	})
}