File: load_balancers_spec.rb

package info (click to toggle)
ruby-azure-sdk 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,400 kB
  • ctags: 12,388
  • sloc: ruby: 168,299; sh: 6; makefile: 2
file content (227 lines) | stat: -rw-r--r-- 9,640 bytes parent folder | download
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
# encoding: utf-8
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.

require_relative 'spec_helper'

include MsRestAzure
include Azure::ARM::Resources
include Azure::ARM::Network
include Azure::ARM::Network::Models

describe 'Load balancers' do
  before(:each) do
    @resource_helper = ResourceHelper.new()
    @client = @resource_helper.network_client.load_balancers
    @location = 'westus'
    @resource_group = @resource_helper.create_resource_group
  end
  after(:each) do
    @resource_helper.delete_resource_group(@resource_group.name)
  end

  it 'should create load balancer' do
    params = build_load_balancer_params
    result = @client.create_or_update_async(@resource_group.name, params.name, params).value!
    expect(result.response.status).to eq(201)
    expect(result.body).not_to be_nil
    expect(result.body.name).to eq(params.name)
  end

  it 'should create load balancer with complex parameter structure' do
    #create public ip address Dns Settings
    dns_settings = PublicIPAddressDnsSettings.new

    #create public ip address
    lb_public_ip_name = 'test_public_ip'
    lb_domain_name_label = 'test-domain8564'
    public_ip = PublicIPAddress.new
    public_ip.location = @location
    public_ip.public_ipallocation_method = 'Dynamic'
    public_ip.dns_settings = dns_settings

    dns_settings.domain_name_label = lb_domain_name_label
    public_ip = @resource_helper.network_client.public_ipaddresses.create_or_update(@resource_group.name, lb_public_ip_name, public_ip)

    #create virtual network
    @resource_helper.create_virtual_network(@resource_group.name)

    #create the load balancer
    lb_name = 'test_lb_name'
    frontend_ip_config_name = 'frontend_ip_config_name'
    backend_address_pool_name = 'backend_address_pool_name'
    load_balancing_rule_name = 'load_balancing_rule_name'
    probe_name = 'probe_name'
    inbound_nat_rule1_name = 'inbound_nat_rule8564'
    inbound_nat_rule2_name = 'inbound_nat_rule8675'

    #populate load_balancer create_or_update parameter
    load_balancer = LoadBalancer.new
    load_balancer.location = @location

    frontend_ip_configuration = FrontendIPConfiguration.new
    frontend_ip_configuration.name = frontend_ip_config_name
    frontend_ip_configuration.public_ipaddress = public_ip
    load_balancer.frontend_ipconfigurations = [frontend_ip_configuration]

    backend_address_pool = BackendAddressPool.new
    backend_address_pool.name = backend_address_pool_name
    load_balancer.backend_address_pools = [backend_address_pool]

    load_balancing_rule = LoadBalancingRule.new
    load_balancing_rule.name = load_balancing_rule_name
    frontend_ip_configuration_sub_resource = MsRestAzure::SubResource.new
    frontend_ip_configuration_sub_resource.id =
        get_child_lb_resource_id(@resource_helper.network_client.subscription_id, @resource_group.name,
                                 lb_name, 'FrontendIPConfigurations', frontend_ip_config_name)
    load_balancing_rule.frontend_ipconfiguration = frontend_ip_configuration_sub_resource
    load_balancing_rule.protocol = 'Tcp'
    load_balancing_rule.frontend_port = 80
    load_balancing_rule.backend_port = 80
    load_balancing_rule.enable_floating_ip = false
    load_balancing_rule.idle_timeout_in_minutes = 15
    backend_address_pool_sub_resource = MsRestAzure::SubResource.new
    backend_address_pool_sub_resource.id =
        get_child_lb_resource_id(@resource_helper.network_client.subscription_id, @resource_group.name,
                                 lb_name, 'backendAddressPools', backend_address_pool_name)
    load_balancing_rule.backend_address_pool = backend_address_pool_sub_resource
    probe_sub_resource = MsRestAzure::SubResource.new
    probe_sub_resource.id =
        get_child_lb_resource_id(@resource_helper.network_client.subscription_id, @resource_group.name,
                                 lb_name, 'probes', probe_name)
    load_balancing_rule.probe = probe_sub_resource

    load_balancer.load_balancing_rules = [load_balancing_rule]

    probe = Probe.new
    probe.name = probe_name
    probe.protocol = 'Http'
    probe.port = 80
    probe.request_path = 'healthcheck.aspx'
    probe.interval_in_seconds = 10
    probe.number_of_probes = 2
    load_balancer.probes = [probe]

    inbound_nat_rule1 = InboundNatRule.new
    inbound_nat_rule1.name = inbound_nat_rule1_name
    inbound_nat_rule1.frontend_ipconfiguration = frontend_ip_configuration_sub_resource
    inbound_nat_rule1.protocol = 'Tcp'
    inbound_nat_rule1.frontend_port = 3389
    inbound_nat_rule1.backend_port = 3389
    inbound_nat_rule1.idle_timeout_in_minutes = 15
    inbound_nat_rule1.enable_floating_ip = false

    inbound_nat_rule2 = InboundNatRule.new
    inbound_nat_rule2.name = inbound_nat_rule2_name
    inbound_nat_rule2.frontend_ipconfiguration = frontend_ip_configuration_sub_resource
    inbound_nat_rule2.protocol = 'Tcp'
    inbound_nat_rule2.frontend_port = 3390
    inbound_nat_rule2.backend_port = 3389
    inbound_nat_rule2.idle_timeout_in_minutes = 15
    inbound_nat_rule2.enable_floating_ip = false

    load_balancer.inbound_nat_rules = [inbound_nat_rule1, inbound_nat_rule2]

    #create load balancer
    result = @client.create_or_update_async(@resource_group.name, lb_name, load_balancer).value!
    expect(result.response.status).to eq(201)
    expect(result.body).not_to be_nil
    expect(result.body.name).to eq(lb_name)
  end

  it 'should work with TCP and UDP balance rules' do
    vnet = @resource_helper.create_virtual_network(@resource_group.name)
    subnet = @resource_helper.create_subnet(vnet, @resource_group, @resource_helper.network_client.subnets)
    params = build_load_balancer_params
    frontend_ip_configuration = FrontendIPConfiguration.new
    params.frontend_ipconfigurations = [frontend_ip_configuration]
    ip_config_name = 'frontend_ip_config'
    frontend_ip_configuration.name = ip_config_name
    frontend_ip_configuration.id = get_child_lb_resource_id(@resource_helper.network_client.subscription_id, @resource_group.name, params.name,'frontendIPConfigurations', ip_config_name)
    frontend_ip_configuration.private_ipallocation_method = 'Dynamic'
    frontend_ip_configuration.subnet = subnet
    udp_rule = LoadBalancingRule.new
    udp_rule.name = 'udp_rule'
    udp_rule.frontend_ipconfiguration = frontend_ip_configuration
    udp_rule.protocol = 'Udp'
    udp_rule.frontend_port = 80
    udp_rule.backend_port = 80
    tcp_rule = LoadBalancingRule.new
    tcp_rule.name = 'tcp_rule'
    tcp_rule.frontend_ipconfiguration = frontend_ip_configuration
    tcp_rule.protocol = 'Tcp'
    tcp_rule.frontend_port = 80
    tcp_rule.backend_port = 80
    params.load_balancing_rules = [udp_rule, tcp_rule]
    inbound_udp = InboundNatRule.new
    inbound_tcp = InboundNatRule.new
    params.inbound_nat_rules = [inbound_udp, inbound_tcp]
    inbound_udp.name = 'inbound_udp_rule'
    inbound_tcp.name = 'inbound_tcp_rule'
    inbound_udp.frontend_ipconfiguration = frontend_ip_configuration
    inbound_tcp.frontend_ipconfiguration = frontend_ip_configuration
    inbound_udp.protocol = 'Udp'
    inbound_tcp.protocol = 'Tcp'
    inbound_udp.frontend_port = 32900
    inbound_tcp.frontend_port = 32900
    inbound_udp.backend_port = 32900
    inbound_tcp.backend_port = 32900
    @client.create_or_update(@resource_group.name, params.name, params)
    result = @client.list_all_async.value!
    expect(result.response.status).to eq(200)
  end

  it 'should get load balancer' do
    load_balancer = create_load_balancer
    result = @client.get_async(@resource_group.name, load_balancer.name).value!
    expect(result.response.status).to eq(200)
    expect(result.body).not_to be_nil
    expect(result.body.name).to eq(load_balancer.name)
  end

  it 'should delete load balancer' do
    load_balancer = create_load_balancer
    result = @client.delete_async(@resource_group.name, load_balancer.name).value!
    expect(result.response.status).to eq(200)
  end

  it 'should list loadbalancers in a subscription' do
    result = @client.list_all_async.value!
    expect(result.response.status).to eq(200)
    expect(result.body).not_to be_nil
    expect(result.body.value).to be_a(Array)
    while !result.body.next_link.nil? && !result.body.next_link.empty? do
      result = @client.list_all_next(result.body.next_link)
      expect(result.body.value).not_to be_nil
      expect(result.body.value).to be_a(Array)
    end
  end

  it 'should list loadbalancers in a resource group' do
    result = @client.list_async(@resource_group.name).value!
    expect(result.response.status).to eq(200)
    expect(result.body).not_to be_nil
    expect(result.body.value).to be_a(Array)
    while !result.body.next_link.nil? && !result.body.next_link.empty? do
      result = @client.list_next(result.body.next_link).value!
      expect(result.body.value).not_to be_nil
      expect(result.body.value).to be_a(Array)
    end
  end

  def get_child_lb_resource_id(subscriptionId, resourceGroupName, lbname, childResourceType, childResourceName)
    "/subscriptions/#{subscriptionId}/resourceGroups/#{resourceGroupName}/providers/Microsoft.Network/loadBalancers/#{lbname}/#{childResourceType}/#{childResourceName}"
  end

  def create_load_balancer
    params = build_load_balancer_params
    @client.create_or_update(@resource_group.name, params.name, params)
  end

  def build_load_balancer_params
    params = LoadBalancer.new
    params.location = @location
    params.name = 'load_balancer_test'
    params
  end
end