pulumi/pkg/codegen/testing/test/go.go

182 lines
5.0 KiB
Go
Raw Permalink Normal View History

Don't publish test code in `pkg/codegen` (#17517) Presently, we implement code generation (e.g. for SDKs and programs/`pulumi convert`) on a per-language basis in `pkg/codegen`. Alongside these implementations, we have a set of tests built using a common framework that use snapshots to verify that code generation doesn't break when changes are made. Unfortunately, due to the way things are currently laid out in our repository, these tests and their dependencies are shipped as part of the `pkg/codegen` package. This commit brings @blampe's work in https://github.com/pulumi/pulumi/pull/16011 up to date and fixes this by taking the following actions: * Test harnesses that were previously located in `pkg/codegen/<language>/test.go` are moved to `pkg/codegen/testing/test/<language>.go`. By default, Go excludes files ending in `_test.go`, but *not* files named e.g. `test.go`. It might seem logical therefore to just rename these files (e.g. to `codegen_test.go`), so that they can continue to live alongside their language implementations. Unfortunately, while this fixes one problem (dependencies pulling in test code), it introduces another -- the existing `test.go` files actually exist to implement an interface which is used by the common codegen-test framework we have. Moving to `*_test.go` files would make them invisible to the actual modules which run the tests through that framework. * Test code is consequently refactored to clean up the separation of test details (e.g. relevant fixtures) and execution implementation (working directory, means of program generation). * The `gen_program_test/generate.go` program, which we use to generate the "batch tests", has been updated so that it respects the new code layout and organisation. As laid out by @blampe in #16011 (and shamelessly copied here), this brings a number of benefits. Test dependencies are no longer included in non-test packages that are consumed downstream. As an example, this takes the `pulumi-language-go` binary from ~61MB down to ~36MB, and speeds up build times: ``` go clean -cache && time go build . (master) go build . 81.06s user 17.36s system 470% cpu 20.933 total (PR) go build . 47.77s user 9.20s system 530% cpu 10.737 total ``` This doesn't completely remove these dependencies from downstream because most also include `pkg/v3/testing/integration` in tests. It does only compile these dependencies into the test binary, though. Before: ``` ❯ go mod why github.com/aws/aws-sdk-go-v2/service/sts github.com/pulumi/pulumi-random/provider/v4/cmd/pulumi-tfgen-random github.com/pulumi/pulumi-terraform-bridge/pf/tfgen github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfgen github.com/pulumi/pulumi/pkg/v3/codegen/dotnet github.com/pulumi/pulumi/pkg/v3/testing/integration github.com/pulumi/pulumi/pkg/v3/resource/stack github.com/pulumi/pulumi/pkg/v3/secrets/cloud github.com/pulumi/pulumi/pkg/v3/secrets/cloud.test github.com/aws/aws-sdk-go-v2/service/sts ``` After: ``` github.com/pulumi/pulumi-random/provider/v4 github.com/pulumi/pulumi-random/provider/v4.test <- expected github.com/pulumi/providertest github.com/pulumi/pulumi/pkg/v3/testing/integration github.com/pulumi/pulumi/pkg/v3/resource/stack github.com/pulumi/pulumi/pkg/v3/secrets/cloud github.com/pulumi/pulumi/pkg/v3/secrets/cloud.test github.com/aws/aws-sdk-go-v2/service/sts ``` Note: this PR has been split into a number of commits, roughly one per language, to make reviewing a bit easier. The commits *do not build individually* due to the nature of the changes, but hopefully it makes understanding the work a bit more feasible. Closes #16011 --------- Co-authored-by: Bryce Lampe <bryce@pulumi.com>
2024-10-09 11:09:54 +00:00
// Copyright 2022-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 test
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/executable"
)
func GenerateGoProgramTest(
t *testing.T,
rootDir string,
genProgram GenProgram,
genProject GenProject,
) {
expectedVersion := map[string]PkgVersionInfo{
"aws-resource-options-4.26": {
Pkg: "github.com/pulumi/pulumi-aws/sdk/v4",
OpAndVersion: "v4.26.0",
},
"aws-resource-options-5.16.2": {
Pkg: "github.com/pulumi/pulumi-aws/sdk/v5",
OpAndVersion: "v5.16.2",
},
"modpath": {
Pkg: "git.example.org/thirdparty/sdk",
OpAndVersion: "v0.1.0",
},
}
sdkDir, err := filepath.Abs(filepath.Join(rootDir, "sdk"))
require.NoError(t, err)
TestProgramCodegen(t,
ProgramCodegenOptions{
Language: "go",
Extension: "go",
OutputFile: "main.go",
Check: func(t *testing.T, path string, dependencies codegen.StringSet) {
checkGo(t, path, dependencies, sdkDir)
},
GenProgram: genProgram,
TestCases: []ProgramTest{
{
Directory: "aws-resource-options-4.26",
Description: "Resource Options",
},
{
Directory: "aws-resource-options-5.16.2",
Description: "Resource Options",
},
{
Directory: "modpath",
Description: "Check that modpath is respected",
MockPluginVersions: map[string]string{
"other": "0.1.0",
},
// We don't compile because the test relies on the `other` package,
// which does not exist.
SkipCompile: codegen.NewStringSet("go"),
},
},
IsGenProject: true,
GenProject: genProject,
ExpectedVersion: expectedVersion,
DependencyFile: "go.mod",
})
}
func GenerateGoBatchTest(
t *testing.T,
rootDir string,
genProgram GenProgram,
testCases []ProgramTest,
) {
sdkDir, err := filepath.Abs(filepath.Join(rootDir, "sdk"))
require.NoError(t, err)
TestProgramCodegen(t,
ProgramCodegenOptions{
Language: "go",
Extension: "go",
OutputFile: "main.go",
Check: func(t *testing.T, path string, dependencies codegen.StringSet) {
checkGo(t, path, dependencies, sdkDir)
},
GenProgram: genProgram,
TestCases: testCases,
})
}
func GenerateGoYAMLBatchTest(t *testing.T, rootDir string, genProgram GenProgram) {
sdkDir, err := filepath.Abs(filepath.Join(rootDir, "sdk"))
require.NoError(t, err)
err = os.Chdir(filepath.Join(rootDir, "pkg", "codegen", "go"))
require.NoError(t, err)
TestProgramCodegen(t,
ProgramCodegenOptions{
Language: "go",
Extension: "go",
OutputFile: "main.go",
Check: func(t *testing.T, path string, dependencies codegen.StringSet) {
checkGo(t, path, dependencies, sdkDir)
},
GenProgram: genProgram,
TestCases: PulumiPulumiYAMLProgramTests,
})
}
func checkGo(t *testing.T, path string, deps codegen.StringSet, pulumiSDKPath string) {
dir := filepath.Dir(path)
ex, err := executable.FindExecutable("go")
require.NoError(t, err)
// We remove go.mod to ensure tests are reproducible.
goMod := filepath.Join(dir, "go.mod")
if err = os.Remove(goMod); !os.IsNotExist(err) {
require.NoError(t, err)
}
err = integration.RunCommand(t, "generate go.mod",
[]string{ex, "mod", "init", "main"},
dir, &integration.ProgramTestOptions{})
require.NoError(t, err)
err = integration.RunCommand(t, "go tidy",
[]string{ex, "mod", "tidy"},
dir, &integration.ProgramTestOptions{})
require.NoError(t, err)
if pulumiSDKPath != "" {
err = integration.RunCommand(t, "point towards local Go SDK",
[]string{
ex, "mod", "edit",
fmt.Sprintf("--replace=%s=%s",
"github.com/pulumi/pulumi/sdk/v3",
pulumiSDKPath),
},
dir, &integration.ProgramTestOptions{})
require.NoError(t, err)
}
typeCheckGo(t, path, deps, pulumiSDKPath)
}
func typeCheckGo(t *testing.T, path string, deps codegen.StringSet, pulumiSDKPath string) {
dir := filepath.Dir(path)
ex, err := executable.FindExecutable("go")
require.NoError(t, err)
err = integration.RunCommand(t, "go tidy after replace",
[]string{ex, "mod", "tidy"},
dir, &integration.ProgramTestOptions{})
require.NoError(t, err)
err = integration.RunCommand(t, "test build", []string{ex, "build", "-v", "all"},
dir, &integration.ProgramTestOptions{})
require.NoError(t, err)
os.Remove(filepath.Join(dir, "main"))
assert.NoError(t, err)
}