pulumi/pkg/backend/display/events_test.go

66 lines
2.2 KiB
Go
Raw Permalink Normal View History

// Copyright 2016-2023, 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 display
// Note: to regenerate the baselines for these tests, run `go test` with `PULUMI_ACCEPT=true`.
import (
Don't omit an empty detailedDiff (#15213) <!--- 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. --> When the detailedDiff object is empty, i.e. `{}`, it is dropped from the serialized json due to the `omitempty`. When it is rehydrated on the service, it is rehydrated as `nil` because it is not present in the json object. That causes [this line](https://github.com/pulumi/pulumi/blob/516979770f11d3a426239429731cf9f327c38836/pkg/backend/display/rows.go#L442) to be passed through and results in an input diff, which leads to the output we see in the cloud of an import showing a lot of changed properties when it should not (See issue for screenshots). Fixes https://github.com/pulumi/pulumi-cloud-requests/issues/339 ## 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 - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] 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-01-23 00:00:14 +00:00
"encoding/json"
"testing"
Don't omit an empty detailedDiff (#15213) <!--- 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. --> When the detailedDiff object is empty, i.e. `{}`, it is dropped from the serialized json due to the `omitempty`. When it is rehydrated on the service, it is rehydrated as `nil` because it is not present in the json object. That causes [this line](https://github.com/pulumi/pulumi/blob/516979770f11d3a426239429731cf9f327c38836/pkg/backend/display/rows.go#L442) to be passed through and results in an input diff, which leads to the output we see in the cloud of an import showing a lot of changed properties when it should not (See issue for screenshots). Fixes https://github.com/pulumi/pulumi-cloud-requests/issues/339 ## 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 - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] 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-01-23 00:00:14 +00:00
"github.com/pulumi/pulumi/pkg/v3/resource/deploy"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/pkg/v3/engine"
"github.com/stretchr/testify/assert"
)
// This test checks that the ANSI control codes are removed from EngineEvents
// converted to be sent to the Pulumi Service API.
func TestRemoveANSI(t *testing.T) {
t.Parallel()
input := "\033[31mHello, World!\033[0m"
expected := "Hello, World!"
e := engine.NewEvent(
engine.DiagEventPayload{
Message: input,
},
)
res, err := ConvertEngineEvent(e, false /* showSecrets */)
assert.NoError(t, err, "unable to convert engine event")
assert.Equal(t, expected, res.DiagnosticEvent.Message)
}
Don't omit an empty detailedDiff (#15213) <!--- 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. --> When the detailedDiff object is empty, i.e. `{}`, it is dropped from the serialized json due to the `omitempty`. When it is rehydrated on the service, it is rehydrated as `nil` because it is not present in the json object. That causes [this line](https://github.com/pulumi/pulumi/blob/516979770f11d3a426239429731cf9f327c38836/pkg/backend/display/rows.go#L442) to be passed through and results in an input diff, which leads to the output we see in the cloud of an import showing a lot of changed properties when it should not (See issue for screenshots). Fixes https://github.com/pulumi/pulumi-cloud-requests/issues/339 ## 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 - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] 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-01-23 00:00:14 +00:00
func TestEmptyDetailedDiff(t *testing.T) {
t.Parallel()
expected := `{"sequence":0,"timestamp":0,"resOutputsEvent":{"metadata":{"op":"import","urn":"urn:pul:resource:type::name","type":"urn:pul:resource:type","old":null,"new":null,"detailedDiff":{},"provider":""}}}` //nolint:lll
e := engine.NewEvent(
engine.ResourceOutputsEventPayload{
Metadata: engine.StepEventMetadata{
Op: deploy.OpImport,
URN: "urn:pul:resource:type::name",
Type: "urn:pul:resource:type",
DetailedDiff: map[string]plugin.PropertyDiff{},
},
},
)
res, err := ConvertEngineEvent(e, false /* showSecrets */)
assert.NoError(t, err, "unable to convert engine event")
jsonEvent, err := json.Marshal(res)
assert.NoError(t, err, "unable to marshal to json")
assert.Equal(t, expected, string(jsonEvent))
}