Skip to content

Commit

Permalink
Add a basic test for resourcedocsgen
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Nov 6, 2024
1 parent 6bcae8c commit b791398
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 2 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/check-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ on:
The file path from the root of the repo to the go code to check. For example:
tools/resourcedocsgen/
Path should be the root of a go module.
jobs:
prerequisites:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
Expand All @@ -29,3 +31,20 @@ jobs:
version: v1.61
working-directory: ${{ inputs.path }}
args: --config $GITHUB_WORKSPACE/.golangci.yml

test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4
with:
sparse-checkout: |
${{ inputs.path }}
.golangci.yml
- name: Setup Go
uses: actions/setup-go@v5
- name: go-test
- run: |
cd $GITHUB_WORKSPACE/${{ inputs.path }}
go test ./... -v
53 changes: 53 additions & 0 deletions tools/resourcedocsgen/cmd/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/hexops/autogold/v2"
"github.com/stretchr/testify/require"
)

func TestMetadataBasic(t *testing.T) {
// t.Parallel() //nolint:paralleltest PackageMetadataCmd relies on global state.
cmd := PackageMetadataCmd()
metadataDir := t.TempDir()
pacakgeDocsDir := t.TempDir()
cmd.SetArgs([]string{
"--repoSlug", "pulumi/pulumi-random",
"--schemaFile", "provider/cmd/pulumi-resource-random/schema.json",
"--version", "v4.16.7",
"--metadataDir", metadataDir,
"--packageDocsDir", pacakgeDocsDir,
})
require.NoError(t, cmd.Execute())
t.Run("random.yaml", func(t *testing.T) {
t.Parallel()
autogold.ExpectFile(t, readFile(t, filepath.Join(metadataDir, "random.yaml")))
})
t.Run("_index.md", func(t *testing.T) {
t.Parallel()
autogold.ExpectFile(t, readFile(t, filepath.Join(pacakgeDocsDir, "_index.md")))
})
}

