-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hcp-sbom: add prepare tests for the provisioner
- Loading branch information
1 parent
34e0f84
commit 4d00d8f
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package hcp_sbom | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/hashicorp/packer-plugin-sdk/template/interpolate" | ||
) | ||
|
||
func TestConfigPrepare(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputConfig map[string]interface{} | ||
interpolateContext interpolate.Context | ||
expectConfig *Config | ||
expectError bool | ||
}{ | ||
{ | ||
"empty config, should error without a source", | ||
map[string]interface{}{}, | ||
interpolate.Context{}, | ||
nil, | ||
true, | ||
}, | ||
{ | ||
"config with full context for interpolation: success", | ||
map[string]interface{}{ | ||
"source": "{{ .Name }}", | ||
}, | ||
interpolate.Context{ | ||
Data: &struct { | ||
Name string | ||
}{ | ||
Name: "testInterpolate", | ||
}, | ||
}, | ||
&Config{ | ||
Source: "testInterpolate", | ||
}, | ||
false, | ||
}, | ||
{ | ||
// Note: this will look weird to reviewers, but is actually | ||
// expected for the moment. | ||
// Refer to the comment in `Prepare` for context as to WHY | ||
// this cannot be considered an error. | ||
"config with sbom name as interpolated value, without it in context, replace with a placeholder", | ||
map[string]interface{}{ | ||
"source": "test", | ||
"sbom_name": "{{ .Name }}", | ||
}, | ||
interpolate.Context{}, | ||
&Config{ | ||
Source: "test", | ||
SbomName: "<no value>", | ||
}, | ||
false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
prov := &Provisioner{} | ||
prov.config.ctx = tt.interpolateContext | ||
err := prov.Prepare(tt.inputConfig) | ||
if err != nil && !tt.expectError { | ||
t.Fatalf("configuration unexpectedly failed to prepare: %s", err) | ||
} | ||
|
||
if err == nil && tt.expectError { | ||
t.Fatalf("configuration succeeded to prepare, but should have failed") | ||
} | ||
|
||
if err != nil { | ||
t.Logf("config had error %q", err) | ||
return | ||
} | ||
|
||
diff := cmp.Diff(prov.config, *tt.expectConfig, cmpopts.IgnoreUnexported(Config{})) | ||
if diff != "" { | ||
t.Errorf("configuration returned by `Prepare` is different from what was expected: %s", diff) | ||
} | ||
}) | ||
} | ||
} |