summaryrefslogtreecommitdiff
path: root/spec/functional/resource/ifconfig_spec.rb
blob: 6b40fbc2e2c80d0addf21c3fe802a5fa16e6cf59 (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
#
# Author:: Kaustubh Deorukhkar (<kaustubh@clogeny.com>)
# Copyright:: Copyright (c) 2013 Opscode, 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 "functional/resource/base"
require "chef/mixin/shell_out"

# run this test only for following platforms.
include_flag = !(["ubuntu", "centos", "aix"].include?(ohai[:platform]))

describe Chef::Resource::Ifconfig, :requires_root, :skip_travis, :external => include_flag do
  # This test does not work in travis because there is no eth0

  include Chef::Mixin::ShellOut

  let(:new_resource) do
    new_resource = Chef::Resource::Ifconfig.new("10.10.0.1", run_context)
    new_resource
  end

  let(:provider) do
    provider = new_resource.provider_for_action(new_resource.action)
    provider
  end

  let(:current_resource) do
    provider.load_current_resource
  end

  def lo_interface_for_test
    # use loopback interface for tests
    case ohai[:platform]
    when "aix"
      "lo0"
    else
      "lo"
    end
  end

  # **Caution: any updates to core interfaces can be risky.
  def en0_interface_for_test
    case ohai[:platform]
    when "aix"
      "en0"
    else
      "eth0"
    end
  end

  def network_interface_alias(interface)
    case ohai[:platform]
    when "aix"
      interface
    else
      interface + ":10"
    end
  end

  # platform specific test setup and validation routines

  def setup_add_interface(resource)
    resource.device network_interface_alias(en0_interface_for_test)
  end

  def setup_enable_interface(resource)
    resource.device network_interface_alias(en0_interface_for_test)
  end

  def interface_should_exists(interface)
    expect(shell_out("ifconfig #{@interface} | grep 10.10.0.1").exitstatus).to eq(0)
  end

  def interface_should_not_exists(interface)
    expect(shell_out("ifconfig #{@interface} | grep 10.10.0.1").exitstatus).to eq(1)
  end

  def interface_persistence_should_exists(interface)
    case ohai[:platform]
    when "aix"
      expect(shell_out("lsattr -E -l #{@interface} | grep 10.10.0.1").exitstatus).to eq(0)
    else
    end
  end

  def interface_persistence_should_not_exists(interface)
    case ohai[:platform]
    when "aix"
      expect(shell_out("lsattr -E -l #{@interface} | grep 10.10.0.1").exitstatus).to eq(1)
    else
    end
  end

  # Actual tests

  describe "#load_current_resource" do
    it "should load given interface" do
      new_resource.device lo_interface_for_test
      expect(current_resource.device).to eql(lo_interface_for_test)
      expect(current_resource.inet_addr).to match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)
    end
  end

  exclude_test = ohai[:platform] != "ubuntu"
  describe "#action_add", :external => exclude_test do
    after do
      new_resource.run_action(:delete)
    end
    it "should add interface (vip)" do
      setup_add_interface(new_resource)
      new_resource.run_action(:add)
      interface_should_exists(network_interface_alias(en0_interface_for_test))
      interface_persistence_should_exists(network_interface_alias(en0_interface_for_test))
    end
  end

  describe "#action_enable", :external => exclude_test do
    after do
      new_resource.run_action(:disable)
    end
    it "should enable interface (vip)" do
      setup_enable_interface(new_resource)
      new_resource.run_action(:enable)
      interface_should_exists(network_interface_alias(en0_interface_for_test))
    end
  end

  describe "#action_disable", :external => exclude_test do
    before do
      setup_enable_interface(new_resource)
      new_resource.run_action(:enable)
    end
    it "should disable interface (vip)" do
      new_resource.run_action(:disable)
      expect(new_resource).to be_updated_by_last_action
      interface_should_not_exists(network_interface_alias(en0_interface_for_test))
    end
  end

  describe "#action_delete", :external => exclude_test do
    before do
      setup_add_interface(new_resource)
      new_resource.run_action(:add)
    end
    it "should delete interface (vip)" do
      new_resource.run_action(:delete)
      expect(new_resource).to be_updated_by_last_action
      interface_should_not_exists(network_interface_alias(en0_interface_for_test))
      interface_persistence_should_not_exists(network_interface_alias(en0_interface_for_test))
    end
  end
end