Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(*): test resource builder #5123

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 19 additions & 98 deletions pkg/core/managers/apis/mesh/mesh_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/kumahq/kuma/pkg/plugins/ca/provided"
"github.com/kumahq/kuma/pkg/plugins/resources/memory"
test_resources "github.com/kumahq/kuma/pkg/test/resources"
"github.com/kumahq/kuma/pkg/test/resources/builders"
"github.com/kumahq/kuma/pkg/test/resources/samples"
"github.com/kumahq/kuma/pkg/tokens/builtin/issuer"
util_proto "github.com/kumahq/kuma/pkg/util/proto"
)
Expand Down Expand Up @@ -56,25 +58,10 @@ var _ = Describe("Mesh Manager", func() {
It("should also ensure that CAs are created", func() {
// given
meshName := "mesh-1"
resKey := model.ResourceKey{
Name: meshName,
}

// when
mesh := core_mesh.MeshResource{
Spec: &mesh_proto.Mesh{
Mtls: &mesh_proto.Mesh_Mtls{
EnabledBackend: "builtin-1",
Backends: []*mesh_proto.CertificateAuthorityBackend{
{
Name: "builtin-1",
Type: "builtin",
},
},
},
},
}
err := resManager.Create(context.Background(), &mesh, store.CreateBy(resKey))
mesh := samples.MeshMTLSBuilder().WithName("mesh-1")
err := mesh.Create(resManager)

// then
Expect(err).ToNot(HaveOccurred())
Expand All @@ -87,13 +74,9 @@ var _ = Describe("Mesh Manager", func() {
It("should create default resources", func() {
// given
meshName := "mesh-1"
resKey := model.ResourceKey{
Name: meshName,
}

// when
mesh := core_mesh.NewMeshResource()
err := resManager.Create(context.Background(), mesh, store.CreateBy(resKey))
err := samples.MeshDefaultBuilder().WithName(meshName).Create(resManager)

// then
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -235,57 +218,23 @@ var _ = Describe("Mesh Manager", func() {

It("should not delete Mesh if there are Dataplanes attached", func() {
// given mesh and dataplane
err := resManager.Create(context.Background(), core_mesh.NewMeshResource(), store.CreateByKey("mesh-1", model.NoMesh))
Expect(err).ToNot(HaveOccurred())

err = resStore.Create(context.Background(), &core_mesh.DataplaneResource{
Spec: &mesh_proto.Dataplane{
Networking: &mesh_proto.Dataplane_Networking{
Address: "127.0.0.1",
Inbound: []*mesh_proto.Dataplane_Networking_Inbound{{
Port: 8080,
ServicePort: 80,
Tags: map[string]string{
"service": "mobile",
"version": "v1",
}},
},
},
},
}, store.CreateByKey("dp-1", "mesh-1"))
Expect(err).ToNot(HaveOccurred())
Expect(samples.MeshDefaultBuilder().WithName("mesh-1").Create(resManager)).To(Succeed())
Expect(samples.DataplaneBackendBuilder().WithMesh("mesh-1").Create(resStore)).To(Succeed())

// when mesh-1 is delete
err = resManager.Delete(context.Background(), core_mesh.NewMeshResource(), store.DeleteByKey("mesh-1", model.NoMesh))
err := resManager.Delete(context.Background(), core_mesh.NewMeshResource(), store.DeleteByKey("mesh-1", model.NoMesh))
// then
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("mesh: unable to delete mesh, there are still some dataplanes attached"))
})

It("should delete Mesh if there are Dataplanes attached when unsafe delete is enabled", func() {
// given mesh and dataplane
err := resManager.Create(context.Background(), core_mesh.NewMeshResource(), store.CreateByKey("mesh-1", model.NoMesh))
Expect(err).ToNot(HaveOccurred())

err = resStore.Create(context.Background(), &core_mesh.DataplaneResource{
Spec: &mesh_proto.Dataplane{
Networking: &mesh_proto.Dataplane_Networking{
Address: "127.0.0.1",
Inbound: []*mesh_proto.Dataplane_Networking_Inbound{{
Port: 8080,
ServicePort: 80,
Tags: map[string]string{
"service": "mobile",
"version": "v1",
}},
},
},
},
}, store.CreateByKey("dp-1", "mesh-1"))
Expect(err).ToNot(HaveOccurred())
Expect(samples.MeshDefaultBuilder().WithName("mesh-1").Create(resManager)).To(Succeed())
Expect(samples.DataplaneBackendBuilder().WithMesh("mesh-1").Create(resStore)).To(Succeed())

// when mesh-1 is deleted
err = unsafeDeleteResManager.Delete(context.Background(), core_mesh.NewMeshResource(), store.DeleteByKey("mesh-1", model.NoMesh))
err := unsafeDeleteResManager.Delete(context.Background(), core_mesh.NewMeshResource(), store.DeleteByKey("mesh-1", model.NoMesh))

// then
Expect(err).ToNot(HaveOccurred())
Expand All @@ -296,33 +245,18 @@ var _ = Describe("Mesh Manager", func() {
It("should not allow to change CA when mTLS is enabled", func() {
// given
meshName := "mesh-1"
resKey := model.ResourceKey{
Name: meshName,
}

// when
mesh := core_mesh.MeshResource{
Spec: &mesh_proto.Mesh{
Mtls: &mesh_proto.Mesh_Mtls{
EnabledBackend: "builtin-1",
Backends: []*mesh_proto.CertificateAuthorityBackend{
{
Name: "builtin-1",
Type: "builtin",
},
},
},
},
}
err := resManager.Create(context.Background(), &mesh, store.CreateBy(resKey))
mesh := samples.MeshMTLSBuilder().WithName(meshName)
err := mesh.Create(resManager)

// then
Expect(err).ToNot(HaveOccurred())

// when trying to change CA
mesh.Spec.Mtls.Backends[0].Name = "builtin-2"
mesh.Spec.Mtls.EnabledBackend = "builtin-2"
err = resManager.Update(context.Background(), &mesh)
err = resManager.Update(context.Background(), mesh.Build())

// then
Expect(err).To(HaveOccurred())
Expand All @@ -339,33 +273,20 @@ var _ = Describe("Mesh Manager", func() {
It("should allow to change CA when mTLS is disabled", func() {
// given
meshName := "mesh-1"
resKey := model.ResourceKey{
Name: meshName,
}

// when
mesh := core_mesh.MeshResource{
Spec: &mesh_proto.Mesh{
Mtls: &mesh_proto.Mesh_Mtls{
EnabledBackend: "",
Backends: []*mesh_proto.CertificateAuthorityBackend{
{
Name: "builtin-1",
Type: "builtin",
},
},
},
},
}
err := resManager.Create(context.Background(), &mesh, store.CreateBy(resKey))
mesh := builders.Mesh().
WithName(meshName).
WithBuiltinMTLSBackend("builtin-1")
err := mesh.Create(resManager)

// then
Expect(err).ToNot(HaveOccurred())

// when trying to enable mTLS change CA
mesh.Spec.Mtls.Backends[0].Name = "builtin-2"
mesh.Spec.Mtls.EnabledBackend = "builtin-2"
err = resManager.Update(context.Background(), &mesh)
err = resManager.Update(context.Background(), mesh.Build())

// then
Expect(err).ToNot(HaveOccurred())
Expand Down
34 changes: 14 additions & 20 deletions pkg/core/policy/dataplane_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/kumahq/kuma/pkg/core/policy"
core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh"
model "github.com/kumahq/kuma/pkg/core/xds"
"github.com/kumahq/kuma/pkg/test/resources/builders"
test_model "github.com/kumahq/kuma/pkg/test/resources/model"
)

Expand Down Expand Up @@ -158,26 +159,19 @@ var _ = Describe("Dataplane matcher", func() {
},
}),
Entry("policies have non-empty selectors (the one with the highest number of matching key-value pairs should become the best match)", testCase{
proxy: &model.Proxy{Dataplane: &core_mesh.DataplaneResource{
Spec: &mesh_proto.Dataplane{
Networking: &mesh_proto.Dataplane_Networking{
Inbound: []*mesh_proto.Dataplane_Networking_Inbound{
{
Tags: map[string]string{
"app": "example",
},
},
{
Tags: map[string]string{
"app": "example",
"version": "1.0",
"env": "prod",
},
},
},
},
},
}},
proxy: &model.Proxy{Dataplane: builders.Dataplane().
WithAddress("192.168.0.1").
AddInboundOfTags(
mesh_proto.ServiceTag, "example",
"app", "example",
).
AddInboundOfTags(
mesh_proto.ServiceTag, "example",
"app", "example",
"version", "1.0",
"env", "prod",
).
Build()},
policies: []policy.DataplanePolicy{
&core_mesh.ProxyTemplateResource{
Meta: &test_model.ResourceMeta{
Expand Down
19 changes: 5 additions & 14 deletions pkg/gc/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,17 @@ import (
"github.com/kumahq/kuma/pkg/gc"
"github.com/kumahq/kuma/pkg/plugins/resources/memory"
"github.com/kumahq/kuma/pkg/test/resources/model"
"github.com/kumahq/kuma/pkg/test/resources/samples"
"github.com/kumahq/kuma/pkg/util/proto"
)

var _ = Describe("Dataplane Collector", func() {
var rm manager.ResourceManager
createDpAndDpInsight := func(name, mesh string, disconnectTime time.Time) {
dp := &core_mesh.DataplaneResource{
Meta: &model.ResourceMeta{Name: name, Mesh: mesh},
Spec: &mesh_proto.Dataplane{
Networking: &mesh_proto.Dataplane_Networking{
Address: "192.168.0.1",
Inbound: []*mesh_proto.Dataplane_Networking_Inbound{{
Port: 8080,
Tags: map[string]string{
"kuma.io/service": "db",
},
}},
},
},
}
dp := samples.DataplaneBackendBuilder().
WithName(name).
WithMesh(mesh).
Build()
dpInsight := &core_mesh.DataplaneInsightResource{
Meta: &model.ResourceMeta{Name: name, Mesh: mesh},
Spec: &mesh_proto.DataplaneInsight{
Expand Down
25 changes: 2 additions & 23 deletions pkg/hds/authn/callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
"github.com/pkg/errors"
"google.golang.org/grpc/metadata"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh"
core_manager "github.com/kumahq/kuma/pkg/core/resources/manager"
"github.com/kumahq/kuma/pkg/core/resources/model"
core_store "github.com/kumahq/kuma/pkg/core/resources/store"
"github.com/kumahq/kuma/pkg/hds/authn"
hds_callbacks "github.com/kumahq/kuma/pkg/hds/callbacks"
"github.com/kumahq/kuma/pkg/plugins/resources/memory"
test_model "github.com/kumahq/kuma/pkg/test/resources/model"
"github.com/kumahq/kuma/pkg/test/resources/samples"
"github.com/kumahq/kuma/pkg/xds/auth"
)

Expand Down Expand Up @@ -47,27 +46,7 @@ var _ = Describe("Authn Callbacks", func() {
var resManager core_manager.ResourceManager
var callbacks hds_callbacks.Callbacks

dpRes := &core_mesh.DataplaneResource{
Meta: &test_model.ResourceMeta{
Name: "web-01",
Mesh: "default",
},
Spec: &mesh_proto.Dataplane{
Networking: &mesh_proto.Dataplane_Networking{
Address: "127.0.0.1",
Inbound: []*mesh_proto.Dataplane_Networking_Inbound{
{
Port: 8080,
ServicePort: 8081,
Tags: map[string]string{
"kuma.io/service": "web",
"kuma.io/protocol": "http",
},
},
},
},
},
}
dpRes := samples.DataplaneWeb()

BeforeEach(func() {
memStore := memory.NewStore()
Expand Down
Loading