func readFile(t *testing.T, path string) string {
b, err := os.ReadFile(path)
require.NoError(t, err)
return string(b)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"---\ntitle: Random Provider\nmeta_desc: Provides an overview on how to configure the Pulumi Random provider.\nlayout: package\n---\n## Installation\n\nThe random provider is available as a package in all Pulumi languages:\n\n* JavaScript/TypeScript: [`@pulumi/random`](https://www.npmjs.com/package/@pulumi/random)\n* Python: [`pulumi-random`](https://pypi.org/project/pulumi-random/)\n* Go: [`github.com/pulumi/pulumi-random/sdk/v4/go/random`](https://github.com/pulumi/pulumi-random)\n* .NET: [`Pulumi.Random`](https://www.nuget.org/packages/Pulumi.Random)\n* Java: [`com.pulumi/random`](https://central.sonatype.com/artifact/com.pulumi/random)\n## Overview\n\nThe \"random\" provider allows the use of randomness within Pulumi\nconfigurations. This is a *logical provider*, which means that it works\nentirely within Pulumi's logic, and doesn't interact with any other\nservices.\n\nUnconstrained randomness within a Pulumi configuration would not be very\nuseful, since Pulumi's goal is to converge on a fixed configuration by\napplying a diff. Because of this, the \"random\" provider provides an idea of\n*managed randomness*: it provides resources that generate random values during\ntheir creation and then hold those values steady until the inputs are changed.\n\nEven with these resources, it is advisable to keep the use of randomness within\nPulumi configuration to a minimum, and retain it for special cases only;\nPulumi works best when the configuration is well-defined, since its behavior\ncan then be more readily predicted.\n\nUnless otherwise stated within the documentation of a specific resource, this\nprovider's results are **not** sufficiently random for cryptographic use.\n\nFor more information on the specific resources available, see the links in the\nnavigation bar. Read on for information on the general patterns that apply\nto this provider's resources.\n## Resource \"Keepers\"\n\nAs noted above, the random resources generate randomness only when they are\ncreated; the results produced are stored in the Pulumi state and re-used\nuntil the inputs change, prompting the resource to be recreated.\n\nThe resources all provide a map argument called `keepers` that can be populated\nwith arbitrary key/value pairs that should be selected such that they remain\nthe same until new random values are desired.\n\nFor example:\n\n{{< chooser language \"typescript,python,go,csharp,java,yaml\" >}}\n{{% choosable language typescript %}}\n```typescript\nimport * as pulumi from \"@pulumi/pulumi\";\nimport * as aws from \"@pulumi/aws\";\nimport * as random from \"@pulumi/random\";\n\nconst server = new random.RandomId(\"server\", {\n keepers: {\n ami_id: amiId,\n },\n byteLength: 8,\n});\nconst serverInstance = new aws.ec2.Instance(\"server\", {\n tags: {\n Name: pulumi.interpolate`web-server ${server.hex}`,\n },\n ami: server.keepers.apply(keepers => keepers?.amiId),\n});\n```\n{{% /choosable %}}\n{{% choosable language python %}}\n```python\nimport pulumi\nimport pulumi_aws as aws\nimport pulumi_random as random\n\nserver = random.RandomId(\"server\",\n keepers={\n \"ami_id\": ami_id,\n },\n byte_length=8)\nserver_instance = aws.ec2.Instance(\"server\",\n tags={\n \"Name\": server.hex.apply(lambda hex: f\"web-server {hex}\"),\n },\n ami=server.keepers[\"amiId\"])\n```\n{{% /choosable %}}\n{{% choosable language csharp %}}\n```csharp\nusing System.Collections.Generic;\nusing System.Linq;\nusing Pulumi;\nusing Aws = Pulumi.Aws;\nusing Random = Pulumi.Random;\n\nreturn await Deployment.RunAsync(() =>\n{\n var server = new Random.RandomId(\"server\", new()\n {\n Keepers =\n {\n { \"ami_id\", amiId },\n },\n ByteLength = 8,\n });\n\n var serverInstance = new Aws.Ec2.Instance(\"server\", new()\n {\n Tags =\n {\n { \"Name\", server.Hex.Apply(hex => $\"web-server {hex}\") },\n },\n Ami = server.Keepers.Apply(keepers => keepers?.AmiId),\n });\n\n});\n\n```\n{{% /choosable %}}\n{{% choosable language go %}}\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/pulumi/pulumi-aws/sdk/v6/go/aws/ec2\"\n\t\"github.com/pulumi/pulumi-random/sdk/v4/go/random\"\n\t\"github.com/pulumi/pulumi/sdk/v3/go/pulumi\"\n)\n\nfunc main() {\n\tpulumi.Run(func(ctx *pulumi.Context) error {\n\t\tserver, err := random.NewRandomId(ctx, \"server\", &random.RandomIdArgs{\n\t\t\tKeepers: pulumi.StringMap{\n\t\t\t\t\"ami_id\": pulumi.Any(amiId),\n\t\t\t},\n\t\t\tByteLength: pulumi.Int(8),\n\t\t})\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\t_, err = ec2.NewInstance(ctx, \"server\", &ec2.InstanceArgs{\n\t\t\tTags: pulumi.StringMap{\n\t\t\t\t\"Name\": server.Hex.ApplyT(func(hex string) (string, error) {\n\t\t\t\t\treturn fmt.Sprintf(\"web-server %v\", hex), nil\n\t\t\t\t}).(pulumi.StringOutput),\n\t\t\t},\n\t\t\tAmi: pulumi.String(server.Keepers.ApplyT(func(keepers map[string]string) (*string, error) {\n\t\t\t\treturn &keepers.AmiId, nil\n\t\t\t}).(pulumi.StringPtrOutput)),\n\t\t})\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t})\n}\n```\n{{% /choosable %}}\n{{% choosable language yaml %}}\n```yaml\nresources:\n server:\n type: random:RandomId\n properties:\n keepers:\n ami_id: ${amiId}\n byteLength: 8\n serverInstance:\n type: aws:ec2:Instance\n name: server\n properties:\n tags:\n Name: web-server ${server.hex}\n ami: ${server.keepers.amiId}\n```\n{{% /choosable %}}\n{{% choosable language java %}}\n```java\npackage generated_program;\n\nimport com.pulumi.Context;\nimport com.pulumi.Pulumi;\nimport com.pulumi.core.Output;\nimport com.pulumi.random.RandomId;\nimport com.pulumi.random.RandomIdArgs;\nimport com.pulumi.aws.ec2.Instance;\nimport com.pulumi.aws.ec2.InstanceArgs;\nimport java.util.List;\nimport java.util.ArrayList;\nimport java.util.Map;\nimport java.io.File;\nimport java.nio.file.Files;\nimport java.nio.file.Paths;\n\npublic class App {\n public static void main(String[] args) {\n Pulumi.run(App::stack);\n }\n\n public static void stack(Context ctx) {\n var server = new RandomId(\"server\", RandomIdArgs.builder()\n .keepers(Map.of(\"ami_id\", amiId))\n .byteLength(8)\n .build());\n\n var serverInstance = new Instance(\"serverInstance\", InstanceArgs.builder()\n .tags(Map.of(\"Name\", server.hex().applyValue(hex -> String.format(\"web-server %s\", hex))))\n .ami(server.keepers().applyValue(keepers -> keepers.amiId()))\n .build());\n\n }\n}\n```\n{{% /choosable %}}\n{{< /chooser >}}\n\nResource \"keepers\" are optional. The other arguments to each resource must\n*also* remain constant in order to retain a random result.\n\n`keepers` are *not* treated as sensitive attributes; a value used for `keepers` will be displayed in Pulumi UI output as plaintext."
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
`category: Utility
component: false
description: A Pulumi package to safely use randomness in Pulumi programs.
featured: false
logo_url: ""
name: random
native: false
package_status: ga
publisher: Pulumi
repo_url: https://github.com/pulumi/pulumi-random
schema_file_path: provider/cmd/pulumi-resource-random/schema.json
title: random
updated_on: 1729058626
version: v4.16.7
`
12 changes: 11 additions & 1 deletion tools/resourcedocsgen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/golang/glog v1.2.0
github.com/hashicorp/hcl/v2 v2.17.0
github.com/hexops/autogold/v2 v2.2.1
github.com/pkg/errors v0.9.1
github.com/pulumi/pulumi/pkg/v3 v3.137.0
github.com/pulumi/pulumi/sdk/v3 v3.137.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
golang.org/x/text v0.16.0
)

Expand All @@ -33,26 +35,32 @@ require (
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.5.0 // indirect
github.com/djherbis/times v1.5.0 // indirect
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/ettle/strcase v0.1.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.12.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/hexops/valast v1.4.4 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
Expand All @@ -63,12 +71,14 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/natefinch/atomic v1.0.1 // indirect
github.com/nightlyone/lockfile v1.0.0 // indirect
github.com/opentracing/basictracer-go v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pgavlin/fx v0.1.6 // indirect
github.com/pgavlin/goldmark v1.1.33-0.20200616210433-b5eb04559386 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/term v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pulumi/appdash v0.0.0-20231130102222-75f619a67231 // indirect
github.com/pulumi/esc v0.10.0 // indirect
github.com/pulumi/inflector v0.1.1 // indirect
Expand All @@ -85,7 +95,6 @@ require (
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/texttheater/golang-levenshtein v1.0.1 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
Expand All @@ -107,4 +116,5 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/frand v1.4.2 // indirect
mvdan.cc/gofumpt v0.5.0 // indirect
)
Loading

0 comments on commit b791398

Please sign in to comment.