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

Migrate examples to Ginkgo and Gomega #2

Merged
merged 1 commit into from
Jan 1, 2025
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- main
pull_request:
jobs:
test:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ W=/workspace
FILES="solution.txt cel_test.go"

# Create container
C=$($CONTAINER_ENGINE container create --rm -w $W $IMAGE_TAG sh -c "go mod download && go test ./...")
C=$($CONTAINER_ENGINE container create --rm -w $W $IMAGE_TAG sh -c "go get github.com/onsi/ginkgo/v2/ginkgo && go install github.com/onsi/ginkgo/v2/ginkgo && go mod download && ginkgo")

# Copy files from the current directory
# example/solution.txt
Expand Down
96 changes: 39 additions & 57 deletions examples/forbid-gitrepo-failing/cel_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
package main
package cel_test

import (
"encoding/json"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/undistro/cel-playground/eval"
"io/ioutil"
"testing"
)

type CelTestSuite struct {
suite.Suite
prog []byte
}
// The user solution is available as userSolution
// var userSolution []byte

func (suite *CelTestSuite) SetupTest() {
prog, err := ioutil.ReadFile("solution.txt")
if err != nil {
require.Failf(suite.T(), "Failed to read solution file: %s", err.Error())
}
suite.prog = prog
}

func (suite *CelTestSuite) TestPodSpecWithoutVolumes() {
podSpecWithoutVolumes := `
var _ = Describe("Forbid gitRepo volumes", func() {
It("should work for a Pod spec without volumes", func() {
podSpecWithoutVolumes := `
object:
spec:
containers:
Expand All @@ -36,16 +25,15 @@ object:
dnsPolicy: ClusterFirst
restartPolicy: Always
`
response, err := eval.CelEval(suite.prog, []byte(podSpecWithoutVolumes))
require.Nil(suite.T(), err)
var actual eval.EvalResponse
err = json.Unmarshal([]byte(response), &actual)
require.Nil(suite.T(), err)
require.True(suite.T(), actual.Result.(bool))
}
response, err := eval.CelEval(userSolution, []byte(podSpecWithoutVolumes))
Expect(err).NotTo(HaveOccurred())
var actual eval.EvalResponse
Expect(json.Unmarshal([]byte(response), &actual)).To(Succeed())
Expect(actual.Result.(bool)).To(BeTrue())
})

func (suite *CelTestSuite) TestPodSpecWithNonGitRepoVolumesOnly() {
podSpecWithNonGitRepoVolumesOnly := `
It("should work for a Pod spec with only non-gitRepo volumes", func() {
podSpecWithNonGitRepoVolumesOnly := `
object:
spec:
containers:
Expand All @@ -65,16 +53,15 @@ object:
persistentVolumeClaim:
claimName: busybox
`
response, err := eval.CelEval(suite.prog, []byte(podSpecWithNonGitRepoVolumesOnly))
require.Nil(suite.T(), err)
var actual eval.EvalResponse
err = json.Unmarshal([]byte(response), &actual)
require.Nil(suite.T(), err)
require.True(suite.T(), actual.Result.(bool))
}
response, err := eval.CelEval(userSolution, []byte(podSpecWithNonGitRepoVolumesOnly))
Expect(err).NotTo(HaveOccurred())
var actual eval.EvalResponse
Expect(json.Unmarshal([]byte(response), &actual)).To(Succeed())
Expect(actual.Result.(bool)).To(BeTrue())
})

func (suite *CelTestSuite) TestPodSpecWithGitRepoVolumesOnly() {
podSpecWithGitRepoVolumesOnly := `
It("should work for a Pod spec with gitRepo volumes only", func() {
podSpecWithGitRepoVolumesOnly := `
object:
spec:
containers:
Expand All @@ -95,16 +82,15 @@ object:
repository: "[email protected]:kubernetes/kubernetes.git"
revision: "9fc9ddc7bceca86e805f674caff7d7acf31fad6c"
`
response, err := eval.CelEval(suite.prog, []byte(podSpecWithGitRepoVolumesOnly))
require.Nil(suite.T(), err)
var actual eval.EvalResponse
err = json.Unmarshal([]byte(response), &actual)
require.Nil(suite.T(), err)
require.False(suite.T(), actual.Result.(bool))
}
response, err := eval.CelEval(userSolution, []byte(podSpecWithGitRepoVolumesOnly))
Expect(err).NotTo(HaveOccurred())
var actual eval.EvalResponse
Expect(json.Unmarshal([]byte(response), &actual)).To(Succeed())
Expect(actual.Result.(bool)).To(BeFalse())
})

func (suite *CelTestSuite) TestPodSpecWithGitRepoVolumes() {
podSpecWithGitRepoVolumes := `
It("should work for a Pod spec with gitRepo volumes and non-gitRepo volumes alike", func() {
podSpecWithGitRepoVolumes := `
object:
spec:
containers:
Expand All @@ -130,14 +116,10 @@ object:
repository: "[email protected]:kubernetes/kubernetes.git"
revision: "9fc9ddc7bceca86e805f674caff7d7acf31fad6c"
`
response, err := eval.CelEval(suite.prog, []byte(podSpecWithGitRepoVolumes))
require.Nil(suite.T(), err)
var actual eval.EvalResponse
err = json.Unmarshal([]byte(response), &actual)
require.Nil(suite.T(), err)
require.False(suite.T(), actual.Result.(bool))
}

func TestForbidGitRepo(t *testing.T) {
suite.Run(t, new(CelTestSuite))
}
response, err := eval.CelEval(userSolution, []byte(podSpecWithGitRepoVolumes))
Expect(err).NotTo(HaveOccurred())
var actual eval.EvalResponse
Expect(json.Unmarshal([]byte(response), &actual)).To(Succeed())
Expect(actual.Result.(bool)).To(BeFalse())
})
})
96 changes: 39 additions & 57 deletions examples/forbid-gitrepo-passing/cel_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
package main
package cel_test

