Skip to content

Commit

Permalink
Change 'Unrecognized properties' from error to log message since it d…
Browse files Browse the repository at this point in the history
…oesn't know about path properties
  • Loading branch information
thomas11 committed Nov 7, 2024
1 parent 4dbfdb5 commit 179c7f2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
7 changes: 4 additions & 3 deletions provider/pkg/convert/sdkInputsToRequestBody.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/pulumi/pulumi-azure-native/v2/provider/pkg/resources"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
)

// SdkInputsToRequestBody converts a map of SDK properties to JSON request body to be sent to an HTTP API.
Expand Down Expand Up @@ -41,16 +42,16 @@ func (k *SdkShapeConverter) SdkInputsToRequestBody(props map[string]resources.Az
container[name] = k.convertSdkPropToRequestBodyPropValue(id, &p, value)
}
}
var err error

if len(unusedValues) > 0 {
unusedKeys := make([]string, 0, len(unusedValues))
for k := range unusedValues {
unusedKeys = append(unusedKeys, k)
}
sort.Strings(unusedKeys)
err = fmt.Errorf("unrecognized properties: %v", unusedKeys)
logging.V(9).Infof("Unrecognized properties in SdkInputsToRequestBody: %v", unusedKeys)
}
return result, err
return result, nil
}

// convertSdkPropToRequestBodyPropValue converts an SDK property to a value to be used in a request body.
Expand Down
58 changes: 48 additions & 10 deletions provider/pkg/convert/sdkInputsToRequestBody_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
package convert

import (
"bufio"
"bytes"
"fmt"
"os"
"testing"

"github.com/pulumi/pulumi-azure-native/v2/provider/pkg/resources"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
"github.com/stretchr/testify/assert"
"pgregory.net/rapid"
)
Expand Down Expand Up @@ -54,6 +58,29 @@ func TestSdkInputsToRequestBodySubResource(t *testing.T) {
assert.Equal(t, expectedBody, actualBody)
}

func captureStderr(f func()) string {
// Create a pipe
r, w, _ := os.Pipe()
// Save the original stderr
originalStderr := os.Stderr
// Redirect stderr to the write end of the pipe
os.Stderr = w

// Run the target function
f()

// Close the write end of the pipe and restore stderr
w.Close()
os.Stderr = originalStderr

// Read the output from the read end of the pipe
var buf bytes.Buffer
reader := bufio.NewReader(r)
buf.ReadFrom(reader)

return buf.String()
}

func TestSdkInputsToRequestBody(t *testing.T) {
type testCaseArgs struct {
id string
Expand Down Expand Up @@ -94,20 +121,31 @@ func TestSdkInputsToRequestBody(t *testing.T) {
})

t.Run("unmatched inputs are reported", func(t *testing.T) {
actual, err := convertWithError(testCaseArgs{
props: map[string]resources.AzureAPIProperty{
"propA": {
Type: "string",
prevLogToStderr := logging.LogToStderr
prevVerbose := logging.Verbose
prevLogFlow := logging.LogFlow
logging.InitLogging(true, 9, true)
defer func() {
logging.InitLogging(prevLogToStderr, prevVerbose, prevLogFlow)
}()

var actual map[string]any
stderr := captureStderr(func() {
actual = convert(testCaseArgs{
props: map[string]resources.AzureAPIProperty{
"propA": {
Type: "string",
},
},
},
inputs: map[string]interface{}{
"propA": "a",
"propB": "b",
},
inputs: map[string]interface{}{
"propA": "a",
"propB": "b",
},
})
})

assert.Equal(t, map[string]interface{}{"propA": "a"}, actual)
assert.Equal(t, err, fmt.Errorf("unrecognized properties: [propB]"))
assert.Contains(t, stderr, "Unrecognized properties in SdkInputsToRequestBody: [propB]")
})

t.Run("untyped non-empty values remain unchanged", rapid.MakeCheck(func(t *rapid.T) {
Expand Down

0 comments on commit 179c7f2

Please sign in to comment.