<commit_before>require 'spec_helper'

describe Chef::Resource::AwsRouteTable do
  extend AWSSupport

  when_the_chef_12_server "exists", organization: 'foo', server_scope: :context do
    with_aws "with a VPC with an internet gateway" do
      aws_vpc "test_vpc" do
        cidr_block '10.0.0.0/24'
        internet_gateway true
      end

      it "aws_route_table 'test_route_table' with no parameters except VPC creates a route table" do
        expect_recipe {
          aws_route_table 'test_route_table' do
            vpc 'test_vpc'
          end
        }.to create_an_aws_route_table('test_route_table',
          routes: [
            { destination_cidr_block: '10.0.0.0/24', gateway_id: 'local', state: "active" }
          ]
        ).and be_idempotent
      end

      it "aws_route_table 'test_route_table' with routes creates a route table" do
        expect_recipe {
          aws_route_table 'test_route_table' do
            vpc 'test_vpc'
            routes '0.0.0.0/0' => :internet_gateway
          end
        }.to create_an_aws_route_table('test_route_table',
          routes: [
            { destination_cidr_block: '10.0.0.0/24', gateway_id: 'local', state: "active" },
            { destination_cidr_block: '0.0.0.0/0', gateway_id: test_vpc.aws_object.internet_gateway.id, state: "active" }
          ]
        ).and be_idempotent
      end

      it "ignores routes whose target matches ignore_route_targets" do
        eni = nil
        expect_recipe {
            aws_subnet 'test_subnet' do
              vpc 'test_vpc'
            end

            eni = aws_network_interface 'test_network_interface' do
              subnet 'test_subnet'
            end

            aws_route_table 'test_route_table' do
              vpc 'test_vpc'
              routes(
                '0.0.0.0/0' => :internet_gateway,
                '172.31.0.0/16' => eni
              )
            end

            aws_route_table 'test_route_table' do
              vpc 'test_vpc'
              routes '0.0.0.0/0' => :internet_gateway
              ignore_route_targets ['^eni-']
            end
          }.to create_an_aws_route_table('test_route_table',
            routes: [
              { destination_cidr_block: '10.0.0.0/24', gateway_id: 'local', state: "active" },
              { destination_cidr_block: '172.31.0.0/16', network_interface_id: eni.aws_object.id, state: "blackhole" },
              { destination_cidr_block: '0.0.0.0/0', gateway_id: test_vpc.aws_object.internet_gateway.id, state: "active" },
            ]
          ).and be_idempotent
      end

      it "creates aws_route_table tags" do
        expect_recipe {
          aws_route_table 'test_route_table' do
            vpc 'test_vpc'
            aws_tags key1: "value"
          end
        }.to create_an_aws_route_table('test_route_table')
        .and have_aws_route_table_tags('test_route_table',
          {
            'Name' => 'test_route_table',
            'key1' => 'value'
          }
        ).and be_idempotent
      end

      it "updates an existing routing table" do
        expect_recipe {
          aws_route_table 'test_route_table' do
            vpc 'test_vpc'
            routes '0.0.0.0/0' => :internet_gateway
          end
        }.to create_an_aws_route_table('test_route_table',
          routes: [
            { destination_cidr_block: '10.0.0.0/24', gateway_id: 'local', state: "active" },
            { destination_cidr_block: '0.0.0.0/0', gateway_id: test_vpc.aws_object.internet_gateway.id, state: "active" },
          ]
        ).and be_idempotent

        expect_recipe {
          aws_route_table 'test_route_table' do
            vpc 'test_vpc'
            routes '0.0.0.0/0' => :internet_gateway,
                   '10.1.0.0/24' => :internet_gateway
          end
        }.to update_an_aws_route_table('test_route_table',
          routes: [
            { destination_cidr_block: '10.1.0.0/24', gateway_id: test_vpc.aws_object.internet_gateway.id, state: "active" },
            { destination_cidr_block: '10.0.0.0/24', gateway_id: 'local', state: "active" },
            { destination_cidr_block: '0.0.0.0/0', gateway_id: test_vpc.aws_object.internet_gateway.id, state: "active" },
          ]
        ).and be_idempotent
      end

      context "with machines" do
        purge_all
        setup_public_vpc

        it "can route to a machine", :super_slow do
          test_machine_aws_obj = nil
          expect_recipe {
            machine 'test_machine' do
              machine_options bootstrap_options: {
                subnet_id: 'test_public_subnet',
                key_name: 'test_key_pair'
              }
              action :ready # The box has to be online for AWS to accept it as routable
            end

            ruby_block 'look up test machine' do
              block do
                test_machine_aws_obj = Chef::Resource::AwsInstance.get_aws_object(
                  'test_machine',
                  run_context: run_context,
                  driver: run_context.chef_provisioning.current_driver,
                  managed_entry_store: Chef::Provisioning.chef_managed_entry_store(run_context.cheffish.current_chef_server)
                )
              end
            end
          }.to create_an_aws_instance('test_machine')

          expect_recipe {
            aws_route_table 'test_route_table' do
              vpc 'test_vpc'
              routes '0.0.0.0/0'   => :internet_gateway,
                     '10.1.0.0/16' => 'test_machine'
            end

          }.to create_an_aws_route_table('test_route_table',
            routes: [
                { destination_cidr_block: '10.0.0.0/24', gateway_id: 'local', state: "active" },
                { destination_cidr_block: '10.1.0.0/16', instance_id: test_machine_aws_obj.id, state: "active" },
                { destination_cidr_block: '0.0.0.0/0', gateway_id: test_vpc.aws_object.internet_gateway.id, state: "active" },
              ]<commit_msg>revise specs per feedback from @tyler-ball<commit_after>require 'spec_helper'

describe Chef::Resource::AwsRouteTable do
  extend AWSSupport

  when_the_chef_12_server "exists", organization: 'foo', server_scope: :context do
    with_aws "with a VPC with an internet gateway" do
      aws_vpc "test_vpc" do
        cidr_block '10.0.0.0/24'
        internet_gateway true
      end

      it "aws_route_table 'test_route_table' with no parameters except VPC creates a route table" do
        expect_recipe {
          aws_route_table 'test_route_table' do
            vpc 'test_vpc'
          end
        }.to create_an_aws_route_table('test_route_table',
          routes: [
            { destination_cidr_block: '10.0.0.0/24', gateway_id: 'local', state: "active" }
          ]
        ).and be_idempotent
      end

      it "aws_route_table 'test_route_table' with routes creates a route table" do
        expect_recipe {
          aws_route_table 'test_route_table' do
            vpc 'test_vpc'
            routes '0.0.0.0/0' => :internet_gateway
          end
        }.to create_an_aws_route_table('test_route_table',
          routes: [
            { destination_cidr_block: '10.0.0.0/24', gateway_id: 'local', state: "active" },
            { destination_cidr_block: '0.0.0.0/0', gateway_id: test_vpc.aws_object.internet_gateway.id, state: "active" }
          ]
        ).and be_idempotent
      end

      it "ignores routes whose target matches ignore_route_targets" do
        eni = nil
        expect_recipe {
            aws_subnet 'test_subnet' do
              vpc 'test_vpc'
            end

            eni = aws_network_interface 'test_network_interface' do
              subnet 'test_subnet'
            end

            aws_route_table 'test_route_table' do
              vpc 'test_vpc'
              routes(
                '0.0.0.0/0' => :internet_gateway,
                '172.31.0.0/16' => eni
              )
            end

            aws_route_table 'test_route_table' do
              vpc 'test_vpc'
              routes '0.0.0.0/0' => :internet_gateway
              ignore_route_targets ['^eni-']
            end
          }.to create_an_aws_route_table('test_route_table',
            routes: [
              { destination_cidr_block: '10.0.0.0/24', gateway_id: 'local', state: "active" },
              { destination_cidr_block: '172.31.0.0/16', network_interface_id: eni.aws_object.id, state: "blackhole" },
              { destination_cidr_block: '0.0.0.0/0', gateway_id: test_vpc.aws_object.internet_gateway.id, state: "active" },
            ]
          ).and be_idempotent
      end

      it "creates aws_route_table tags" do
        expect_recipe {
          aws_route_table 'test_route_table' do
            vpc 'test_vpc'
            aws_tags key1: "value"
          end
        }.to create_an_aws_route_table('test_route_table')
        .and have_aws_route_table_tags('test_route_table',
          {
            'Name' => 'test_route_table',
            'key1' => 'value'
          }
        ).and be_idempotent
      end

      context "with an existing routing table" do
        aws_route_table 'test_route_table' do
          vpc 'test_vpc'
          routes '0.0.0.0/0' => :internet_gateway
        end

        it "updates an existing routing table" do
          expect_recipe {
            aws_route_table 'test_route_table' do
              vpc 'test_vpc'
              routes '0.0.0.0/0' => :internet_gateway,
                    '10.1.0.0/24' => :internet_gateway
            end
          }.to update_an_aws_route_table('test_route_table',
            routes: [
              { destination_cidr_block: '10.1.0.0/24', gateway_id: test_vpc.aws_object.internet_gateway.id, state: "active" },
              { destination_cidr_block: '10.0.0.0/24', gateway_id: 'local', state: "active" },
              { destination_cidr_block: '0.0.0.0/0', gateway_id: test_vpc.aws_object.internet_gateway.id, state: "active" },
            ]
          ).and be_idempotent
        end
      end

      context "with machines", :super_slow do
        purge_all
        setup_public_vpc

        machine 'test_machine' do
          machine_options bootstrap_options: {
            subnet_id: 'test_public_subnet',
            key_name: 'test_key_pair'
          }
          action :ready # The box has to be online for AWS to accept it as routable
        end

        it "can route to a machine", :super_slow do
          test_machine_aws_obj = nil
          expect_recipe {
            ruby_block 'look up test machine' do
              block do
                test_machine_aws_obj = Chef::Resource::AwsInstance.get_aws_object(
                  'test_machine',
                  run_context: run_context,
                  driver: run_context.chef_provisioning.current_driver,
                  managed_entry_store: Chef::Provisioning.chef_managed_entry_store(run_context.cheffish.current_chef_server)
                )
              end
            end
          }

          expect_recipe {
            aws_route_table 'test_route_table' do
              vpc 'test_vpc'
              routes '0.0.0.0/0'   => :internet_gateway,
                     '10.1.0.0/16' => 'test_machine'
            end

          }.to create_an_aws_route_table('test_route_table',
            routes: [
                { destination_cidr_block: '10.0.0.0/24', gateway_id: 'local', state: "active" },
                { destination_cidr_block: '10.1.0.0/16', instance_id: test_machine_aws_obj.id, state: "active" },
                { destination_cidr_block: '0.0.0.0/0', gateway_id: test_vpc.aws_object.internet_gateway.id, state: "active" },
              ]
{'fraction_non_alphanumeric': 0.09684065934065934, 'fraction_numerical': 0.025412087912087912, 'mean_word_length': 1.855847021328757, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 46, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1201230', 'n_tokens_mistral': 3902, 'n_tokens_neox': 3750, 'n_words': 720}