@@ -246,6 +246,7 @@ func TestConfigureInstallation(t *testing.T) {
246
246
assert .Eventually (t , func () bool {
247
247
return sm .CurrentState () == tt .expectedState
248
248
}, time .Second , 100 * time .Millisecond , "state should be %s but is %s" , tt .expectedState , sm .CurrentState ())
249
+ assert .False (t , sm .IsLockAcquired (), "state machine should not be locked after configuration" )
249
250
250
251
mockManager .AssertExpectations (t )
251
252
})
@@ -413,6 +414,7 @@ func TestRunHostPreflights(t *testing.T) {
413
414
assert .Eventually (t , func () bool {
414
415
return sm .CurrentState () == tt .expectedState
415
416
}, time .Second , 100 * time .Millisecond , "state should be %s but is %s" , tt .expectedState , sm .CurrentState ())
417
+ assert .False (t , sm .IsLockAcquired (), "state machine should not be locked after running preflights" )
416
418
417
419
mockPreflightManager .AssertExpectations (t )
418
420
})
@@ -639,27 +641,33 @@ func TestGetInstallationStatus(t *testing.T) {
639
641
640
642
func TestSetupInfra (t * testing.T ) {
641
643
tests := []struct {
642
- name string
643
- currentState statemachine.State
644
- expectedState statemachine.State
645
- setupMocks func (runtimeconfig.RuntimeConfig , * preflight.MockHostPreflightManager , * installation.MockInstallationManager , * infra.MockInfraManager , * metrics.MockReporter )
646
- expectedErr bool
644
+ name string
645
+ clientIgnoreHostPreflights bool // From HTTP request
646
+ serverAllowIgnoreHostPreflights bool // From CLI flag
647
+ currentState statemachine.State
648
+ expectedState statemachine.State
649
+ setupMocks func (runtimeconfig.RuntimeConfig , * preflight.MockHostPreflightManager , * installation.MockInstallationManager , * infra.MockInfraManager , * metrics.MockReporter )
650
+ expectedErr error
647
651
}{
648
652
{
649
- name : "successful setup with passed preflights" ,
650
- currentState : StatePreflightsSucceeded ,
651
- expectedState : StateSucceeded ,
653
+ name : "successful setup with passed preflights" ,
654
+ clientIgnoreHostPreflights : false ,
655
+ serverAllowIgnoreHostPreflights : true ,
656
+ currentState : StatePreflightsSucceeded ,
657
+ expectedState : StateSucceeded ,
652
658
setupMocks : func (rc runtimeconfig.RuntimeConfig , pm * preflight.MockHostPreflightManager , im * installation.MockInstallationManager , fm * infra.MockInfraManager , r * metrics.MockReporter ) {
653
659
mock .InOrder (
654
660
fm .On ("Install" , mock .Anything , rc ).Return (nil ),
655
661
)
656
662
},
657
- expectedErr : false ,
663
+ expectedErr : nil ,
658
664
},
659
665
{
660
- name : "successful setup with failed preflights" ,
661
- currentState : StatePreflightsFailed ,
662
- expectedState : StateSucceeded ,
666
+ name : "successful setup with failed preflights - ignored with CLI flag" ,
667
+ clientIgnoreHostPreflights : true ,
668
+ serverAllowIgnoreHostPreflights : true ,
669
+ currentState : StatePreflightsFailed ,
670
+ expectedState : StateSucceeded ,
663
671
setupMocks : func (rc runtimeconfig.RuntimeConfig , pm * preflight.MockHostPreflightManager , im * installation.MockInstallationManager , fm * infra.MockInfraManager , r * metrics.MockReporter ) {
664
672
preflightOutput := & types.HostPreflightsOutput {
665
673
Fail : []types.HostPreflightsRecord {
@@ -675,37 +683,73 @@ func TestSetupInfra(t *testing.T) {
675
683
fm .On ("Install" , mock .Anything , rc ).Return (nil ),
676
684
)
677
685
},
678
- expectedErr : false ,
686
+ expectedErr : nil ,
679
687
},
680
688
{
681
- name : "preflight output error" ,
682
- currentState : StatePreflightsFailed ,
683
- expectedState : StatePreflightsFailed ,
689
+ name : "failed setup with failed preflights - not ignored" ,
690
+ clientIgnoreHostPreflights : false ,
691
+ serverAllowIgnoreHostPreflights : true ,
692
+ currentState : StatePreflightsFailed ,
693
+ expectedState : StatePreflightsFailed ,
694
+ setupMocks : func (rc runtimeconfig.RuntimeConfig , pm * preflight.MockHostPreflightManager , im * installation.MockInstallationManager , fm * infra.MockInfraManager , r * metrics.MockReporter ) {
695
+ },
696
+ expectedErr : types .NewBadRequestError (ErrPreflightChecksFailed ),
697
+ },
698
+ {
699
+ name : "preflight output error" ,
700
+ clientIgnoreHostPreflights : true ,
701
+ serverAllowIgnoreHostPreflights : true ,
702
+ currentState : StatePreflightsFailed ,
703
+ expectedState : StatePreflightsFailed ,
684
704
setupMocks : func (rc runtimeconfig.RuntimeConfig , pm * preflight.MockHostPreflightManager , im * installation.MockInstallationManager , fm * infra.MockInfraManager , r * metrics.MockReporter ) {
685
705
mock .InOrder (
686
706
pm .On ("GetHostPreflightOutput" , t .Context ()).Return (nil , errors .New ("get output error" )),
687
707
)
688
708
},
689
- expectedErr : true ,
709
+ expectedErr : errors . New ( "any error" ), // Just check that an error occurs, don't care about exact message
690
710
},
691
711
{
692
- name : "install infra error" ,
693
- currentState : StatePreflightsSucceeded ,
694
- expectedState : StateFailed ,
712
+ name : "install infra error" ,
713
+ clientIgnoreHostPreflights : false ,
714
+ serverAllowIgnoreHostPreflights : true ,
715
+ currentState : StatePreflightsSucceeded ,
716
+ expectedState : StateFailed ,
695
717
setupMocks : func (rc runtimeconfig.RuntimeConfig , pm * preflight.MockHostPreflightManager , im * installation.MockInstallationManager , fm * infra.MockInfraManager , r * metrics.MockReporter ) {
696
718
mock .InOrder (
697
719
fm .On ("Install" , mock .Anything , rc ).Return (errors .New ("install error" )),
698
720
)
699
721
},
700
- expectedErr : false ,
722
+ expectedErr : nil ,
701
723
},
702
724
{
703
- name : "invalid state transition" ,
704
- currentState : StateInstallationConfigured ,
705
- expectedState : StateInstallationConfigured ,
725
+ name : "invalid state transition" ,
726
+ clientIgnoreHostPreflights : false ,
727
+ serverAllowIgnoreHostPreflights : true ,
728
+ currentState : StateInstallationConfigured ,
729
+ expectedState : StateInstallationConfigured ,
706
730
setupMocks : func (rc runtimeconfig.RuntimeConfig , pm * preflight.MockHostPreflightManager , im * installation.MockInstallationManager , fm * infra.MockInfraManager , r * metrics.MockReporter ) {
707
731
},
708
- expectedErr : true ,
732
+ expectedErr : errors .New ("invalid transition" ), // Just check that an error occurs, don't care about exact message
733
+ },
734
+ {
735
+ name : "failed preflights with ignore flag but CLI flag disabled" ,
736
+ clientIgnoreHostPreflights : true ,
737
+ serverAllowIgnoreHostPreflights : false ,
738
+ currentState : StatePreflightsFailed ,
739
+ expectedState : StatePreflightsFailed ,
740
+ setupMocks : func (rc runtimeconfig.RuntimeConfig , pm * preflight.MockHostPreflightManager , im * installation.MockInstallationManager , fm * infra.MockInfraManager , r * metrics.MockReporter ) {
741
+ },
742
+ expectedErr : types .NewBadRequestError (ErrPreflightChecksFailed ),
743
+ },
744
+ {
745
+ name : "failed preflights without ignore flag and CLI flag disabled" ,
746
+ clientIgnoreHostPreflights : false ,
747
+ serverAllowIgnoreHostPreflights : false ,
748
+ currentState : StatePreflightsFailed ,
749
+ expectedState : StatePreflightsFailed ,
750
+ setupMocks : func (rc runtimeconfig.RuntimeConfig , pm * preflight.MockHostPreflightManager , im * installation.MockInstallationManager , fm * infra.MockInfraManager , r * metrics.MockReporter ) {
751
+ },
752
+ expectedErr : types .NewBadRequestError (ErrPreflightChecksFailed ),
709
753
},
710
754
}
711
755
@@ -730,13 +774,24 @@ func TestSetupInfra(t *testing.T) {
730
774
WithInstallationManager (mockInstallationManager ),
731
775
WithInfraManager (mockInfraManager ),
732
776
WithMetricsReporter (mockMetricsReporter ),
777
+ WithAllowIgnoreHostPreflights (tt .serverAllowIgnoreHostPreflights ),
733
778
)
734
779
require .NoError (t , err )
735
780
736
- err = controller .SetupInfra (t .Context ())
781
+ err = controller .SetupInfra (t .Context (), tt . clientIgnoreHostPreflights )
737
782
738
- if tt .expectedErr {
783
+ if tt .expectedErr != nil {
739
784
require .Error (t , err )
785
+
786
+ // Check for specific error types
787
+ var expectedAPIErr * types.APIError
788
+ if errors .As (tt .expectedErr , & expectedAPIErr ) {
789
+ // For API errors, check the exact type and status code
790
+ var actualAPIErr * types.APIError
791
+ require .True (t , errors .As (err , & actualAPIErr ), "expected error to be of type *types.APIError, got %T" , err )
792
+ assert .Equal (t , expectedAPIErr .StatusCode , actualAPIErr .StatusCode , "status codes should match" )
793
+ assert .Contains (t , actualAPIErr .Error (), expectedAPIErr .Unwrap ().Error (), "error messages should contain expected content" )
794
+ }
740
795
} else {
741
796
require .NoError (t , err )
742
797
@@ -746,6 +801,7 @@ func TestSetupInfra(t *testing.T) {
746
801
assert .Eventually (t , func () bool {
747
802
return sm .CurrentState () == tt .expectedState
748
803
}, time .Second , 100 * time .Millisecond , "state should be %s but is %s" , tt .expectedState , sm .CurrentState ())
804
+ assert .False (t , sm .IsLockAcquired (), "state machine should not be locked after running infra setup" )
749
805
750
806
mockPreflightManager .AssertExpectations (t )
751
807
mockInstallationManager .AssertExpectations (t )
0 commit comments