Skip to content

Commit f36453e

Browse files
authored
chore: add pulumi test package (#31)
* chore: add pulumi test package * chore: add tests * chore: update readme file * chore: remove some descriptions from docs
1 parent 3641010 commit f36453e

File tree

4 files changed

+589
-4
lines changed

4 files changed

+589
-4
lines changed

README.md

Lines changed: 144 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
1-
# PulumiConfig
1+
### About the projects:
2+
<details>
3+
<summary>Table of contents</summary>
4+
<ol>
5+
<li>
6+
<a href="#pulumi-config">PulumiConfig Section</a>
7+
<ul>
8+
<li><a href="#features">Features</a></li>
9+
<li><a href="#installation">Installation</a></li>
10+
<li><a href="#usage">Usage</a></li>
11+
<li><a href="#examples">Examples</a></li>
12+
</ul>
13+
</li>
14+
<li>
15+
<a href="#pulumi-test">PulumiTest Section</a>
16+
<ul>
17+
<li><a href="#features-1">Features</a></li>
18+
<li><a href="#installation-1">Installation</a></li>
19+
<li><a href="#usage-1">Usage</a></li>
20+
<li><a href="#examples-1">Examples</a></li>
21+
</ul>
22+
</li>
23+
</ol>
24+
</details>
25+
26+
27+
<!-- pulumi-config -->
28+
## PulumiConfig <a id="pulumi-config"></a>
229

330
PulumiConfig is a Golang library designed to improve the way developers manage configuration in Pulumi. By leveraging Golang structs, it simplifies the process of tracking and validating configuration keys, ensuring a more efficient and error-free deployment process in cloud infrastructure projects.
431

5-
## Features
32+
### Features <a id="features"></a>
633

734
- **Seamless Integration**: Effortlessly integrates with Pulumi and Golang projects.
835
- **Automated Key Tracking**: Automatically tracks configuration keys using Golang structs.
936
- **JSON Tagging**: Supports JSON tagging for Pulumi configuration keys, including nested structs.
1037
- **[go-playground/validator](https://github.com/go-playground/validator)**, letting you define both field- and struct-level validations., allowing required values and complex validations.
1138
- **Namespace Overrides**: Use `overrideConfigNamespace` to override specific fields with values from a different namespace.
1239

13-
## Installation
40+
### Installation <a id="installation"></a>
41+
1442

1543
To integrate PulumiConfig into your Golang project, follow these steps:
1644

@@ -151,7 +179,7 @@ func main() {
151179

152180
You can use `overrideConfigNamespace` on any field-level struct tag. PulumiConfig will first load from the main namespace, and then—if `overrideConfigNamespace` is set—load the separate namespace and merge those values in.
153181

154-
### Example: Custom Field and Struct Validators
182+
### Example: Custom Field and Struct Validators <a id="examples"></a>
155183

156184
Below is a more in-depth example illustrating how you can combine PulumiConfig with the Pulumi DigitalOcean provider for domain-specific validation:
157185

@@ -290,6 +318,118 @@ func main() {
290318

291319
By combining `StructValidation` and `FieldValidation`, you can enforce both global and per-field checks for your Pulumi configurations. Adjust or extend as needed for your own providers or custom logic.
292320

321+
322+
323+
324+
325+
<!-- pulumi-test -->
326+
## PulumiTest <a id="pulumi-test"></a>
327+
328+
329+
`pulumitest` is a Go package that provides helper functions to simplify testing Pulumi resources and outputs. It simplifies Pulumi testing by abstracting common patterns and offering intuitive assertions for resource validation.
330+
331+
### Features <a id="features-1"></a>
332+
333+
- **Simplifies Testing**: Provides utility functions to compare Pulumi outputs and resources without writing boilerplate code.
334+
- **Fast Test Workflow**: Enables quick and efficient testing of Pulumi programs in Go.
335+
336+
337+
### Installation <a id="installation-1"></a>
338+
339+
To use `pulumitest`, import it into your test files:
340+
341+
```go
342+
import ("github.com/exivity/pulumiconfig/pkg/pulumitest")
343+
```
344+
345+
## Usage
346+
347+
### Basic Usage
348+
Below are the public functions provided by `pulumitest` and how to use them:
349+
350+
```bash
351+
pulumitest.AssertStringOutputEqual(t, expectedOutput, actualOutput)
352+
pulumitest.AssertMapEqual(t, expectedMap, actualMap)
353+
pulumitest.AssertStringMapEqual(t, expectedStringMap, actualStringMap)
354+
```
355+
356+
Sets the Pulumi configuration for a test.
357+
```bash
358+
pulumitest.SetPulumiConfig(t, map[string]string{
359+
"key1": "value1",
360+
"key2": "value2",
361+
})
362+
```
363+
364+
### Example: Testing Map Outputs <a id="examples-1"></a>
365+
Compares two pulumi.MapOutput values and reports if they are not equal.
366+
367+
```go
368+
package test
369+
370+
import (
371+
"testing"
372+
373+
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
374+
"github.com/exivity/pulumiconfig/pkg/pulumitest"
375+
)
376+
377+
func TestAssertMapEqual(t *testing.T) {
378+
type args struct {
379+
expected pulumi.MapOutput
380+
actual pulumi.MapOutput
381+
msgAndArgs []interface{}
382+
}
383+
tests := []struct {
384+
name string
385+
args args
386+
wantFailed bool
387+
}{
388+
{
389+
name: "nil outputs", // Both maps are empty.
390+
args: args{
391+
expected: pulumi.Map{}.ToMapOutput(),
392+
actual: pulumi.Map{}.ToMapOutput(),
393+
},
394+
wantFailed: false,
395+
},
396+
{
397+
name: "expect and actual are equal", // Both maps have the same key-value pairs.
398+
args: args{
399+
expected: pulumi.Map{
400+
"key": pulumi.String("value"),
401+
}.ToMapOutput(),
402+
actual: pulumi.Map{
403+
"key": pulumi.String("value"),
404+
}.ToMapOutput(),
405+
},
406+
wantFailed: false,
407+
},
408+
{
409+
name: "expect and actual are not equal", // Maps have different values for the same key.
410+
args: args{
411+
expected: pulumi.Map{
412+
"key": pulumi.String("value"),
413+
}.ToMapOutput(),
414+
actual: pulumi.Map{
415+
"key": pulumi.String(""),
416+
}.ToMapOutput(),
417+
},
418+
wantFailed: true,
419+
},
420+
}
421+
for _, tt := range tests {
422+
t.Run(tt.name, func(t *testing.T) {
423+
testT := &testing.T{}
424+
AssertMapEqual(testT, tt.args.expected, tt.args.actual, tt.args.msgAndArgs...)
425+
assert.Equal(t, tt.wantFailed, testT.Failed())
426+
})
427+
}
428+
}
429+
430+
```
431+
293432
## License
294433

295434
PulumiConfig is released under MIT. See the [LICENSE](./LICENSE) file for more details.
435+

pkg/pulumiconfig/validators_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
// Package pulumiconfig contains tests for validating the behavior of utility functions.
2+
// This file specifically tests the `string2Number` function, which converts strings to numbers.
3+
14
package pulumiconfig
25

36
import (
47
"reflect"
58
"testing"
69
)
710

11+
// It ensures that the function correctly converts strings to numeric types (e.g., int64, uint64, float64)
12+
// and handles invalid inputs or unsupported conversion types.
813
func Test_string2Number(t *testing.T) {
914
type args struct {
1015
s string

0 commit comments

Comments
 (0)