From 955adbfa3211f055e702afbec56abc806ebc70a4 Mon Sep 17 00:00:00 2001 From: Franco Date: Fri, 25 Oct 2024 21:18:17 -0300 Subject: [PATCH 1/3] Go mod tidy --- go.sum | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.sum b/go.sum index 2339512..b02b1b6 100644 --- a/go.sum +++ b/go.sum @@ -951,8 +951,8 @@ k8s.io/apiextensions-apiserver v0.31.0 h1:fZgCVhGwsclj3qCw1buVXCV6khjRzKC5eCFt24 k8s.io/apiextensions-apiserver v0.31.0/go.mod h1:b9aMDEYaEe5sdK+1T0KU78ApR/5ZVp4i56VacZYEHxk= k8s.io/apimachinery v0.31.2 h1:i4vUt2hPK56W6mlT7Ry+AO8eEsyxMD1U44NR22CLTYw= k8s.io/apimachinery v0.31.2/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo= -k8s.io/client-go v0.31.1 h1:f0ugtWSbWpxHR7sjVpQwuvw9a3ZKLXX0u0itkFXufb0= -k8s.io/client-go v0.31.1/go.mod h1:sKI8871MJN2OyeqRlmA4W4KM9KBdBUpDLu/43eGemCg= +k8s.io/client-go v0.31.2 h1:Y2F4dxU5d3AQj+ybwSMqQnpZH9F30//1ObxOKlTI9yc= +k8s.io/client-go v0.31.2/go.mod h1:NPa74jSVR/+eez2dFsEIHNa+3o09vtNaWwWwb1qSxSs= k8s.io/component-base v0.31.0 h1:/KIzGM5EvPNQcYgwq5NwoQBaOlVFrghoVGr8lG6vNRs= k8s.io/component-base v0.31.0/go.mod h1:TYVuzI1QmN4L5ItVdMSXKvH7/DtvIuas5/mm8YT3rTo= k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= From c581d4d3f4c4589d63d6b474f218922cec5cf011 Mon Sep 17 00:00:00 2001 From: Franco Date: Fri, 25 Oct 2024 21:55:58 -0300 Subject: [PATCH 2/3] Add PolMan tests --- internal/utils/policymanifest/export_test.go | 4 ++ .../policymanifest_suite_test.go | 13 ++++ internal/utils/policymanifest/polman_test.go | 68 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 internal/utils/policymanifest/export_test.go create mode 100644 internal/utils/policymanifest/policymanifest_suite_test.go create mode 100644 internal/utils/policymanifest/polman_test.go diff --git a/internal/utils/policymanifest/export_test.go b/internal/utils/policymanifest/export_test.go new file mode 100644 index 0000000..c799aa3 --- /dev/null +++ b/internal/utils/policymanifest/export_test.go @@ -0,0 +1,4 @@ +package polman + +// Export private functions for testing +var TranslateEdgedbExceptions = translateEdgedbExceptions diff --git a/internal/utils/policymanifest/policymanifest_suite_test.go b/internal/utils/policymanifest/policymanifest_suite_test.go new file mode 100644 index 0000000..fe3b36d --- /dev/null +++ b/internal/utils/policymanifest/policymanifest_suite_test.go @@ -0,0 +1,13 @@ +package polman_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestPolicymanifest(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "PolicyManifest Suite") +} diff --git a/internal/utils/policymanifest/polman_test.go b/internal/utils/policymanifest/polman_test.go new file mode 100644 index 0000000..ec9a146 --- /dev/null +++ b/internal/utils/policymanifest/polman_test.go @@ -0,0 +1,68 @@ +package polman_test + +import ( + "github.com/edgedb/edgedb-go" + policyAPI "github.com/giantswarm/policy-api/api/v1alpha1" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + + edgedbutils "github.com/giantswarm/policy-meta-operator/internal/utils/edgedb" + polman "github.com/giantswarm/policy-meta-operator/internal/utils/policymanifest" +) + +var ( + policyName = "disallow-capabilities" + // policyState = "warming" + workloadName = "hello-world" + workloadNamespace = "default" + workloadKind = "Deployment" +) + +var _ = Describe("PolicyManifest Util Package", func() { + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + + var EdgedbExceptions []edgedbutils.Exception + + BeforeEach(func() { + EdgedbExceptions = []edgedbutils.Exception{ + { + Targets: []edgedbutils.Target{ + { + Kind: workloadKind, + Names: []string{workloadName}, + Namespaces: []string{workloadNamespace}, + }, + }, + Policies: []edgedbutils.Policy{ + { + Name: policyName, + DefaultPolicyState: edgedb.OptionalStr{}, + ID: edgedb.UUID{}, + }, + }, + ID: edgedb.UUID{}, + }, + } + + }) + Context("When reconciling an edgedb Exception", func() { + It("should extract the correct PolicyAPI targets", func() { + testTranslateEdgedbExceptions := polman.TranslateEdgedbExceptions + + resultTargets := testTranslateEdgedbExceptions(EdgedbExceptions) + + expectedTargets := []policyAPI.Target{ + { + Kind: workloadKind, + Names: []string{workloadName}, + Namespaces: []string{workloadNamespace}, + }, + } + + Expect(resultTargets).To(Equal(expectedTargets)) + }) + }) + +}) From 8c8665c12e3ff85091c2d83597d24e20df51c0d0 Mon Sep 17 00:00:00 2001 From: Franco Date: Tue, 29 Oct 2024 20:44:38 -0300 Subject: [PATCH 3/3] Add edgedbutils tests --- internal/utils/edgedb/edgedb_suite_test.go | 13 ++++++ internal/utils/edgedb/edgedb_test.go | 54 ++++++++++++++++++++++ internal/utils/edgedb/export_test.go | 4 ++ 3 files changed, 71 insertions(+) create mode 100644 internal/utils/edgedb/edgedb_suite_test.go create mode 100644 internal/utils/edgedb/edgedb_test.go create mode 100644 internal/utils/edgedb/export_test.go diff --git a/internal/utils/edgedb/edgedb_suite_test.go b/internal/utils/edgedb/edgedb_suite_test.go new file mode 100644 index 0000000..cc29228 --- /dev/null +++ b/internal/utils/edgedb/edgedb_suite_test.go @@ -0,0 +1,13 @@ +package edgedbutils_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestEdgedb(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Edgedb Suite") +} diff --git a/internal/utils/edgedb/edgedb_test.go b/internal/utils/edgedb/edgedb_test.go new file mode 100644 index 0000000..ce28dba --- /dev/null +++ b/internal/utils/edgedb/edgedb_test.go @@ -0,0 +1,54 @@ +package edgedbutils_test + +import ( + "github.com/edgedb/edgedb-go" + policyAPI "github.com/giantswarm/policy-api/api/v1alpha1" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + + edgedbutils "github.com/giantswarm/policy-meta-operator/internal/utils/edgedb" +) + +var ( + workloadName = "hello-world" + workloadNamespace = "default" + workloadKind = "Deployment" +) + +var _ = Describe("edgedb Util Package", func() { + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + + var TestTargets []policyAPI.Target + + BeforeEach(func() { + TestTargets = []policyAPI.Target{ + { + Kind: workloadKind, + Names: []string{workloadName}, + Namespaces: []string{workloadNamespace}, + }, + } + + }) + Context("When reconciling a Policy API Target array", func() { + It("it should extract the correct edgedb Targets", func() { + testTranslateTargets := edgedbutils.TranslateTargetsToEdgedbTypes + + resultTargets := testTranslateTargets(TestTargets) + + expectedTargets := []edgedbutils.Target{ + { + ID: edgedb.UUID{}, + Names: []string{workloadName}, + Namespaces: []string{workloadNamespace}, + Kind: workloadKind, + }, + } + + Expect(resultTargets).To(Equal(expectedTargets)) + }) + }) + +}) diff --git a/internal/utils/edgedb/export_test.go b/internal/utils/edgedb/export_test.go new file mode 100644 index 0000000..6641a1f --- /dev/null +++ b/internal/utils/edgedb/export_test.go @@ -0,0 +1,4 @@ +package edgedbutils + +// Export private functions for testing +var TranslateTargetsToEdgedbTypes = translateTargetsToEdgedbTypes