Skip to content

Commit f5c5c64

Browse files
authored
Update agent pool tests (#670)
1 parent 9024e9c commit f5c5c64

30 files changed

+151
-183
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ OPERATOR_SDK_VERSION ?= v1.37.0
5252
# Image URL to use all building/pushing image targets
5353
IMG ?= $(IMAGE_TAG_BASE):$(VERSION)
5454
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
55-
ENVTEST_K8S_VERSION = 1.30.0
55+
ENVTEST_K8S_VERSION = 1.34.0
5656

5757
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
5858
ifeq (,$(shell go env GOBIN))
@@ -96,6 +96,7 @@ help: ## Display this help.
9696
.PHONY: docs
9797
docs: crd-ref-docs ## Generate API reference documentation.
9898
$(CRD_REF_DOCS) --renderer=markdown \
99+
--max-depth=20 \
99100
--source-path ./api/v1alpha2/ \
100101
--config=./docs/config.yaml \
101102
--templates-dir=./docs/templates/markdown \

api/v1alpha2/agentpool_validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package v1alpha2
66
import (
77
"fmt"
88

9-
apierrors "k8s.io/apimachinery/pkg/api/errors"
9+
kerrors "k8s.io/apimachinery/pkg/api/errors"
1010
"k8s.io/apimachinery/pkg/runtime/schema"
1111
"k8s.io/apimachinery/pkg/util/validation/field"
1212
)
@@ -30,7 +30,7 @@ func (ap *AgentPool) ValidateSpec() error {
3030
return nil
3131
}
3232

33-
return apierrors.NewInvalid(
33+
return kerrors.NewInvalid(
3434
schema.GroupKind{Group: "", Kind: "AgentPool"},
3535
ap.Name,
3636
allErrs,

api/v1alpha2/agenttoken_validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package v1alpha2
66
import (
77
"fmt"
88

9-
apierrors "k8s.io/apimachinery/pkg/api/errors"
9+
kerrors "k8s.io/apimachinery/pkg/api/errors"
1010
"k8s.io/apimachinery/pkg/runtime/schema"
1111
"k8s.io/apimachinery/pkg/util/validation/field"
1212
)
@@ -20,7 +20,7 @@ func (t *AgentToken) ValidateSpec() error {
2020
return nil
2121
}
2222

23-
return apierrors.NewInvalid(
23+
return kerrors.NewInvalid(
2424
schema.GroupKind{Group: "", Kind: "AgentToken"},
2525
t.Name,
2626
allErrs,

api/v1alpha2/module_validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package v1alpha2
55

66
import (
7-
apierrors "k8s.io/apimachinery/pkg/api/errors"
7+
kerrors "k8s.io/apimachinery/pkg/api/errors"
88
"k8s.io/apimachinery/pkg/runtime/schema"
99
"k8s.io/apimachinery/pkg/util/validation/field"
1010
)
@@ -18,7 +18,7 @@ func (m *Module) ValidateSpec() error {
1818
return nil
1919
}
2020

21-
return apierrors.NewInvalid(
21+
return kerrors.NewInvalid(
2222
schema.GroupKind{Group: "", Kind: "Module"},
2323
m.Name,
2424
allErrs,

api/v1alpha2/project_validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"fmt"
88

99
tfc "github.com/hashicorp/go-tfe"
10-
apierrors "k8s.io/apimachinery/pkg/api/errors"
10+
kerrors "k8s.io/apimachinery/pkg/api/errors"
1111
"k8s.io/apimachinery/pkg/runtime/schema"
1212
"k8s.io/apimachinery/pkg/util/validation/field"
1313
)
@@ -21,7 +21,7 @@ func (p *Project) ValidateSpec() error {
2121
return nil
2222
}
2323

24-
return apierrors.NewInvalid(
24+
return kerrors.NewInvalid(
2525
schema.GroupKind{Group: "", Kind: "Project"},
2626
p.Name,
2727
allErrs,

api/v1alpha2/runscollector_validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package v1alpha2
55

66
import (
7-
apierrors "k8s.io/apimachinery/pkg/api/errors"
7+
kerrors "k8s.io/apimachinery/pkg/api/errors"
88
"k8s.io/apimachinery/pkg/runtime/schema"
99
"k8s.io/apimachinery/pkg/util/validation/field"
1010
)
@@ -18,7 +18,7 @@ func (rc *RunsCollector) ValidateSpec() error {
1818
return nil
1919
}
2020

21-
return apierrors.NewInvalid(
21+
return kerrors.NewInvalid(
2222
schema.GroupKind{Group: "", Kind: "RunsCollector"},
2323
rc.Name,
2424
allErrs,

api/v1alpha2/workspace_validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"fmt"
88

99
tfc "github.com/hashicorp/go-tfe"
10-
apierrors "k8s.io/apimachinery/pkg/api/errors"
10+
kerrors "k8s.io/apimachinery/pkg/api/errors"
1111
"k8s.io/apimachinery/pkg/runtime/schema"
1212
"k8s.io/apimachinery/pkg/util/validation/field"
1313
)
@@ -33,7 +33,7 @@ func (w *Workspace) ValidateSpec() error {
3333
return nil
3434
}
3535

36-
return apierrors.NewInvalid(
36+
return kerrors.NewInvalid(
3737
schema.GroupKind{Group: "", Kind: "Workspace"},
3838
w.Name,
3939
allErrs,

internal/controller/agentpool_controller.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
"os"
1212
"strconv"
1313

14+
"github.com/go-logr/logr"
15+
tfc "github.com/hashicorp/go-tfe"
1416
corev1 "k8s.io/api/core/v1"
15-
"k8s.io/apimachinery/pkg/api/errors"
17+
kerrors "k8s.io/apimachinery/pkg/api/errors"
1618
"k8s.io/apimachinery/pkg/runtime"
1719
"k8s.io/apimachinery/pkg/types"
1820
"k8s.io/client-go/tools/record"
@@ -22,9 +24,6 @@ import (
2224
"sigs.k8s.io/controller-runtime/pkg/log"
2325
"sigs.k8s.io/controller-runtime/pkg/predicate"
2426

25-
"github.com/go-logr/logr"
26-
tfc "github.com/hashicorp/go-tfe"
27-
2827
appv1alpha2 "github.com/hashicorp/hcp-terraform-operator/api/v1alpha2"
2928
"github.com/hashicorp/hcp-terraform-operator/version"
3029
)
@@ -60,7 +59,7 @@ func (r *AgentPoolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
6059
if err != nil {
6160
// 'Not found' error occurs when an object is removed from the Kubernetes
6261
// No actions are required in this case
63-
if errors.IsNotFound(err) {
62+
if kerrors.IsNotFound(err) {
6463
ap.log.Info("Agent Pool Controller", "msg", "the object is removed no further action is required")
6564
return doNotRequeue()
6665
}

internal/controller/agentpool_controller_autoscaling_test.go

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,25 @@ import (
1313
"github.com/go-logr/logr"
1414
tfc "github.com/hashicorp/go-tfe"
1515
"github.com/hashicorp/go-tfe/mocks"
16-
appv1alpha2 "github.com/hashicorp/hcp-terraform-operator/api/v1alpha2"
17-
"github.com/hashicorp/hcp-terraform-operator/internal/pointer"
1816
. "github.com/onsi/ginkgo/v2"
1917
. "github.com/onsi/gomega"
20-
gomock "go.uber.org/mock/gomock"
18+
"github.com/stretchr/testify/assert"
19+
"go.uber.org/mock/gomock"
20+
appsv1 "k8s.io/api/apps/v1"
2121
corev1 "k8s.io/api/core/v1"
22-
k8sapierrors "k8s.io/apimachinery/pkg/api/errors"
22+
kerrors "k8s.io/apimachinery/pkg/api/errors"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/apimachinery/pkg/types"
2425

25-
"github.com/stretchr/testify/assert"
26+
appv1alpha2 "github.com/hashicorp/hcp-terraform-operator/api/v1alpha2"
27+
"github.com/hashicorp/hcp-terraform-operator/internal/pointer"
2628
)
2729

2830
var _ = Describe("Agent Pool controller", Ordered, func() {
2931
var (
3032
instance *appv1alpha2.AgentPool
31-
namespacedName = newNamespacedName()
32-
agentPool = fmt.Sprintf("kubernetes-operator-agent-pool-%v", randomNumber())
33+
namespacedName types.NamespacedName
34+
agentPool string
3335
workspace = fmt.Sprintf("kubernetes-operator-%v", randomNumber())
3436
)
3537

@@ -40,6 +42,8 @@ var _ = Describe("Agent Pool controller", Ordered, func() {
4042
})
4143

4244
BeforeEach(func() {
45+
namespacedName = newNamespacedName()
46+
agentPool = fmt.Sprintf("kubernetes-operator-agent-pool-%v", randomNumber())
4347
// Create a new module object for each test
4448
instance = &appv1alpha2.AgentPool{
4549
TypeMeta: metav1.TypeMeta{
@@ -81,12 +85,8 @@ var _ = Describe("Agent Pool controller", Ordered, func() {
8185

8286
AfterEach(func() {
8387
Expect(tfClient.Workspaces.Delete(ctx, organization, workspace)).To(Succeed())
84-
// Delete Agent Pool CR
85-
Expect(k8sClient.Delete(ctx, instance)).To(Succeed())
86-
Eventually(func() bool {
87-
err := k8sClient.Get(ctx, namespacedName, instance)
88-
return k8sapierrors.IsNotFound(err)
89-
}).Should(BeTrue())
88+
cleanUpAgentPoolDeployment(instance)
89+
cleanUpAgentPool(instance, namespacedName)
9090
})
9191

9292
Context("Autoscaling", func() {
@@ -345,3 +345,36 @@ func TestPendingWorkspaceRuns(t *testing.T) {
345345
})
346346
}
347347
}
348+
349+
func cleanUpAgentPool(instance *appv1alpha2.AgentPool, nn types.NamespacedName) {
350+
Eventually(func() bool {
351+
err := k8sClient.Delete(ctx, instance)
352+
return kerrors.IsNotFound(err) || err == nil
353+
}).Should(BeTrue())
354+
355+
Eventually(func() bool {
356+
err := k8sClient.Get(ctx, nn, instance)
357+
return kerrors.IsNotFound(err)
358+
}).Should(BeTrue())
359+
360+
Eventually(func() bool {
361+
if instance.Status.AgentPoolID == "" {
362+
return true
363+
}
364+
err := tfClient.AgentPools.Delete(ctx, instance.Status.AgentPoolID)
365+
return err == nil || err == tfc.ErrResourceNotFound
366+
}).Should(BeTrue())
367+
}
368+
369+
func cleanUpAgentPoolDeployment(instance *appv1alpha2.AgentPool) {
370+
d := &appsv1.Deployment{
371+
ObjectMeta: metav1.ObjectMeta{
372+
Name: agentPoolDeploymentName(instance),
373+
Namespace: instance.GetNamespace(),
374+
},
375+
}
376+
Eventually(func() bool {
377+
err := k8sClient.Delete(ctx, d)
378+
return kerrors.IsNotFound(err)
379+
}).Should(BeTrue())
380+
}

internal/controller/agentpool_controller_deletion_policy_test.go

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import (
1010
tfc "github.com/hashicorp/go-tfe"
1111
. "github.com/onsi/ginkgo/v2"
1212
. "github.com/onsi/gomega"
13-
appsv1 "k8s.io/api/apps/v1"
1413
corev1 "k8s.io/api/core/v1"
15-
"k8s.io/apimachinery/pkg/api/errors"
14+
kerrors "k8s.io/apimachinery/pkg/api/errors"
1615
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1716
"k8s.io/apimachinery/pkg/types"
1817

@@ -22,8 +21,8 @@ import (
2221
var _ = Describe("Agent Pool controller", Ordered, func() {
2322
var (
2423
instance *appv1alpha2.AgentPool
25-
namespacedName = newNamespacedName()
26-
agentPool = fmt.Sprintf("kubernetes-operator-agent-pool-%v", randomNumber())
24+
namespacedName types.NamespacedName
25+
agentPool string
2726
)
2827

2928
BeforeAll(func() {
@@ -33,6 +32,8 @@ var _ = Describe("Agent Pool controller", Ordered, func() {
3332
})
3433

3534
BeforeEach(func() {
35+
namespacedName = newNamespacedName()
36+
agentPool = fmt.Sprintf("kubernetes-operator-agent-pool-%v", randomNumber())
3637
// Create a new agent pool custom resource for each test
3738
instance = &appv1alpha2.AgentPool{
3839
TypeMeta: metav1.TypeMeta{
@@ -66,44 +67,8 @@ var _ = Describe("Agent Pool controller", Ordered, func() {
6667
})
6768

6869
AfterEach(func() {
69-
// DELETE AGENT POOL DEPLOYMENT
70-
did := types.NamespacedName{
71-
Name: agentPoolDeploymentName(instance),
72-
Namespace: instance.GetNamespace(),
73-
}
74-
d := &appsv1.Deployment{
75-
ObjectMeta: metav1.ObjectMeta{
76-
Name: did.Name,
77-
Namespace: did.Namespace,
78-
},
79-
}
80-
Eventually(func() bool {
81-
err := k8sClient.Delete(ctx, d)
82-
return errors.IsNotFound(err) || err == nil
83-
}).Should(BeTrue())
84-
Eventually(func() bool {
85-
err := k8sClient.Get(ctx, did, d)
86-
return errors.IsNotFound(err)
87-
}).Should(BeTrue())
88-
89-
// DELETE AGENT POOL
90-
Eventually(func() bool {
91-
err := k8sClient.Delete(ctx, instance)
92-
return errors.IsNotFound(err) || err == nil
93-
}).Should(BeTrue())
94-
95-
Eventually(func() bool {
96-
err := k8sClient.Get(ctx, namespacedName, instance)
97-
return errors.IsNotFound(err)
98-
}).Should(BeTrue())
99-
100-
Eventually(func() bool {
101-
if instance.Status.AgentPoolID == "" {
102-
return true
103-
}
104-
err := tfClient.AgentPools.Delete(ctx, instance.Status.AgentPoolID)
105-
return err == tfc.ErrResourceNotFound || err == nil
106-
}).Should(BeTrue())
70+
cleanUpAgentPoolDeployment(instance)
71+
cleanUpAgentPool(instance, namespacedName)
10772
})
10873

10974
Context("Deletion Policy", func() {
@@ -123,7 +88,7 @@ var _ = Describe("Agent Pool controller", Ordered, func() {
12388
Expect(k8sClient.Delete(ctx, instance)).To(Succeed())
12489
Eventually(func() bool {
12590
err := k8sClient.Get(ctx, namespacedName, instance)
126-
return errors.IsNotFound(err)
91+
return kerrors.IsNotFound(err)
12792
}).Should(BeTrue())
12893

12994
ap, err := tfClient.AgentPools.Read(ctx, apID)
@@ -142,7 +107,7 @@ var _ = Describe("Agent Pool controller", Ordered, func() {
142107
Expect(k8sClient.Delete(ctx, instance)).To(Succeed())
143108
Eventually(func() bool {
144109
err := k8sClient.Get(ctx, namespacedName, instance)
145-
return errors.IsNotFound(err)
110+
return kerrors.IsNotFound(err)
146111
}).Should(BeTrue())
147112

148113
ap, err := tfClient.AgentPools.Read(ctx, apID)

0 commit comments

Comments
 (0)