@@ -1116,6 +1116,9 @@ def expects_neither_new_or_cached_catalog
1116
1116
converged_environment: #{ last_server_specified_environment }
1117
1117
run_mode: agent
1118
1118
SUMMARY
1119
+
1120
+ expect ( Puppet ::Node . indirection ) . not_to receive ( :find )
1121
+ . with ( anything , hash_including ( :ignore_cache => true , :fail_on_404 => true ) )
1119
1122
end
1120
1123
1121
1124
it "prefers the environment set via cli" do
@@ -1125,26 +1128,27 @@ def expects_neither_new_or_cached_catalog
1125
1128
expect ( configurer . environment ) . to eq ( 'usethis' )
1126
1129
end
1127
1130
1128
- it "prefers the environment set via config" do
1131
+ it "prefers the environment set via lastrunfile over config" do
1129
1132
FileUtils . mkdir_p ( Puppet [ :confdir ] )
1130
1133
set_puppet_conf ( Puppet [ :confdir ] , <<~CONF )
1131
1134
[main]
1132
1135
environment = usethis
1136
+ lastrunfile = #{ Puppet [ :lastrunfile ] }
1133
1137
CONF
1134
1138
1135
1139
Puppet . initialize_settings
1136
1140
configurer . run
1137
1141
1138
- expect ( configurer . environment ) . to eq ( 'usethis' )
1142
+ expect ( configurer . environment ) . to eq ( last_server_specified_environment )
1139
1143
end
1140
1144
1141
- it "uses environment from Puppet[:environment] if given a catalog" do
1145
+ it "uses the environment from Puppet[:environment] if given a catalog" do
1142
1146
configurer . run ( catalog : catalog )
1143
1147
1144
1148
expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1145
1149
end
1146
1150
1147
- it "uses environment from Puppet[:environment] if use_cached_catalog = true" do
1151
+ it "uses the environment from Puppet[:environment] if use_cached_catalog = true" do
1148
1152
Puppet [ :use_cached_catalog ] = true
1149
1153
expects_cached_catalog_only ( catalog )
1150
1154
configurer . run
@@ -1173,14 +1177,14 @@ def expects_neither_new_or_cached_catalog
1173
1177
configurer . run
1174
1178
end
1175
1179
1176
- it "uses environment from Puppet[:environment] if strict_environment_mode is set" do
1180
+ it "uses the environment from Puppet[:environment] if strict_environment_mode is set" do
1177
1181
Puppet [ :strict_environment_mode ] = true
1178
1182
configurer . run
1179
1183
1180
1184
expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1181
1185
end
1182
1186
1183
- it "uses environment from Puppet[:environment] if initial_environment is the same as converged_environment" do
1187
+ it "uses the environment from Puppet[:environment] if initial_environment is the same as converged_environment" do
1184
1188
Puppet [ :lastrunfile ] = file_containing ( 'last_run_summary.yaml' , <<~SUMMARY )
1185
1189
---
1186
1190
version:
@@ -1195,41 +1199,84 @@ def expects_neither_new_or_cached_catalog
1195
1199
1196
1200
expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1197
1201
end
1202
+ end
1203
+ end
1204
+
1205
+ describe "when the last used environment is not available" do
1206
+ describe "when the node request succeeds" do
1207
+ let ( :node_environment ) { Puppet ::Node ::Environment . remote ( :salam ) }
1208
+ let ( :node ) { double ( Puppet ::Node , :environment => node_environment ) }
1209
+ let ( :last_server_specified_environment ) { 'development' }
1210
+
1211
+ before do
1212
+ allow ( Puppet ::Node . indirection ) . to receive ( :find )
1213
+ allow ( Puppet ::Node . indirection ) . to receive ( :find )
1214
+ . with ( anything , hash_including ( :ignore_cache => true , :fail_on_404 => true ) )
1215
+ . and_return ( node )
1216
+ end
1198
1217
1199
- it "uses environment from Puppet[:environment] if the run mode doesn't match" do
1218
+ it "uses the environment from the node request if the run mode doesn't match" do
1200
1219
Puppet [ :lastrunfile ] = file_containing ( 'last_run_summary.yaml' , <<~SUMMARY )
1201
- ---
1202
- version:
1203
- config: 1624882680
1204
- puppet: 6.24.0
1205
- application:
1206
- initial_environment: #{ Puppet [ :environment ] }
1207
- converged_environment: #{ last_server_specified_environment }
1208
- run_mode: user
1220
+ ---
1221
+ version:
1222
+ config: 1624882680
1223
+ puppet: 6.24.0
1224
+ application:
1225
+ initial_environment: #{ Puppet [ :environment ] }
1226
+ converged_environment: #{ last_server_specified_environment }
1227
+ run_mode: user
1209
1228
SUMMARY
1210
1229
configurer . run
1211
1230
1212
- expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1231
+ expect ( configurer . environment ) . to eq ( node_environment . name . to_s )
1213
1232
end
1214
1233
1215
- it "uses environment from Puppet[:environment] if lastrunfile is invalid YAML " do
1234
+ it "uses the environment from the node request if lastrunfile does not contain the expected keys " do
1216
1235
Puppet [ :lastrunfile ] = file_containing ( 'last_run_summary.yaml' , <<~SUMMARY )
1217
- Key: 'this is my very very very ' +
1218
- 'long string'
1236
+ ---
1237
+ version:
1238
+ config: 1624882680
1239
+ puppet: 6.24.0
1219
1240
SUMMARY
1220
1241
configurer . run
1221
1242
1222
- expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1243
+ expect ( configurer . environment ) . to eq ( node_environment . name . to_s )
1223
1244
end
1224
1245
1225
- it "uses environment from Puppet[:environment] if lastrunfile exists but is empty" do
1246
+ it "uses the environment from the node request if lastrunfile is invalid YAML" do
1247
+ Puppet [ :lastrunfile ] = file_containing ( 'last_run_summary.yaml' , <<~SUMMARY )
1248
+ Key: 'this is my very very very ' +
1249
+ 'long string'
1250
+ SUMMARY
1251
+ configurer . run
1252
+
1253
+ expect ( configurer . environment ) . to eq ( node_environment . name . to_s )
1254
+ end
1255
+
1256
+ it "uses the environment from the node request if lastrunfile exists but is empty" do
1226
1257
Puppet [ :lastrunfile ] = file_containing ( 'last_run_summary.yaml' , '' )
1227
1258
configurer . run
1228
1259
1229
- expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1260
+ expect ( configurer . environment ) . to eq ( node_environment . name . to_s )
1261
+ end
1262
+
1263
+ it "uses the environment from the node request if the last used one cannot be found" do
1264
+ Puppet [ :lastrunfile ] = tmpfile ( 'last_run_summary.yaml' )
1265
+ configurer . run
1266
+
1267
+ expect ( configurer . environment ) . to eq ( node_environment . name . to_s )
1268
+ end
1269
+ end
1270
+
1271
+ describe "when the node request fails" do
1272
+ before do
1273
+ allow ( Puppet ::Node . indirection ) . to receive ( :find ) . and_call_original
1274
+ allow ( Puppet ::Node . indirection ) . to receive ( :find )
1275
+ . with ( anything , hash_including ( :ignore_cache => true , :fail_on_404 => true ) )
1276
+ . and_raise ( Puppet ::Error )
1230
1277
end
1231
1278
1232
- it "uses environment from Puppet[:environment] if the last used one cannot be found" do
1279
+ it "uses the environment from Puppet[:environment] if the last used one cannot be found" do
1233
1280
Puppet [ :lastrunfile ] = tmpfile ( 'last_run_summary.yaml' )
1234
1281
configurer . run
1235
1282
0 commit comments