Skip to content

Commit

Permalink
Refine minimal example controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Starttoaster committed Dec 17, 2023
1 parent dbb3dc1 commit cef0a2f
Show file tree
Hide file tree
Showing 7 changed files with 495 additions and 337 deletions.
65 changes: 65 additions & 0 deletions internal/controller/chiaca_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2023 Chia Network Inc.
*/

package controller

import (
"context"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

apiv1 "github.com/chia-network/chia-operator/api/v1"
)

var _ = Describe("ChiaCA controller", func() {
var (
timeout = time.Second * 10
interval = time.Millisecond * 250
)

Context("When creating ChiaCA", func() {
It("should update its Spec with API defaults", func() {
By("By creating a new ChiaCA")
ctx := context.Background()
testCA := &apiv1.ChiaCA{
TypeMeta: metav1.TypeMeta{
APIVersion: "k8s.chia.net/v1",
Kind: "ChiaCA",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-chiaca",
Namespace: "default",
},
Spec: apiv1.ChiaCASpec{
Secret: "test-secret",
},
}
expect := &apiv1.ChiaCA{
Spec: apiv1.ChiaCASpec{
Image: "ghcr.io/chia-network/chia-operator/ca-gen:latest",
Secret: "test-secret",
},
}

// Create ChiaCA
Expect(k8sClient.Create(ctx, testCA)).Should(Succeed())

// Look up the created ChiaCA
lookupKey := types.NamespacedName{Name: testCA.Name, Namespace: testCA.Namespace}
createdChiaCA := &apiv1.ChiaCA{}
Eventually(func() bool {
err := k8sClient.Get(ctx, lookupKey, createdChiaCA)
return err == nil
}, timeout, interval).Should(BeTrue())

// Ensure the ChiaCA's spec is equal to the expected spec
Expect(createdChiaCA.Spec).Should(Equal(expect.Spec))
})
})
})
91 changes: 91 additions & 0 deletions internal/controller/chiafarmer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2023 Chia Network Inc.
*/

package controller

import (
"context"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

apiv1 "github.com/chia-network/chia-operator/api/v1"
)

var _ = Describe("ChiaFarmer controller", func() {
var (
timeout = time.Second * 10
interval = time.Millisecond * 250
)

Context("When creating ChiaFarmer", func() {
It("should update its Spec with API defaults", func() {
By("By creating a new ChiaFarmer")
ctx := context.Background()
testFarmer := &apiv1.ChiaFarmer{
TypeMeta: metav1.TypeMeta{
APIVersion: "k8s.chia.net/v1",
Kind: "ChiaFarmer",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-chiafarmer",
Namespace: "default",
},
Spec: apiv1.ChiaFarmerSpec{
ChiaConfig: apiv1.ChiaFarmerConfigSpec{
CommonChiaConfigSpec: apiv1.CommonChiaConfigSpec{
CASecretName: "test-secret",
},
FullNodePeer: "node.default.svc.cluster.local:58444",
SecretKeySpec: apiv1.ChiaKeysSpec{
Name: "testkeys",
Key: "key.txt",
},
},
},
}
expect := &apiv1.ChiaFarmer{
Spec: apiv1.ChiaFarmerSpec{
ChiaConfig: apiv1.ChiaFarmerConfigSpec{
CommonChiaConfigSpec: apiv1.CommonChiaConfigSpec{
Image: "ghcr.io/chia-network/chia:latest",
CASecretName: "test-secret",
},
FullNodePeer: "node.default.svc.cluster.local:58444",
SecretKeySpec: apiv1.ChiaKeysSpec{
Name: "testkeys",
Key: "key.txt",
},
},
CommonSpec: apiv1.CommonSpec{
ServiceType: "ClusterIP",
ImagePullPolicy: "Always",
ChiaExporterConfig: apiv1.ChiaExporterConfigSpec{
Enabled: true,
Image: "ghcr.io/chia-network/chia-exporter:latest",
},
},
},
}

// Create ChiaFarmer
Expect(k8sClient.Create(ctx, testFarmer)).Should(Succeed())

// Look up the created ChiaFarmer
lookupKey := types.NamespacedName{Name: testFarmer.Name, Namespace: testFarmer.Namespace}
createdChiaFarmer := &apiv1.ChiaFarmer{}
Eventually(func() bool {
err := k8sClient.Get(ctx, lookupKey, createdChiaFarmer)
return err == nil
}, timeout, interval).Should(BeTrue())

// Ensure the ChiaFarmer's spec equals the expected spec
Expect(createdChiaFarmer.Spec).Should(Equal(expect.Spec))
})
})
})
83 changes: 83 additions & 0 deletions internal/controller/chiaharvester_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2023 Chia Network Inc.
*/

