require 'spec_helper' require 'ionoscloud_networkloadbalancer_get' Chef::Knife::IonoscloudNetworkloadbalancerGet.load_deps describe Chef::Knife::IonoscloudNetworkloadbalancerGet do before :each do subject { Chef::Knife::IonoscloudNetworkloadbalancerGet.new } allow(subject).to receive(:puts) allow(subject).to receive(:print) end describe '#run' do it 'should call NetworkInterfacesApi.datacenters_servers_nics_find_by_id' do network_loadbalancer = network_loadbalancer_mock subject_config = { ionoscloud_username: 'email', ionoscloud_password: 'password', datacenter_id: 'datacenter_id', network_loadbalancer_id: network_loadbalancer.id, yes: true, } subject_config.each { |key, value| subject.config[key] = value } expect(subject).to receive(:puts).with("ID: #{network_loadbalancer.id}") expect(subject).to receive(:puts).with("Name: #{network_loadbalancer.properties.name}") expect(subject).to receive(:puts).with("Listener LAN: #{network_loadbalancer.properties.listener_lan}") expect(subject).to receive(:puts).with("IPS: #{network_loadbalancer.properties.ips}") expect(subject).to receive(:puts).with("Target LAN: #{network_loadbalancer.properties.target_lan}") expect(subject).to receive(:puts).with("Private IPS: #{network_loadbalancer.properties.lb_private_ips}") expect(subject).to receive(:puts).with("Forwarding Rules: #{network_loadbalancer.entities.forwardingrules.items.map do |rule| { id: rule.id, name: rule.properties.name, algorithm: rule.properties.algorithm, protocol: rule.properties.protocol, listener_ip: rule.properties.listener_ip, listener_port: rule.properties.listener_port, health_check: rule.properties.health_check.nil? ? nil : rule.properties.health_check.to_hash, targets: (rule.properties.targets.nil? ? [] : rule.properties.targets.map { |target| target.to_hash }), } end}") expect(subject).to receive(:puts).with("Flowlogs: #{network_loadbalancer.entities.flowlogs.items.map { |flowlog| flowlog.id }}") expect(subject.api_client).not_to receive(:wait_for) mock_call_api( subject, [ { method: 'GET', path: "/datacenters/#{subject_config[:datacenter_id]}/networkloadbalancers/#{subject_config[:network_loadbalancer_id]}", operation: :'NetworkLoadBalancersApi.datacenters_networkloadbalancers_find_by_network_load_balancer_id', return_type: 'NetworkLoadBalancer', result: network_loadbalancer, }, ], ) expect { subject.run }.not_to raise_error(Exception) end it 'should not make any call if any required option is missing' do required_options = subject.instance_variable_get(:@required_options) arrays_without_one_element(required_options).each do |test_case| test_case[:array].each { |value| subject.config[value] = 'test' } expect(subject).to receive(:puts).with("Missing required parameters #{test_case[:removed]}") expect(subject.api_client).not_to receive(:call_api) expect { subject.run }.to raise_error(SystemExit) do |error| expect(error.status).to eq(1) end required_options.each { |value| subject.config[value] = nil } end end end end