2
2
require 'puppet/configurer'
3
3
4
4
describe Puppet ::Configurer do
5
+ include PuppetSpec ::Files
6
+
5
7
before do
6
8
Puppet [ :server ] = "puppetmaster"
7
9
Puppet [ :report ] = true
10
12
allow_any_instance_of ( described_class ) . to (
11
13
receive ( :valid_server_environment? ) . and_return ( true )
12
14
)
15
+
16
+ Puppet [ :lastrunfile ] = file_containing ( 'last_run_summary.yaml' , <<~SUMMARY )
17
+ ---
18
+ version:
19
+ config: 1624882680
20
+ puppet: #{ Puppet . version }
21
+ application:
22
+ initial_environment: #{ Puppet [ :environment ] }
23
+ converged_environment: #{ Puppet [ :environment ] }
24
+ run_mode: agent
25
+ SUMMARY
13
26
end
14
27
15
28
let ( :node_name ) { Puppet [ :node_name_value ] }
@@ -1099,7 +1112,6 @@ def expects_neither_new_or_cached_catalog
1099
1112
end
1100
1113
1101
1114
describe "when selecting an environment" do
1102
- include PuppetSpec ::Files
1103
1115
include PuppetSpec ::Settings
1104
1116
1105
1117
describe "when the last used environment is available" do
@@ -1116,6 +1128,9 @@ def expects_neither_new_or_cached_catalog
1116
1128
converged_environment: #{ last_server_specified_environment }
1117
1129
run_mode: agent
1118
1130
SUMMARY
1131
+
1132
+ expect ( Puppet ::Node . indirection ) . not_to receive ( :find )
1133
+ . with ( anything , hash_including ( :ignore_cache => true , :fail_on_404 => true ) )
1119
1134
end
1120
1135
1121
1136
it "prefers the environment set via cli" do
@@ -1125,26 +1140,27 @@ def expects_neither_new_or_cached_catalog
1125
1140
expect ( configurer . environment ) . to eq ( 'usethis' )
1126
1141
end
1127
1142
1128
- it "prefers the environment set via config" do
1143
+ it "prefers the environment set via lastrunfile over config" do
1129
1144
FileUtils . mkdir_p ( Puppet [ :confdir ] )
1130
1145
set_puppet_conf ( Puppet [ :confdir ] , <<~CONF )
1131
1146
[main]
1132
1147
environment = usethis
1148
+ lastrunfile = #{ Puppet [ :lastrunfile ] }
1133
1149
CONF
1134
1150
1135
1151
Puppet . initialize_settings
1136
1152
configurer . run
1137
1153
1138
- expect ( configurer . environment ) . to eq ( 'usethis' )
1154
+ expect ( configurer . environment ) . to eq ( last_server_specified_environment )
1139
1155
end
1140
1156
1141
- it "uses environment from Puppet[:environment] if given a catalog" do
1157
+ it "uses the environment from Puppet[:environment] if given a catalog" do
1142
1158
configurer . run ( catalog : catalog )
1143
1159
1144
1160
expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1145
1161
end
1146
1162
1147
- it "uses environment from Puppet[:environment] if use_cached_catalog = true" do
1163
+ it "uses the environment from Puppet[:environment] if use_cached_catalog = true" do
1148
1164
Puppet [ :use_cached_catalog ] = true
1149
1165
expects_cached_catalog_only ( catalog )
1150
1166
configurer . run
@@ -1173,14 +1189,14 @@ def expects_neither_new_or_cached_catalog
1173
1189
configurer . run
1174
1190
end
1175
1191
1176
- it "uses environment from Puppet[:environment] if strict_environment_mode is set" do
1192
+ it "uses the environment from Puppet[:environment] if strict_environment_mode is set" do
1177
1193
Puppet [ :strict_environment_mode ] = true
1178
1194
configurer . run
1179
1195
1180
1196
expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1181
1197
end
1182
1198
1183
- it "uses environment from Puppet[:environment] if initial_environment is the same as converged_environment" do
1199
+ it "uses the environment from Puppet[:environment] if initial_environment is the same as converged_environment" do
1184
1200
Puppet [ :lastrunfile ] = file_containing ( 'last_run_summary.yaml' , <<~SUMMARY )
1185
1201
---
1186
1202
version:
@@ -1195,41 +1211,86 @@ def expects_neither_new_or_cached_catalog
1195
1211
1196
1212
expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1197
1213
end
1214
+ end
1215
+ end
1216
+
1217
+ describe "when the last used environment is not available" do
1218
+ describe "when the node request succeeds" do
1219
+ let ( :node_environment ) { Puppet ::Node ::Environment . remote ( :salam ) }
1220
+ let ( :node ) { Puppet ::Node . new ( Puppet [ :node_name_value ] ) }
1221
+ let ( :last_server_specified_environment ) { 'development' }
1222
+
1223
+ before do
1224
+ node . environment = node_environment
1225
+
1226
+ allow ( Puppet ::Node . indirection ) . to receive ( :find )
1227
+ allow ( Puppet ::Node . indirection ) . to receive ( :find )
1228
+ . with ( anything , hash_including ( :ignore_cache => true , :fail_on_404 => true ) )
1229
+ . and_return ( node )
1230
+ end
1198
1231
1199
- it "uses environment from Puppet[:environment] if the run mode doesn't match" do
1232
+ it "uses the environment from the node request if the run mode doesn't match" do
1200
1233
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
1234
+ ---
1235
+ version:
1236
+ config: 1624882680
1237
+ puppet: 6.24.0
1238
+ application:
1239
+ initial_environment: #{ Puppet [ :environment ] }
1240
+ converged_environment: #{ last_server_specified_environment }
1241
+ run_mode: user
1209
1242
SUMMARY
1210
1243
configurer . run
1211
1244
1212
- expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1245
+ expect ( configurer . environment ) . to eq ( node_environment . name . to_s )
1213
1246
end
1214
1247
1215
- it "uses environment from Puppet[:environment] if lastrunfile is invalid YAML " do
1248
+ it "uses the environment from the node request if lastrunfile does not contain the expected keys " do
1216
1249
Puppet [ :lastrunfile ] = file_containing ( 'last_run_summary.yaml' , <<~SUMMARY )
1217
- Key: 'this is my very very very ' +
1218
- 'long string'
1250
+ ---
1251
+ version:
1252
+ config: 1624882680
1253
+ puppet: 6.24.0
1219
1254
SUMMARY
1220
1255
configurer . run
1221
1256
1222
- expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1257
+ expect ( configurer . environment ) . to eq ( node_environment . name . to_s )
1223
1258
end
1224
1259
1225
- it "uses environment from Puppet[:environment] if lastrunfile exists but is empty" do
1260
+ it "uses the environment from the node request if lastrunfile is invalid YAML" do
1261
+ Puppet [ :lastrunfile ] = file_containing ( 'last_run_summary.yaml' , <<~SUMMARY )
1262
+ Key: 'this is my very very very ' +
1263
+ 'long string'
1264
+ SUMMARY
1265
+ configurer . run
1266
+
1267
+ expect ( configurer . environment ) . to eq ( node_environment . name . to_s )
1268
+ end
1269
+
1270
+ it "uses the environment from the node request if lastrunfile exists but is empty" do
1226
1271
Puppet [ :lastrunfile ] = file_containing ( 'last_run_summary.yaml' , '' )
1227
1272
configurer . run
1228
1273
1229
- expect ( configurer . environment ) . to eq ( Puppet [ :environment ] )
1274
+ expect ( configurer . environment ) . to eq ( node_environment . name . to_s )
1275
+ end
1276
+
1277
+ it "uses the environment from the node request if the last used one cannot be found" do
1278
+ Puppet [ :lastrunfile ] = tmpfile ( 'last_run_summary.yaml' )
1279
+ configurer . run
1280
+
1281
+ expect ( configurer . environment ) . to eq ( node_environment . name . to_s )
1282
+ end
1283
+ end
1284
+
1285
+ describe "when the node request fails" do
1286
+ before do
1287
+ allow ( Puppet ::Node . indirection ) . to receive ( :find ) . and_call_original
1288
+ allow ( Puppet ::Node . indirection ) . to receive ( :find )
1289
+ . with ( anything , hash_including ( :ignore_cache => true , :fail_on_404 => true ) )
1290
+ . and_raise ( Puppet ::Error )
1230
1291
end
1231
1292
1232
- it "uses environment from Puppet[:environment] if the last used one cannot be found" do
1293
+ it "uses the environment from Puppet[:environment] if the last used one cannot be found" do
1233
1294
Puppet [ :lastrunfile ] = tmpfile ( 'last_run_summary.yaml' )
1234
1295
configurer . run
1235
1296
0 commit comments