package controller

import (
"context"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

apiv1 "github.com/chia-network/chia-operator/api/v1"
)

var _ = Describe("ChiaHarvester controller", func() {
var (
timeout = time.Second * 10
interval = time.Millisecond * 250
)

Context("When creating ChiaHarvester", func() {
It("should update its Spec with API defaults", func() {
By("By creating a new ChiaHarvester")
ctx := context.Background()
testHarvester := &apiv1.ChiaHarvester{
TypeMeta: metav1.TypeMeta{
APIVersion: "k8s.chia.net/v1",
Kind: "ChiaHarvester",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-chiaharvester",
Namespace: "default",
},
Spec: apiv1.ChiaHarvesterSpec{
ChiaConfig: apiv1.ChiaHarvesterConfigSpec{
CommonChiaConfigSpec: apiv1.CommonChiaConfigSpec{
CASecretName: "test-secret",
},
FarmerAddress: "farmer.default.svc.cluster.local",
},
},
}
expect := &apiv1.ChiaHarvester{
Spec: apiv1.ChiaHarvesterSpec{
ChiaConfig: apiv1.ChiaHarvesterConfigSpec{
CommonChiaConfigSpec: apiv1.CommonChiaConfigSpec{
Image: "ghcr.io/chia-network/chia:latest",
CASecretName: "test-secret",
},
FarmerAddress: "farmer.default.svc.cluster.local",
},
CommonSpec: apiv1.CommonSpec{
ServiceType: "ClusterIP",
ImagePullPolicy: "Always",
ChiaExporterConfig: apiv1.ChiaExporterConfigSpec{
Enabled: true,
Image: "ghcr.io/chia-network/chia-exporter:latest",
},
},
},
}

// Create ChiaHarvester
Expect(k8sClient.Create(ctx, testHarvester)).Should(Succeed())

// Look up the created ChiaHarvester
lookupKey := types.NamespacedName{Name: testHarvester.Name, Namespace: testHarvester.Namespace}
createdChiaHarvester := &apiv1.ChiaHarvester{}
Eventually(func() bool {
err := k8sClient.Get(ctx, lookupKey, createdChiaHarvester)
return err == nil
}, timeout, interval).Should(BeTrue())

// Ensure the ChiaHarvester's spec equals the expected spec
Expect(createdChiaHarvester.Spec).Should(Equal(expect.Spec))
})
})
})
82 changes: 82 additions & 0 deletions internal/controller/chianode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2023 Chia Network Inc.
*/

package controller

import (
"context"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

apiv1 "github.com/chia-network/chia-operator/api/v1"
)

var _ = Describe("ChiaNode controller", func() {
var (
timeout = time.Second * 10
interval = time.Millisecond * 250
)

Context("When creating ChiaNode", func() {
It("should update its Spec with API defaults", func() {
By("By creating a new ChiaNode")
ctx := context.Background()
testNode := &apiv1.ChiaNode{
TypeMeta: metav1.TypeMeta{
APIVersion: "k8s.chia.net/v1",
Kind: "ChiaNode",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-chianode",
Namespace: "default",
},
Spec: apiv1.ChiaNodeSpec{
ChiaConfig: apiv1.ChiaNodeConfigSpec{
CommonChiaConfigSpec: apiv1.CommonChiaConfigSpec{
CASecretName: "test-secret",
},
},
},
}
expect := &apiv1.ChiaNode{
Spec: apiv1.ChiaNodeSpec{
Replicas: 1,
ChiaConfig: apiv1.ChiaNodeConfigSpec{
CommonChiaConfigSpec: apiv1.CommonChiaConfigSpec{
Image: "ghcr.io/chia-network/chia:latest",
CASecretName: "test-secret",
},
},
CommonSpec: apiv1.CommonSpec{
ServiceType: "ClusterIP",
ImagePullPolicy: "Always",
ChiaExporterConfig: apiv1.ChiaExporterConfigSpec{
Enabled: true,
Image: "ghcr.io/chia-network/chia-exporter:latest",
},
},
},
}

// Create ChiaNode
Expect(k8sClient.Create(ctx, testNode)).Should(Succeed())

// Look up the created ChiaNode
lookupKey := types.NamespacedName{Name: testNode.Name, Namespace: testNode.Namespace}
createdChiaNode := &apiv1.ChiaNode{}
Eventually(func() bool {
err := k8sClient.Get(ctx, lookupKey, createdChiaNode)
return err == nil
}, timeout, interval).Should(BeTrue())

// Ensure the ChiaNode's spec equals the expected spec
Expect(createdChiaNode.Spec).Should(Equal(expect.Spec))
})
})
})
Loading

0 comments on commit cef0a2f

Please sign in to comment.