summaryrefslogtreecommitdiff
path: root/spec/unit/plugins/network_spec.rb
blob: 31780b574d87ee641c1793ebcc474272a8c3c27c (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
#
#  Author:: Laurent Desarmes <laurent.desarmes@u-picardie.fr>
#  Copyright:: Copyright (c) 2012 Laurent Desarmes
#  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 File.expand_path(File.dirname(__FILE__) + "/../../spec_helper.rb")

def it_doesnt_fail
  it "doesnt fail" do
    allow(Ohai::Log).to receive(:warn)
    expect(Ohai::Log).not_to receive(:debug).with(/^Plugin network threw exception/)
    @plugin.run
  end
end

# basic sanity check that is called in all describes below
def it_populates_ipaddress_attributes
  source = caller[0]

  it "populates ipaddress, macaddress and ip6address" do
    begin
      allow(Ohai::Log).to receive(:warn)
      expect(Ohai::Log).not_to receive(:debug).with(/^Plugin network threw exception/)
      @plugin.run
      %w{ ipaddress macaddress ip6address }.each do |attribute|
        expect(@plugin).to have_key(attribute)
      end
    rescue Exception
      puts "RSpec context: #{source}"
      raise
    end
  end
end

describe Ohai::System, "Network Plugin" do

  # output of network plugins on particular platforms to mock plugin runs
  basic_data = {
    "freebsd" => {
      "network" => {
        "interfaces" => {
          "vr0" => {
            "type" => "vr",
            "number" => "0",
            "flags" => %w{UP BROADCAST RUNNING SIMPLEX MULTICAST},
            "addresses" => {
              "00:00:24:c9:5e:b8" => {
                "family" => "lladdr",
              },
              "fe80::200:24ff:fec9:5eb8" => {
                "family" => "inet6",
                "zoneid" => "vr0",
                "prefixlen" => "64",
                "scopeid" => "0x1",
              },
              "76.91.1.255" => {
                "family" => "inet",
                "netmask" => "255.255.252.0",
                "broadcast" => "255.255.255.255",
              },
            },
            "arp" => {
              "76.91.1.255" => "00:00:24:c9:5e:b8",
              "76.91.0.1" => "00:01:5c:24:8c:01",
            },
          },
          "vr1" => {
            "type" => "vr",
            "number" => "1",
            "flags" => %w{UP BROADCAST RUNNING PROMISC SIMPLEX MULTICAST},
            "addresses" => {
              "00:00:24:c9:5e:b9" => {
                "family" => "lladdr",
              },
              "fe80::200:24ff:fec9:5eb9" => {
                "family" => "inet6",
                "zoneid" => "vr1",
                "prefixlen" => "64",
                "scopeid" => "0x2",
              },
            },
          },
          "vr2" => {
            "type" => "vr",
            "number" => "2",
            "flags" => %w{UP BROADCAST RUNNING PROMISC SIMPLEX MULTICAST},
            "addresses" => {
              "00:00:24:c9:5e:ba" => {
                "family" => "lladdr",
              },
              "fe80::200:24ff:fec9:5eba" => {
                "family" => "inet6",
                "zoneid" => "vr2",
                "prefixlen" => "64",
                "scopeid" => "0x3",
              },
            },
          },
          "vr3" => {
            "type" => "vr",
            "number" => "3",
            "flags" => %w{UP BROADCAST RUNNING PROMISC SIMPLEX MULTICAST},
            "addresses" => {
              "00:00:24:c9:5e:bb" => {
                "family" => "lladdr",
              },
              "fe80::200:24ff:fec9:5ebb" => {
                "family" => "inet6",
                "zoneid" => "vr3",
                "prefixlen" => "64",
                "scopeid" => "0x4",
              },
            },
          },
          "ipfw0" => {
            # OHAI-492: Ensure network plugin works with interfaces without addresses.
            "type" => "ipfw",
            "number" => "0",
            "flags" => %w{UP SIMPLEX MULTICAST},
          },
          "lo0" => {
            "type" => "lo",
            "number" => "0",
            "flags" => %w{UP LOOPBACK RUNNING MULTICAST},
            "addresses" => {
              "127.0.0.1" => {
                "family" => "inet",
                "netmask" => "255.0.0.0",
              },
              "::1" => {
                "family" => "inet6",
                "prefixlen" => "128",
              },
              "fe80::1" => {
                "family" => "inet6",
                "zoneid" => "lo0",
                "prefixlen" => "64",
                "scopeid" => "0x8",
              },
            },
          },
          "bridge0" => {
            "type" => "bridge",
            "number" => "0",
            "flags" => %w{LEARNING DISCOVER AUTOEDGE AUTOPTP},
            "addresses" => {
              "02:20:6f:d2:c4:00" => {
                "family" => "lladdr",
              },
              "192.168.2.1" => {
                "family" => "inet",
                "netmask" => "255.255.255.0",
                "broadcast" => "192.168.2.255",
              },
              "2001:470:d:cb4::1" => {
                "family" => "inet6",
                "prefixlen" => "64",
              },
              "fe80::cafe:babe:dead:beef" => {
                "family" => "inet6",
                "zoneid" => "bridge0",
                "prefixlen" => "64",
                "scopeid" => "0x9",
              },
            },
            "arp" => {
              "192.168.2.142" => "60:67:20:75:a2:0c",
              "192.168.2.205" => "c0:c1:c0:f9:40:ed",
              "192.168.2.160" => "cc:3a:61:cf:67:13",
              "192.168.2.1" => "02:20:6f:d2:c4:00",
              "192.168.2.135" => "f8:0c:f3:d7:c6:b6",
              "192.168.2.165" => "f8:8f:ca:24:49:ad",
              "192.168.2.158" => "48:5d:60:1f:ea:d1",
              "192.168.2.150" => "60:a4:4c:60:b3:d9",
            },
          },
          "gif0" => {
            "type" => "gif",
            "number" => "0",
            "flags" => %w{UP POINTOPOINT RUNNING MULTICAST},
            "addresses" => {
              "fe80::200:24ff:fec9:5eb8" => {
                "family" => "inet6",
                "zoneid" => "gif0",
                "prefixlen" => "64",
                "scopeid" => "0xa",
              },
            },
          },
        },
        "default_gateway" => "76.91.0.1",
        "default_interface" => "vr0",
        "default_inet6_gateway" => "2001:470:d:cb4::2",
        "default_inet6_interface" => "bridge0",
      },
    },
    "linux" => {
      "network" => {
        # pp Hash[node['network']] from chef-shell to get the network data
        # have just removed the neighbour and route entries by hand
        "interfaces" => {
          "lo" => {
            "flags" => %w{LOOPBACK UP},
            "addresses" => {
              "::1" => {
                "scope" => "Node",
                "prefixlen" => "128",
                "family" => "inet6",
              },
              "127.0.0.1" => {
                "scope" => "Node",
                "netmask" => "255.0.0.0",
                "prefixlen" => "8",
                "family" => "inet",
              },
            },
            "mtu" => "16436",
            "encapsulation" => "Loopback",
            "state" => "unknown",
          },
          "eth0" => {
            "flags" => %w{BROADCAST MULTICAST UP},
            "number" => "0",
            "addresses" => {
              "fe80::216:3eff:fe2f:3679" => {
                "scope" => "Link",
                "prefixlen" => "64",
                "family" => "inet6",
              },
              "00:16:3E:2F:36:79" => {
                "family" => "lladdr",
              },
              "192.168.66.33" => {
                "scope" => "Global",
                "netmask" => "255.255.255.0",
                "broadcast" => "192.168.66.255",
                "prefixlen" => "24",
                "family" => "inet",
              },
              "3ffe:1111:2222::33" => {
                "prefixlen" => "48",
                "family" => "inet6",
                "scope" => "Global",
                "state" => "up",
              },
            },
            "mtu" => "1500",
            "type" => "eth",
            "encapsulation" => "Ethernet",
          },
          "eth1" => {
            "flags" => %w{BROADCAST MULTICAST UP},
            "number" => "1",
            "addresses" => {
              "fe80::216:3eff:fe2f:3680" => {
                "scope" => "Link",
                "prefixlen" => "64",
                "family" => "inet6",
              },
              "00:16:3E:2F:36:80" => {
                "family" => "lladdr",
              },
              "192.168.99.11" => {
                "scope" => "Global",
                "netmask" => "255.255.255.0",
                "broadcast" => "192.168.99.255",
                "prefixlen" => "24",
                "family" => "inet",
              },
              "3ffe:1111:3333::1" => {
                "prefixlen" => "48",
                "family" => "inet6",
                "scope" => "Global",
              },
            },
            "mtu" => "1500",
            "type" => "eth",
            "encapsulation" => "Ethernet",
          },
        },
        "default_gateway" => "192.168.66.15",
        "default_interface" => "eth0",
        "default_inet6_gateway" => "3ffe:1111:2222::",
        "default_inet6_interface" => "eth0",
      },
    },
    "windows" => {
      "network" => {
        "interfaces" => {
          "0xb" => {
            "addresses" => {
              "172.19.0.130" => {
                "prefixlen" => "24",
                "netmask" => "255.255.255.0",
                "broadcast" => "172.19.0.255",
                "family" => "inet",
              },
              "fe80::698d:3e37:7950:b28c" => {
                "prefixlen" => "64",
                "family" => "inet6",
                "scope" => "Link",
              },
              "52:54:44:66:66:02" => {
                "family" => "lladdr",
              },
            },
            "mtu" => nil,
            "type" => "Ethernet 802.3",
            "encapsulation" => "Ethernet",
          },
        },
        "default_gateway" => "172.19.0.1",
        "default_interface" => "0xb",
      },
    },
  }

  describe "on linux" do
    before(:each) do
      @plugin = get_plugin("network")
      @plugin["network"] = basic_data["linux"]["network"]
    end

    describe "when the linux::network plugin hasn't set any of {ip,ip6,mac}address attributes" do
      describe "simple network setup" do
        it_populates_ipaddress_attributes

        it "detects {ip,ip6,mac}address" do
          @plugin.run
          expect(@plugin["ipaddress"]).to eq("192.168.66.33")
          expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:79")
          expect(@plugin["ip6address"]).to eq("3ffe:1111:2222::33")
        end
      end

      describe "default ipv4 and ipv6 gateway on different interfaces" do
        describe "both interfaces have an ARP" do
          before do
            @plugin["network"]["default_inet6_gateway"] = "3ffe:1111:3333::"
            @plugin["network"]["default_inet6_interface"] = "eth1"
          end

          it_populates_ipaddress_attributes

          it "detects {ip,ip6}address" do
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.66.33")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:3333::1")
          end

          it "set macaddress from the ipv4 setup" do
            @plugin.run
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:79")
          end

          it "informs about this setup" do
            expect(Ohai::Log).to receive(:debug).with(/ipaddress and ip6address are set from different interfaces/)
            allow(Ohai::Log).to receive(:debug)
            @plugin.run
          end
        end

        describe "ipv4 interface has no ARP" do
          before do
            @plugin["network"]["interfaces"]["eth0"]["addresses"].delete_if { |k, kv| kv["family"] == "lladdr" }
            # not really checked by this pluging
            @plugin["network"]["interfaces"]["eth0"]["flags"] << "NOARP"
            @plugin["network"]["default_inet6_gateway"] = "3ffe:1111:3333::"
            @plugin["network"]["default_inet6_interface"] = "eth1"
          end

          it_populates_ipaddress_attributes

          it "detects {ip,ip6}address" do
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.66.33")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:3333::1")
          end

          it "sets macaddress to the ipv6 interface because it hadn't set one for ipv4 first" do
            @plugin.run
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:80")
          end

          it "informs about this setup" do
            expect(Ohai::Log).to receive(:debug).with(/ipaddress and ip6address are set from different interfaces/)
            allow(Ohai::Log).to receive(:debug)
            @plugin.run
          end
        end
      end

      describe "conflicting results from the linux::network plugin" do
        describe "default interface doesn't match the default_gateway" do
          before do
            @plugin["network"]["default_interface"] = "eth1"
            @plugin["network"]["default_inet6_interface"] = "eth1"
          end

          it_populates_ipaddress_attributes

          it "picks {ip,ip6,mac}address" do
            allow(Ohai::Log).to receive(:warn)
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.99.11")
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:80")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:3333::1")
          end

          it "warns about this conflict" do
            expect(Ohai::Log).to receive(:debug).with(/\[inet\] no ipaddress\/mask on eth1/).once
            expect(Ohai::Log).to receive(:debug).with(/\[inet6\] no ipaddress\/mask on eth1/).once
            allow(Ohai::Log).to receive(:debug)
            @plugin.run
          end
        end

        describe "there's a default gateway, none of the configured ip/mask theorically allows to reach it" do
          before do
            @plugin["network"]["default_gateway"] = "172.16.12.42"
            @plugin["network"]["default_inet6_gateway"] = "3ffe:12:42::7070"
          end

          it "picks {ip,ip6,mac}address" do
            allow(Ohai::Log).to receive(:warn)
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.66.33")
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:79")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:2222::33")
          end

        end

        describe "no ip address for the given default interface/gateway" do
          before do
            @plugin["network"]["interfaces"]["eth0"]["addresses"].delete_if { |k, v| %w{inet inet6}.include? v["family"] }
          end

          it_doesnt_fail

          it "doesn't detect {ip,ip6,mac}address" do
            allow(Ohai::Log).to receive(:warn)
            @plugin.run
            expect(@plugin["ipaddress"]).to be_nil
            expect(@plugin["macaddress"]).to be_nil
            expect(@plugin["ip6address"]).to be_nil
          end

          it "warns about this conflict" do
            expect(Ohai::Log).to receive(:warn).with(/unable to detect ipaddress/).once
            expect(Ohai::Log).to receive(:warn).with(/\[inet\] no ip address on eth0/).once
            expect(Ohai::Log).to receive(:debug).with(/unable to detect ip6address/).once
            expect(Ohai::Log).to receive(:debug).with(/unable to detect macaddress/).twice # for each family
            expect(Ohai::Log).to receive(:warn).with(/\[inet6\] no ip address on eth0/).once
            @plugin.run
          end
        end

        describe "no ip at all" do
          before do
            @plugin["network"]["default_gateway"] = nil
            @plugin["network"]["default_interface"] = nil
            @plugin["network"]["default_inet6_gateway"] = nil
            @plugin["network"]["default_inet6_interface"] = nil
            @plugin["network"]["interfaces"].each do |i, iv|
              iv["addresses"].delete_if { |k, kv| %w{inet inet6}.include? kv["family"] }
            end
          end

          it_doesnt_fail

          it "doesn't detect {ip,ip6,mac}address" do
            allow(Ohai::Log).to receive(:warn)
            @plugin.run
            expect(@plugin["ipaddress"]).to be_nil
            expect(@plugin["macaddress"]).to be_nil
            expect(@plugin["ip6address"]).to be_nil
          end

          it "should warn about it" do
            expect(Ohai::Log).to receive(:warn).with(/unable to detect ipaddress/).once
            expect(Ohai::Log).to receive(:debug).with(/unable to detect macaddress/).twice # for each family
            expect(Ohai::Log).to receive(:debug).with(/unable to detect ip6address/).once
            @plugin.run
          end
        end
      end

      describe "several ipaddresses matching the default route" do
        describe "bigger prefix not set on the default interface" do
          before do
            @plugin["network"]["interfaces"]["eth2"] = {
              "flags" => %w{BROADCAST MULTICAST UP},
              "number" => "2",
              "addresses" => {
                "fe80::216:3eff:fe2f:3681" => {
                  "scope" => "Link",
                  "prefixlen" => "64",
                  "family" => "inet6",
                },
                "00:16:3E:2F:36:81" => { "family" => "lladdr" },
                "192.168.66.99" => {
                  "scope" => "Global",
                  "netmask" => "255.255.255.128",
                  "broadcast" => "192.168.99.127",
                  "prefixlen" => "25",
                  "family" => "inet",
                },
                "3ffe:1111:2222:0:4444::1" => {
                  "prefixlen" => "64",
                  "family" => "inet6",
                  "scope" => "Global",
                },
              },
            }
          end

          it_populates_ipaddress_attributes

          it "sets {ip,ip6,mac}address correctly" do
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.66.33")
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:79")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:2222::33")
          end
        end

        describe "bigger prefix set on the default interface" do
          before do
            @plugin["network"]["interfaces"]["eth0"]["addresses"]["192.168.66.99"] = {
              "scope" => "Global",
              "netmask" => "255.255.255.128",
              "broadcast" => "192.168.66.127",
              "prefixlen" => "25",
              "family" => "inet",
            }
            @plugin["network"]["interfaces"]["eth0"]["addresses"]["3ffe:1111:2222:0:4444::1"] = {
              "prefixlen" => "64",
              "family" => "inet6",
              "scope" => "Global",
            }
          end

          it_populates_ipaddress_attributes

          it "sets {ip,ip6,mac}address correctly" do
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.66.99")
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:79")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:2222:0:4444::1")
          end
        end

        describe "smallest ip not set on the default_interface" do
          before do
            @plugin["network"]["interfaces"]["eth2"] = {
              "flags" => %w{BROADCAST MULTICAST UP},
              "number" => "2",
              "addresses" => {
                "fe80::216:3eff:fe2f:3681" => {
                  "scope" => "Link",
                  "prefixlen" => "64",
                  "family" => "inet6",
                },
                "00:16:3E:2F:36:81" => { "family" => "lladdr" },
                "192.168.66.32" => {
                  "scope" => "Global",
                  "netmask" => "255.255.255.0",
                  "broadcast" => "192.168.66.255",
                  "prefixlen" => "24",
                  "family" => "inet",
                },
                "3ffe:1111:2222::32" => {
                  "prefixlen" => "48",
                  "family" => "inet6",
                  "scope" => "Global",
                },
              },
            }
          end

          it_populates_ipaddress_attributes

          it "sets {ip,ip6,mac}address correctly" do
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.66.33")
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:79")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:2222::33")
          end
        end

        describe "smallest ip set on the default_interface" do
          before do
            @plugin["network"]["interfaces"]["eth0"]["addresses"]["192.168.66.32"] = {
              "scope" => "Global",
              "netmask" => "255.255.255.0",
              "broadcast" => "192.168.66.255",
              "prefixlen" => "24",
              "family" => "inet",
            }
            @plugin["network"]["interfaces"]["eth0"]["addresses"]["3ffe:1111:2222::32"] = {
              "prefixlen" => "48",
              "family" => "inet6",
              "scope" => "Global",
            }
          end

          it_populates_ipaddress_attributes

          it "sets {ip,ip6,mac}address correctly" do
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.66.32")
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:79")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:2222::32")
          end
        end
      end

      describe "no default route" do
        describe "first interface is not the best choice" do
          before do
            @plugin["network"]["default_gateway"] = nil
            @plugin["network"]["default_interface"] = nil
            @plugin["network"]["default_inet6_gateway"] = nil
            @plugin["network"]["default_inet6_interface"] = nil
            # removing inet* addresses from eth0, to complicate things a bit
            @plugin["network"]["interfaces"]["eth0"]["addresses"].delete_if { |k, v| %w{inet inet6}.include? v["family"] }
          end

          it_populates_ipaddress_attributes

          it "picks {ip,mac,ip6}address from the first interface" do
            expect(Ohai::Log).to receive(:debug).with(/\[inet\] no default interface/).once
            expect(Ohai::Log).to receive(:debug).with(/\[inet6\] no default interface/).once
            allow(Ohai::Log).to receive(:debug)
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.99.11")
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:80")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:3333::1")
          end
        end

        describe "can choose from addresses with different scopes" do
          before do
            @plugin["network"]["default_gateway"] = nil
            @plugin["network"]["default_interface"] = nil
            @plugin["network"]["default_inet6_gateway"] = nil
            @plugin["network"]["default_inet6_interface"] = nil
            # just changing scopes to lInK for eth0 addresses
            @plugin["network"]["interfaces"]["eth0"]["addresses"].each { |k, v| v[:scope] = "lInK" if %w{inet inet6}.include? v["family"] }
          end

          it_populates_ipaddress_attributes

          it "prefers global scope addressses to set {ip,mac,ip6}address" do
            expect(Ohai::Log).to receive(:debug).with(/\[inet\] no default interface/).once
            expect(Ohai::Log).to receive(:debug).with(/\[inet6\] no default interface/).once
            allow(Ohai::Log).to receive(:debug)
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.99.11")
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:80")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:3333::1")
          end
        end
      end

      describe "link level default route" do
        describe "simple setup" do
          before do
            @plugin["network"]["default_gateway"] = "0.0.0.0"
            @plugin["network"]["default_interface"] = "eth1"
            @plugin["network"]["default_inet6_gateway"] = "::"
            @plugin["network"]["default_inet6_interface"] = "eth1"
          end

          it_populates_ipaddress_attributes

          it "picks {ip,mac,ip6}address from the default interface" do
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.99.11")
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:80")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:3333::1")
          end
        end

        describe "fe80::1 as a default gateway" do
          before do
            @plugin["network"]["default_inet6_gateway"] = "fe80::1"
          end

          it_populates_ipaddress_attributes

          it "picks {ip,mac,ip6}address from the default interface" do
            @plugin.run
            expect(@plugin["ip6address"]).to eq("3ffe:1111:2222::33")
          end
        end

        describe "can choose from addresses with different scopes" do
          before do
            @plugin["network"]["default_gateway"] = "0.0.0.0"
            @plugin["network"]["default_interface"] = "eth1"
            @plugin["network"]["default_inet6_gateway"] = "::"
            @plugin["network"]["default_inet6_interface"] = "eth1"
            @plugin["network"]["interfaces"]["eth1"]["addresses"]["127.0.0.2"] = {
              "scope" => "host",
              "netmask" => "255.255.255.255",
              "prefixlen" => "32",
              "family" => "inet",
            }
          end

          it_populates_ipaddress_attributes

          it "picks {ip,mac,ip6}address from the default interface" do
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("192.168.99.11")
            expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:80")
            expect(@plugin["ip6address"]).to eq("3ffe:1111:3333::1")
          end
        end
      end

      describe "point to point address" do
        before do
          @plugin["network"]["interfaces"]["eth2"] = {
            "flags" => %w{POINTOPOINT BROADCAST MULTICAST UP},
            "number" => "2",
            "addresses" => {
              "fe80::216:3eff:fe2f:3681" => {
                "scope" => "Link",
                "prefixlen" => "64",
                "family" => "inet6",
              },
              "00:16:3E:2F:36:81" => { "family" => "lladdr" },
              "192.168.66.99" => {
                "scope" => "Global",
                "netmask" => "255.255.255.255",
                "peer" => "192.168.99.126",
                "prefixlen" => "32",
                "family" => "inet",
              },
              "3ffe:1111:2222:0:4444::1" => {
                "prefixlen" => "128",
                "peer" => "3ffe:1111:2222:0:4444::2",
                "family" => "inet6",
                "scope" => "Global",
              },
            },
          }
          @plugin["network"]["default_gateway"] = "192.168.99.126"
          @plugin["network"]["default_interface"] = "eth2"
          @plugin["network"]["default_inet6_gateway"] = "3ffe:1111:2222:0:4444::2"
          @plugin["network"]["default_inet6_interface"] = "eth2"
        end

        it_populates_ipaddress_attributes

        it "picks {ip,mac,ip6}address from the default interface" do
          @plugin.run
          expect(@plugin["ipaddress"]).to eq("192.168.66.99")
          expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:81")
          expect(@plugin["ip6address"]).to eq("3ffe:1111:2222:0:4444::1")
        end
      end

      describe "ipv6 only node" do
        before do
          @plugin["network"]["default_gateway"] = nil
          @plugin["network"]["default_interface"] = nil
          @plugin["network"]["interfaces"].each do |i, iv|
            iv["addresses"].delete_if { |k, kv| kv["family"] == "inet" }
          end
        end

        it_doesnt_fail

        it "can't detect ipaddress" do
          allow(Ohai::Log).to receive(:warn)
          @plugin.run
          expect(@plugin["ipaddress"]).to be_nil
        end

        it "warns about not being able to set {ip,mac}address (ipv4)" do
          expect(Ohai::Log).to receive(:warn).with(/unable to detect ipaddress/).once
          expect(Ohai::Log).to receive(:debug).with(/unable to detect macaddress/) # for ipv4
          expect(Ohai::Log).to receive(:debug).with(/setting macaddress to/) # for ipv6
          expect(Ohai::Log).to receive(:debug).with(/\[inet6\] Using default interface eth0 and default gateway/) # for ipv6
          @plugin.run
        end

        it "sets {ip6,mac}address" do
          allow(Ohai::Log).to receive(:warn)
          @plugin.run
          expect(@plugin["ip6address"]).to eq("3ffe:1111:2222::33")
          expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:79")
        end

        it "informs about macaddress being set using the ipv6 setup" do
          expect(Ohai::Log).to receive(:debug).with(/setting macaddress to '00:16:3E:2F:36:79'/)
          allow(Ohai::Log).to receive(:debug)
          @plugin.run
        end
      end

      describe "ipv6 only with ipv4 loopback" do
        before do
          @plugin["network"]["default_gateway"] = nil
          @plugin["network"]["default_interface"] = nil
          @plugin["network"]["interfaces"].each do |i, iv|
            next if i == "lo"
            iv["addresses"].delete_if { |k, kv| kv["family"] == "inet" }
          end
        end

        it_doesnt_fail

        it "can't detect ipaddress" do
          allow(Ohai::Log).to receive(:warn)
          @plugin.run
          expect(@plugin["ipaddress"]).to eq("127.0.0.1")
        end

        it "sets {ip6,mac}address" do
          allow(Ohai::Log).to receive(:warn)
          @plugin.run
          expect(@plugin["ip6address"]).to eq("3ffe:1111:2222::33")
          expect(@plugin["macaddress"]).to eq("00:16:3E:2F:36:79")
        end

        it "informs about macaddress being set using the ipv6 setup" do
          expect(Ohai::Log).to receive(:debug).with(/setting macaddress to '00:16:3E:2F:36:79'/)
          allow(Ohai::Log).to receive(:debug)
          @plugin.run
        end
      end
    end

    # specs using network plugin data for each mocked OS (freebsd,linux,windows)
    basic_data.keys.sort.each do |os|
      describe "the #{os}::network has already set some of the {ip,mac,ip6}address attributes" do
        before(:each) do
          @plugin["network"] = basic_data[os]["network"]
        end

        describe "{ip,mac}address are already set" do
          before do
            @plugin["ipaddress"] = "10.11.12.13"
            @plugin["macaddress"] = "00:AA:BB:CC:DD:EE"
            @expected_results = {
              "freebsd" => {
                "ip6address" => "2001:470:d:cb4::1",
                "macaddress" => "02:20:6f:d2:c4:00",
              },
              "linux" => {
                "ip6address" => "3ffe:1111:2222::33",
                "macaddress" => "00:16:3E:2F:36:79",
              },
              "windows" => {
                "ip6address" => "fe80::698d:3e37:7950:b28c",
                "macaddress" => "00:AA:BB:CC:DD:EE",
              },
            }
          end

          it_populates_ipaddress_attributes

          it "detects ip6address" do
            @plugin.run
            expect(@plugin["ip6address"]).to eq(@expected_results[os]["ip6address"])
          end

          it "doesn't overwrite {ip,mac}address" do
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("10.11.12.13")
            expect(@plugin["macaddress"]).to eq("00:AA:BB:CC:DD:EE")
          end
        end

        describe "ip6address is already set" do
          describe "node has ipv4 and ipv6" do
            before do
              @plugin["ip6address"] = "3ffe:8888:9999::1"
              @expected_results = {
                "freebsd" => {
                  "ipaddress" => "76.91.1.255",
                  "macaddress" => "00:00:24:c9:5e:b8",
                },
                "linux" => {
                  "ipaddress" => "192.168.66.33",
                  "macaddress" => "00:16:3E:2F:36:79",
                },
                "windows" => {
                  "ipaddress" => "172.19.0.130",
                  "macaddress" => "52:54:44:66:66:02",
                },
              }
            end

            it_populates_ipaddress_attributes

            it "detects {ip,mac}address" do
              @plugin.run
              expect(@plugin["ipaddress"]).to eq(@expected_results[os]["ipaddress"])
              expect(@plugin["macaddress"]).to eq(@expected_results[os]["macaddress"])
            end

            it "doesn't overwrite ip6address" do
              @plugin.run
              expect(@plugin["ip6address"]).to eq("3ffe:8888:9999::1")
            end
          end

          describe "ipv6 only node" do
            before do
              @plugin["network"]["default_gateway"] = nil
              @plugin["network"]["default_interface"] = nil
              @plugin["network"]["interfaces"].each do |i, iv|
                if iv.has_key? "addresses"
                  iv["addresses"].delete_if { |k, kv| kv["family"] == "inet" }
                end
              end
              @plugin["ip6address"] = "3ffe:8888:9999::1"
              @expected_results = {
                "freebsd" => {
                  "macaddress" => "02:20:6f:d2:c4:00",
                },
                "linux" => {
                  "macaddress" => "00:16:3E:2F:36:79",
                },
                "windows" => {
                  "macaddress" => "52:54:44:66:66:02",
                },
              }
            end

            it_doesnt_fail

            it "can't detect ipaddress (ipv4)" do
              allow(Ohai::Log).to receive(:warn)
              @plugin.run
              expect(@plugin["ipaddress"]).to be_nil
            end

            it "takes the macaddress from ipv6" do
              allow(Ohai::Log).to receive(:warn)
              @plugin.run
              expect(@plugin["macaddress"]).to eq(@expected_results[os]["macaddress"])
            end

            it "warns about not being able to set ipaddress" do
              expect(Ohai::Log).to receive(:warn).with(/unable to detect ipaddress/).once
              @plugin.run
            end

            it "doesn't overwrite ip6address" do
              allow(Ohai::Log).to receive(:warn)
              @plugin.run
              expect(@plugin["ip6address"]).to eq("3ffe:8888:9999::1")
            end
          end
        end

        describe "{mac,ip6}address are already set" do
          describe "valid ipv4 setup" do
            before do
              @plugin["macaddress"] = "00:AA:BB:CC:DD:EE"
              @plugin["ip6address"] = "3ffe:8888:9999::1"
              @expected_results = {
                "freebsd" => {
                  "ipaddress" => "76.91.1.255",
                  "macaddress" => "00:00:24:c9:5e:b8",
                },
                "linux" => {
                  "ipaddress" => "192.168.66.33",
                  "macaddress" => "00:16:3E:2F:36:79",
                },
                "windows" => {
                  "ipaddress" => "172.19.0.130",
                  "macaddress" => "52:54:44:66:66:02",
                },
              }
            end

            it_populates_ipaddress_attributes

            it "detects ipaddress and does not overwrite macaddress" do
              @plugin.run
              expect(@plugin["ipaddress"]).to eq(@expected_results[os]["ipaddress"])
              expect(@plugin["macaddress"]).to eq(@plugin["macaddress"])
            end

            it "doesn't overwrite ip6address" do
              @plugin.run
              expect(@plugin["ip6address"]).to eq("3ffe:8888:9999::1")
            end
          end

          describe "ipv6 only node" do
            before do
              @plugin["network"]["default_gateway"] = nil
              @plugin["network"]["default_interface"] = nil
              @plugin["network"]["interfaces"].each do |i, iv|
                if iv.has_key? "addresses"
                  iv["addresses"].delete_if { |k, kv| kv["family"] == "inet" }
                end
              end
              @plugin["macaddress"] = "00:AA:BB:CC:DD:EE"
              @plugin["ip6address"] = "3ffe:8888:9999::1"
            end

            it_doesnt_fail

            it "can't set ipaddress" do
              allow(Ohai::Log).to receive(:warn)
              @plugin.run
              expect(@plugin["ipaddress"]).to be_nil
            end

            it "doesn't overwrite {ip6,mac}address" do
              allow(Ohai::Log).to receive(:warn)
              @plugin.run
              expect(@plugin["ip6address"]).to eq("3ffe:8888:9999::1")
              expect(@plugin["macaddress"]).to eq("00:AA:BB:CC:DD:EE")
            end
          end
        end

        describe "{ip,mac,ip6}address are already set" do
          before do
            @plugin["ipaddress"] = "10.11.12.13"
            @plugin["macaddress"] = "00:AA:BB:CC:DD:EE"
            @plugin["ip6address"] = "3ffe:8888:9999::1"
          end

          it_populates_ipaddress_attributes

          it "doesn't overwrite {ip,mac,ip6}address" do
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("10.11.12.13")
            expect(@plugin["macaddress"]).to eq("00:AA:BB:CC:DD:EE")
            expect(@plugin["ip6address"]).to eq("3ffe:8888:9999::1")
          end
        end

        describe "{ip,ip6}address are already set" do
          before do
            @plugin["ipaddress"] = "10.11.12.13"
            @plugin["ip6address"] = "3ffe:8888:9999::1"
          end

          it_doesnt_fail

          it "doesn't overwrite {ip,ip6}address" do
            @plugin.run
            expect(@plugin["ipaddress"]).to eq("10.11.12.13")
            expect(@plugin["ip6address"]).to eq("3ffe:8888:9999::1")
          end
        end

      end
    end
  end
end