kuma

Форк
0
/
snapshot_test.go 
193 строки · 5.7 Кб
1
package cache_test
2

3
import (
4
	envoy_types "github.com/envoyproxy/go-control-plane/pkg/cache/types"
5
	. "github.com/onsi/ginkgo/v2"
6
	. "github.com/onsi/gomega"
7
	"google.golang.org/protobuf/proto"
8
	"google.golang.org/protobuf/types/known/anypb"
9

10
	mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
11
	core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh"
12
	"github.com/kumahq/kuma/pkg/kds/cache"
13
)
14

15
var _ = Describe("Snapshot", func() {
16
	mustMarshalAny := func(pb proto.Message) *anypb.Any {
17
		a, err := anypb.New(pb)
18
		if err != nil {
19
			panic(err)
20
		}
21
		return a
22
	}
23

24
	Describe("Consistent()", func() {
25
		It("should handle `nil`", func() {
26
			// when
27
			var snapshot *cache.Snapshot
28
			// then
29
			Expect(snapshot.Consistent()).To(MatchError("nil snapshot"))
30
		})
31

32
		It("non-`nil` snapshot should be always consistet", func() {
33
			// when
34
			snapshot := cache.NewSnapshotBuilder().Build("v1")
35
			// then
36
			Expect(snapshot.Consistent()).To(Succeed())
37

38
			// when
39
			snapshot = cache.NewSnapshotBuilder().
40
				With(string(core_mesh.MeshType), []envoy_types.Resource{
41
					&mesh_proto.KumaResource{
42
						Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
43
						Spec: mustMarshalAny(&mesh_proto.Mesh{}),
44
					},
45
				}).
46
				Build("v2")
47
			// then
48
			Expect(snapshot.Consistent()).To(Succeed())
49
		})
50
	})
51

52
	Describe("GetResources()", func() {
53
		It("should handle `nil`", func() {
54
			// when
55
			var snapshot *cache.Snapshot
56
			// then
57
			Expect(snapshot.GetResources(string(core_mesh.MeshType))).To(BeNil())
58
		})
59

60
		It("should return Meshes", func() {
61
			// given
62
			resources := &mesh_proto.KumaResource{
63
				Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
64
				Spec: mustMarshalAny(&mesh_proto.Mesh{}),
65
			}
66
			// when
67
			snapshot := cache.NewSnapshotBuilder().
68
				With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
69
				Build("v1")
70
			// then
71
			expected := map[string]envoy_types.Resource{
72
				"mesh1.mesh1": resources,
73
			}
74
			Expect(snapshot.GetResources(string(core_mesh.MeshType))).To(Equal(expected))
75
		})
76

77
		It("should return `nil` for unsupported resource types", func() {
78
			// given
79
			resources := &mesh_proto.KumaResource{
80
				Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
81
				Spec: mustMarshalAny(&mesh_proto.Mesh{}),
82
			}
83
			// when
84
			snapshot := cache.NewSnapshotBuilder().
85
				With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
86
				Build("v1")
87
			// then
88
			Expect(snapshot.GetResources("UnsupportedType")).To(BeNil())
89
		})
90
	})
91

92
	Describe("GetVersion()", func() {
93
		It("should handle `nil`", func() {
94
			// when
95
			var snapshot *cache.Snapshot
96
			// then
97
			Expect(snapshot.GetVersion(string(core_mesh.MeshType))).To(Equal(""))
98
		})
99

100
		It("should return proper version for a supported resource type", func() {
101
			// given
102
			resources := &mesh_proto.KumaResource{
103
				Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
104
				Spec: mustMarshalAny(&mesh_proto.Mesh{}),
105
			}
106
			// when
107
			snapshot := cache.NewSnapshotBuilder().
108
				With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
109
				Build("v1")
110
			// then
111
			Expect(snapshot.GetVersion(string(core_mesh.MeshType))).To(Equal("v1"))
112
		})
113

114
		It("should return an empty string for unsupported resource type", func() {
115
			// given
116
			resources := &mesh_proto.KumaResource{
117
				Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
118
				Spec: mustMarshalAny(&mesh_proto.Mesh{}),
119
			}
120
			// when
121
			snapshot := cache.NewSnapshotBuilder().
122
				With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
123
				Build("v1")
124
			// then
125
			Expect(snapshot.GetVersion("unsupported type")).To(Equal(""))
126
		})
127
	})
128

129
	Describe("WithVersion()", func() {
130
		It("should handle `nil`", func() {
131
			// given
132
			var snapshot *cache.Snapshot
133
			// when
134
			actual := snapshot.WithVersion(string(core_mesh.MeshType), "v1")
135
			// then
136
			Expect(actual).To(BeNil())
137
		})
138

139
		It("should return a new snapshot if version has changed", func() {
140
			// given
141
			resources := &mesh_proto.KumaResource{
142
				Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
143
				Spec: mustMarshalAny(&mesh_proto.Mesh{}),
144
			}
145
			// when
146
			snapshot := cache.NewSnapshotBuilder().
147
				With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
148
				Build("v1")
149
			// when
150
			actual := snapshot.WithVersion(string(core_mesh.MeshType), "v2")
151
			// then
152
			Expect(actual.GetVersion(string(core_mesh.MeshType))).To(Equal("v2"))
153
			// and
154
			Expect(actual.GetVersion(string(core_mesh.CircuitBreakerType))).To(Equal("v1"))
155
		})
156

157
		It("should return the same snapshot if version has not changed", func() {
158
			// given
159
			resources := &mesh_proto.KumaResource{
160
				Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
161
				Spec: mustMarshalAny(&mesh_proto.Mesh{}),
162
			}
163
			// when
164
			snapshot := cache.NewSnapshotBuilder().
165
				With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
166
				Build("v1")
167
			// when
168
			actual := snapshot.WithVersion(string(core_mesh.MeshType), "v1")
169
			// then
170
			Expect(actual.GetVersion(string(core_mesh.MeshType))).To(Equal("v1"))
171
			// and
172
			Expect(actual).To(BeIdenticalTo(snapshot))
173
		})
174

175
		It("should return the same snapshot if resource type is not supported", func() {
176
			// given
177
			resources := &mesh_proto.KumaResource{
178
				Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
179
				Spec: mustMarshalAny(&mesh_proto.Mesh{}),
180
			}
181
			// when
182
			snapshot := cache.NewSnapshotBuilder().
183
				With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
184
				Build("v1")
185
			// when
186
			actual := snapshot.WithVersion("unsupported type", "v2")
187
			// then
188
			Expect(actual.GetVersion(string(core_mesh.MeshType))).To(Equal("v1"))
189
			// and
190
			Expect(actual).To(BeIdenticalTo(snapshot))
191
		})
192
	})
193
})
194

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.