-
Notifications
You must be signed in to change notification settings - Fork 310
start adding unit tests for Ironic.InspectHardware #649
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
metal3-io-bot
merged 1 commit into
metal3-io:master
from
andfasano:unit-test-inspect-hardware
Oct 9, 2020
Merged
Changes from all commits
Commits
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,179 @@ | ||
| package ironic | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/metal3-io/baremetal-operator/pkg/bmc" | ||
| "github.com/metal3-io/baremetal-operator/pkg/provisioner/ironic/clients" | ||
| "github.com/metal3-io/baremetal-operator/pkg/provisioner/ironic/testserver" | ||
|
|
||
| "github.com/gophercloud/gophercloud/openstack/baremetal/v1/nodes" | ||
| "github.com/gophercloud/gophercloud/openstack/baremetalintrospection/v1/introspection" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestInspectHardware(t *testing.T) { | ||
|
|
||
| nodeUUID := "33ce8659-7400-4c68-9535-d10766f07a58" | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| ironic *testserver.IronicMock | ||
| inspector *testserver.InspectorMock | ||
|
|
||
| expectedDirty bool | ||
| expectedRequestAfter int | ||
| expectedResultError string | ||
| expectedDetailsHost string | ||
|
|
||
| expectedPublish string | ||
| expectedError string | ||
| }{ | ||
| { | ||
| name: "introspection-status-start-new-hardware-inspection", | ||
| ironic: testserver.NewIronic(t).Ready().WithNode(nodes.Node{ | ||
| UUID: nodeUUID, | ||
| ProvisionState: "active", | ||
| }).WithNodeStatesProvision(nodeUUID), | ||
| inspector: testserver.NewInspector(t).Ready().WithIntrospectionFailed(nodeUUID, http.StatusNotFound), | ||
|
|
||
| expectedDirty: true, | ||
| expectedRequestAfter: 10, | ||
| expectedPublish: "InspectionStarted Hardware inspection started", | ||
| }, | ||
| { | ||
| name: "introspection-data-failed", | ||
| ironic: testserver.NewIronic(t).Ready().WithNode(nodes.Node{ | ||
| UUID: nodeUUID, | ||
| }), | ||
| inspector: testserver.NewInspector(t).Ready(). | ||
| WithIntrospection(nodeUUID, introspection.Introspection{ | ||
| Finished: true, | ||
| }). | ||
| WithIntrospectionDataFailed(nodeUUID, http.StatusBadRequest), | ||
|
|
||
| expectedError: "failed to retrieve hardware introspection data: Bad request with: \\[GET http://127.0.0.1:.*/v1/introspection/33ce8659-7400-4c68-9535-d10766f07a58/data\\], error message: An error\\\n", | ||
| }, | ||
| { | ||
| name: "introspection-status-failed-404-retry-on-wait", | ||
| ironic: testserver.NewIronic(t).Ready().WithNode(nodes.Node{ | ||
| UUID: nodeUUID, | ||
| ProvisionState: "inspect wait", | ||
| }), | ||
| inspector: testserver.NewInspector(t).Ready().WithIntrospectionFailed(nodeUUID, http.StatusNotFound), | ||
|
|
||
| expectedDirty: true, | ||
| expectedRequestAfter: 15, | ||
| }, | ||
| { | ||
| name: "introspection-status-failed-extraction", | ||
| ironic: testserver.NewIronic(t).Ready().WithNode(nodes.Node{ | ||
| UUID: nodeUUID, | ||
| ProvisionState: "inspecting", | ||
| }), | ||
| inspector: testserver.NewInspector(t).Ready().WithIntrospectionFailed(nodeUUID, http.StatusBadRequest), | ||
|
|
||
| expectedError: "failed to extract hardware inspection status: Bad request with: \\[GET http://127.0.0.1:.*/v1/introspection/33ce8659-7400-4c68-9535-d10766f07a58\\], error message: An error\\\n", | ||
| }, | ||
| { | ||
| name: "introspection-status-failed-404-retry", | ||
| ironic: testserver.NewIronic(t).Ready().WithNode(nodes.Node{ | ||
| UUID: nodeUUID, | ||
| ProvisionState: "inspecting", | ||
| }), | ||
| inspector: testserver.NewInspector(t).Ready().WithIntrospectionFailed(nodeUUID, http.StatusNotFound), | ||
|
|
||
| expectedDirty: true, | ||
| expectedRequestAfter: 15, | ||
| }, | ||
| { | ||
| name: "introspection-aborted", | ||
| ironic: testserver.NewIronic(t).Ready().WithNode(nodes.Node{ | ||
| UUID: nodeUUID, | ||
| }), | ||
| inspector: testserver.NewInspector(t).Ready().WithIntrospection(nodeUUID, introspection.Introspection{ | ||
| Finished: true, | ||
| Error: "Canceled by operator", | ||
| }), | ||
|
|
||
| expectedResultError: "Canceled by operator", | ||
| }, | ||
| { | ||
| name: "inspection-in-progress", | ||
| ironic: testserver.NewIronic(t).Ready().WithNode(nodes.Node{ | ||
| UUID: nodeUUID, | ||
| }), | ||
| inspector: testserver.NewInspector(t).Ready().WithIntrospection(nodeUUID, introspection.Introspection{ | ||
| Finished: false, | ||
| }), | ||
| expectedDirty: true, | ||
| expectedRequestAfter: 15, | ||
| }, | ||
| { | ||
| name: "inspection-completed", | ||
| ironic: testserver.NewIronic(t).Ready().WithNode(nodes.Node{ | ||
| UUID: nodeUUID, | ||
| }), | ||
| inspector: testserver.NewInspector(t).Ready(). | ||
| WithIntrospection(nodeUUID, introspection.Introspection{ | ||
| Finished: true, | ||
| }). | ||
| WithIntrospectionData(nodeUUID, introspection.Data{ | ||
| Inventory: introspection.InventoryType{ | ||
| Hostname: "node-0", | ||
| }, | ||
| }), | ||
|
|
||
| expectedDirty: false, | ||
| expectedDetailsHost: "node-0", | ||
| expectedPublish: "InspectionComplete Hardware inspection completed", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if tc.ironic != nil { | ||
| tc.ironic.Start() | ||
| defer tc.ironic.Stop() | ||
| } | ||
|
|
||
| if tc.inspector != nil { | ||
| tc.inspector.Start() | ||
| defer tc.inspector.Stop() | ||
| } | ||
|
|
||
| host := makeHost() | ||
| publishedMsg := "" | ||
| publisher := func(reason, message string) { | ||
| publishedMsg = reason + " " + message | ||
| } | ||
| auth := clients.AuthConfig{Type: clients.NoAuth} | ||
| prov, err := newProvisionerWithSettings(host, bmc.Credentials{}, publisher, | ||
| tc.ironic.Endpoint(), auth, tc.inspector.Endpoint(), auth, | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("could not create provisioner: %s", err) | ||
| } | ||
|
|
||
| prov.status.ID = nodeUUID | ||
| result, details, err := prov.InspectHardware() | ||
|
|
||
| assert.Equal(t, tc.expectedDirty, result.Dirty) | ||
| assert.Equal(t, time.Second*time.Duration(tc.expectedRequestAfter), result.RequeueAfter) | ||
| assert.Equal(t, tc.expectedResultError, result.ErrorMessage) | ||
|
|
||
| if details != nil { | ||
| assert.Equal(t, tc.expectedDetailsHost, details.Hostname) | ||
| } | ||
| assert.Equal(t, tc.expectedPublish, publishedMsg) | ||
| if tc.expectedError == "" { | ||
| assert.NoError(t, err) | ||
| } else { | ||
| assert.Error(t, err) | ||
| assert.Regexp(t, tc.expectedError, err.Error()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does the name come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just followed the Ironic Inspector API, ie https://docs.openstack.org/ironic-inspector/5.1.0/http-api.html#get-introspection-data, to have roughly a one-to-one relationship