import (
"encoding/json"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/undistro/cel-playground/eval"
"io/ioutil"
"testing"
)

type CelTestSuite struct {
suite.Suite
prog []byte
}
// The user solution is available as userSolution
// var userSolution []byte

func (suite *CelTestSuite) SetupTest() {
prog, err := ioutil.ReadFile("solution.txt")
if err != nil {
require.Failf(suite.T(), "Failed to read solution file: %s", err.Error())
}
suite.prog = prog
}

func (suite *CelTestSuite) TestPodSpecWithoutVolumes() {
podSpecWithoutVolumes := `
var _ = Describe("Forbid gitRepo volumes", func() {
It("should work for a Pod spec without volumes", func() {
podSpecWithoutVolumes := `
object:
spec:
containers:
Expand All @@ -36,16 +25,15 @@ object:
dnsPolicy: ClusterFirst
restartPolicy: Always
`
response, err := eval.CelEval(suite.prog, []byte(podSpecWithoutVolumes))
require.Nil(suite.T(), err)
var actual eval.EvalResponse
err = json.Unmarshal([]byte(response), &actual)
require.Nil(suite.T(), err)
require.True(suite.T(), actual.Result.(bool))
}
response, err := eval.CelEval(userSolution, []byte(podSpecWithoutVolumes))
Expect(err).NotTo(HaveOccurred())
var actual eval.EvalResponse
Expect(json.Unmarshal([]byte(response), &actual)).To(Succeed())
Expect(actual.Result.(bool)).To(BeTrue())
})

func (suite *CelTestSuite) TestPodSpecWithNonGitRepoVolumesOnly() {
podSpecWithNonGitRepoVolumesOnly := `
It("should work for a Pod spec with only non-gitRepo volumes", func() {
podSpecWithNonGitRepoVolumesOnly := `
object:
spec:
containers:
Expand All @@ -65,16 +53,15 @@ object:
persistentVolumeClaim:
claimName: busybox
`
response, err := eval.CelEval(suite.prog, []byte(podSpecWithNonGitRepoVolumesOnly))
require.Nil(suite.T(), err)
var actual eval.EvalResponse
err = json.Unmarshal([]byte(response), &actual)
require.Nil(suite.T(), err)
require.True(suite.T(), actual.Result.(bool))
}
response, err := eval.CelEval(userSolution, []byte(podSpecWithNonGitRepoVolumesOnly))
Expect(err).NotTo(HaveOccurred())
var actual eval.EvalResponse
Expect(json.Unmarshal([]byte(response), &actual)).To(Succeed())
Expect(actual.Result.(bool)).To(BeTrue())
})

func (suite *CelTestSuite) TestPodSpecWithGitRepoVolumesOnly() {
podSpecWithGitRepoVolumesOnly := `
It("should work for a Pod spec with gitRepo volumes only", func() {
podSpecWithGitRepoVolumesOnly := `
object:
spec:
containers:
Expand All @@ -95,16 +82,15 @@ object:
repository: "[email protected]:kubernetes/kubernetes.git"
revision: "9fc9ddc7bceca86e805f674caff7d7acf31fad6c"
`
response, err := eval.CelEval(suite.prog, []byte(podSpecWithGitRepoVolumesOnly))
require.Nil(suite.T(), err)
var actual eval.EvalResponse
err = json.Unmarshal([]byte(response), &actual)
require.Nil(suite.T(), err)
require.False(suite.T(), actual.Result.(bool))
}
response, err := eval.CelEval(userSolution, []byte(podSpecWithGitRepoVolumesOnly))
Expect(err).NotTo(HaveOccurred())
var actual eval.EvalResponse
Expect(json.Unmarshal([]byte(response), &actual)).To(Succeed())
Expect(actual.Result.(bool)).To(BeFalse())
})

func (suite *CelTestSuite) TestPodSpecWithGitRepoVolumes() {
podSpecWithGitRepoVolumes := `
It("should work for a Pod spec with gitRepo volumes and non-gitRepo volumes alike", func() {
podSpecWithGitRepoVolumes := `
object:
spec:
containers:
Expand All @@ -130,14 +116,10 @@ object:
repository: "[email protected]:kubernetes/kubernetes.git"
revision: "9fc9ddc7bceca86e805f674caff7d7acf31fad6c"
`
response, err := eval.CelEval(suite.prog, []byte(podSpecWithGitRepoVolumes))
require.Nil(suite.T(), err)
var actual eval.EvalResponse
err = json.Unmarshal([]byte(response), &actual)
require.Nil(suite.T(), err)
require.False(suite.T(), actual.Result.(bool))
}

func TestForbidGitRepo(t *testing.T) {
suite.Run(t, new(CelTestSuite))
}
response, err := eval.CelEval(userSolution, []byte(podSpecWithGitRepoVolumes))
Expect(err).NotTo(HaveOccurred())
var actual eval.EvalResponse
Expect(json.Unmarshal([]byte(response), &actual)).To(Succeed())
Expect(actual.Result.(bool)).To(BeFalse())
})
})
Loading
Loading