-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code review: replace mutable
SetVersion()
operation with an immutab…
…le `WithVersion()`
- Loading branch information
Showing
4 changed files
with
213 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cache_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestCache(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Cache Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package cache_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
envoy_cache "github.com/envoyproxy/go-control-plane/pkg/cache" | ||
|
||
. "github.com/Kong/kuma/pkg/mads/cache" | ||
|
||
observability_proto "github.com/Kong/kuma/api/observability/v1alpha1" | ||
) | ||
|
||
var _ = Describe("Snapshot", func() { | ||
|
||
expectedType := "type.googleapis.com/kuma.observability.v1alpha1.MonitoringAssignment" | ||
|
||
Describe("GetSupportedTypes()", func() { | ||
It("should always return ['type.googleapis.com/kuma.observability.v1alpha1.MonitoringAssignment']", func() { | ||
// when | ||
var snapshot *Snapshot | ||
// then | ||
Expect(snapshot.GetSupportedTypes()).To(Equal([]string{expectedType})) | ||
|
||
// when | ||
snapshot = &Snapshot{} | ||
// then | ||
Expect(snapshot.GetSupportedTypes()).To(Equal([]string{expectedType})) | ||
}) | ||
}) | ||
|
||
Describe("Consistent()", func() { | ||
It("should handle `nil`", func() { | ||
// when | ||
var snapshot *Snapshot | ||
// then | ||
Expect(snapshot.Consistent()).To(MatchError("nil snapshot")) | ||
}) | ||
|
||
It("non-`nil` snapshot should be always consistet", func() { | ||
// when | ||
snapshot := NewSnapshot("v1", nil) | ||
// then | ||
Expect(snapshot.Consistent()).To(BeNil()) | ||
|
||
// when | ||
snapshot = NewSnapshot("v2", map[string]envoy_cache.Resource{ | ||
"backend": &observability_proto.MonitoringAssignment{ | ||
Name: "/meshes/default/dataplanes/backend", | ||
}, | ||
}) | ||
// then | ||
Expect(snapshot.Consistent()).To(BeNil()) | ||
}) | ||
}) | ||
|
||
Describe("GetResources()", func() { | ||
It("should handle `nil`", func() { | ||
// when | ||
var snapshot *Snapshot | ||
// then | ||
Expect(snapshot.GetResources(expectedType)).To(BeNil()) | ||
}) | ||
|
||
It("should return MonitoringAssignments", func() { | ||
// given | ||
assignments := map[string]envoy_cache.Resource{ | ||
"backend": &observability_proto.MonitoringAssignment{ | ||
Name: "/meshes/default/dataplanes/backend", | ||
}, | ||
} | ||
// when | ||
snapshot := NewSnapshot("v1", assignments) | ||
// then | ||
Expect(snapshot.GetResources(expectedType)).To(Equal(assignments)) | ||
}) | ||
|
||
It("should return `nil` for unsupported resource types", func() { | ||
// given | ||
assignments := map[string]envoy_cache.Resource{ | ||
"backend": &observability_proto.MonitoringAssignment{ | ||
Name: "/meshes/default/dataplanes/backend", | ||
}, | ||
} | ||
// when | ||
snapshot := NewSnapshot("v1", assignments) | ||
// then | ||
Expect(snapshot.GetResources("unsupported type")).To(BeNil()) | ||
}) | ||
}) | ||
|
||
Describe("GetVersion()", func() { | ||
It("should handle `nil`", func() { | ||
// when | ||
var snapshot *Snapshot | ||
// then | ||
Expect(snapshot.GetVersion(expectedType)).To(Equal("")) | ||
}) | ||
|
||
It("should return proper version for a supported resource type", func() { | ||
// given | ||
assignments := map[string]envoy_cache.Resource{ | ||
"backend": &observability_proto.MonitoringAssignment{ | ||
Name: "/meshes/default/dataplanes/backend", | ||
}, | ||
} | ||
// when | ||
snapshot := NewSnapshot("v1", assignments) | ||
// then | ||
Expect(snapshot.GetVersion(expectedType)).To(Equal("v1")) | ||
}) | ||
|
||
It("should return an empty string for unsupported resource type", func() { | ||
// given | ||
assignments := map[string]envoy_cache.Resource{ | ||
"backend": &observability_proto.MonitoringAssignment{ | ||
Name: "/meshes/default/dataplanes/backend", | ||
}, | ||
} | ||
// when | ||
snapshot := NewSnapshot("v1", assignments) | ||
// then | ||
Expect(snapshot.GetVersion("unsupported type")).To(Equal("")) | ||
}) | ||
}) | ||
|
||
Describe("WithVersion()", func() { | ||
It("should handle `nil`", func() { | ||
// given | ||
var snapshot *Snapshot | ||
// when | ||
actual := snapshot.WithVersion(expectedType, "v1") | ||
// then | ||
Expect(actual).To(BeNil()) | ||
}) | ||
|
||
It("should return a new snapshot if version has changed", func() { | ||
// given | ||
assignments := map[string]envoy_cache.Resource{ | ||
"backend": &observability_proto.MonitoringAssignment{ | ||
Name: "/meshes/default/dataplanes/backend", | ||
}, | ||
} | ||
snapshot := NewSnapshot("v1", assignments) | ||
// when | ||
actual := snapshot.WithVersion(expectedType, "v2") | ||
// then | ||
Expect(actual.GetVersion(expectedType)).To(Equal("v2")) | ||
// and | ||
Expect(actual).To(Equal(NewSnapshot("v2", assignments))) | ||
}) | ||
|
||
It("should return the same snapshot if version has not changed", func() { | ||
// given | ||
assignments := map[string]envoy_cache.Resource{ | ||
"backend": &observability_proto.MonitoringAssignment{ | ||
Name: "/meshes/default/dataplanes/backend", | ||
}, | ||
} | ||
snapshot := NewSnapshot("v1", assignments) | ||
// when | ||
actual := snapshot.WithVersion(expectedType, "v1") | ||
// then | ||
Expect(actual.GetVersion(expectedType)).To(Equal("v1")) | ||
// and | ||
Expect(actual).To(BeIdenticalTo(snapshot)) | ||
}) | ||
|
||
It("should return the same snapshot if resource type is not supported", func() { | ||
// given | ||
assignments := map[string]envoy_cache.Resource{ | ||
"backend": &observability_proto.MonitoringAssignment{ | ||
Name: "/meshes/default/dataplanes/backend", | ||
}, | ||
} | ||
snapshot := NewSnapshot("v1", assignments) | ||
// when | ||
actual := snapshot.WithVersion("unsupported type", "v2") | ||
// then | ||
Expect(actual.GetVersion(expectedType)).To(Equal("v1")) | ||
// and | ||
Expect(actual).To(BeIdenticalTo(snapshot)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters