Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPv6 in API & tests #650

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-10-04 07:15:50 UTC using RuboCop version 1.23.0.
# on 2025-01-14 09:07:23 UTC using RuboCop version 1.23.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -143,7 +143,7 @@ Layout/FirstArgumentIndentation:
Layout/FirstArrayElementIndentation:
EnforcedStyle: consistent

# Offense count: 20
# Offense count: 22
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: special_inside_parentheses, consistent, align_braces
Expand All @@ -157,7 +157,7 @@ Layout/FirstHashElementIndentation:
- 'test/functional/discovered_hosts_controller_test.rb'
- 'test/unit/host_discovered_test.rb'

# Offense count: 57
# Offense count: 59
# Cop supports --auto-correct.
# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle.
# SupportedHashRocketStyles: key, separator, table
Expand Down Expand Up @@ -345,15 +345,14 @@ Layout/SpaceInsideParens:
- 'app/helpers/discovered_hosts_helper.rb'
- 'test/unit/discovery_rule_test.rb'

# Offense count: 3
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: final_newline, final_blank_line
Layout/TrailingEmptyLines:
Exclude:
- 'app/services/foreman_discovery/fact_to_category_resolver.rb'
- 'db/migrate/20141126165451_add_discovery_id_to_subnet.rb'
- 'locale/action_names.rb'

# Offense count: 4
# Cop supports --auto-correct.
Expand Down Expand Up @@ -437,7 +436,7 @@ Lint/SendWithMixinArgument:
Exclude:
- 'lib/foreman_discovery/engine.rb'

# Offense count: 6
# Offense count: 5
# Cop supports --auto-correct.
# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments.
Lint/UnusedBlockArgument:
Expand Down Expand Up @@ -471,11 +470,11 @@ Lint/UselessMethodDefinition:
Metrics/AbcSize:
Max: 78

# Offense count: 20
# Offense count: 21
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
# IgnoredMethods: refine
Metrics/BlockLength:
Max: 433
Max: 257

# Offense count: 11
# Configuration parameters: CountComments, CountAsOne.
Expand All @@ -487,17 +486,17 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 20

# Offense count: 48
# Offense count: 49
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Metrics/MethodLength:
Max: 40
Max: 43

# Offense count: 2
# Configuration parameters: Max, CountKeywordArgs.
Metrics/ParameterLists:
MaxOptionalParameters: 4

# Offense count: 5
# Offense count: 4
# Configuration parameters: IgnoredMethods.
Metrics/PerceivedComplexity:
Max: 23
Expand Down Expand Up @@ -793,8 +792,6 @@ Style/ClassAndModuleChildren:
- 'app/services/foreman_discovery/node_api/power_service.rb'
- 'test/functional/api/v2/discovered_hosts_controller_test.rb'
- 'test/functional/api/v2/discovery_rules_controller_test.rb'
- 'app/controllers/concerns/foreman/controller/parameters/discovered_host.rb'
- 'app/controllers/concerns/foreman/controller/parameters/discovery_rule.rb'

# Offense count: 2
# Cop supports --auto-correct.
Expand All @@ -812,15 +809,14 @@ Style/ColonMethodCall:
- 'app/services/foreman_discovery/fact_parser.rb'
- 'test/functional/api/v2/discovered_hosts_controller_test.rb'

# Offense count: 7
# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SingleLineConditionsOnly, IncludeTernaryExpressions.
# SupportedStyles: assign_to_condition, assign_inside_condition
Style/ConditionalAssignment:
Exclude:
- 'app/helpers/discovery_rules_helper.rb'
- 'app/models/host/discovered.rb'
- 'lib/foreman_discovery.rake'
- 'test/functional/discovered_hosts_controller_test.rb'
- 'test/unit/discovered_extensions_test.rb'

Expand Down Expand Up @@ -853,7 +849,7 @@ Style/FormatStringToken:
- 'app/services/foreman_discovery/fact_parser.rb'
- 'app/services/foreman_discovery/node_api/node_resource.rb'

