@@ -962,7 +962,48 @@ func TestStateStore_Session_Invalidate_PreparedQuery_Delete(t *testing.T) {
962
962
}
963
963
}
964
964
965
- func TestHealthCheck_SessionDestroy (t * testing.T ) {
965
+ func TestHealthCheck_SessionRegistrationFail (t * testing.T ) {
966
+ s := testStateStore (t )
967
+
968
+ var check * structs.HealthCheck
969
+ // setup node
970
+ testRegisterNode (t , s , 1 , "foo-node" )
971
+ testRegisterCheckCustom (t , s , 1 , "foo" , func (chk * structs.HealthCheck ) {
972
+ chk .Node = "foo-node"
973
+ chk .Type = "tll"
974
+ chk .Status = api .HealthCritical
975
+ chk .Definition = structs.HealthCheckDefinition {
976
+ SessionName : "test-session" ,
977
+ }
978
+ check = chk
979
+ })
980
+
981
+ // Ensure the index was not updated if nothing was destroyed.
982
+ if idx := s .maxIndex ("sessions" ); idx != 0 {
983
+ t .Fatalf ("bad index: %d" , idx )
984
+ }
985
+
986
+ // Register a new session
987
+ sess := & structs.Session {
988
+ ID : testUUID (),
989
+ Node : "foo-node" ,
990
+ Name : "test-session" ,
991
+ Checks : make ([]types.CheckID , 0 ),
992
+ }
993
+
994
+ sess .Checks = append (sess .Checks , check .CheckID )
995
+ // assert the check is critical initially
996
+ assertHealthCheckStatus (t , s , sess , check .CheckID , api .HealthCritical )
997
+
998
+ if err := s .SessionCreate (2 , sess ); err == nil {
999
+ // expecting error: Check 'foo' is in critical state
1000
+ t .Fatalf ("expected error, got nil" )
1001
+ }
1002
+ }
1003
+
1004
+ // Allow the session to be created even if the check is critical.
1005
+ // This is mainly to discount the health check of type `session`
1006
+ func TestHealthCheck_SessionRegistrationAllow (t * testing.T ) {
966
1007
s := testStateStore (t )
967
1008
968
1009
var check * structs.HealthCheck
@@ -971,7 +1012,7 @@ func TestHealthCheck_SessionDestroy(t *testing.T) {
971
1012
testRegisterCheckCustom (t , s , 1 , "foo" , func (chk * structs.HealthCheck ) {
972
1013
chk .Node = "foo-node"
973
1014
chk .Type = "session"
974
- chk .Status = api .HealthPassing
1015
+ chk .Status = api .HealthCritical
975
1016
chk .Definition = structs.HealthCheckDefinition {
976
1017
SessionName : "test-session" ,
977
1018
}
@@ -985,50 +1026,80 @@ func TestHealthCheck_SessionDestroy(t *testing.T) {
985
1026
986
1027
// Register a new session
987
1028
sess := & structs.Session {
988
- ID : testUUID (),
989
- Node : "foo-node" ,
990
- Name : "test-session" ,
1029
+ ID : testUUID (),
1030
+ Node : "foo-node" ,
1031
+ Name : "test-session" ,
1032
+ Checks : make ([]types.CheckID , 0 ),
991
1033
}
1034
+
1035
+ sess .Checks = append (sess .Checks , check .CheckID )
1036
+ // assert the check is critical initially
1037
+ assertHealthCheckStatus (t , s , sess , check .CheckID , api .HealthCritical )
1038
+
992
1039
if err := s .SessionCreate (2 , sess ); err != nil {
993
- t .Fatalf ("err: %s" , err )
1040
+ t .Fatalf ("The system shall allow session to be created ignoring the session check is critical. err: %s" , err )
994
1041
}
1042
+ }
995
1043
996
- _ , hc , err := s .ChecksInState (nil , api .HealthAny , structs .DefaultEnterpriseMetaInPartition ("" ), structs .DefaultPeerKeyword )
997
- if err != nil {
998
- t .Fatalf ("err: %s" , err )
999
- }
1000
- // iterate over checks and find the right one
1001
- found := false
1002
- for _ , c := range hc {
1003
- if c .CheckID == check .CheckID && c .Status == api .HealthPassing {
1004
- found = true
1005
- break
1044
+ func TestHealthCheck_Session (t * testing.T ) {
1045
+ s := testStateStore (t )
1046
+
1047
+ var check * structs.HealthCheck
1048
+ // setup node
1049
+ testRegisterNode (t , s , 1 , "foo-node" )
1050
+ testRegisterCheckCustom (t , s , 1 , "foo" , func (chk * structs.HealthCheck ) {
1051
+ chk .Node = "foo-node"
1052
+ chk .Type = "session"
1053
+ chk .Status = api .HealthCritical
1054
+ chk .Definition = structs.HealthCheckDefinition {
1055
+ SessionName : "test-session" ,
1006
1056
}
1057
+ check = chk
1058
+ })
1059
+
1060
+ // Ensure the index was not updated if nothing was destroyed.
1061
+ if idx := s .maxIndex ("sessions" ); idx != 0 {
1062
+ t .Fatalf ("bad index: %d" , idx )
1063
+ }
1064
+
1065
+ // Register a new session
1066
+ sess := & structs.Session {
1067
+ ID : testUUID (),
1068
+ Node : "foo-node" ,
1069
+ Name : "test-session" ,
1007
1070
}
1071
+ // assert the check is critical initially
1072
+ assertHealthCheckStatus (t , s , sess , check .CheckID , api .HealthCritical )
1008
1073
1009
- if ! found {
1010
- t .Fatalf ("check is expected to be passing" )
1074
+ if err := s . SessionCreate ( 2 , sess ); err != nil {
1075
+ t .Fatalf ("The system shall allow session to be created ignoring the session check is critical. err: %s" , err )
1011
1076
}
1077
+ // assert the check is critical after session creation
1078
+ assertHealthCheckStatus (t , s , sess , check .CheckID , api .HealthPassing )
1012
1079
1013
1080
// Destroy the session.
1014
1081
if err := s .SessionDestroy (3 , sess .ID , nil ); err != nil {
1015
1082
t .Fatalf ("err: %s" , err )
1016
1083
}
1084
+ // assert the check is critical after session destroy
1085
+ assertHealthCheckStatus (t , s , sess , check .CheckID , api .HealthCritical )
1086
+ }
1017
1087
1018
- _ , hc , err = s .ChecksInState (nil , api .HealthAny , structs .DefaultEnterpriseMetaInPartition ("" ), structs .DefaultPeerKeyword )
1088
+ func assertHealthCheckStatus (t * testing.T , s * Store , session * structs.Session , checkID types.CheckID , expectedStatus string ) {
1089
+ _ , hc , err := s .NodeChecks (nil , session .Node , structs .DefaultEnterpriseMetaInPartition ("" ), structs .DefaultPeerKeyword )
1019
1090
if err != nil {
1020
1091
t .Fatalf ("err: %s" , err )
1021
1092
}
1022
- // iterate over checks and find the right one
1023
- found = false
1093
+ // assert the check is healthy
1024
1094
for _ , c := range hc {
1025
- if c .CheckID == check .CheckID && c .Status == api .HealthCritical {
1026
- found = true
1027
- break
1095
+ if c .CheckID == checkID {
1096
+ if c .Status != expectedStatus {
1097
+ t .Fatalf ("check is expected to be %s but actually it is %s" , expectedStatus , c .Status )
1098
+ } else {
1099
+ return
1100
+ }
1028
1101
}
1029
1102
}
1030
1103
1031
- if ! found {
1032
- t .Fatalf ("check is expected to be critical" )
1033
- }
1104
+ t .Fatalf ("check %s, is not found" , string (checkID ))
1034
1105
}
0 commit comments