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"
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"
15
var _ = Describe("Snapshot", func() {
16
mustMarshalAny := func(pb proto.Message) *anypb.Any {
17
a, err := anypb.New(pb)
24
Describe("Consistent()", func() {
25
It("should handle `nil`", func() {
27
var snapshot *cache.Snapshot
29
Expect(snapshot.Consistent()).To(MatchError("nil snapshot"))
32
It("non-`nil` snapshot should be always consistet", func() {
34
snapshot := cache.NewSnapshotBuilder().Build("v1")
36
Expect(snapshot.Consistent()).To(Succeed())
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{}),
48
Expect(snapshot.Consistent()).To(Succeed())
52
Describe("GetResources()", func() {
53
It("should handle `nil`", func() {
55
var snapshot *cache.Snapshot
57
Expect(snapshot.GetResources(string(core_mesh.MeshType))).To(BeNil())
60
It("should return Meshes", func() {
62
resources := &mesh_proto.KumaResource{
63
Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
64
Spec: mustMarshalAny(&mesh_proto.Mesh{}),
67
snapshot := cache.NewSnapshotBuilder().
68
With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
71
expected := map[string]envoy_types.Resource{
72
"mesh1.mesh1": resources,
74
Expect(snapshot.GetResources(string(core_mesh.MeshType))).To(Equal(expected))
77
It("should return `nil` for unsupported resource types", func() {
79
resources := &mesh_proto.KumaResource{
80
Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
81
Spec: mustMarshalAny(&mesh_proto.Mesh{}),
84
snapshot := cache.NewSnapshotBuilder().
85
With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
88
Expect(snapshot.GetResources("UnsupportedType")).To(BeNil())
92
Describe("GetVersion()", func() {
93
It("should handle `nil`", func() {
95
var snapshot *cache.Snapshot
97
Expect(snapshot.GetVersion(string(core_mesh.MeshType))).To(Equal(""))
100
It("should return proper version for a supported resource type", func() {
102
resources := &mesh_proto.KumaResource{
103
Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
104
Spec: mustMarshalAny(&mesh_proto.Mesh{}),
107
snapshot := cache.NewSnapshotBuilder().
108
With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
111
Expect(snapshot.GetVersion(string(core_mesh.MeshType))).To(Equal("v1"))
114
It("should return an empty string for unsupported resource type", func() {
116
resources := &mesh_proto.KumaResource{
117
Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
118
Spec: mustMarshalAny(&mesh_proto.Mesh{}),
121
snapshot := cache.NewSnapshotBuilder().
122
With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
125
Expect(snapshot.GetVersion("unsupported type")).To(Equal(""))
129
Describe("WithVersion()", func() {
130
It("should handle `nil`", func() {
132
var snapshot *cache.Snapshot
134
actual := snapshot.WithVersion(string(core_mesh.MeshType), "v1")
136
Expect(actual).To(BeNil())
139
It("should return a new snapshot if version has changed", func() {
141
resources := &mesh_proto.KumaResource{
142
Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
143
Spec: mustMarshalAny(&mesh_proto.Mesh{}),
146
snapshot := cache.NewSnapshotBuilder().
147
With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
150
actual := snapshot.WithVersion(string(core_mesh.MeshType), "v2")
152
Expect(actual.GetVersion(string(core_mesh.MeshType))).To(Equal("v2"))
154
Expect(actual.GetVersion(string(core_mesh.CircuitBreakerType))).To(Equal("v1"))
157
It("should return the same snapshot if version has not changed", func() {
159
resources := &mesh_proto.KumaResource{
160
Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
161
Spec: mustMarshalAny(&mesh_proto.Mesh{}),
164
snapshot := cache.NewSnapshotBuilder().
165
With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
168
actual := snapshot.WithVersion(string(core_mesh.MeshType), "v1")
170
Expect(actual.GetVersion(string(core_mesh.MeshType))).To(Equal("v1"))
172
Expect(actual).To(BeIdenticalTo(snapshot))
175
It("should return the same snapshot if resource type is not supported", func() {
177
resources := &mesh_proto.KumaResource{
178
Meta: &mesh_proto.KumaResource_Meta{Name: "mesh1", Mesh: "mesh1"},
179
Spec: mustMarshalAny(&mesh_proto.Mesh{}),
182
snapshot := cache.NewSnapshotBuilder().
183
With(string(core_mesh.MeshType), []envoy_types.Resource{resources}).
186
actual := snapshot.WithVersion("unsupported type", "v2")
188
Expect(actual.GetVersion(string(core_mesh.MeshType))).To(Equal("v1"))
190
Expect(actual).To(BeIdenticalTo(snapshot))