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

Add extra unit tests #51

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
13 changes: 13 additions & 0 deletions internal/utils/edgedb/edgedb_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
54 changes: 54 additions & 0 deletions internal/utils/edgedb/edgedb_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})

})
4 changes: 4 additions & 0 deletions internal/utils/edgedb/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package edgedbutils

// Export private functions for testing
var TranslateTargetsToEdgedbTypes = translateTargetsToEdgedbTypes
4 changes: 4 additions & 0 deletions internal/utils/policymanifest/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package polman

// Export private functions for testing
var TranslateEdgedbExceptions = translateEdgedbExceptions
13 changes: 13 additions & 0 deletions internal/utils/policymanifest/policymanifest_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
68 changes: 68 additions & 0 deletions internal/utils/policymanifest/polman_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})

})