Skip to content

Commit 781d0ab

Browse files
committed
test: Add TypeScript tests for CRDs functionality and precedence behavior
1 parent 87cf444 commit 781d0ab

File tree

20 files changed

+474
-127
lines changed

20 files changed

+474
-127
lines changed

README.md

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Pulumi Cert Manager Component
1+
# Pulumi Cert Manager Component
22

33
This repo contains the Pulumi Cert Manager component for Kubernetes. This add-on automates the
44
management and issuance of TLS certificates from various issuing sources. It ensures certificates
@@ -29,6 +29,7 @@ go get github.com/pulumi/pulumi-kubernetes-cert-manager/sdk/go/kubernetes-cert-m
2929
Afterwards, import the library and instantiate it within your Pulumi program:
3030

3131
### TypeScript
32+
3233
```typescript
3334
import * as pulumi from "@pulumi/pulumi";
3435
import * as k8s from "@pulumi/kubernetes";
@@ -60,6 +61,7 @@ const cm = new certmanager.CertManager("cert-manager-deployment", {
6061
```
6162

6263
### Python
64+
6365
```python
6466
import pulumi
6567
import pulumi_kubernetes as k8s
@@ -89,51 +91,52 @@ cm = certmanager.CertManager("cert-manager-deployment",
8991
```
9092

9193
### Go
94+
9295
```go
9396
package main
9497

9598
import (
96-
kubernetes_cert_manager "github.com/pulumi/pulumi-kubernetes-cert-manager/sdk/go/kubernetes-cert-manager"
97-
corev1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/core/v1"
98-
metav1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/meta/v1"
99-
helmv3 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v3"
100-
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
99+
kubernetes_cert_manager "github.com/pulumi/pulumi-kubernetes-cert-manager/sdk/go/kubernetes-cert-manager"
100+
corev1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/core/v1"
101+
metav1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/meta/v1"
102+
helmv3 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v3"
103+
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
101104
)
102105

103106
func main() {
104-
pulumi.Run(func(ctx *pulumi.Context) error {
105-
// Create a namespace for cert-manager
106-
ns, err := corev1.NewNamespace(ctx, "cert-manager-namespace", &corev1.NamespaceArgs{
107-
Metadata: &metav1.ObjectMetaArgs{
108-
Name: pulumi.String("cert-system"),
109-
},
110-
})
111-
if err != nil {
112-
return err
113-
}
114-
115-
// Option 1: Using the new recommended approach
116-
enabled := true
117-
keep := true
118-
119-
// Install cert-manager with CRDs
120-
_, err = kubernetes_cert_manager.NewCertManager(ctx, "cert-manager-deployment", &kubernetes_cert_manager.CertManagerArgs{
121-
Crds: &kubernetes_cert_manager.CertManagerCrdsArgs{
122-
Enabled: pulumi.BoolPtr(enabled),
123-
Keep: pulumi.BoolPtr(keep), // Set to true to keep CRDs after uninstall
124-
},
125-
126-
// Option 2: Using deprecated option (not recommended)
127-
// When both InstallCRDs and Crds.Enabled are specified, Crds.Enabled takes precedence
128-
// InstallCRDs: pulumi.BoolPtr(enabled),
129-
130-
HelmOptions: &helmv3.ReleaseArgs{
131-
Namespace: ns.Metadata.Name(),
132-
},
133-
})
134-
135-
return err
136-
})
107+
pulumi.Run(func(ctx *pulumi.Context) error {
108+
// Create a namespace for cert-manager
109+
ns, err := corev1.NewNamespace(ctx, "cert-manager-namespace", &corev1.NamespaceArgs{
110+
Metadata: &metav1.ObjectMetaArgs{
111+
Name: pulumi.String("cert-system"),
112+
},
113+
})
114+
if err != nil {
115+
return err
116+
}
117+
118+
// Option 1: Using the new recommended approach
119+
enabled := true
120+
keep := true
121+
122+
// Install cert-manager with CRDs
123+
_, err = kubernetes_cert_manager.NewCertManager(ctx, "cert-manager-deployment", &kubernetes_cert_manager.CertManagerArgs{
124+
Crds: &kubernetes_cert_manager.CertManagerCrdsArgs{
125+
Enabled: pulumi.BoolPtr(enabled),
126+
Keep: pulumi.BoolPtr(keep), // Set to true to keep CRDs after uninstall
127+
},
128+
129+
// Option 2: Using deprecated option (not recommended)
130+
// When both InstallCRDs and Crds.Enabled are specified, Crds.Enabled takes precedence
131+
// InstallCRDs: pulumi.BoolPtr(enabled),
132+
133+
HelmOptions: &helmv3.ReleaseArgs{
134+
Namespace: ns.Metadata.Name(),
135+
},
136+
})
137+
138+
return err
139+
})
137140
}
138141
```
139142

