File: vpc_tests.rb

package info (click to toggle)
ruby-fog-aws 3.33.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,180 kB
  • sloc: ruby: 75,405; javascript: 14; makefile: 9; sh: 4
file content (222 lines) | stat: -rw-r--r-- 9,187 bytes parent folder | download | duplicates (4)
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
Shindo.tests('Fog::Compute[:aws] | vpc requests', ['aws']) do

  @create_vpcs_format = {
    'vpcSet' => [{
      'vpcId'           => String,
      'state'           => String,
      'cidrBlock'       => String,
      'dhcpOptionsId'   => String,
      'tagSet'          => Hash
    }],
    'requestId' => String
  }

  @describe_vpcs_classic_link_format = {
    'vpcSet' => [{
      'vpcId'              => String,
      'tagSet'             => Hash,
      'classicLinkEnabled' => Fog::Boolean
    }],
    'requestId' => String
  }
  @describe_classic_link_instances = {
    'instancesSet' => [{
      'vpcId'              => String,
      'tagSet'             => Hash,
      'instanceId'         => String,
      'groups'             => [{'groupId' => String, 'groupName' => String}]
    }],
    'requestId' => String,
    'NextToken' => Fog::Nullable::String
  }

  @describe_vpcs_format = {
    'vpcSet' => [{
      'vpcId'                       => String,
      'state'                       => String,
      'cidrBlock'                   => String,
      'dhcpOptionsId'               => String,
      'tagSet'                      => Hash,
      'instanceTenancy'             => Fog::Nullable::String,
      'cidrBlockAssociationSet'     => [{'cidrBlock'     => String, 'associationId' => String, 'state' => String}],
      'ipv6CidrBlockAssociationSet' => [{'ipv6CidrBlock' => String, 'associationId' => String, 'state' => String}]
    }],
    'requestId' => String
  }

  @describe_vpc_classic_link_dns_support_format = {
    "vpcs" => [{
      "vpcId"                   => String,
      "classicLinkDnsSupported" => Fog::Boolean
    }]
  }

  tests('success') do

    @vpc_id = nil

    tests('#create_vpc').formats(@create_vpcs_format) do
      data = Fog::Compute[:aws].create_vpc('10.255.254.0/28').body
      @vpc_id = data['vpcSet'].first['vpcId']
      data
    end

    tests('#create_vpc').formats(@create_vpcs_format) do
      data = Fog::Compute[:aws].create_vpc('10.255.254.0/28',
        {'InstanceTenancy' => 'default'}).body
      @vpc_id = data['vpcSet'].first['vpcId']
      data
    end

    tests("#create_vpc('10.255.254.0/28', {'InstanceTenancy' => 'dedicated'})").returns('dedicated') do
      data = Fog::Compute[:aws].create_vpc('10.255.254.0/28',
        {'InstanceTenancy' => 'dedicated'}).body
      data['vpcSet'].first['instanceTenancy']
    end

    tests('#describe_vpcs').formats(@describe_vpcs_format) do
      Fog::Compute[:aws].describe_vpcs.body
    end

    [ 'enableDnsSupport', 'enableDnsHostnames'].each do |attrib|
      tests("#describe_vpc_attribute('#{@vpc_id}', #{attrib})").returns(@vpc_id) do
        Fog::Compute[:aws].describe_vpc_attribute(@vpc_id, attrib).body['vpcId']
      end
    end

    tests("#modify_vpc_attribute('#{@vpc_id}', {'EnableDnsSupport.Value' => false})").formats(AWS::Compute::Formats::BASIC) do
      Fog::Compute[:aws].modify_vpc_attribute(@vpc_id, {'EnableDnsSupport.Value' => false}).body
    end
    tests("#describe_vpc_attribute(#{@vpc_id}, 'enableDnsSupport')").returns(false) do
      Fog::Compute[:aws].describe_vpc_attribute(@vpc_id, 'enableDnsSupport').body["enableDnsSupport"]
    end
    tests("#modify_vpc_attribute('#{@vpc_id}', {'EnableDnsSupport.Value' => true})").formats(AWS::Compute::Formats::BASIC) do
      Fog::Compute[:aws].modify_vpc_attribute(@vpc_id, {'EnableDnsSupport.Value' => true}).body
    end
    tests("#describe_vpc_attribute(#{@vpc_id}, 'enableDnsSupport')").returns(true) do
      Fog::Compute[:aws].describe_vpc_attribute(@vpc_id, 'enableDnsSupport').body["enableDnsSupport"]
    end

    tests("#modify_vpc_attribute('#{@vpc_id}', {'EnableDnsHostnames.Value' => true})").formats(AWS::Compute::Formats::BASIC) do
      Fog::Compute[:aws].modify_vpc_attribute(@vpc_id, {'EnableDnsHostnames.Value' => true}).body
    end
    tests("#describe_vpc_attribute(#{@vpc_id}, 'enableDnsHostnames')").returns(true) do
      Fog::Compute[:aws].describe_vpc_attribute(@vpc_id, 'enableDnsHostnames').body["enableDnsHostnames"]
    end
    tests("#modify_vpc_attribute('#{@vpc_id}', {'EnableDnsHostnames.Value' => false})").formats(AWS::Compute::Formats::BASIC) do
      Fog::Compute[:aws].modify_vpc_attribute(@vpc_id, {'EnableDnsHostnames.Value' => false}).body
    end
    tests("#describe_vpc_attribute(#{@vpc_id}, 'enableDnsHostnames')").returns(false) do
      Fog::Compute[:aws].describe_vpc_attribute(@vpc_id, 'enableDnsHostnames').body["enableDnsHostnames"]
    end

    tests("#modify_vpc_attribute('#{@vpc_id}')").raises(Fog::AWS::Compute::Error) do
      Fog::Compute[:aws].modify_vpc_attribute(@vpc_id).body
    end

    tests("#modify_vpc_attribute('#{@vpc_id}', {'EnableDnsSupport.Value' => true, 'EnableDnsHostnames.Value' => true})").raises(Fog::AWS::Compute::Error) do
      Fog::Compute[:aws].modify_vpc_attribute(@vpc_id, {'EnableDnsSupport.Value' => true, 'EnableDnsHostnames.Value' => true}).body
    end

    # Create another vpc to test tag filters
    test_tags = {'foo' => 'bar'}
    @another_vpc = Fog::Compute[:aws].vpcs.create :cidr_block => '1.2.3.4/24', :tags => test_tags

    tests("#describe_vpcs('tag-key' => 'foo')").formats(@describe_vpcs_format)do
      body = Fog::Compute[:aws].describe_vpcs('tag-key' => 'foo').body
      tests("returns 1 vpc").returns(1) { body['vpcSet'].size }
      body
    end

    tests("#describe_vpcs('tag-value' => 'bar')").formats(@describe_vpcs_format)do
      body = Fog::Compute[:aws].describe_vpcs('tag-value' => 'bar').body
      tests("returns 1 vpc").returns(1) { body['vpcSet'].size }
      body
    end

    tests("#describe_vpcs('tag:foo' => 'bar')").formats(@describe_vpcs_format)do
      body = Fog::Compute[:aws].describe_vpcs('tag:foo' => 'bar').body
      tests("returns 1 vpc").returns(1) { body['vpcSet'].size }
      body
    end

    tests("describe_vpc_classic_link(:filters => {'tag-key' => 'foo'}").formats(@describe_vpcs_classic_link_format) do
      body = Fog::Compute[:aws].describe_vpc_classic_link(:filters => {'tag-key' => 'foo'}).body
      tests("returns 1 vpc").returns(1) { body['vpcSet'].size }
      body
    end

    tests("enable_vpc_classic_link").returns(true) do
      Fog::Compute[:aws].enable_vpc_classic_link @vpc_id
      body = Fog::Compute[:aws].describe_vpc_classic_link(:vpc_ids => [@vpc_id]).body
      body['vpcSet'].first['classicLinkEnabled']
    end

    @server = Fog::Compute[:aws].servers.create
    @server.wait_for {ready?}

    @group = Fog::Compute[:aws].security_groups.create :name => 'test-group', :description => 'vpc security group'

    tests("attach_classic_link_vpc") do
      Fog::Compute[:aws].attach_classic_link_vpc(@server.id, @vpc_id, [@group.group_id])
    end

    tests('describe_classic_link_instances').formats(@describe_classic_link_instances) do
      Fog::Compute[:aws].describe_classic_link_instances().body
    end

    tests("detach_classic_link_vpc").returns([]) do
      Fog::Compute[:aws].detach_classic_link_vpc(@server.id, @vpc_id)
      Fog::Compute[:aws].describe_classic_link_instances().body['instancesSet']
    end

    tests("enable_vpc_classic_link_dns_support('#{@vpc_id}')").formats(AWS::Compute::Formats::BASIC) do
      body = Fog::Compute[:aws].enable_vpc_classic_link_dns_support(@vpc_id).body
      body
    end

    tests("#describe_vpc_classic_link_dns_support").formats(@describe_vpc_classic_link_dns_support_format) do
      Fog::Compute[:aws].describe_vpc_classic_link_dns_support.body
    end

    tests("#describe_vpc_classic_link_dns_support(:vpc_ids => ['#{@vpc_id}'])").formats(@describe_vpc_classic_link_dns_support_format) do
      body = Fog::Compute[:aws].describe_vpc_classic_link_dns_support(:vpc_ids => [@vpc_id]).body
      returns(1)       { body['vpcs'].count }
      returns(@vpc_id) { body['vpcs'].first['vpcId'] }
      returns(true)    { body['vpcs'].first['classicLinkDnsSupported'] }
      body
    end

    tests("disable_vpc_classic_link_dns_support('#{@vpc_id}')").formats(AWS::Compute::Formats::BASIC) do
      Fog::Compute[:aws].disable_vpc_classic_link_dns_support(@vpc_id).body
    end

    tests("#describe_vpc_classic_link_dns_support(:vpc_ids => ['#{@vpc_id}'])").formats(@describe_vpc_classic_link_dns_support_format) do
      body = Fog::Compute[:aws].describe_vpc_classic_link_dns_support(:vpc_ids => [@vpc_id]).body
      returns(1)       { body['vpcs'].count }
      returns(@vpc_id) { body['vpcs'].first['vpcId'] }
      returns(false)   { body['vpcs'].first['classicLinkDnsSupported'] }
      body
    end

    if !Fog.mocking?
      @server.destroy
      @server.wait_for {state == 'terminated'}
    end

    tests("disable_vpc_classic_link").returns(false) do
      Fog::Compute[:aws].disable_vpc_classic_link @vpc_id
      body = Fog::Compute[:aws].describe_vpc_classic_link(:vpc_ids => [@vpc_id]).body
      body['vpcSet'].first['classicLinkEnabled']
    end

    tests("#delete_vpc('#{@vpc_id}')").formats(AWS::Compute::Formats::BASIC) do
      Fog::Compute[:aws].delete_vpc(@vpc_id).body
    end

    # Clean up
    Fog::Compute[:aws].delete_tags(@another_vpc.id, test_tags)
    @another_vpc.destroy
    Fog::AWS::Compute::Mock.reset if Fog.mocking?
  end
end