<commit_before>require_relative '../aws_helper'
require_relative '../aws_stubs'
require_relative '../aws_refresher_spec_common'

describe ManageIQ::Providers::Amazon::NetworkManager::Refresher do
  include AwsRefresherSpecCommon
  include AwsStubs

  describe "refresh" do
    before do
      _guid, _server, zone = EvmSpecHelper.create_guid_miq_server_zone
      @ems                 = FactoryGirl.create(:ems_amazon, :zone => zone)
      @ems.update_authentication(:default => {:userid => "0123456789", :password => "<PASSWORD>"})
      EvmSpecHelper.local_miq_server(:zone => Zone.seed)
    end

    (AwsRefresherSpecCommon::ALL_GRAPH_REFRESH_SETTINGS + AwsRefresherSpecCommon::ALL_OLD_REFRESH_SETTINGS
    ).each do |settings|
      context "with settings #{settings}" do
        before :each do
          stub_refresh_settings(
            settings.merge(
              :get_private_images => true,
              :get_shared_images  => false,
              :get_public_images  => false
            )
          )
          @inventory_object_settings = settings
        end

        it "2 refreshes, first creates all entities, second updates all entitites" do
          2.times do
            # Make sure we don't do any delete&create instead of update
            # TODO(lsmola) :inventory_object_refresh => false is doing some non allowed deletes, investigate
            assert_do_not_delete if @inventory_object_settings[:inventory_object_refresh]

            refresh_spec
          end
        end

        it "2 refreshes, first creates all entities, second updates exiting and deletes missing entitites" do
          @data_scaling         = 2
          @disconnect_inv_count = 0
          2.times do
            refresh_spec
            @data_scaling         -= 1
            @disconnect_inv_count += 1
          end
        end

        it "2 refreshes, first creates all entities, second updates existing and creates new entitites" do
          @data_scaling = 1
          2.times do
            # Make sure we don't do any delete&create instead of update
            # TODO(lsmola) :inventory_object_refresh => false is doing some non allowed deletes, investigate
            assert_do_not_delete if @inventory_object_settings[:inventory_object_refresh]

            refresh_spec
            @data_scaling += 1
          end
        end

        it "2 refreshes, first creates all entities, second deletes all entitites" do
          @data_scaling         = 1
          @disconnect_inv_count = 0

          2.times do
            refresh_spec
            @data_scaling         -= 1
            @disconnect_inv_count += 1
          end
        end

        it "2 refreshes, first creates all entities, second deletes all entitites from db" do
          # This spec verifies that all disconnected entities clean up correctly
          @data_scaling = 1

          2.times do
            refresh_spec do
              VmOrTemplate.all.map(&:destroy) if @data_scaling == 0
            end
            @data_scaling -= 1
          end
        end
      end
    end
  end

  def refresh_spec
    @ems.reload

    with_aws_stubbed(stub_responses) do
      EmsRefresh.refresh(@ems)
    end

    @ems.reload

    yield if block_given?

    assert_table_counts
    assert_ems
  end

  def stub_responses
    {
      :ec2            => {
        :describe_regions            => mocked_regions,
        :describe_availability_zones => mocked_availability_zones,
        :describe_instances          => mocked_instances,
        :describe_key_pairs          => mocked_key_pairs,
        :describe_images             => mocked_images,
      },
      :cloudformation => {
        :describe_stacks      => mocked_stacks,
        :list_stack_resources => mocked_stack_resources
      }
    }
  end

  def expected_table_counts(disconnect = nil)
    disconnect  ||= disconnect_inv_factor
    vm_count    = test_counts[:instance_vpc_count] + test_counts[:instance_ec2_count]
    image_count = test_counts[:image_count]

    # We have 3 custom_attributes per each vm + custom attributes of disconnected vm
    custom_attribute_count = (test_counts[:instance_vpc_count] * 3 + test_counts[:instance_ec2_count] * 3 +
      test_counts[:image_count] * 3)
    custom_attribute_count += disconnect * (test_counts(1)[:instance_vpc_count] * 3 +
      test_counts(1)[:instance_ec2_count] * 3 + test_counts(1)[:image_count] * 3)

    # Disconnect_inv count, when these objects are not found in the API, they are not deleted in DB, but just marked
    # as disconnected
    vm_count_plus_disconnect_inv    = vm_count + disconnect * (test_counts(1)[:instance_vpc_count] +
      test_counts(1)[:instance_ec2_count])
    image_count_plus_disconnect_inv = image_count + disconnect * test_counts(1)[:image_count]

    {
      :auth_private_key                  => test_counts[:key_pair_count],
      :ext_management_system             => expected_ext_management_systems_count,
      # TODO(lsmola) collect all flavors for original refresh
      :flavor                            => @inventory_object_settings[:inventory_object_refresh] ? 82 : 80,
      :availability_zone                 => 5,
      :vm_or_template                    => vm_count_plus_disconnect_inv + image_count_plus_disconnect_inv,
      :vm                                => vm_count_plus_disconnect_inv,
      :miq_template                      => image_count_plus_disconnect_inv,
      :disk                              => vm_count_plus_disconnect_inv,
      :guest_device                      => 0,
      :hardware                          => vm_count_plus_disconnect_inv + image_count_plus_disconnect_inv,
      :network                           => vm_count_plus_disconnect_inv * 2,
      :operating_system                  => 0,
      :snapshot                          => 0,
      :system_service                    => 0,
      :relationship                      => vm_count_plus_disconnect_inv + image_count_plus_disconnect_inv,
      # :miq_queue                         => 2,
      :orchestration_template            => 1,<commit_msg>Fix stubbed specs to have also disconnected OperatingSystem<commit_after>require_relative '../aws_helper'
require_relative '../aws_stubs'
require_relative '../aws_refresher_spec_common'

describe ManageIQ::Providers::Amazon::NetworkManager::Refresher do
  include AwsRefresherSpecCommon
  include AwsStubs

  describe "refresh" do
    before do
      _guid, _server, zone = EvmSpecHelper.create_guid_miq_server_zone
      @ems                 = FactoryGirl.create(:ems_amazon, :zone => zone)
      @ems.update_authentication(:default => {:userid => "0123456789", :password => "<PASSWORD>"})
      EvmSpecHelper.local_miq_server(:zone => Zone.seed)
    end

    (AwsRefresherSpecCommon::ALL_GRAPH_REFRESH_SETTINGS + AwsRefresherSpecCommon::ALL_OLD_REFRESH_SETTINGS
    ).each do |settings|
      context "with settings #{settings}" do
        before :each do
          stub_refresh_settings(
            settings.merge(
              :get_private_images => true,
              :get_shared_images  => false,
              :get_public_images  => false
            )
          )
          @inventory_object_settings = settings
        end

        it "2 refreshes, first creates all entities, second updates all entitites" do
          2.times do
            # Make sure we don't do any delete&create instead of update
            # TODO(lsmola) :inventory_object_refresh => false is doing some non allowed deletes, investigate
            assert_do_not_delete if @inventory_object_settings[:inventory_object_refresh]

            refresh_spec
          end
        end

        it "2 refreshes, first creates all entities, second updates exiting and deletes missing entitites" do
          @data_scaling         = 2
          @disconnect_inv_count = 0
          2.times do
            refresh_spec
            @data_scaling         -= 1
            @disconnect_inv_count += 1
          end
        end

        it "2 refreshes, first creates all entities, second updates existing and creates new entitites" do
          @data_scaling = 1
          2.times do
            # Make sure we don't do any delete&create instead of update
            # TODO(lsmola) :inventory_object_refresh => false is doing some non allowed deletes, investigate
            assert_do_not_delete if @inventory_object_settings[:inventory_object_refresh]

            refresh_spec
            @data_scaling += 1
          end
        end

        it "2 refreshes, first creates all entities, second deletes all entitites" do
          @data_scaling         = 1
          @disconnect_inv_count = 0

          2.times do
            refresh_spec
            @data_scaling         -= 1
            @disconnect_inv_count += 1
          end
        end

        it "2 refreshes, first creates all entities, second deletes all entitites from db" do
          # This spec verifies that all disconnected entities clean up correctly
          @data_scaling = 1

          2.times do
            refresh_spec do
              VmOrTemplate.all.map(&:destroy) if @data_scaling == 0
            end
            @data_scaling -= 1
          end
        end
      end
    end
  end

  def refresh_spec
    @ems.reload

    with_aws_stubbed(stub_responses) do
      EmsRefresh.refresh(@ems)
    end

    @ems.reload

    yield if block_given?

    assert_table_counts
    assert_ems
  end

  def stub_responses
    {
      :ec2            => {
        :describe_regions            => mocked_regions,
        :describe_availability_zones => mocked_availability_zones,
        :describe_instances          => mocked_instances,
        :describe_key_pairs          => mocked_key_pairs,
        :describe_images             => mocked_images,
      },
      :cloudformation => {
        :describe_stacks      => mocked_stacks,
        :list_stack_resources => mocked_stack_resources
      }
    }
  end

  def expected_table_counts(disconnect = nil)
    disconnect  ||= disconnect_inv_factor
    vm_count    = test_counts[:instance_vpc_count] + test_counts[:instance_ec2_count]
    image_count = test_counts[:image_count]

    # We have 3 custom_attributes per each vm + custom attributes of disconnected vm
    custom_attribute_count = (test_counts[:instance_vpc_count] * 3 + test_counts[:instance_ec2_count] * 3 +
      test_counts[:image_count] * 3)
    custom_attribute_count += disconnect * (test_counts(1)[:instance_vpc_count] * 3 +
      test_counts(1)[:instance_ec2_count] * 3 + test_counts(1)[:image_count] * 3)

    # Disconnect_inv count, when these objects are not found in the API, they are not deleted in DB, but just marked
    # as disconnected
    vm_count_plus_disconnect_inv    = vm_count + disconnect * (test_counts(1)[:instance_vpc_count] +
      test_counts(1)[:instance_ec2_count])
    image_count_plus_disconnect_inv = image_count + disconnect * test_counts(1)[:image_count]

    {
      :auth_private_key                  => test_counts[:key_pair_count],
      :ext_management_system             => expected_ext_management_systems_count,
      # TODO(lsmola) collect all flavors for original refresh
      :flavor                            => @inventory_object_settings[:inventory_object_refresh] ? 82 : 80,
      :availability_zone                 => 5,
      :vm_or_template                    => vm_count_plus_disconnect_inv + image_count_plus_disconnect_inv,
      :vm                                => vm_count_plus_disconnect_inv,
      :miq_template                      => image_count_plus_disconnect_inv,
      :disk                              => vm_count_plus_disconnect_inv,
      :guest_device                      => 0,
      :hardware                          => vm_count_plus_disconnect_inv + image_count_plus_disconnect_inv,
      :network                           => vm_count_plus_disconnect_inv * 2,
      :operating_system                  => vm_count_plus_disconnect_inv + image_count_plus_disconnect_inv,
      :snapshot                          => 0,
      :system_service                    => 0,
      :relationship                      => vm_count_plus_disconnect_inv + image_count_plus_disconnect_inv,
      # :miq_queue                         => 2,
      :orchestration_template            => 1,
{'fraction_non_alphanumeric': 0.06935523213269372, 'fraction_numerical': 0.010163427920969184, 'mean_word_length': 2.106060606060606, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4760459', 'n_tokens_mistral': 3622, 'n_tokens_neox': 3409, 'n_words': 783}