summaryrefslogtreecommitdiff
path: root/chef-config/spec/unit/config_spec.rb
blob: b7f070c0fa02d477c367f585a177d1581ac36c0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
#
# Author:: Adam Jacob (<adam@chef.io>)
# Author:: Kyle Goodwin (<kgoodwin@primerevenue.com>)
# Copyright:: Copyright 2008-2019, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"
require "chef-config/config"

RSpec.describe ChefConfig::Config do
  before(:each) do
    ChefConfig::Config.reset

    # By default, treat deprecation warnings as errors in tests.
    ChefConfig::Config.treat_deprecation_warnings_as_errors(true)

    # Set environment variable so the setting persists in child processes
    ENV["CHEF_TREAT_DEPRECATION_WARNINGS_AS_ERRORS"] = "1"
  end

  describe "config attribute writer: chef_server_url" do
    before do
      ChefConfig::Config.chef_server_url = "https://junglist.gen.nz"
    end

    it "sets the server url" do
      expect(ChefConfig::Config.chef_server_url).to eq("https://junglist.gen.nz")
    end

    context "when the url has a leading space" do
      before do
        ChefConfig::Config.chef_server_url = " https://junglist.gen.nz"
      end

      it "strips the space from the url when setting" do
        expect(ChefConfig::Config.chef_server_url).to eq("https://junglist.gen.nz")
      end

    end

    context "when the url is a frozen string" do
      before do
        ChefConfig::Config.chef_server_url = " https://junglist.gen.nz".freeze
      end

      it "strips the space from the url when setting without raising an error" do
        expect(ChefConfig::Config.chef_server_url).to eq("https://junglist.gen.nz")
      end
    end

    context "when the url is invalid" do
      it "raises an exception" do
        expect { ChefConfig::Config.chef_server_url = "127.0.0.1" }.to raise_error(ChefConfig::ConfigurationError)
      end
    end
  end

  describe "parsing arbitrary config from the CLI" do

    def apply_config
      described_class.apply_extra_config_options(extra_config_options)
    end

    context "when no arbitrary config is given" do

      let(:extra_config_options) { nil }

      it "succeeds" do
        expect { apply_config }.to_not raise_error
      end

    end

    context "when given a simple string option" do

      let(:extra_config_options) { [ "node_name=bobotclown" ] }

      it "applies the string option" do
        apply_config
        expect(described_class[:node_name]).to eq("bobotclown")
      end

    end

    context "when given a blank value" do

      let(:extra_config_options) { [ "http_retries=" ] }

      it "sets the value to nil" do
        # ensure the value is actually changed in the test
        described_class[:http_retries] = 55
        apply_config
        expect(described_class[:http_retries]).to eq(nil)
      end
    end

    context "when given spaces between `key = value`" do

      let(:extra_config_options) { [ "node_name = bobo" ] }

      it "handles the extra spaces and applies the config option" do
        apply_config
        expect(described_class[:node_name]).to eq("bobo")
      end

    end

    context "when given an integer value" do

      let(:extra_config_options) { [ "http_retries=9000" ] }

      it "converts to a numeric type and applies the config option" do
        apply_config
        expect(described_class[:http_retries]).to eq(9000)
      end

    end

    context "when given a boolean" do

      let(:extra_config_options) { [ "boolean_thing=true" ] }

      it "converts to a boolean type and applies the config option" do
        apply_config
        expect(described_class[:boolean_thing]).to eq(true)
      end

    end

    context "when given input that is not in key=value form" do

      let(:extra_config_options) { [ "http_retries:9000" ] }

      it "raises UnparsableConfigOption" do
        message = 'Unparsable config option "http_retries:9000"'
        expect { apply_config }.to raise_error(ChefConfig::UnparsableConfigOption, message)
      end

    end

    describe "expand relative paths" do
      let(:current_directory) { Dir.pwd }

      context "when given cookbook_path" do
        let(:extra_config_options) { [ "cookbook_path=cookbooks/" ] }

        it "expanded cookbook_path" do
          apply_config
          expect(described_class[:cookbook_path]).to eq("#{current_directory}/cookbooks")
        end
      end

      context "when passes multiple config options" do
        let(:extra_config_options) { ["data_bag_path=data_bags/", "cookbook_path=cookbooks", "chef_repo_path=."] }

        it "expanded paths" do
          apply_config
          expect(described_class[:data_bag_path]).to eq("#{current_directory}/data_bags")
          expect(described_class[:cookbook_path]).to eq("#{current_directory}/cookbooks")
          expect(described_class[:chef_repo_path]).to eq("#{current_directory}")
        end
      end

      context "when passes multiple cookbook_paths in config options" do
        let(:extra_config_options) { ["cookbook_path=[first_cookbook, secound_cookbooks]"] }

        it "expanded paths" do
          apply_config
          expect(described_class[:cookbook_path]).to eq(["#{current_directory}/first_cookbook", "#{current_directory}/secound_cookbooks"])
        end
      end
    end
  end

  describe "when configuring formatters" do
      # if TTY and not(force-logger)
      #   formatter = configured formatter or default formatter
      #   formatter goes to STDOUT/ERR
      #   if log file is writeable
      #     log level is configured level or info
      #     log location is file
      #   else
      #     log level is warn
      #     log location is STDERR
      #    end
      # elsif not(TTY) and force formatter
      #   formatter = configured formatter or default formatter
      #   if log_location specified
      #     formatter goes to log_location
      #   else
      #     formatter goes to STDOUT/ERR
      #   end
      # else
      #   formatter = "null"
      #   log_location = configured-value or defualt
      #   log_level = info or defualt
      # end
      #
    it "has an empty list of formatters by default" do
      expect(ChefConfig::Config.formatters).to eq([])
    end

    it "configures a formatter with a short name" do
      ChefConfig::Config.add_formatter(:doc)
      expect(ChefConfig::Config.formatters).to eq([[:doc, nil]])
    end

    it "configures a formatter with a file output" do
      ChefConfig::Config.add_formatter(:doc, "/var/log/formatter.log")
      expect(ChefConfig::Config.formatters).to eq([[:doc, "/var/log/formatter.log"]])
    end

  end

  [ false, true ].each do |is_windows|

    context "On #{is_windows ? "Windows" : "Unix"}" do

      before :each do
        allow(ChefUtils).to receive(:windows?).and_return(is_windows)
      end
      describe "class method: windows_installation_drive" do
        before do
          allow(File).to receive(:expand_path).and_return("D:/Path/To/Executable")
        end
        if is_windows
          it "should return D: on a windows system" do
            expect(ChefConfig::Config.windows_installation_drive).to eq("D:")
          end
        else
          it "should return nil on a non-windows system" do
            expect(ChefConfig::Config.windows_installation_drive).to eq(nil)
          end
        end
      end
      describe "class method: platform_specific_path" do
        before do
          allow(ChefConfig::Config).to receive(:env).and_return({ "SYSTEMDRIVE" => "C:" })
        end
        if is_windows
          path = "/etc/chef/cookbooks"
          context "a windows system with chef installed on C: drive" do
            before do
              allow(ChefConfig::Config).to receive(:windows_installation_drive).and_return("C:")
            end
            it "should return a windows path rooted in C:" do
              expect(ChefConfig::Config.platform_specific_path(path)).to eq("C:\\chef\\cookbooks")
            end
          end
          context "a windows system with chef installed on D: drive" do
            before do
              allow(ChefConfig::Config).to receive(:windows_installation_drive).and_return("D:")
            end
            it "should return a windows path rooted in D:" do
              expect(ChefConfig::Config.platform_specific_path(path)).to eq("D:\\chef\\cookbooks")
            end
          end
        else
          it "should return given path on non-windows systems" do
            path = "/etc/chef/cookbooks"
            expect(ChefConfig::Config.platform_specific_path(path)).to eq("/etc/chef/cookbooks")
          end
        end
      end

      describe "default values" do
        let(:system_drive) { ChefConfig::Config.env["SYSTEMDRIVE"] } if is_windows
        let :primary_cache_path do
          if is_windows
            "#{system_drive}\\chef"
          else
            "/var/chef"
          end
        end

        let :secondary_cache_path do
          if is_windows
            "#{ChefConfig::Config[:user_home]}\\.chef"
          else
            "#{ChefConfig::Config[:user_home]}/.chef"
          end
        end

        before do
          if is_windows
            allow(ChefConfig::Config).to receive(:env).and_return({ "SYSTEMDRIVE" => "C:" })
            ChefConfig::Config[:user_home] = 'C:\Users\charlie'
          else
            ChefConfig::Config[:user_home] = "/Users/charlie"
          end

          allow(ChefConfig::Config).to receive(:path_accessible?).and_return(false)
        end

        describe "ChefConfig::Config[:client_key]" do
          let(:path_to_client_key) { ChefConfig::Config.etc_chef_dir + ChefConfig::PathHelper.path_separator }

          it "sets the default path to the client key" do
            expect(ChefConfig::Config.client_key).to eq(path_to_client_key + "client.pem")
          end

          context "when target mode is enabled" do
            let(:target_mode_host) { "fluffy.kittens.org" }

            before do
              ChefConfig::Config.target_mode.enabled = true
              ChefConfig::Config.target_mode.host = target_mode_host
            end

            it "sets the default path to the client key with the target host name" do
              expect(ChefConfig::Config.client_key).to eq(path_to_client_key + target_mode_host + ChefConfig::PathHelper.path_separator + "client.pem")
            end
          end

          context "when local mode is enabled" do
            before { ChefConfig::Config[:local_mode] = true }

            it "returns nil" do
              expect(ChefConfig::Config.client_key).to be_nil
            end
          end
        end

        describe "ChefConfig::Config[:fips]" do
          let(:fips_enabled) { false }

          before(:all) do
            @original_env = ENV.to_hash
          end

          after(:all) do
            ENV.clear
            ENV.update(@original_env)
          end

          before(:each) do
            ENV["CHEF_FIPS"] = nil
            allow(ChefConfig).to receive(:fips?).and_return(fips_enabled)
          end

          it "returns false when no environment is set and not enabled on system" do
            expect(ChefConfig::Config[:fips]).to eq(false)
          end

          context "when ENV['CHEF_FIPS'] is empty" do
            before do
              ENV["CHEF_FIPS"] = ""
            end

            it "returns false" do
              expect(ChefConfig::Config[:fips]).to eq(false)
            end
          end

          context "when ENV['CHEF_FIPS'] is set" do
            before do
              ENV["CHEF_FIPS"] = "1"
            end

            it "returns true" do
              expect(ChefConfig::Config[:fips]).to eq(true)
            end
          end

          context "when fips is enabled on system" do
            let(:fips_enabled) { true }

            it "returns true" do
              expect(ChefConfig::Config[:fips]).to eq(true)
            end
          end
        end

        describe "ChefConfig::Config[:chef_server_root]" do
          context "when chef_server_url isn't set manually" do
            it "returns the default of 'https://localhost:443'" do
              expect(ChefConfig::Config[:chef_server_root]).to eq("https://localhost:443")
            end
          end

          context "when chef_server_url matches '../organizations/*' without a trailing slash" do
            before do
              ChefConfig::Config[:chef_server_url] = "https://example.com/organizations/myorg"
            end
            it "returns the full URL without /organizations/*" do
              expect(ChefConfig::Config[:chef_server_root]).to eq("https://example.com")
            end
          end

          context "when chef_server_url matches '../organizations/*' with a trailing slash" do
            before do
              ChefConfig::Config[:chef_server_url] = "https://example.com/organizations/myorg/"
            end
            it "returns the full URL without /organizations/*" do
              expect(ChefConfig::Config[:chef_server_root]).to eq("https://example.com")
            end
          end

          context "when chef_server_url matches '..organizations..' but not '../organizations/*'" do
            before do
              ChefConfig::Config[:chef_server_url] = "https://organizations.com/organizations"
            end
            it "returns the full URL without any modifications" do
              expect(ChefConfig::Config[:chef_server_root]).to eq(ChefConfig::Config[:chef_server_url])
            end
          end

          context "when chef_server_url is a standard URL without the string organization(s)" do
            before do
              ChefConfig::Config[:chef_server_url] = "https://example.com/some_other_string"
            end
            it "returns the full URL without any modifications" do
              expect(ChefConfig::Config[:chef_server_root]).to eq(ChefConfig::Config[:chef_server_url])
            end
          end
        end

        describe "ChefConfig::Config[:cache_path]" do
          let(:target_mode_host) { "fluffy.kittens.org" }
          let(:target_mode_primary_cache_path) { "#{primary_cache_path}/#{target_mode_host}" }
          let(:target_mode_secondary_cache_path) { "#{secondary_cache_path}/#{target_mode_host}" }

          before do
            if is_windows
              allow(File).to receive(:expand_path).and_return("#{system_drive}/Path/To/Executable")
            end
          end

          context "when /var/chef exists and is accessible" do
            before do
              allow(ChefConfig::Config).to receive(:path_accessible?).with(ChefConfig::Config.var_chef_dir).and_return(true)
            end

            it "defaults to /var/chef" do
              expect(ChefConfig::Config[:cache_path]).to eq(primary_cache_path)
            end

            context "and target mode is enabled" do
              it "cache path includes the target host name" do
                ChefConfig::Config.target_mode.enabled = true
                ChefConfig::Config.target_mode.host = target_mode_host
                expect(ChefConfig::Config[:cache_path]).to eq(target_mode_primary_cache_path)
              end
            end
          end

          context "when /var/chef does not exist and /var is accessible" do
            it "defaults to /var/chef" do
              allow(File).to receive(:exists?).with(ChefConfig::Config.var_chef_dir).and_return(false)
              allow(ChefConfig::Config).to receive(:path_accessible?).with(ChefConfig::Config.var_root_dir).and_return(true)
              expect(ChefConfig::Config[:cache_path]).to eq(primary_cache_path)
            end
          end

          context "when /var/chef does not exist and /var is not accessible" do
            it "defaults to $HOME/.chef" do
              allow(File).to receive(:exists?).with(ChefConfig::Config.var_chef_dir).and_return(false)
              allow(ChefConfig::Config).to receive(:path_accessible?).with(ChefConfig::Config.var_root_dir).and_return(false)
              expect(ChefConfig::Config[:cache_path]).to eq(secondary_cache_path)
            end
          end

          context "when /var/chef exists and is not accessible" do
            before do
              allow(File).to receive(:exists?).with(ChefConfig::Config.var_chef_dir).and_return(true)
              allow(File).to receive(:readable?).with(ChefConfig::Config.var_chef_dir).and_return(true)
              allow(File).to receive(:writable?).with(ChefConfig::Config.var_chef_dir).and_return(false)
            end

            it "defaults to $HOME/.chef" do
              expect(ChefConfig::Config[:cache_path]).to eq(secondary_cache_path)
            end

            context "and target mode is enabled" do
              it "cache path defaults to $HOME/.chef with the target host name" do
                ChefConfig::Config.target_mode.enabled = true
                ChefConfig::Config.target_mode.host = target_mode_host
                expect(ChefConfig::Config[:cache_path]).to eq(target_mode_secondary_cache_path)
              end
            end
          end

          context "when chef is running in local mode" do
            before do
              ChefConfig::Config.local_mode = true
            end

            context "and config_dir is /a/b/c" do
              before do
                ChefConfig::Config.config_dir ChefConfig::PathHelper.cleanpath("/a/b/c")
              end

              it "cache_path is /a/b/c/local-mode-cache" do
                expect(ChefConfig::Config.cache_path).to eq(ChefConfig::PathHelper.cleanpath("/a/b/c/local-mode-cache"))
              end
            end

            context "and config_dir is /a/b/c/" do
              before do
                ChefConfig::Config.config_dir ChefConfig::PathHelper.cleanpath("/a/b/c/")
              end

              it "cache_path is /a/b/c/local-mode-cache" do
                expect(ChefConfig::Config.cache_path).to eq(ChefConfig::PathHelper.cleanpath("/a/b/c/local-mode-cache"))
              end
            end
          end
        end

        it "ChefConfig::Config[:stream_execute_output] defaults to false" do
          expect(ChefConfig::Config[:stream_execute_output]).to eq(false)
        end

        it "ChefConfig::Config[:show_download_progress] defaults to false" do
          expect(ChefConfig::Config[:show_download_progress]).to eq(false)
        end

        it "ChefConfig::Config[:download_progress_interval] defaults to every 10%" do
          expect(ChefConfig::Config[:download_progress_interval]).to eq(10)
        end

        it "ChefConfig::Config[:file_backup_path] defaults to /var/chef/backup" do
          allow(ChefConfig::Config).to receive(:cache_path).and_return(primary_cache_path)
          backup_path = is_windows ? "#{primary_cache_path}\\backup" : "#{primary_cache_path}/backup"
          expect(ChefConfig::Config[:file_backup_path]).to eq(backup_path)
        end

        it "ChefConfig::Config[:ssl_verify_mode] defaults to :verify_peer" do
          expect(ChefConfig::Config[:ssl_verify_mode]).to eq(:verify_peer)
        end

        it "ChefConfig::Config[:ssl_ca_path] defaults to nil" do
          expect(ChefConfig::Config[:ssl_ca_path]).to be_nil
        end

        describe "ChefConfig::Config[:repo_mode]" do

          context "when local mode is enabled" do

            before { ChefConfig::Config[:local_mode] = true }

            it "defaults to 'hosted_everything'" do
              expect(ChefConfig::Config[:repo_mode]).to eq("hosted_everything")
            end

            context "and osc_compat is enabled" do

              before { ChefConfig::Config.chef_zero.osc_compat = true }

              it "defaults to 'everything'" do
                expect(ChefConfig::Config[:repo_mode]).to eq("everything")
              end
            end
          end

          context "when local mode is not enabled" do

            context "and the chef_server_url is multi-tenant" do

              before { ChefConfig::Config[:chef_server_url] = "https://chef.example/organizations/example" }

              it "defaults to 'hosted_everything'" do
                expect(ChefConfig::Config[:repo_mode]).to eq("hosted_everything")
              end

            end

            context "and the chef_server_url is not multi-tenant" do

              before { ChefConfig::Config[:chef_server_url] = "https://chef.example/" }

              it "defaults to 'everything'" do
                expect(ChefConfig::Config[:repo_mode]).to eq("everything")
              end
            end
          end
        end

        describe "ChefConfig::Config[:chef_repo_path]" do

          context "when cookbook_path is set to a single path" do

            before { ChefConfig::Config[:cookbook_path] = "/home/anne/repo/cookbooks" }

            it "is set to a path one directory up from the cookbook_path" do
              expected = File.expand_path("/home/anne/repo")
              expect(ChefConfig::Config[:chef_repo_path]).to eq(expected)
            end

          end

          context "when cookbook_path is set to multiple paths" do

            before do
              ChefConfig::Config[:cookbook_path] = [
                "/home/anne/repo/cookbooks",
                "/home/anne/other_repo/cookbooks",
              ]
            end

            it "is set to an Array of paths one directory up from the cookbook_paths" do
              expected = [ "/home/anne/repo", "/home/anne/other_repo"].map { |p| File.expand_path(p) }
              expect(ChefConfig::Config[:chef_repo_path]).to eq(expected)
            end

          end

          context "when cookbook_path is not set but cookbook_artifact_path is set" do

            before do
              ChefConfig::Config[:cookbook_path] = nil
              ChefConfig::Config[:cookbook_artifact_path] = "/home/roxie/repo/cookbook_artifacts"
            end

            it "is set to a path one directory up from the cookbook_artifact_path" do
              expected = File.expand_path("/home/roxie/repo")
              expect(ChefConfig::Config[:chef_repo_path]).to eq(expected)
            end

          end

          context "when cookbook_path is not set" do

            before { ChefConfig::Config[:cookbook_path] = nil }

            it "is set to the cache_path" do
              expect(ChefConfig::Config[:chef_repo_path]).to eq(ChefConfig::Config[:cache_path])
            end

          end

        end

        # On Windows, we'll detect an omnibus build and set this to the
        # cacert.pem included in the package, but it's nil if you're on Windows
        # w/o omnibus (e.g., doing development on Windows, custom build, etc.)
        unless is_windows
          it "ChefConfig::Config[:ssl_ca_file] defaults to nil" do
            expect(ChefConfig::Config[:ssl_ca_file]).to be_nil
          end
        end

        it "ChefConfig::Config[:data_bag_path] defaults to /var/chef/data_bags" do
          allow(ChefConfig::Config).to receive(:cache_path).and_return(primary_cache_path)
          data_bag_path = is_windows ? "#{primary_cache_path}\\data_bags" : "#{primary_cache_path}/data_bags"
          expect(ChefConfig::Config[:data_bag_path]).to eq(data_bag_path)
        end

        it "ChefConfig::Config[:environment_path] defaults to /var/chef/environments" do
          allow(ChefConfig::Config).to receive(:cache_path).and_return(primary_cache_path)
          environment_path = is_windows ? "#{primary_cache_path}\\environments" : "#{primary_cache_path}/environments"
          expect(ChefConfig::Config[:environment_path]).to eq(environment_path)
        end

        it "ChefConfig::Config[:cookbook_artifact_path] defaults to /var/chef/cookbook_artifacts" do
          allow(ChefConfig::Config).to receive(:cache_path).and_return(primary_cache_path)
          environment_path = is_windows ? "#{primary_cache_path}\\cookbook_artifacts" : "#{primary_cache_path}/cookbook_artifacts"
          expect(ChefConfig::Config[:cookbook_artifact_path]).to eq(environment_path)
        end

        describe "setting the config dir" do

          context "when the config file is given with a relative path" do

            before do
              ChefConfig::Config.config_file = "client.rb"
            end

            it "expands the path when determining config_dir" do
              # config_dir goes through ChefConfig::PathHelper.canonical_path, which
              # downcases on windows because the FS is case insensitive, so we
              # have to downcase expected and actual to make the tests work.
              expect(ChefConfig::Config.config_dir.downcase).to eq(ChefConfig::PathHelper.cleanpath(Dir.pwd).downcase)
            end

            it "does not set derived paths at FS root" do
              ChefConfig::Config.local_mode = true
              expect(ChefConfig::Config.cache_path.downcase).to eq(ChefConfig::PathHelper.cleanpath(File.join(Dir.pwd, "local-mode-cache")).downcase)
            end

          end

          context "when the config file is /etc/chef/client.rb" do

            before do
              config_location = ChefConfig::PathHelper.cleanpath(ChefConfig::PathHelper.join(ChefConfig::Config.etc_chef_dir, "client.rb")).downcase
              allow(File).to receive(:absolute_path).with(config_location).and_return(config_location)
              ChefConfig::Config.config_file = config_location
            end

            it "config_dir is /etc/chef" do
              expect(ChefConfig::Config.config_dir).to eq(ChefConfig::Config.etc_chef_dir.downcase)
            end

            context "and chef is running in local mode" do
              before do
                ChefConfig::Config.local_mode = true
              end

              it "config_dir is /etc/chef" do
                expect(ChefConfig::Config.config_dir).to eq(ChefConfig::Config.etc_chef_dir.downcase)
              end
            end

            context "when config_dir is set to /other/config/dir/" do
              before do
                ChefConfig::Config.config_dir = ChefConfig::PathHelper.cleanpath("/other/config/dir/")
              end

              it "yields the explicit value" do
                expect(ChefConfig::Config.config_dir).to eq(ChefConfig::PathHelper.cleanpath("/other/config/dir/"))
              end
            end

          end

          context "when the user's home dir is /home/charlie/" do
            before do
              ChefConfig::Config.user_home = "/home/charlie/"
            end

            it "config_dir is /home/charlie/.chef/" do
              expect(ChefConfig::Config.config_dir).to eq(ChefConfig::PathHelper.join(ChefConfig::PathHelper.cleanpath("/home/charlie/"), ".chef", ""))
            end

            context "and chef is running in local mode" do
              before do
                ChefConfig::Config.local_mode = true
              end

              it "config_dir is /home/charlie/.chef/" do
                expect(ChefConfig::Config.config_dir).to eq(ChefConfig::PathHelper.join(ChefConfig::PathHelper.cleanpath("/home/charlie/"), ".chef", ""))
              end
            end
          end

          if is_windows
            context "when the user's home dir is windows specific" do
              before do
                ChefConfig::Config.user_home = ChefConfig::PathHelper.cleanpath("/home/charlie/")
              end

              it "config_dir is with backslashes" do
                expect(ChefConfig::Config.config_dir).to eq(ChefConfig::PathHelper.join(ChefConfig::PathHelper.cleanpath("/home/charlie/"), ".chef", ""))
              end

              context "and chef is running in local mode" do
                before do
                  ChefConfig::Config.local_mode = true
                end

                it "config_dir is with backslashes" do
                  expect(ChefConfig::Config.config_dir).to eq(ChefConfig::PathHelper.join(ChefConfig::PathHelper.cleanpath("/home/charlie/"), ".chef", ""))
                end
              end
            end

          end

        end

        if is_windows
          describe "finding the windows embedded dir" do
            let(:default_config_location) { "c:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.6.0/lib/chef/config.rb" }
            let(:alternate_install_location) { "c:/my/alternate/install/place/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.6.0/lib/chef/config.rb" }
            let(:non_omnibus_location) { "c:/my/dev/stuff/lib/ruby/gems/1.9.1/gems/chef-11.6.0/lib/chef/config.rb" }

            let(:default_ca_file) { "c:/opscode/chef/embedded/ssl/certs/cacert.pem" }

            it "finds the embedded dir in the default location" do
              allow(ChefConfig::Config).to receive(:_this_file).and_return(default_config_location)
              expect(ChefConfig::Config.embedded_dir).to eq("c:/opscode/chef/embedded")
            end

            it "finds the embedded dir in a custom install location" do
              allow(ChefConfig::Config).to receive(:_this_file).and_return(alternate_install_location)
              expect(ChefConfig::Config.embedded_dir).to eq("c:/my/alternate/install/place/chef/embedded")
            end

            it "doesn't error when not in an omnibus install" do
              allow(ChefConfig::Config).to receive(:_this_file).and_return(non_omnibus_location)
              expect(ChefConfig::Config.embedded_dir).to be_nil
            end

            it "sets the ssl_ca_cert path if the cert file is available" do
              allow(ChefConfig::Config).to receive(:_this_file).and_return(default_config_location)
              allow(File).to receive(:exist?).with(default_ca_file).and_return(true)
              expect(ChefConfig::Config.ssl_ca_file).to eq(default_ca_file)
            end
          end
        end
      end

      describe "ChefConfig::Config[:user_home]" do
        it "should set when HOME is provided" do
          expected = ChefConfig::PathHelper.cleanpath("/home/kitten")
          allow(ChefConfig::PathHelper).to receive(:home).and_return(expected)
          expect(ChefConfig::Config[:user_home]).to eq(expected)
        end

        it "falls back to the current working directory when HOME and USERPROFILE is not set" do
          allow(ChefConfig::PathHelper).to receive(:home).and_return(nil)
          expect(ChefConfig::Config[:user_home]).to eq(Dir.pwd)
        end
      end

      describe "ChefConfig::Config[:encrypted_data_bag_secret]" do
        let(:db_secret_default_path) { ChefConfig::PathHelper.cleanpath("#{ChefConfig::Config.etc_chef_dir}/encrypted_data_bag_secret") }

        before do
          allow(File).to receive(:exist?).with(db_secret_default_path).and_return(secret_exists)
        end

        context "/etc/chef/encrypted_data_bag_secret exists" do
          let(:secret_exists) { true }
          it "sets the value to /etc/chef/encrypted_data_bag_secret" do
            expect(ChefConfig::Config[:encrypted_data_bag_secret]).to eq db_secret_default_path
          end
        end

        context "/etc/chef/encrypted_data_bag_secret does not exist" do
          let(:secret_exists) { false }
          it "sets the value to nil" do
            expect(ChefConfig::Config[:encrypted_data_bag_secret]).to be_nil
          end
        end
      end

      describe "ChefConfig::Config[:event_handlers]" do
        it "sets a event_handlers to an empty array by default" do
          expect(ChefConfig::Config[:event_handlers]).to eq([])
        end
        it "should be able to add custom handlers" do
          o = Object.new
          ChefConfig::Config[:event_handlers] << o
          expect(ChefConfig::Config[:event_handlers]).to be_include(o)
        end
      end

      describe "ChefConfig::Config[:user_valid_regex]" do
        context "on a platform that is not Windows" do
          it "allows one letter usernames" do
            any_match = ChefConfig::Config[:user_valid_regex].any? { |regex| regex.match("a") }
            expect(any_match).to be_truthy
          end
        end
      end

      describe "ChefConfig::Config[:internal_locale]" do
        let(:shell_out) do
          cmd = instance_double("Mixlib::ShellOut", exitstatus: 0, stdout: locales, error!: nil)
          allow(cmd).to receive(:run_command).and_return(cmd)
          cmd
        end

        let(:locales) { locale_array.join("\n") }

        before do
          allow(Mixlib::ShellOut).to receive(:new).with("locale -a").and_return(shell_out)
        end

        shared_examples_for "a suitable locale" do
          it "returns an English UTF-8 locale" do
            expect(ChefConfig.logger).to_not receive(:warn).with(/Please install an English UTF-8 locale for Chef to use/)
            expect(ChefConfig.logger).to_not receive(:trace).with(/Defaulting to locale en_US.UTF-8 on Windows/)
            expect(ChefConfig.logger).to_not receive(:trace).with(/No usable locale -a command found/)
            expect(ChefConfig::Config.guess_internal_locale).to eq expected_locale
          end
        end

        context "when the result includes 'C.UTF-8'" do
          include_examples "a suitable locale" do
            let(:locale_array) { [expected_locale, "en_US.UTF-8"] }
            let(:expected_locale) { "C.UTF-8" }
          end
        end

        context "when the result includes 'en_US.UTF-8'" do
          include_examples "a suitable locale" do
            let(:locale_array) { ["en_CA.UTF-8", expected_locale, "en_NZ.UTF-8"] }
            let(:expected_locale) { "en_US.UTF-8" }
          end
        end

        context "when the result includes 'en_US.utf8'" do
          include_examples "a suitable locale" do
            let(:locale_array) { ["en_CA.utf8", "en_US.utf8", "en_NZ.utf8"] }
            let(:expected_locale) { "en_US.UTF-8" }
          end
        end

        context "when the result includes 'en.UTF-8'" do
          include_examples "a suitable locale" do
            let(:locale_array) { ["en.ISO8859-1", expected_locale] }
            let(:expected_locale) { "en.UTF-8" }
          end
        end

        context "when the result includes 'en_*.UTF-8'" do
          include_examples "a suitable locale" do
            let(:locale_array) { [expected_locale, "en_CA.UTF-8", "en_GB.UTF-8"] }
            let(:expected_locale) { "en_AU.UTF-8" }
          end
        end

        context "when the result includes 'en_*.utf8'" do
          include_examples "a suitable locale" do
            let(:locale_array) { ["en_AU.utf8", "en_CA.utf8", "en_GB.utf8"] }
            let(:expected_locale) { "en_AU.UTF-8" }
          end
        end

        context "when the result does not include 'en_*.UTF-8'" do
          let(:locale_array) { ["af_ZA", "af_ZA.ISO8859-1", "af_ZA.ISO8859-15", "af_ZA.UTF-8"] }

          it "should fall back to C locale" do
            expect(ChefConfig.logger).to receive(:warn).with("Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support.")
            expect(ChefConfig::Config.guess_internal_locale).to eq "C"
          end
        end

        context "on error" do
          let(:locale_array) { [] }

          let(:shell_out_cmd) { instance_double("Mixlib::ShellOut") }

          before do
            allow(Mixlib::ShellOut).to receive(:new).and_return(shell_out_cmd)
            allow(shell_out_cmd).to receive(:run_command)
            allow(shell_out_cmd).to receive(:error!).and_raise(Mixlib::ShellOut::ShellCommandFailed, "this is an error")
          end

          it "should default to 'en_US.UTF-8'" do
            if is_windows
              expect(ChefConfig.logger).to receive(:trace).with("Defaulting to locale en_US.UTF-8 on Windows, until it matters that we do something else.")
            else
              expect(ChefConfig.logger).to receive(:trace).with("No usable locale -a command found, assuming you have en_US.UTF-8 installed.")
            end
            expect(ChefConfig::Config.guess_internal_locale).to eq "en_US.UTF-8"
          end
        end
      end
    end
  end

  describe "export_proxies" do
    before(:all) do
      @original_env = ENV.to_hash
      ENV["http_proxy"] = nil
      ENV["HTTP_PROXY"] = nil
      ENV["https_proxy"] = nil
      ENV["HTTPS_PROXY"] = nil
      ENV["ftp_proxy"] = nil
      ENV["FTP_PROXY"] = nil
      ENV["no_proxy"] = nil
      ENV["NO_PROXY"] = nil
    end

    after(:all) do
      ENV.clear
      ENV.update(@original_env)
    end

    let(:http_proxy) { "http://localhost:7979" }
    let(:https_proxy) { "https://localhost:7979" }
    let(:ftp_proxy) { "ftp://localhost:7979" }
    let(:proxy_user) { "http_user" }
    let(:proxy_pass) { "http_pass" }

    context "when http_proxy, proxy_pass and proxy_user are set" do
      before do
        ChefConfig::Config.http_proxy = http_proxy
        ChefConfig::Config.http_proxy_user = proxy_user
        ChefConfig::Config.http_proxy_pass = proxy_pass
      end
      it "exports ENV['http_proxy']" do
        expect(ENV).to receive(:[]=).with("http_proxy", "http://http_user:http_pass@localhost:7979")
        expect(ENV).to receive(:[]=).with("HTTP_PROXY", "http://http_user:http_pass@localhost:7979")
        ChefConfig::Config.export_proxies
      end
    end

    context "when https_proxy, proxy_pass and proxy_user are set" do
      before do
        ChefConfig::Config.https_proxy = https_proxy
        ChefConfig::Config.https_proxy_user = proxy_user
        ChefConfig::Config.https_proxy_pass = proxy_pass
      end
      it "exports ENV['https_proxy']" do
        expect(ENV).to receive(:[]=).with("https_proxy", "https://http_user:http_pass@localhost:7979")
        expect(ENV).to receive(:[]=).with("HTTPS_PROXY", "https://http_user:http_pass@localhost:7979")
        ChefConfig::Config.export_proxies
      end
    end

    context "when ftp_proxy, proxy_pass and proxy_user are set" do
      before do
        ChefConfig::Config.ftp_proxy = ftp_proxy
        ChefConfig::Config.ftp_proxy_user = proxy_user
        ChefConfig::Config.ftp_proxy_pass = proxy_pass
      end
      it "exports ENV['ftp_proxy']" do
        expect(ENV).to receive(:[]=).with("ftp_proxy", "ftp://http_user:http_pass@localhost:7979")
        expect(ENV).to receive(:[]=).with("FTP_PROXY", "ftp://http_user:http_pass@localhost:7979")
        ChefConfig::Config.export_proxies
      end
    end

    shared_examples "no user pass" do
      it "does not populate the user or password" do
        expect(ENV).to receive(:[]=).with("http_proxy", "http://localhost:7979")
        expect(ENV).to receive(:[]=).with("HTTP_PROXY", "http://localhost:7979")
        ChefConfig::Config.export_proxies
      end
    end

    context "when proxy_pass and proxy_user are passed as empty strings" do
      before do
        ChefConfig::Config.http_proxy = http_proxy
        ChefConfig::Config.http_proxy_user = ""
        ChefConfig::Config.http_proxy_pass = proxy_pass
      end
      include_examples "no user pass"
    end

    context "when proxy_pass and proxy_user are not provided" do
      before do
        ChefConfig::Config.http_proxy = http_proxy
      end
      include_examples "no user pass"
    end

    context "when the proxy is provided without a scheme" do
      before do
        ChefConfig::Config.http_proxy = "localhost:1111"
      end
      it "automatically adds the scheme to the proxy url" do
        expect(ENV).to receive(:[]=).with("http_proxy", "http://localhost:1111")
        expect(ENV).to receive(:[]=).with("HTTP_PROXY", "http://localhost:1111")
        ChefConfig::Config.export_proxies
      end
    end

    shared_examples "no export" do
      it "does not export any proxy settings" do
        ChefConfig::Config.export_proxies
        expect(ENV["http_proxy"]).to eq(nil)
        expect(ENV["https_proxy"]).to eq(nil)
        expect(ENV["ftp_proxy"]).to eq(nil)
        expect(ENV["no_proxy"]).to eq(nil)
      end
    end

    context "when nothing is set" do
      include_examples "no export"
    end

    context "when all the users and passwords are set but no proxies are set" do
      before do
        ChefConfig::Config.http_proxy_user = proxy_user
        ChefConfig::Config.http_proxy_pass = proxy_pass
        ChefConfig::Config.https_proxy_user = proxy_user
        ChefConfig::Config.https_proxy_pass = proxy_pass
        ChefConfig::Config.ftp_proxy_user = proxy_user
        ChefConfig::Config.ftp_proxy_pass = proxy_pass
      end
      include_examples "no export"
    end

    context "no_proxy is set" do
      before do
        ChefConfig::Config.no_proxy = "localhost"
      end
      it "exports ENV['no_proxy']" do
        expect(ENV).to receive(:[]=).with("no_proxy", "localhost")
        expect(ENV).to receive(:[]=).with("NO_PROXY", "localhost")
        ChefConfig::Config.export_proxies
      end
    end
  end

  describe "proxy_uri" do
    subject(:proxy_uri) { described_class.proxy_uri(scheme, host, port) }
    let(:env) { {} }
    let(:scheme) { "http" }
    let(:host) { "test.example.com" }
    let(:port) { 8080 }
    let(:proxy) { "#{proxy_prefix}#{proxy_host}:#{proxy_port}" }
    let(:proxy_prefix) { "http://" }
    let(:proxy_host) { "proxy.mycorp.com" }
    let(:proxy_port) { 8080 }

    before do
      stub_const("ENV", env)
    end

    shared_examples_for "a proxy uri" do
      it "contains the host" do
        expect(proxy_uri.host).to eq(proxy_host)
      end

      it "contains the port" do
        expect(proxy_uri.port).to eq(proxy_port)
      end
    end

    context "when the config setting is normalized (does not contain the scheme)" do
      include_examples "a proxy uri" do

        let(:proxy_prefix) { "" }

        let(:env) do
          {
            "#{scheme}_proxy" => proxy,
            "no_proxy" => nil,
          }
        end
      end
    end

    context "when the proxy is set by the environment" do
      include_examples "a proxy uri" do
        let(:scheme) { "https" }
        let(:env) do
          {
            "https_proxy" => "https://jane_username:opensesame@proxy.mycorp.com:8080",
          }
        end
      end
    end

    context "when an empty proxy is set by the environment" do
      let(:env) do
        {
          "https_proxy" => "",
        }
      end

      it "does not fail with URI parse exception" do
        expect { proxy_uri }.to_not raise_error
      end
    end

    context "when no_proxy is set" do
      context "when no_proxy is the exact host" do
        let(:env) do
          {
            "http_proxy" => proxy,
            "no_proxy" => host,
          }
        end

        it { is_expected.to eq nil }
      end

      context "when no_proxy includes the same domain with a wildcard" do
        let(:env) do
          {
            "http_proxy" => proxy,
            "no_proxy" => "*.example.com",
          }
        end

        it { is_expected.to eq nil }
      end

      context "when no_proxy is included on a list" do
        let(:env) do
          {
            "http_proxy" => proxy,
            "no_proxy" => "chef.io,getchef.com,opscode.com,test.example.com",
          }
        end

        it { is_expected.to eq nil }
      end

      context "when no_proxy is included on a list with wildcards" do
        let(:env) do
          {
            "http_proxy" => proxy,
            "no_proxy" => "10.*,*.example.com",
          }
        end

        it { is_expected.to eq nil }
      end

      context "when no_proxy is a domain with a dot prefix" do
        let(:env) do
          {
            "http_proxy" => proxy,
            "no_proxy" => ".example.com",
          }
        end

        it { is_expected.to eq nil }
      end

      context "when no_proxy is a domain with no wildcard" do
        let(:env) do
          {
            "http_proxy" => proxy,
            "no_proxy" => "example.com",
          }
        end

        it { is_expected.to eq nil }
      end
    end
  end

  describe "allowing chefdk configuration outside of chefdk" do

    it "allows arbitrary settings in the chefdk config context" do
      expect { ChefConfig::Config.chefdk.generator_cookbook("/path") }.to_not raise_error
    end

  end

  describe "Treating deprecation warnings as errors" do

    context "when using our default RSpec configuration" do

      it "defaults to treating deprecation warnings as errors" do
        expect(ChefConfig::Config[:treat_deprecation_warnings_as_errors]).to be(true)
      end

      it "sets CHEF_TREAT_DEPRECATION_WARNINGS_AS_ERRORS environment variable" do
        expect(ENV["CHEF_TREAT_DEPRECATION_WARNINGS_AS_ERRORS"]).to eq("1")
      end

      it "treats deprecation warnings as errors in child processes when testing" do
        # Doing a full integration test where we launch a child process is slow
        # and liable to break for weird reasons (bundler env stuff, etc.), so
        # we're just checking that the presence of the environment variable
        # causes treat_deprecation_warnings_as_errors to be set to true after a
        # config reset.
        ChefConfig::Config.reset
        expect(ChefConfig::Config[:treat_deprecation_warnings_as_errors]).to be(true)
      end

    end

    context "outside of our test environment" do

      before do
        ENV.delete("CHEF_TREAT_DEPRECATION_WARNINGS_AS_ERRORS")
        ChefConfig::Config.reset
      end

      it "defaults to NOT treating deprecation warnings as errors" do
        expect(ChefConfig::Config[:treat_deprecation_warnings_as_errors]).to be(false)
      end
    end

  end

  describe "data collector URL" do

    context "when using default settings" do

      context "for Chef Client" do

        it "configures the data collector URL as a relative path to the Chef Server URL" do
          ChefConfig::Config[:chef_server_url] = "https://chef.example/organizations/myorg"
          expect(ChefConfig::Config[:data_collector][:server_url]).to eq("https://chef.example/organizations/myorg/data-collector")
        end

      end

      context "for Chef Solo legacy mode" do

        before do
          ChefConfig::Config[:solo_legacy_mode] = true
        end

        it "sets the data collector server URL to nil" do
          ChefConfig::Config[:chef_server_url] = "https://chef.example/organizations/myorg"
          expect(ChefConfig::Config[:data_collector][:server_url]).to be_nil
        end

      end

      context "for local mode" do

        before do
          ChefConfig::Config[:local_mode] = true
        end

        it "sets the data collector server URL to nil" do
          ChefConfig::Config[:chef_server_url] = "https://chef.example/organizations/myorg"
          expect(ChefConfig::Config[:data_collector][:server_url]).to be_nil
        end

      end

    end

  end

  describe "validation_client_name" do
    context "with a normal server URL" do
      before { ChefConfig::Config[:chef_server_url] = "https://chef.example/organizations/myorg" }

      it "sets the validation client to myorg-validator" do
        expect(ChefConfig::Config[:validation_client_name]).to eq "myorg-validator"
      end
    end

    context "with an unusual server URL" do
      before { ChefConfig::Config[:chef_server_url] = "https://chef.example/myorg" }

      it "sets the validation client to chef-validator" do
        expect(ChefConfig::Config[:validation_client_name]).to eq "chef-validator"
      end
    end
  end

end