-
Notifications
You must be signed in to change notification settings - Fork 482
chore: Add metadata cmd unit test. #2668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bharathkkb
merged 5 commits into
GoogleCloudPlatform:master
from
qz267:374836259-display-yaml-unit-test
Oct 29, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,155 @@ | ||
package bpmetadata | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCmdExecution(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
args []string | ||
expectErr bool | ||
}{ | ||
{ | ||
description: "execute metadata command with valid inputs", | ||
args: []string{"metadata", "--help"}, | ||
expectErr: false, | ||
}, | ||
{ | ||
description: "execute metadata command with invalid inputs", | ||
args: []string{"metadata", "--invalid-flag"}, | ||
expectErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.description, func(t *testing.T) { | ||
cmd := &cobra.Command{Use: "cft"} | ||
cmd.SetArgs(tt.args) | ||
|
||
execCmd := func() error { | ||
_, err := cmd.ExecuteC() | ||
return err | ||
} | ||
|
||
if tt.expectErr { | ||
assert.Error(t, execCmd(), "Command should return an error") | ||
} else { | ||
assert.NoError(t, execCmd(), "Command should execute without error") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCreateBlueprintDisplayMetadata(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
bpPath string | ||
bpDisp *BlueprintMetadata | ||
bpCore *BlueprintMetadata | ||
expectErr bool | ||
}{ | ||
{ | ||
description: "create metadata with nil Spec.UI.Input", | ||
qz267 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
bpPath: "/path/to/blueprint", | ||
bpDisp: &BlueprintMetadata{}, | ||
bpCore: &BlueprintMetadata{ | ||
ApiVersion: "v1", | ||
Kind: "Blueprint", | ||
Metadata: &ResourceTypeMeta{ | ||
Name: "core-blueprint", | ||
Labels: map[string]string{ | ||
"env": "core", | ||
}, | ||
}, | ||
Spec: &BlueprintMetadataSpec{ | ||
Info: &BlueprintInfo{ | ||
Title: "Core Blueprint", | ||
Version: "1.0.0", | ||
Icon: "assets/core_icon.png", | ||
SingleDeployment: false, | ||
}, | ||
Interfaces: &BlueprintInterface{ | ||
Variables: []*BlueprintVariable{ | ||
{ | ||
Name: "test_var_1", | ||
}, | ||
}, | ||
}, | ||
Ui: &BlueprintUI{ | ||
Input: nil, | ||
}, | ||
}, | ||
}, | ||
expectErr: false, | ||
}, | ||
{ | ||
description: "create metadata with valid input", | ||
bpPath: "/path/to/blueprint", | ||
bpDisp: &BlueprintMetadata{ | ||
Spec: &BlueprintMetadataSpec{ | ||
Ui: &BlueprintUI{ | ||
Input: &BlueprintUIInput{ | ||
Variables: map[string]*DisplayVariable{ | ||
"test_var_1": { | ||
Name: "test var 1", | ||
Title: "This is a test input", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
bpCore: &BlueprintMetadata{ | ||
ApiVersion: "v1", | ||
Kind: "Blueprint", | ||
Metadata: &ResourceTypeMeta{ | ||
Name: "core-blueprint", | ||
Labels: map[string]string{ | ||
"env": "core", | ||
}, | ||
}, | ||
Spec: &BlueprintMetadataSpec{ | ||
Info: &BlueprintInfo{ | ||
Title: "Core Blueprint", | ||
Version: "1.0.0", | ||
Icon: "assets/core_icon.png", | ||
SingleDeployment: false, | ||
}, | ||
Interfaces: &BlueprintInterface{ | ||
Variables: []*BlueprintVariable{ | ||
{ | ||
Name: "test_var_1", | ||
}, | ||
}, | ||
}, | ||
Ui: &BlueprintUI{ | ||
Input: nil, | ||
}, | ||
}, | ||
}, | ||
expectErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.description, func(t *testing.T) { | ||
metadata, err := CreateBlueprintDisplayMetadata(tt.bpPath, tt.bpDisp, tt.bpCore) | ||
if tt.expectErr { | ||
assert.Error(t, err, "Function should return an error") | ||
assert.Nil(t, metadata, "Metadata should be nil when there is an error") | ||
} else { | ||
assert.NoError(t, err, "Function should not return an error") | ||
assert.NotNil(t, metadata, "Metadata should not be nil") | ||
if tt.bpDisp != nil { | ||
assert.Equal(t, tt.bpDisp.Metadata.Name, metadata.Metadata.Name, "Metadata name should match the input") | ||
assert.Equal(t, tt.bpDisp.Spec.Info.Title, metadata.Spec.Info.Title, "Metadata title should match the input") | ||
assert.Equal(t, tt.bpDisp.Spec.Info.Version, metadata.Spec.Info.Version, "Metadata version should match the input") | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.