diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0a4e41936..f47ebc880 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 @@ -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 @@ -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 @@ -345,7 +345,7 @@ 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 @@ -353,7 +353,6 @@ 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. @@ -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: @@ -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. @@ -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 @@ -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. @@ -812,7 +809,7 @@ 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 @@ -820,7 +817,6 @@ 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' @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/app/controllers/api/v2/discovered_hosts_controller.rb b/app/controllers/api/v2/discovered_hosts_controller.rb index e5e4353d9..996a499c1 100644 --- a/app/controllers/api/v2/discovered_hosts_controller.rb +++ b/app/controllers/api/v2/discovered_hosts_controller.rb @@ -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") @@ -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") param :sp_subnet_id, :number param :model_id, :number param :hostgroup_id, :number diff --git a/app/views/api/v2/discovered_hosts/main.json.rabl b/app/views/api/v2/discovered_hosts/main.json.rabl index ddc55c376..bfde7cfed 100644 --- a/app/views/api/v2/discovered_hosts/main.json.rabl +++ b/app/views/api/v2/discovered_hosts/main.json.rabl @@ -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| diff --git a/test/functional/api/v2/discovered_hosts_controller_test.rb b/test/functional/api/v2/discovered_hosts_controller_test.rb index 682897737..53fc52283 100644 --- a/test/functional/api/v2/discovered_hosts_controller_test.rb +++ b/test/functional/api/v2/discovered_hosts_controller_test.rb @@ -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) @@ -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 } @@ -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') diff --git a/test/test_helper_discovery.rb b/test/test_helper_discovery.rb index 60d1c2bfb..b33ceb977 100644 --- a/test/test_helper_discovery.rb +++ b/test/test_helper_discovery.rb @@ -70,9 +70,11 @@ 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 = { @@ -80,11 +82,11 @@ def setup_hostgroup(host) :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 @@ -92,15 +94,21 @@ def setup_hostgroup(host) 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]