# Offense count: 106
# Offense count: 107
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Expand Down Expand Up @@ -901,7 +897,7 @@ Style/HashEachMethods:
- 'app/services/foreman_discovery/import_hooks/discovery_attribute.rb'
- 'app/services/foreman_discovery/lldp_neighbors.rb'

# Offense count: 1080
# Offense count: 1084
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
Expand Down Expand Up @@ -1097,14 +1093,13 @@ Style/RedundantSelf:
- 'app/models/nic/managed_extensions.rb'
- 'app/services/foreman_discovery/lldp_neighbors.rb'

# Offense count: 5
# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, AllowInnerSlashes.
# SupportedStyles: slashes, percent_r, mixed
Style/RegexpLiteral:
Exclude:
- 'config/routes.rb'
- 'test/functional/discovery_rules_controller_test.rb'
- 'test/unit/managed_extensions_test.rb'

# Offense count: 4
Expand Down Expand Up @@ -1161,7 +1156,7 @@ Style/StringConcatenation:
- 'test/factories/discovery_host_related.rb'
- 'test/unit/discovery_rule_test.rb'

# Offense count: 1000
# Offense count: 1003
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Expand Down Expand Up @@ -1209,7 +1204,7 @@ Style/TrailingCommaInArrayLiteral:
- 'foreman_discovery.gemspec'
- 'test/migrations/20221102075151_migrate_discovery_hostname_and_fact_column_to_array_test.rb'

# Offense count: 32
# Offense count: 35
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyleForMultiline.
# SupportedStylesForMultiline: comma, consistent_comma, no_comma
Expand Down Expand Up @@ -1249,7 +1244,7 @@ Style/ZeroLengthPredicate:
- 'app/services/foreman_discovery/fact_parser.rb'
- 'db/migrate/20150331132115_remove_old_permissions.rb'

# Offense count: 153
# Offense count: 154
# Cop supports --auto-correct.
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/api/v2/discovered_hosts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def create
param :name, String
param :environment_id, String, :desc => N_("required if host is managed and value is not inherited from host group")
param :ip, String, :desc => N_("not required if using a subnet with DHCP proxy")
param :ip6, String, :desc => N_("not required if using an IPv6 subnet with DHCP proxy")
param :mac, String, :desc => N_("not required if it's a virtual machine")
param :architecture_id, :number, :desc => N_("required if host is managed and value is not inherited from host group")
param :domain_id, :number, :desc => N_("required if host is managed and value is not inherited from host group")
Expand All @@ -62,6 +63,7 @@ def create
param :medium_id, String, :desc => N_("required if not imaged based provisioning and host is managed and value is not inherited from host group")
param :ptable_id, :number, :desc => N_("required if host is managed and custom partition has not been defined")
param :subnet_id, :number, :desc => N_("required if host is managed and value is not inherited from host group")
param :subnet6_id, :number, :desc => N_("required if host is managed, does not have IPv4 IP / Subnet, or the value is not inherited from the host group")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it mean does not have IPv4 IP / Subnet?

param :sp_subnet_id, :number
param :model_id, :number
param :hostgroup_id, :number
Expand Down
3 changes: 2 additions & 1 deletion app/views/api/v2/discovered_hosts/main.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ object @discovered_host

extends "api/v2/discovered_hosts/base"

attributes :ip, :mac, :last_report, :subnet_id, :subnet_name, :hardware_model_name, :memory, :disk_count, :disks_size
attributes :ip, :ip6, :mac, :last_report, :subnet_id,
:subnet_name, :hardware_model_name, :memory, :disk_count, :disks_size
attribute :cpu_count => :cpus

node :subnet_name do |host|
Expand Down
34 changes: 34 additions & 0 deletions test/functional/api/v2/discovered_hosts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def switch_controller(klass)
"memorysize_mb" => "42000.42",
"discovery_version" => "3.0.0",
}

