Skip to content

Commit

Permalink
Merge pull request #175 from obnoxxx/fix-unit-test
Browse files Browse the repository at this point in the history
 fix the  Driver controller and ClientProfile controller  unit tests
  • Loading branch information
nb-ohad authored Nov 20, 2024
2 parents 62dc94e + 8a420d9 commit 47aebb6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/unit-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Unit Tests
on:
pull_request:
paths-ignore:
- 'vendor/*'
branches:
- main
- release-*

# cancel the in-progress workflow when PR is refreshed.
concurrency:
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
cancel-in-progress: true

permissions:
contents: read

jobs:
unittests:
name: unittests
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: setup go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
check-latest: true
- name: run unit tests
run: make test
38 changes: 36 additions & 2 deletions internal/controller/clientprofile_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

csiv1alpha1 "github.com/ceph/ceph-csi-operator/api/v1alpha1"
Expand All @@ -33,25 +34,51 @@ import (
var _ = Describe("ClientProfile Controller", func() {
Context("When reconciling a resource", func() {
const resourceName = "test-resource"
const cephConnectionName = "foo"
const namespaceName = "default"

ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default", // TODO(user):Modify as needed
Namespace: namespaceName, // TODO(user):Modify as needed
}
clientProfile := &csiv1alpha1.ClientProfile{}
typeCephConnectionName := types.NamespacedName{
Name: cephConnectionName,
Namespace: namespaceName,
}
cephConn := &csiv1alpha1.CephConnection{}

BeforeEach(func() {
err := k8sClient.Get(ctx, typeCephConnectionName, cephConn)
if err != nil && errors.IsNotFound(err) {
resource := &csiv1alpha1.CephConnection{
ObjectMeta: metav1.ObjectMeta{
Name: cephConnectionName,
Namespace: namespaceName,
},
Spec: csiv1alpha1.CephConnectionSpec{
Monitors: []string{"10.98.44.171:6789"},
},
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
}

By("creating the custom resource for the Kind ClientProfile")
err := k8sClient.Get(ctx, typeNamespacedName, clientProfile)
err = k8sClient.Get(ctx, typeNamespacedName, clientProfile)
if err != nil && errors.IsNotFound(err) {
resource := &csiv1alpha1.ClientProfile{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: "default",
},
// TODO(user): Specify other spec details if needed.
Spec: csiv1alpha1.ClientProfileSpec{
CephConnectionRef: corev1.LocalObjectReference{
Name: "foo",
},
},
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
}
Expand All @@ -65,6 +92,13 @@ var _ = Describe("ClientProfile Controller", func() {

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

cephConnection := &csiv1alpha1.CephConnection{}
err = k8sClient.Get(ctx, typeCephConnectionName, cephConnection)
Expect(err).NotTo(HaveOccurred())

By("Cleanup the cephConnection")
Expect(k8sClient.Delete(ctx, cephConnection)).To(Succeed())
})
It("should successfully reconcile the resource", func() {
By("Reconciling the created resource")
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/driver_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

var _ = Describe("Driver Controller", func() {
Context("When reconciling a resource", func() {
const resourceName = "test-resource"
const resourceName = "test.rbd.csi.ceph.com"

ctx := context.Background()

Expand Down

0 comments on commit 47aebb6

Please sign in to comment.