2020-07-09 02:12:37 +00:00
|
|
|
// Copyright 2016-2020, 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 importer
|
|
|
|
|
|
|
|
import (
|
Lift context parameter to SerializeDeployment/Resource/Operations/Properties (#15929)
<!---
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->
# Description
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
SerializePropertyValue needed a `context.Context` object to pass to the
`config.Encrypter`. It was using `context.TODO()`, this change instead
accepts a context on the parameters and lifts that up to
SerializeProperties, SerializeResource, SerializeOperation, and
SerializeDeployment.
There were a few call sites for those methods that already had a context
on hand, and they now pass that context. The other calls sites now use
`context.TODO()`, we should continue to iterate in this area to ensure
everywhere that needs a context has one passed in.
## Checklist
- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
- [ ] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-04-15 07:45:46 +00:00
|
|
|
"context"
|
2020-07-09 02:12:37 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-30 03:11:56 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
2022-02-07 11:10:04 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/utils"
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/resource/stack"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
|
2020-07-09 02:12:37 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerateLanguageDefinition(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
2022-10-11 10:56:29 +00:00
|
|
|
loader := schema.NewPluginLoader(utils.NewHost(testdataPath))
|
2020-07-09 02:12:37 +00:00
|
|
|
|
|
|
|
cases, err := readTestCases("testdata/cases.json")
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:17:41 +00:00
|
|
|
//nolint:paralleltest // false positive because range var isn't used directly in t.Run(name) arg
|
2020-07-09 02:12:37 +00:00
|
|
|
for _, s := range cases.Resources {
|
2022-03-04 08:17:41 +00:00
|
|
|
s := s
|
2020-07-09 02:12:37 +00:00
|
|
|
t.Run(string(s.URN), func(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
2020-07-09 02:12:37 +00:00
|
|
|
state, err := stack.DeserializeResource(s, config.NopDecrypter, config.NopEncrypter)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
|
|
|
|
var actualState *resource.State
|
2023-01-06 22:39:16 +00:00
|
|
|
err = GenerateLanguageDefinitions(io.Discard, loader, func(_ io.Writer, p *pcl.Program) error {
|
2020-07-09 02:12:37 +00:00
|
|
|
if !assert.Len(t, p.Nodes, 1) {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
|
2021-09-30 03:11:56 +00:00
|
|
|
res, isResource := p.Nodes[0].(*pcl.Resource)
|
2020-07-09 02:12:37 +00:00
|
|
|
if !assert.True(t, isResource) {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
|
|
|
|
actualState = renderResource(t, res)
|
|
|
|
return nil
|
|
|
|
}, []*resource.State{state}, names)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, state.Type, actualState.Type)
|
|
|
|
assert.Equal(t, state.URN, actualState.URN)
|
|
|
|
assert.Equal(t, state.Parent, actualState.Parent)
|
|
|
|
assert.Equal(t, state.Provider, actualState.Provider)
|
|
|
|
assert.Equal(t, state.Protect, actualState.Protect)
|
|
|
|
if !assert.True(t, actualState.Inputs.DeepEquals(state.Inputs)) {
|
Lift context parameter to SerializeDeployment/Resource/Operations/Properties (#15929)
<!---
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->
# Description
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
SerializePropertyValue needed a `context.Context` object to pass to the
`config.Encrypter`. It was using `context.TODO()`, this change instead
accepts a context on the parameters and lifts that up to
SerializeProperties, SerializeResource, SerializeOperation, and
SerializeDeployment.
There were a few call sites for those methods that already had a context
on hand, and they now pass that context. The other calls sites now use
`context.TODO()`, we should continue to iterate in this area to ensure
everywhere that needs a context has one passed in.
## Checklist
- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
- [ ] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-04-15 07:45:46 +00:00
|
|
|
actual, err := stack.SerializeResource(context.Background(), actualState, config.NopEncrypter, false)
|
2020-07-09 02:12:37 +00:00
|
|
|
contract.IgnoreError(err)
|
|
|
|
|
|
|
|
sb, err := json.MarshalIndent(s, "", " ")
|
|
|
|
contract.IgnoreError(err)
|
|
|
|
|
|
|
|
ab, err := json.MarshalIndent(actual, "", " ")
|
|
|
|
contract.IgnoreError(err)
|
|
|
|
|
|
|
|
t.Logf("%v\n\n%v\n", string(sb), string(ab))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|