@facts6 = @facts.merge({
"ipaddress" => "2001:db8::1",
"ipaddress6_eth0" => "2001:db8::1",
})

set_default_settings
::ForemanDiscovery::NodeAPI::PowerService.any_instance.stubs(:reboot).returns(true)
::ForemanDiscovery::HostConverter.stubs(:unused_ip_for_host)
Expand Down Expand Up @@ -52,6 +58,16 @@ def test_show_host
assert_equal Setting[:discovery_location], show_response["location_name"]
end

def test_show_host_ipv6
host = discover_host_from_facts(@facts6)
get :show, params: { :id => host.id }
assert_response :success

response = ActiveSupport::JSON.decode(@response.body)
assert_equal "macaabbccddeeff", response["name"]
assert_equal @facts6["ipaddress6_eth0"], response["ip6"]
end

def test_delete_discovered_host
host = discover_host_from_facts(@facts)
delete :destroy, params: { :id => host.id }
Expand Down Expand Up @@ -91,6 +107,24 @@ def test_provision_host
assert actual.build?
end

def test_provision_ipv6_host
subnet6 = FactoryBot.create(:subnet_ipv6, :network => "2001:db8::")
host = discover_host_from_facts(@facts6)
hostgroup = setup_hostgroup(host, subnets: { subnet6: subnet6 })

put :update, params: {
id: host.id,
discovered_host: {
hostgroup_id: hostgroup.id,
build: true
}
}

assert_response :success
actual = Host.unscoped.find(host.id)
assert actual.build?
end

def test_provision_host_without_build_flag
FactoryBot.create(:organization, :name => 'SomeOrg')
FactoryBot.create(:location, :name => 'SomeLoc')
Expand Down
22 changes: 15 additions & 7 deletions test/test_helper_discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,45 @@ def set_default_settings
Setting['discovery_auto_bond'] = false
end

def setup_hostgroup(host)
def setup_hostgroup(host, subnets: nil)
domain = FactoryBot.create(:domain)
subnet = FactoryBot.create(:subnet_ipv4, :network => "192.168.100.0")
subnets ||= {
subnet: FactoryBot.create(:subnet_ipv4, :network => "192.168.100.0")
}
medium = FactoryBot.create(:medium, :organizations => [host.organization], :locations => [host.location])
os = FactoryBot.create(:operatingsystem, :with_ptables, :with_archs, :media => [medium])
args = {
:operatingsystem => os,
:architecture => os.architectures.first,
:ptable => os.ptables.first,
:medium => os.media.first,
:subnet => subnet,
:domain => domain,
:organizations => [host.organization],
:locations => [host.location]
}
}.merge(subnets)

if defined?(ForemanPuppet)
environment = FactoryBot.create(:environment, :organizations => [host.organization], :locations => [host.location])
args[:environment] = environment
hostgroup = FactoryBot.create(:hostgroup, :with_rootpass, :with_puppet_enc, **args)
else
hostgroup = FactoryBot.create(:hostgroup, :with_rootpass, **args)
end
domain.subnets << hostgroup.subnet

hostgroup_subnets = [hostgroup.subnet6, hostgroup.subnet].compact
domain.subnets << hostgroup_subnets
hostgroup.medium.organizations |= [host.organization]
hostgroup.medium.locations |= [host.location]
hostgroup.ptable.organizations |= [host.organization]
hostgroup.ptable.locations |= [host.location]
hostgroup.domain.organizations |= [host.organization]
hostgroup.domain.locations |= [host.location]
hostgroup.subnet.organizations |= [host.organization]
hostgroup.subnet.locations |= [host.location]

hostgroup_subnets.each do |subnet|
subnet.organizations |= [host.organization]
subnet.locations |= [host.location]
end

if defined?(ForemanPuppet)
hostgroup.environment.organizations |= [host.organization]
hostgroup.environment.locations |= [host.location]
Expand Down
Loading