-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a basic test for resourcedocsgen
- Loading branch information
Showing
6 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
1 change: 1 addition & 0 deletions
1
tools/resourcedocsgen/cmd/testdata/TestMetadataBasic/_index.md.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |
15 changes: 15 additions & 0 deletions
15
tools/resourcedocsgen/cmd/testdata/TestMetadataBasic/random.yaml.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.