2021-09-22 19:55:20 +02:00
|
|
|
// Copyright 2016-2021, Pulumi Corporation.
|
2020-09-30 22:41:52 +02:00
|
|
|
//
|
|
|
|
// 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 (
|
2021-08-23 22:46:09 +02:00
|
|
|
"bytes"
|
2020-09-30 22:41:52 +02:00
|
|
|
"encoding/json"
|
2021-09-22 19:55:20 +02:00
|
|
|
"fmt"
|
2021-08-23 22:46:09 +02:00
|
|
|
"io"
|
2020-09-30 22:41:52 +02:00
|
|
|
"io/ioutil"
|
2021-04-29 22:08:22 +02:00
|
|
|
"os"
|
2020-09-30 22:41:52 +02:00
|
|
|
"path/filepath"
|
2021-09-22 19:55:20 +02:00
|
|
|
"sort"
|
2020-09-30 22:41:52 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-07-07 00:40:53 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-07-14 01:41:40 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
2021-09-23 19:42:20 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/executable"
|
2020-09-30 22:41:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GenPkgSignature corresponds to the shape of the codegen GeneratePackage functions.
|
|
|
|
type GenPkgSignature func(string, *schema.Package, map[string][]byte) (map[string][]byte, error)
|
|
|
|
|
|
|
|
// GeneratePackageFilesFromSchema loads a schema and generates files using the provided GeneratePackage function.
|
|
|
|
func GeneratePackageFilesFromSchema(schemaPath string, genPackageFunc GenPkgSignature) (map[string][]byte, error) {
|
|
|
|
// Read in, decode, and import the schema.
|
|
|
|
schemaBytes, err := ioutil.ReadFile(schemaPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-14 01:41:40 +02:00
|
|
|
ext := filepath.Ext(schemaPath)
|
|
|
|
|
2020-09-30 22:41:52 +02:00
|
|
|
var pkgSpec schema.PackageSpec
|
2021-07-14 01:41:40 +02:00
|
|
|
if ext == ".yaml" || ext == ".yml" {
|
|
|
|
err = yaml.Unmarshal(schemaBytes, &pkgSpec)
|
|
|
|
} else {
|
|
|
|
err = json.Unmarshal(schemaBytes, &pkgSpec)
|
|
|
|
}
|
2020-09-30 22:41:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg, err := schema.ImportSpec(pkgSpec, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return genPackageFunc("test", pkg, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFiles loads the provided list of files from a directory.
|
2020-10-10 22:47:23 +02:00
|
|
|
func LoadFiles(dir, lang string, files []string) (map[string][]byte, error) {
|
2020-09-30 22:41:52 +02:00
|
|
|
result := map[string][]byte{}
|
|
|
|
for _, file := range files {
|
2020-10-10 22:47:23 +02:00
|
|
|
fileBytes, err := ioutil.ReadFile(filepath.Join(dir, lang, file))
|
2020-09-30 22:41:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result[file] = fileBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
func PathExists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// `LoadBaseline` loads the contents of the given baseline directory,
|
|
|
|
// by inspecting its `codegen-manifest.json`.
|
|
|
|
func LoadBaseline(dir, lang string) (map[string][]byte, error) {
|
|
|
|
cm := &codegenManifest{}
|
|
|
|
err := cm.load(filepath.Join(dir, lang))
|
2021-07-07 00:40:53 +02:00
|
|
|
if err != nil {
|
2021-09-22 19:55:20 +02:00
|
|
|
return nil, fmt.Errorf("Failed to load codegen-manifest.json: %w", err)
|
2021-07-07 00:40:53 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
files := make(map[string][]byte)
|
2021-07-07 00:40:53 +02:00
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
for _, f := range cm.EmittedFiles {
|
|
|
|
bytes, err := ioutil.ReadFile(filepath.Join(dir, lang, f))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to load file %s referenced in codegen-manifest.json: %w", f, err)
|
2021-07-07 00:40:53 +02:00
|
|
|
}
|
2021-09-22 19:55:20 +02:00
|
|
|
files[f] = bytes
|
2021-07-07 00:40:53 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
return files, nil
|
2021-07-07 00:40:53 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
type codegenManifest struct {
|
|
|
|
EmittedFiles []string `json:"emittedFiles"`
|
|
|
|
}
|
2021-07-07 00:40:53 +02:00
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
func (cm *codegenManifest) load(dir string) error {
|
|
|
|
bytes, err := ioutil.ReadFile(filepath.Join(dir, "codegen-manifest.json"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal(bytes, cm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *codegenManifest) save(dir string) error {
|
|
|
|
sort.Strings(cm.EmittedFiles)
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
err := enc.Encode(cm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-07-07 00:40:53 +02:00
|
|
|
}
|
2021-09-22 19:55:20 +02:00
|
|
|
data := buf.Bytes()
|
|
|
|
return ioutil.WriteFile(filepath.Join(dir, "codegen-manifest.json"), data, 0600)
|
2021-07-07 00:40:53 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 22:41:52 +02:00
|
|
|
// ValidateFileEquality compares maps of files for equality.
|
2021-07-27 04:23:17 +02:00
|
|
|
func ValidateFileEquality(t *testing.T, actual, expected map[string][]byte) bool {
|
|
|
|
ok := true
|
2020-09-30 22:41:52 +02:00
|
|
|
for name, file := range expected {
|
2021-09-22 19:55:20 +02:00
|
|
|
_, inActual := actual[name]
|
|
|
|
if inActual {
|
|
|
|
if !assert.Equal(t, string(file), string(actual[name]), name) {
|
|
|
|
t.Logf("%s did not agree", name)
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Logf("File %s was expected but is missing from the actual fileset", name)
|
2021-07-27 04:23:17 +02:00
|
|
|
ok = false
|
|
|
|
}
|
2020-09-30 22:41:52 +02:00
|
|
|
}
|
2021-06-24 18:17:55 +02:00
|
|
|
for name := range actual {
|
2021-09-22 19:55:20 +02:00
|
|
|
if _, inExpected := expected[name]; !inExpected {
|
|
|
|
t.Logf("File %s from the actual fileset was not expected", name)
|
2021-07-27 04:23:17 +02:00
|
|
|
ok = false
|
2021-06-24 18:17:55 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-27 04:23:17 +02:00
|
|
|
return ok
|
2020-09-30 22:41:52 +02:00
|
|
|
}
|
2021-01-15 19:09:09 +01:00
|
|
|
|
2021-07-07 00:40:53 +02:00
|
|
|
// If PULUMI_ACCEPT is set, writes out actual output to the expected
|
2021-04-29 22:08:22 +02:00
|
|
|
// file set, so we can continue enjoying golden tests without manually
|
|
|
|
// modifying the expected output.
|
2021-07-07 00:40:53 +02:00
|
|
|
func RewriteFilesWhenPulumiAccept(t *testing.T, dir, lang string, actual map[string][]byte) bool {
|
|
|
|
if os.Getenv("PULUMI_ACCEPT") == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
cm := &codegenManifest{}
|
|
|
|
|
2021-07-07 00:40:53 +02:00
|
|
|
baseline := filepath.Join(dir, lang)
|
|
|
|
|
|
|
|
// Remove the baseline directory's current contents.
|
2021-09-22 19:55:20 +02:00
|
|
|
_, err := os.ReadDir(baseline)
|
2021-07-07 00:40:53 +02:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
2021-09-22 19:55:20 +02:00
|
|
|
err = os.RemoveAll(baseline)
|
|
|
|
require.NoError(t, err)
|
2021-07-07 00:40:53 +02:00
|
|
|
case os.IsNotExist(err):
|
|
|
|
// OK
|
|
|
|
default:
|
|
|
|
require.NoError(t, err)
|
2021-04-29 22:08:22 +02:00
|
|
|
}
|
2021-07-07 00:40:53 +02:00
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
for file, bytes := range actual {
|
|
|
|
relPath := filepath.FromSlash(file)
|
|
|
|
path := filepath.Join(dir, lang, relPath)
|
|
|
|
cm.EmittedFiles = append(cm.EmittedFiles, relPath)
|
|
|
|
err := writeFileEnsuringDir(path, bytes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cm.save(filepath.Join(dir, lang))
|
|
|
|
require.NoError(t, err)
|
2021-09-15 18:49:36 +02:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
// Useful for populating code-generated destination
|
|
|
|
// `codeDir=$dir/$lang` with extra manually written files such as the
|
|
|
|
// unit test files. These files are copied from `$dir/$lang-extras`
|
|
|
|
// folder if present.
|
|
|
|
func CopyExtraFiles(t *testing.T, dir, lang string) {
|
|
|
|
codeDir := filepath.Join(dir, lang)
|
|
|
|
extrasDir := filepath.Join(dir, fmt.Sprintf("%s-extras", lang))
|
|
|
|
gotExtras, err := PathExists(extrasDir)
|
2021-07-07 00:40:53 +02:00
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
if !gotExtras {
|
|
|
|
return
|
|
|
|
}
|
2021-07-07 00:40:53 +02:00
|
|
|
|
2021-09-22 19:55:20 +02:00
|
|
|
if err != nil {
|
2021-07-07 00:40:53 +02:00
|
|
|
require.NoError(t, err)
|
2021-09-22 19:55:20 +02:00
|
|
|
return
|
2021-07-07 00:40:53 +02:00
|
|
|
}
|
2021-09-22 19:55:20 +02:00
|
|
|
|
|
|
|
err = filepath.Walk(extrasDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
relPath, err := filepath.Rel(extrasDir, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
destPath := filepath.Join(codeDir, relPath)
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = writeFileEnsuringDir(destPath, bytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.Logf("Copied %s to %s", path, destPath)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFileEnsuringDir(path string, bytes []byte) error {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(path, bytes, 0600)
|
2021-04-29 22:08:22 +02:00
|
|
|
}
|
|
|
|
|
2021-01-15 19:09:09 +01:00
|
|
|
// CheckAllFilesGenerated ensures that the set of expected and actual files generated
|
|
|
|
// are exactly equivalent.
|
|
|
|
func CheckAllFilesGenerated(t *testing.T, actual, expected map[string][]byte) {
|
|
|
|
seen := map[string]bool{}
|
|
|
|
for x := range expected {
|
|
|
|
seen[x] = true
|
|
|
|
}
|
|
|
|
for a := range actual {
|
|
|
|
assert.Contains(t, seen, a, "Unexpected file generated: %s", a)
|
|
|
|
if seen[a] {
|
|
|
|
delete(seen, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for s := range seen {
|
|
|
|
assert.Fail(t, "No content generated for expected file %s", s)
|
|
|
|
}
|
|
|
|
}
|
2021-08-23 22:46:09 +02:00
|
|
|
|
|
|
|
// Validates a transformer on a single file.
|
|
|
|
func ValidateFileTransformer(
|
|
|
|
t *testing.T,
|
|
|
|
inputFile string,
|
|
|
|
expectedOutputFile string,
|
|
|
|
transformer func(reader io.Reader, writer io.Writer) error) {
|
|
|
|
|
|
|
|
reader, err := os.Open(inputFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
err = transformer(reader, &buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
actualBytes := buf.Bytes()
|
|
|
|
|
|
|
|
if os.Getenv("PULUMI_ACCEPT") != "" {
|
|
|
|
err := ioutil.WriteFile(expectedOutputFile, actualBytes, 0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := map[string][]byte{expectedOutputFile: actualBytes}
|
|
|
|
|
|
|
|
expectedBytes, err := ioutil.ReadFile(expectedOutputFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string][]byte{expectedOutputFile: expectedBytes}
|
|
|
|
|
|
|
|
ValidateFileEquality(t, actual, expected)
|
|
|
|
}
|
2021-09-23 19:42:20 +02:00
|
|
|
|
|
|
|
func RunCommand(t *testing.T, name string, cwd string, exec string, args ...string) {
|
|
|
|
exec, err := executable.FindExecutable(exec)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
wd, err := filepath.Abs(cwd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmdOptions := integration.ProgramTestOptions{Stderr: &stderr, Stdout: &stdout, Verbose: true}
|
|
|
|
err = integration.RunCommand(t,
|
|
|
|
name,
|
|
|
|
append([]string{exec}, args...),
|
|
|
|
wd,
|
|
|
|
&cmdOptions)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if err != nil {
|
|
|
|
stdout := stdout.String()
|
|
|
|
stderr := stderr.String()
|
|
|
|
if len(stdout) > 0 {
|
|
|
|
t.Logf("stdout: %s", stdout)
|
|
|
|
}
|
|
|
|
if len(stderr) > 0 {
|
|
|
|
t.Logf("stderr: %s", stderr)
|
|
|
|
}
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|