Skip to content

Commit 78ef843

Browse files
committed
revert remove of unused func.
1 parent c09fb2e commit 78ef843

File tree

2 files changed

+282
-6
lines changed

2 files changed

+282
-6
lines changed

pkg/services/baremetal/host/host.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,46 @@ func getDeviceNames(wwn []string, storageDevices []infrav1.Storage) []string {
14681468
return deviceNames
14691469
}
14701470

1471+
func analyzeSSHOutputInstallImage(out sshclient.Output, sshClient sshclient.Client, port int) (isTimeout, isConnectionRefused bool, reterr error) {
1472+
// check err
1473+
if out.Err != nil {
1474+
switch {
1475+
case os.IsTimeout(out.Err) || sshclient.IsTimeoutError(out.Err):
1476+
isTimeout = true
1477+
return isTimeout, false, nil
1478+
case sshclient.IsAuthenticationFailedError(out.Err):
1479+
if err := handleAuthenticationFailed(sshClient, port); err != nil {
1480+
return false, false, fmt.Errorf("original ssh error: %w. err: %w", out.Err, err)
1481+
}
1482+
return false, false, handleAuthenticationFailed(sshClient, port)
1483+
case sshclient.IsConnectionRefusedError(out.Err):
1484+
return false, verifyConnectionRefused(sshClient, port), nil
1485+
}
1486+
1487+
return false, false, fmt.Errorf("unhandled ssh error while getting hostname: %w", out.Err)
1488+
}
1489+
1490+
// check stderr
1491+
if out.StdErr != "" {
1492+
// This is an unexpected error
1493+
return false, false, fmt.Errorf("%w: StdErr: %s", errSSHGetHostname, out.StdErr)
1494+
}
1495+
1496+
// check stdout
1497+
hostname := trimLineBreak(out.StdOut)
1498+
switch hostname {
1499+
case "":
1500+
// Hostname should not be empty. This is unexpected.
1501+
return false, false, errEmptyHostName
1502+
case rescue: // We are in wrong boot, nothing has to be done to trigger reboot
1503+
return false, false, nil
1504+
}
1505+
1506+
// We are in the case that hostName != rescue && StdOut != hostName
1507+
// This is unexpected
1508+
return false, false, fmt.Errorf("%w: %s", errUnexpectedHostName, hostname)
1509+
}
1510+
14711511
func handleAuthenticationFailed(sshClient sshclient.Client, port int) error {
14721512
// Check whether we are in the wrong system in the case that rescue and os system might be running on the same port.
14731513
if port == rescuePort {

pkg/services/baremetal/host/host_test.go

Lines changed: 242 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -725,15 +725,74 @@ var _ = Describe("analyzeSSHOutputInstallImage", func() {
725725
expectedErrMessage string
726726
}
727727

728-
type testCaseAnalyzeSSHOutputRegistering struct {
728+
DescribeTable("analyzeSSHOutputInstallImage - out.Err",
729+
func(tc testCaseAnalyzeSSHOutputInstallImageOutErr) {
730+
host := helpers.BareMetalHost(
731+
"test-host",
732+
"default",
733+
)
734+
735+
robotMock := robotmock.Client{}
736+
robotMock.On("GetBootRescue", mock.Anything).Return(&models.Rescue{Active: tc.rescueActive}, nil)
737+
738+
service := newTestService(host, &robotMock, nil, nil, nil)
739+
740+
isTimeout, isConnectionRefused, err := service.analyzeSSHOutputRegistering(sshclient.Output{Err: tc.err})
741+
Expect(isTimeout).To(Equal(tc.expectedIsTimeout))
742+
Expect(isConnectionRefused).To(Equal(tc.expectedIsConnectionRefused))
743+
if tc.expectedErrMessage != "" {
744+
Expect(err).To(Not(BeNil()))
745+
Expect(err.Error()).To(ContainSubstring(tc.expectedErrMessage))
746+
} else {
747+
Expect(err).To(BeNil())
748+
}
749+
},
750+
Entry("timeout error", testCaseAnalyzeSSHOutputInstallImageOutErr{
751+
err: timeout,
752+
rescueActive: true,
753+
expectedIsTimeout: true,
754+
expectedIsConnectionRefused: false,
755+
expectedErrMessage: "",
756+
}),
757+
Entry("authenticationFailed error, rescue active", testCaseAnalyzeSSHOutputInstallImageOutErr{
758+
err: sshclient.ErrAuthenticationFailed,
759+
rescueActive: true,
760+
expectedIsTimeout: false,
761+
expectedIsConnectionRefused: false,
762+
expectedErrMessage: "",
763+
}),
764+
Entry("authenticationFailed error, rescue not active", testCaseAnalyzeSSHOutputInstallImageOutErr{
765+
err: sshclient.ErrAuthenticationFailed,
766+
rescueActive: false,
767+
expectedIsTimeout: false,
768+
expectedIsConnectionRefused: false,
769+
expectedErrMessage: "wrong ssh key",
770+
}),
771+
Entry("connectionRefused error, rescue active", testCaseAnalyzeSSHOutputInstallImageOutErr{
772+
err: sshclient.ErrConnectionRefused,
773+
rescueActive: true,
774+
expectedIsTimeout: false,
775+
expectedIsConnectionRefused: true,
776+
expectedErrMessage: "",
777+
}),
778+
Entry("connectionRefused error, rescue not active", testCaseAnalyzeSSHOutputInstallImageOutErr{
779+
err: sshclient.ErrConnectionRefused,
780+
rescueActive: false,
781+
expectedIsTimeout: false,
782+
expectedIsConnectionRefused: true,
783+
expectedErrMessage: "",
784+
}),
785+
)
786+
787+
type testCaseAnalyzeSSHOutputInstallImageStdErr struct {
729788
hasNilErr bool
730789
stdErr string
731790
hostName string
732791
expectedErrMessage string
733792
}
734793

735794
DescribeTable("analyzeSSHOutputRegistering - toggle stdErr and hostName",
736-
func(tc testCaseAnalyzeSSHOutputRegistering) {
795+
func(tc testCaseAnalyzeSSHOutputInstallImageStdErr) {
737796
var err error
738797
if !tc.hasNilErr {
739798
err = errTest
@@ -765,25 +824,25 @@ var _ = Describe("analyzeSSHOutputInstallImage", func() {
765824
Expect(err).To(BeNil())
766825
}
767826
},
768-
Entry("stderr not empty", testCaseAnalyzeSSHOutputRegistering{
827+
Entry("stderr not empty", testCaseAnalyzeSSHOutputInstallImageStdErr{
769828
hasNilErr: true,
770829
stdErr: "command failed",
771830
hostName: "hostName",
772831
expectedErrMessage: "failed to get hostname via ssh: StdErr:",
773832
}),
774-
Entry("stderr not empty - err != nil", testCaseAnalyzeSSHOutputRegistering{
833+
Entry("stderr not empty - err != nil", testCaseAnalyzeSSHOutputInstallImageStdErr{
775834
hasNilErr: false,
776835
stdErr: "command failed",
777836
hostName: "",
778837
expectedErrMessage: "unhandled ssh error while getting hostname",
779838
}),
780-
Entry("stderr not empty - wrong hostName", testCaseAnalyzeSSHOutputRegistering{
839+
Entry("stderr not empty - wrong hostName", testCaseAnalyzeSSHOutputInstallImageStdErr{
781840
hasNilErr: true,
782841
stdErr: "command failed",
783842
hostName: "",
784843
expectedErrMessage: "failed to get hostname via ssh: StdErr:",
785844
}),
786-
Entry("stderr empty - wrong hostName", testCaseAnalyzeSSHOutputRegistering{
845+
Entry("stderr empty - wrong hostName", testCaseAnalyzeSSHOutputInstallImageStdErr{
787846
hasNilErr: true,
788847
stdErr: "",
789848
hostName: "",
@@ -802,19 +861,196 @@ var _ = Describe("analyzeSSHOutputInstallImage", func() {
802861
expectedErrMessage string
803862
}
804863

864+
DescribeTable("analyzeSSHOutputInstallImage - out.Err",
865+
func(tc testCaseAnalyzeSSHOutputInstallImageOutErr) {
866+
sshMock := &sshmock.Client{}
867+
var errFromGetHostName error
868+
if !tc.errFromGetHostNameNil {
869+
errFromGetHostName = errTest
870+
}
871+
sshMock.On("GetHostName").Return(sshclient.Output{Err: errFromGetHostName})
872+
873+
isTimeout, isConnectionRefused, err := analyzeSSHOutputInstallImage(sshclient.Output{Err: tc.err}, sshMock, tc.port)
874+
Expect(isTimeout).To(Equal(tc.expectedIsTimeout))
875+
Expect(isConnectionRefused).To(Equal(tc.expectedIsConnectionRefused))
876+
if tc.expectedErrMessage != "" {
877+
Expect(err).To(Not(BeNil()))
878+
Expect(err.Error()).To(ContainSubstring(tc.expectedErrMessage))
879+
} else {
880+
Expect(err).To(BeNil())
881+
}
882+
},
883+
Entry("timeout error", testCaseAnalyzeSSHOutputInstallImageOutErr{
884+
err: timeout,
885+
errFromGetHostNameNil: true,
886+
port: 22,
887+
expectedIsTimeout: true,
888+
expectedIsConnectionRefused: false,
889+
expectedErrMessage: "",
890+
}),
891+
Entry("authenticationFailed error, port 22, no hostName error", testCaseAnalyzeSSHOutputInstallImageOutErr{
892+
err: sshclient.ErrAuthenticationFailed,
893+
errFromGetHostNameNil: true,
894+
port: 22,
895+
expectedIsTimeout: false,
896+
expectedIsConnectionRefused: false,
897+
expectedErrMessage: "",
898+
}),
899+
Entry("authenticationFailed error, port 22, hostName error", testCaseAnalyzeSSHOutputInstallImageOutErr{
900+
err: sshclient.ErrAuthenticationFailed,
901+
errFromGetHostNameNil: false,
902+
port: 22,
903+
expectedIsTimeout: false,
904+
expectedIsConnectionRefused: false,
905+
expectedErrMessage: "wrong ssh key",
906+
}),
907+
Entry("authenticationFailed error, port != 22", testCaseAnalyzeSSHOutputInstallImageOutErr{
908+
err: sshclient.ErrAuthenticationFailed,
909+
errFromGetHostNameNil: true,
910+
port: 23,
911+
expectedIsTimeout: false,
912+
expectedIsConnectionRefused: false,
913+
expectedErrMessage: "wrong ssh key",
914+
}),
915+
Entry("connectionRefused error, port 22", testCaseAnalyzeSSHOutputInstallImageOutErr{
916+
err: sshclient.ErrConnectionRefused,
917+
errFromGetHostNameNil: true,
918+
port: 22,
919+
expectedIsTimeout: false,
920+
expectedIsConnectionRefused: true,
921+
expectedErrMessage: "",
922+
}),
923+
Entry("connectionRefused error, port != 22, hostname error", testCaseAnalyzeSSHOutputInstallImageOutErr{
924+
err: sshclient.ErrConnectionRefused,
925+
errFromGetHostNameNil: false,
926+
port: 23,
927+
expectedIsTimeout: false,
928+
expectedIsConnectionRefused: true,
929+
expectedErrMessage: "",
930+
}),
931+
Entry("connectionRefused error, port != 22, no hostname error", testCaseAnalyzeSSHOutputInstallImageOutErr{
932+
err: sshclient.ErrConnectionRefused,
933+
errFromGetHostNameNil: true,
934+
port: 23,
935+
expectedIsTimeout: false,
936+
expectedIsConnectionRefused: false,
937+
expectedErrMessage: "",
938+
}),
939+
)
940+
805941
type testCaseAnalyzeSSHOutputInstallImageStdErr struct {
806942
hasNilErr bool
807943
stdErr string
808944
hasWrongHostName bool
809945
expectedErrMessage string
810946
}
811947

948+
DescribeTable("analyzeSSHOutputInstallImage - StdErr not empty",
949+
func(tc testCaseAnalyzeSSHOutputInstallImageStdErr) {
950+
var err error
951+
if !tc.hasNilErr {
952+
err = errTest
953+
}
954+
hostName := "rescue"
955+
if tc.hasWrongHostName {
956+
hostName = "wrongHostName"
957+
}
958+
959+
out := sshclient.Output{
960+
StdOut: hostName,
961+
StdErr: tc.stdErr,
962+
Err: err,
963+
}
964+
isTimeout, isConnectionRefused, err := analyzeSSHOutputInstallImage(out, nil, 22)
965+
Expect(isTimeout).To(Equal(false))
966+
Expect(isConnectionRefused).To(Equal(false))
967+
if tc.expectedErrMessage != "" {
968+
Expect(err).To(Not(BeNil()))
969+
Expect(err.Error()).To(ContainSubstring(tc.expectedErrMessage))
970+
} else {
971+
Expect(err).To(BeNil())
972+
}
973+
},
974+
Entry("stderr not empty", testCaseAnalyzeSSHOutputInstallImageStdErr{
975+
hasNilErr: true,
976+
stdErr: "command failed",
977+
hasWrongHostName: false,
978+
expectedErrMessage: "failed to get hostname via ssh: StdErr:",
979+
}),
980+
Entry("stderr not empty - err != nil", testCaseAnalyzeSSHOutputInstallImageStdErr{
981+
hasNilErr: false,
982+
stdErr: "command failed",
983+
hasWrongHostName: false,
984+
expectedErrMessage: "unhandled ssh error while getting hostname",
985+
}),
986+
Entry("stderr not empty - wrong hostName", testCaseAnalyzeSSHOutputInstallImageStdErr{
987+
hasNilErr: true,
988+
stdErr: "command failed",
989+
hasWrongHostName: true,
990+
expectedErrMessage: "failed to get hostname via ssh: StdErr:",
991+
}),
992+
)
993+
812994
type testCaseAnalyzeSSHOutputInstallImageWrongHostname struct {
813995
hasNilErr bool
814996
stdErr string
815997
hostName string
816998
expectedErrMessage string
817999
}
1000+
1001+
DescribeTable("analyzeSSHOutputInstallImage - wrong hostName",
1002+
func(tc testCaseAnalyzeSSHOutputInstallImageWrongHostname) {
1003+
var err error
1004+
if !tc.hasNilErr {
1005+
err = errTest
1006+
}
1007+
1008+
out := sshclient.Output{
1009+
StdOut: tc.hostName,
1010+
StdErr: tc.stdErr,
1011+
Err: err,
1012+
}
1013+
isTimeout, isConnectionRefused, err := analyzeSSHOutputInstallImage(out, nil, 22)
1014+
Expect(isTimeout).To(Equal(false))
1015+
Expect(isConnectionRefused).To(Equal(false))
1016+
if tc.expectedErrMessage != "" {
1017+
Expect(err).To(Not(BeNil()))
1018+
Expect(err.Error()).To(ContainSubstring(tc.expectedErrMessage))
1019+
} else {
1020+
Expect(err).To(BeNil())
1021+
}
1022+
},
1023+
Entry("empty hostname", testCaseAnalyzeSSHOutputInstallImageWrongHostname{
1024+
hasNilErr: true,
1025+
stdErr: "",
1026+
hostName: "",
1027+
expectedErrMessage: "hostname is empty",
1028+
}),
1029+
Entry("empty hostname - err not empty", testCaseAnalyzeSSHOutputInstallImageWrongHostname{
1030+
hasNilErr: false,
1031+
stdErr: "",
1032+
hostName: "",
1033+
expectedErrMessage: "unhandled ssh error while getting hostname",
1034+
}),
1035+
Entry("empty hostname stderr not empty", testCaseAnalyzeSSHOutputInstallImageWrongHostname{
1036+
hasNilErr: true,
1037+
stdErr: "command failed",
1038+
hostName: "",
1039+
expectedErrMessage: "failed to get hostname via ssh: StdErr:",
1040+
}),
1041+
Entry("hostname == rescue", testCaseAnalyzeSSHOutputInstallImageWrongHostname{
1042+
hasNilErr: true,
1043+
stdErr: "",
1044+
hostName: "rescue",
1045+
expectedErrMessage: "",
1046+
}),
1047+
Entry("hostname == otherHostName", testCaseAnalyzeSSHOutputInstallImageWrongHostname{
1048+
hasNilErr: true,
1049+
stdErr: "",
1050+
hostName: "otherHostName",
1051+
expectedErrMessage: "unexpected hostname",
1052+
}),
1053+
)
8181054
})
8191055

8201056
var _ = Describe("analyzeSSHOutputProvisioned", func() {

0 commit comments

Comments
 (0)