@@ -5,12 +5,13 @@ import (
5
5
"testing"
6
6
"time"
7
7
8
+ msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
9
+ "github.com/stretchr/testify/require"
10
+
8
11
"github.com/hashicorp/consul/acl"
9
12
"github.com/hashicorp/consul/agent/structs"
10
13
"github.com/hashicorp/consul/sdk/testutil/retry"
11
14
"github.com/hashicorp/consul/testrpc"
12
- msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
13
- "github.com/stretchr/testify/require"
14
15
)
15
16
16
17
func TestConfigEntry_Apply (t * testing.T ) {
@@ -811,6 +812,9 @@ func TestConfigEntry_ResolveServiceConfig_Blocking(t *testing.T) {
811
812
// of the blocking query does NOT bleed over into the next run. Concretely
812
813
// in this test the data present in the initial proxy-defaults should not
813
814
// be present when we are woken up due to proxy-defaults being deleted.
815
+ //
816
+ // This test does not pertain to upstreams, see:
817
+ // TestConfigEntry_ResolveServiceConfig_Upstreams_Blocking
814
818
815
819
state := s1 .fsm .State ()
816
820
require .NoError (state .EnsureConfigEntry (1 , & structs.ProxyConfigEntry {
@@ -962,6 +966,205 @@ func TestConfigEntry_ResolveServiceConfig_Blocking(t *testing.T) {
962
966
}
963
967
}
964
968
969
+ func TestConfigEntry_ResolveServiceConfig_Upstreams_Blocking (t * testing.T ) {
970
+ if testing .Short () {
971
+ t .Skip ("too slow for testing.Short" )
972
+ }
973
+
974
+ t .Parallel ()
975
+
976
+ dir1 , s1 := testServer (t )
977
+ defer os .RemoveAll (dir1 )
978
+ defer s1 .Shutdown ()
979
+ codec := rpcClient (t , s1 )
980
+ defer codec .Close ()
981
+
982
+ // The main thing this should test is that information from one iteration
983
+ // of the blocking query does NOT bleed over into the next run. Concretely
984
+ // in this test the data present in the initial proxy-defaults should not
985
+ // be present when we are woken up due to proxy-defaults being deleted.
986
+ //
987
+ // This test is about fields in upstreams, see:
988
+ // TestConfigEntry_ResolveServiceConfig_Blocking
989
+
990
+ state := s1 .fsm .State ()
991
+ require .NoError (t , state .EnsureConfigEntry (1 , & structs.ServiceConfigEntry {
992
+ Kind : structs .ServiceDefaults ,
993
+ Name : "foo" ,
994
+ Protocol : "http" ,
995
+ }, nil ))
996
+ require .NoError (t , state .EnsureConfigEntry (2 , & structs.ServiceConfigEntry {
997
+ Kind : structs .ServiceDefaults ,
998
+ Name : "bar" ,
999
+ Protocol : "http" ,
1000
+ }, nil ))
1001
+
1002
+ var index uint64
1003
+
1004
+ runStep (t , "foo and bar should be both http" , func (t * testing.T ) {
1005
+ // Verify that we get the results of service-defaults for 'foo' and 'bar'.
1006
+ var out structs.ServiceConfigResponse
1007
+ require .NoError (t , msgpackrpc .CallWithCodec (codec , "ConfigEntry.ResolveServiceConfig" ,
1008
+ & structs.ServiceConfigRequest {
1009
+ Name : "foo" ,
1010
+ Datacenter : "dc1" ,
1011
+ UpstreamIDs : []structs.ServiceID {
1012
+ structs .NewServiceID ("bar" , nil ),
1013
+ structs .NewServiceID ("other" , nil ),
1014
+ },
1015
+ },
1016
+ & out ,
1017
+ ))
1018
+
1019
+ expected := structs.ServiceConfigResponse {
1020
+ ProxyConfig : map [string ]interface {}{
1021
+ "protocol" : "http" ,
1022
+ },
1023
+ UpstreamIDConfigs : []structs.UpstreamConfig {
1024
+ {
1025
+ Upstream : structs .NewServiceID ("bar" , nil ),
1026
+ Config : map [string ]interface {}{
1027
+ "protocol" : "http" ,
1028
+ },
1029
+ },
1030
+ },
1031
+ QueryMeta : out .QueryMeta , // don't care
1032
+ }
1033
+
1034
+ require .Equal (t , expected , out )
1035
+ index = out .Index
1036
+ })
1037
+
1038
+ runStep (t , "blocking query for foo wakes on bar entry delete" , func (t * testing.T ) {
1039
+ // Now setup a blocking query for 'foo' while we erase the
1040
+ // service-defaults for bar.
1041
+
1042
+ // Async cause a change
1043
+ start := time .Now ()
1044
+ go func () {
1045
+ time .Sleep (100 * time .Millisecond )
1046
+ err := state .DeleteConfigEntry (index + 1 ,
1047
+ structs .ServiceDefaults ,
1048
+ "bar" ,
1049
+ nil ,
1050
+ )
1051
+ if err != nil {
1052
+ t .Errorf ("delete config entry failed: %v" , err )
1053
+ }
1054
+ }()
1055
+
1056
+ // Re-run the query
1057
+ var out structs.ServiceConfigResponse
1058
+ require .NoError (t , msgpackrpc .CallWithCodec (codec , "ConfigEntry.ResolveServiceConfig" ,
1059
+ & structs.ServiceConfigRequest {
1060
+ Name : "foo" ,
1061
+ Datacenter : "dc1" ,
1062
+ UpstreamIDs : []structs.ServiceID {
1063
+ structs .NewServiceID ("bar" , nil ),
1064
+ structs .NewServiceID ("other" , nil ),
1065
+ },
1066
+ QueryOptions : structs.QueryOptions {
1067
+ MinQueryIndex : index ,
1068
+ MaxQueryTime : time .Second ,
1069
+ },
1070
+ },
1071
+ & out ,
1072
+ ))
1073
+
1074
+ // Should block at least 100ms
1075
+ require .True (t , time .Since (start ) >= 100 * time .Millisecond , "too fast" )
1076
+
1077
+ // Check the indexes
1078
+ require .Equal (t , out .Index , index + 1 )
1079
+
1080
+ expected := structs.ServiceConfigResponse {
1081
+ ProxyConfig : map [string ]interface {}{
1082
+ "protocol" : "http" ,
1083
+ },
1084
+ QueryMeta : out .QueryMeta , // don't care
1085
+ }
1086
+
1087
+ require .Equal (t , expected , out )
1088
+ index = out .Index
1089
+ })
1090
+
1091
+ runStep (t , "foo should be http and bar should be unset" , func (t * testing.T ) {
1092
+ // Verify that we get the results of service-defaults for just 'foo'.
1093
+ var out structs.ServiceConfigResponse
1094
+ require .NoError (t , msgpackrpc .CallWithCodec (codec , "ConfigEntry.ResolveServiceConfig" ,
1095
+ & structs.ServiceConfigRequest {
1096
+ Name : "foo" ,
1097
+ Datacenter : "dc1" ,
1098
+ UpstreamIDs : []structs.ServiceID {
1099
+ structs .NewServiceID ("bar" , nil ),
1100
+ structs .NewServiceID ("other" , nil ),
1101
+ },
1102
+ },
1103
+ & out ,
1104
+ ))
1105
+
1106
+ expected := structs.ServiceConfigResponse {
1107
+ ProxyConfig : map [string ]interface {}{
1108
+ "protocol" : "http" ,
1109
+ },
1110
+ QueryMeta : out .QueryMeta , // don't care
1111
+ }
1112
+
1113
+ require .Equal (t , expected , out )
1114
+ index = out .Index
1115
+ })
1116
+
1117
+ runStep (t , "blocking query for foo wakes on foo entry delete" , func (t * testing.T ) {
1118
+ // Now setup a blocking query for 'foo' while we erase the
1119
+ // service-defaults for foo.
1120
+
1121
+ // Async cause a change
1122
+ start := time .Now ()
1123
+ go func () {
1124
+ time .Sleep (100 * time .Millisecond )
1125
+ err := state .DeleteConfigEntry (index + 1 ,
1126
+ structs .ServiceDefaults ,
1127
+ "foo" ,
1128
+ nil ,
1129
+ )
1130
+ if err != nil {
1131
+ t .Errorf ("delete config entry failed: %v" , err )
1132
+ }
1133
+ }()
1134
+
1135
+ // Re-run the query
1136
+ var out structs.ServiceConfigResponse
1137
+ require .NoError (t , msgpackrpc .CallWithCodec (codec , "ConfigEntry.ResolveServiceConfig" ,
1138
+ & structs.ServiceConfigRequest {
1139
+ Name : "foo" ,
1140
+ Datacenter : "dc1" ,
1141
+ UpstreamIDs : []structs.ServiceID {
1142
+ structs .NewServiceID ("bar" , nil ),
1143
+ structs .NewServiceID ("other" , nil ),
1144
+ },
1145
+ QueryOptions : structs.QueryOptions {
1146
+ MinQueryIndex : index ,
1147
+ MaxQueryTime : time .Second ,
1148
+ },
1149
+ },
1150
+ & out ,
1151
+ ))
1152
+
1153
+ // Should block at least 100ms
1154
+ require .True (t , time .Since (start ) >= 100 * time .Millisecond , "too fast" )
1155
+
1156
+ // Check the indexes
1157
+ require .Equal (t , out .Index , index + 1 )
1158
+
1159
+ expected := structs.ServiceConfigResponse {
1160
+ QueryMeta : out .QueryMeta , // don't care
1161
+ }
1162
+
1163
+ require .Equal (t , expected , out )
1164
+ index = out .Index
1165
+ })
1166
+ }
1167
+
965
1168
func TestConfigEntry_ResolveServiceConfig_UpstreamProxyDefaultsProtocol (t * testing.T ) {
966
1169
t .Parallel ()
967
1170
@@ -1219,3 +1422,10 @@ func TestConfigEntry_ProxyDefaultsExposeConfig(t *testing.T) {
1219
1422
require .True (t , ok )
1220
1423
require .Equal (t , expose , proxyConf .Expose )
1221
1424
}
1425
+
1426
+ func runStep (t * testing.T , name string , fn func (t * testing.T )) {
1427
+ t .Helper ()
1428
+ if ! t .Run (name , fn ) {
1429
+ t .FailNow ()
1430
+ }
1431
+ }
0 commit comments