@@ -148,4 +151,4 @@ if you need to override them, you may do so using the `helmOptions` parameter. R
148151
[the API docs for the `kubernetes:helm/v3:Release` Pulumi type](
149152
https://www.pulumi.com/docs/reference/pkg/kubernetes/helm/v3/release/#inputs) for a full set of choices.
150153

151-
For complete details, refer to the Pulumi Package details within the Pulumi Registry.
154+
For complete details, refer to the Pulumi Package details within the Pulumi Registry.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: crds-precedence-test-ts
2+
runtime: nodejs
3+
description: Test for verifying precedence between installCRDs and crds.enabled
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# CRDs Precedence Test for Kubernetes Cert Manager
2+
3+
This directory contains a focused test to verify the precedence behavior between the deprecated `installCRDs` field and the new `crds.enabled` field.
4+
5+
## Test Description
6+
7+
This test specifically configures the two parameters with opposing values:
8+
- `installCRDs: false` (would normally prevent CRD installation)
9+
- `crds: { enabled: true, keep: true }` (enables CRD installation)
10+
11+
The test verifies that when both parameters are specified with conflicting values, `crds.enabled` takes precedence over `installCRDs`, which is the documented behavior.
12+
13+
## Verification Method
14+
15+
The test creates:
16+
1. A cert-manager deployment with the conflicting configuration
17+
2. A custom Issuer resource that depends on the cert-manager CRDs
18+
3. A Certificate resource that further depends on the Issuer
19+
20+
If the precedence behavior is correct (crds.enabled takes precedence), then:
21+
- The CRDs will be installed (following crds.enabled=true, despite installCRDs=false)
22+
- The Issuer and Certificate resources will be created successfully
23+
24+
If the precedence behavior is incorrect, the test will fail as the custom resources cannot be created without the CRDs.
25+
26+
## Running the Test
27+
28+
```bash
29+
go test ./examples -run=TestCrdsPrecedenceTs
30+
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import * as k8s from "@pulumi/kubernetes";
2+
import * as certmanager from "@pulumi/kubernetes-cert-manager";
3+
import * as pulumi from "@pulumi/pulumi";
4+
5+
// Create test namespace for the conflicting case
6+
const ns = new k8s.core.v1.Namespace("test-ns");
7+
8+
// Test the conflict case where crds.enabled should take precedence over installCRDs
9+
// This test configures them in opposition - installCRDs=false, crds.enabled=true
10+
// If precedence works correctly, CRDs will be installed (following crds.enabled)
11+
const certManager = new certmanager.CertManager("cert-manager", {
12+
// Set the deprecated option to false - this should be overridden
13+
installCRDs: false,
14+
15+
// Set the new option to true - this should take precedence
16+
crds: {
17+
enabled: true,
18+
keep: true,
19+
},
20+
21+
helmOptions: {
22+
namespace: ns.metadata.name,
23+
version: "v1.15.3",
24+
},
25+
});
26+
27+
// Create an Issuer to verify the CRDs are properly installed
28+
// This will only succeed if crds.enabled takes precedence over installCRDs
29+
// because we set installCRDs: false but crds.enabled: true
30+
const issuer = new k8s.apiextensions.CustomResource(
31+
"test-issuer",
32+
{
33+
apiVersion: "cert-manager.io/v1",
34+
kind: "Issuer",
35+
metadata: {
36+
name: "selfsigned-issuer",
37+
namespace: ns.metadata.name,
38+
},
39+
spec: {
40+
selfSigned: {},
41+
},
42+
},
43+
{ dependsOn: certManager }
44+
);
45+
46+
// Export status for verification
47+
export const certManagerStatus = certManager.status;
48+
export const issuerName = issuer.metadata.name;
49+
50+
// Add a verification test that needs the CRD to succeed
51+
// This creates a certificate using the issuer - which will fail if CRDs weren't installed
52+
const certificate = new k8s.apiextensions.CustomResource(
53+
"test-certificate",
54+
{
55+
apiVersion: "cert-manager.io/v1",
56+
kind: "Certificate",
57+
metadata: {
58+
name: "selfsigned-cert",
59+
namespace: ns.metadata.name,
60+
},
61+
spec: {
62+
secretName: "selfsigned-cert-tls",
63+
duration: "2160h", // 90d
64+
renewBefore: "360h", // 15d
65+
subject: {
66+
organizations: ["Pulumi Test"],
67+
},
68+
isCA: false,
69+
privateKey: {
70+
algorithm: "RSA",
71+
encoding: "PKCS1",
72+
size: 2048,
73+
},
74+
usages: [
75+
"server auth",
76+
"client auth",
77+
],
78+
dnsNames: [
79+
"example.com",
80+
"www.example.com",
81+
],
82+
issuerRef: {
83+
name: issuer.metadata.name,
84+
kind: "Issuer",
85+
group: "cert-manager.io",
86+
},
87+
},
88+
},
89+
{ dependsOn: issuer }
90+
);
91+
92+
export const certificateName = certificate.metadata.name;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "crds-precedence-test-ts",
3+
"devDependencies": {
4+
"@types/node": "^14"
5+
},
6+
"dependencies": {
7+
"@pulumi/pulumi": "^3.0.0",
8+
"@pulumi/kubernetes": "^4.0.0",
9+
"@pulumi/kubernetes-cert-manager": "latest"
10+
}
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "bin",
4+
"target": "es2016",
5+
"module": "commonjs",
6+
"moduleResolution": "node",
7+
"sourceMap": true,
8+
"experimentalDecorators": true,
9+
"pretty": true,
10+
"noFallthroughCasesInSwitch": true,
11+
"noImplicitAny": true,
12+
"noImplicitReturns": true,
13+
"forceConsistentCasingInFileNames": true,
14+
"strictNullChecks": true
15+
},
16+
"files": [
17+
"index.ts"
18+
]
19+
}

examples/crds-tests-ts/Pulumi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: crds-tests-ts
2+
runtime: nodejs
3+
description: Tests for verifying CRDs functionality and backward compatibility

examples/crds-tests-ts/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# CRDs Tests for Kubernetes Cert Manager
2+
3+
This directory contains tests to verify the CRDs functionality and backward compatibility between the deprecated `installCRDs` field and the new `crds` struct.
4+
5+
## Tests Included
6+
7+
1. **Legacy Mode Test**: Uses the deprecated `installCRDs: true` parameter to install cert-manager with CRDs.
8+
2. **New Mode Test**: Uses the new `crds: { enabled: true, keep: true }` structure to install cert-manager with CRDs.
9+
3. **Conflict Mode Test**: Sets both parameters (`installCRDs: false` and `crds: { enabled: true, keep: true }`),
10+
verifying that `crds.enabled` takes precedence when both fields are specified.
11+
12+
Each test ensures that:
13+
- The cert-manager deployment succeeds
14+
- CRDs are properly installed (by creating an Issuer custom resource that depends on them)
15+
- The custom resources can be created and managed
16+
17+
## Running Tests
18+
19+
The tests can be run as part of the regular test suite:
20+
21+
```bash
22+
go test ./examples -run=TestCrdsTs
23+
```
24+
25+
Or specifically for the precedence test:
26+
27+
```bash
28+
go test ./examples -run=TestCrdsPrecedenceTs
29+
```
30+
31+
## Expected Outcomes
32+
33+
- All three deployment methods should succeed
34+
- The Custom Resources creation should succeed in all cases
35+
- For the conflict test with opposing settings (installCRDs=false, crds.enabled=true),
36+
the test will validate that crds.enabled takes precedence, resulting in CRDs being installed

0 commit comments

Comments
 (0)