Skip to content

Commit

Permalink
Fix missing nil check on docInfo for doc discovery (#1522)
Browse files Browse the repository at this point in the history
This change adds a nil check on docInfo for handling of missing docs
settings, without which an upgrade in GCP panicked on creation of a new
resource whose docs could not be found.

With this change, `getDocsForResource` behaves in the following way:

1. Check if `PULUMI_MISSING_DOCS_ERROR` is set to true - if no, continue
with a warning
2. if yes, verify the resource has any docs info - if no, return a docs
error as per env var setting
3. if yes, there is docs info, check if `AllowMissing` is set to true -
if no, return a docs error as docs are missing and we expect an error as
per env var setting
4. if yes, continue with a warning, as we allow docs to be missed.
  • Loading branch information
guineveresaenger authored Nov 15, 2023
2 parents 77f3f11 + eec2329 commit 6085352
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pkg/tfgen/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,11 @@ func getDocsForResource(g *Generator, source DocsSource, kind DocKind,
msg := fmt.Sprintf("could not find docs for %v %v. Override the Docs property in the %v mapping. See "+
"type tfbridge.DocInfo for details.", kind, formatEntityName(rawname), kind)

if cmdutil.IsTruthy(os.Getenv("PULUMI_MISSING_DOCS_ERROR")) && !info.GetDocs().AllowMissing {
g.error(msg)
return entityDocs{}, fmt.Errorf(msg)
if cmdutil.IsTruthy(os.Getenv("PULUMI_MISSING_DOCS_ERROR")) {
if docInfo == nil || !docInfo.AllowMissing {
g.error(msg)
return entityDocs{}, fmt.Errorf(msg)
}
}

// Ideally, we would still want to still return an error here and let upstream callers handle it, but at the
Expand Down
27 changes: 27 additions & 0 deletions pkg/tfgen/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,12 @@ func TestErrorMissingDocs(t *testing.T) {
docs: tfbridge.DocInfo{AllowMissing: true},
forbidMissingDocsEnv: "true",
},
// DocInfo is nil so we error because docs are missing
{
source: mockSource{},
forbidMissingDocsEnv: "true",
expectErr: true,
},
}

for _, tt := range tests {
Expand All @@ -1186,6 +1192,18 @@ func TestErrorMissingDocs(t *testing.T) {
})
}
}
func TestErrorNilDocs(t *testing.T) {
t.Run("", func(t *testing.T) {
g := &Generator{
sink: mockSink{t},
}
rawName := "nil_docs"
t.Setenv("PULUMI_MISSING_DOCS_ERROR", "true")
info := mockNilDocsResource{token: tokens.Token(rawName)}
_, err := getDocsForResource(g, mockSource{}, ResourceDocs, rawName, &info)
assert.NotNil(t, err)
})
}

type mockSource map[string]string

Expand Down Expand Up @@ -1240,6 +1258,15 @@ func (r *mockResource) GetTok() tokens.Token {
return r.token
}

type mockNilDocsResource struct {
token tokens.Token
mockResource
}

func (nr *mockNilDocsResource) GetDocs() *tfbridge.DocInfo {
return nil
}

func readfile(t *testing.T, file string) string {
t.Helper()
bytes, err := os.ReadFile(file)
Expand Down

0 comments on commit 6085352

Please sign in to comment.