Skip to content

Commit

Permalink
Fix and run existing unit/functional tests from operator-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
mangelajo committed Sep 13, 2024
1 parent 3a84dd9 commit 1e1057a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Unit/Functional tests
on:
workflow_dispatch:
pull_request:
branches:
- main

jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Run make deploy
run: make test
2 changes: 1 addition & 1 deletion config/samples/v1alpha1_exporter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ kind: Exporter
metadata:
labels:
app.kubernetes.io/name: jumpstarter-router
app.kubernetes.io/managed-by: kustomize
dut: fancy-hardware
name: exporter-sample
spec: {}
15 changes: 12 additions & 3 deletions internal/controller/client_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand All @@ -40,11 +41,11 @@ var _ = Describe("Identity Controller", func() {
Name: resourceName,
Namespace: "default", // TODO(user):Modify as needed
}
identity := &jumpstarterdevv1alpha1.Client{}
client := &jumpstarterdevv1alpha1.Client{}

BeforeEach(func() {
By("creating the custom resource for the Kind Identity")
err := k8sClient.Get(ctx, typeNamespacedName, identity)
By("creating the custom resource for the Kind Client")
err := k8sClient.Get(ctx, typeNamespacedName, client)
if err != nil && errors.IsNotFound(err) {
resource := &jumpstarterdevv1alpha1.Client{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -65,6 +66,14 @@ var _ = Describe("Identity Controller", func() {

By("Cleanup the specific resource instance Identity")
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())

// the cascade delete of secrets does not seem to work in this mocked env
Expect(k8sClient.Delete(ctx, &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName + "-token",
Namespace: "default",
},
})).To(Succeed())
})
It("should successfully reconcile the resource", func() {
By("Reconciling the created resource")
Expand Down
5 changes: 4 additions & 1 deletion internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
Expand Down Expand Up @@ -52,9 +53,11 @@ func TestControllers(t *testing.T) {
var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

os.Setenv("CONTROLLER_KEY", "somekey")

Check failure on line 56 in internal/controller/suite_test.go

View workflow job for this annotation

GitHub Actions / lint-go

Error return value of `os.Setenv` is not checked (errcheck)

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
CRDDirectoryPaths: []string{filepath.Join("..", "..", "deploy", "helm", "jumpstarter", "charts", "jumpstarter-controller", "templates", "crds")},
ErrorIfCRDPathMissing: true,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
Expand Down

0 comments on commit 1e1057a

Please sign in to comment.