<commit_before>'use strict';

/**
 * Create a CloudFormation template with the following:
 *  + Two EC2 instances
 *  + One ElastiCache cluster with a single node
 *  + An Elastic Load Balancer
 *  + Two security groups
 *      - One for the EC2 instances
 *      - One for the ElastiCache cluster
 *  + A VPC to which the EC2 instances belong (and therefore do NOT use the security group)
 *
 *  Save it out to a template, and then read it back in, asserting that the
 *  loaded template equals the one initially created. Then modify the new
 *  template, and assert that the original values are no longer present in the
 *  modified version.
 **/

var Template = require('../lib/Template.js');
var Validator = require('../lib/Validator.js');
var filePath = '/tmp/modify_cf_test.json';
var t = new Template();

exports.createOriginalTemplate = function(test){

    t.strParam('EC2InstanceType', 'c3.large', 'Type of EC2 Instance to launch')
        .allowedValues([
            'c3.large',
            'c3.xlarge',
            'c3.2xlarge',
            'c3.4xlarge',
            'c3.8xlarge'
        ]);
    t.strParam('EC2InstanceAMI', 'ami-6aff5e02', 'AMI Instance ID to use for the EC2 instances');
    t.strParam('KeyName', null, 'Name of the EC2 Keypair to enable SSH access to the boxes');
    t.strParam('OpenCidrIp', '0.0.0.0/0');

    t.elastiCacheCluster('TestECC')
        .autoMinorVersionUpgrade('true')
        .cacheNodeType('cache.m3.medium')
        .engine('memcached')
        .numCacheNodes(1);

    var webServerPort = 80;
    t.numParam('RDPPort', 3389);
    t.numParam('WebServerPort', webServerPort);

    // Create EC2 Instances
    var instanceIds = [];
    for(var i=0; i<2; i++){
        instanceIds.push('loadBalacnedEc2Instance' + i);
    }

   for(var j in instanceIds){
        t.ec2Instance(instanceIds[j])
            .keyName( t.ref('KeyName') )
            .imageId( t.ref('EC2InstanceAMI') )
            .instanceType( t.ref('EC2InstanceType') );
            //.securityGroups( t.ref('InstanceSecurityGroup') );
    }

    // Create the load balancer (for the ec2 instances)
    var lbHealth = {
        'Target': 'HTTP:' + webServerPort + '/',
        'HealthyThreshold': '3',
        'UnhealthyThreshold': '5',
        'Interval': '30',
        'Timeout': '5'
    };
    t.loadBalancer('ec2LoadBalancer')
        .availabilityZones()
        .healthCheck(lbHealth)
        .instances(instanceIds)
        .listeners({
          'LoadBalancerPort' : '80',
          'InstancePort' : { 'Ref' : 'WebServerPort' },
          'Protocol' : 'HTTP'
        });

    t.save(filePath);

    // Validate the template
    test.expect(2);

    function validationCallback(isValid, message){
        if(!isValid){
            console.log(message);
        }
        test.ok(isValid);
        test.ok(!!message);
        test.done();
    }

    var v = new Validator(filePath);<commit_msg>Teamplate loading test now tests deep equality of entire template<commit_after>'use strict';

/**
 * Create a CloudFormation template with the following:
 *  + Two EC2 instances
 *  + One ElastiCache cluster with a single node
 *  + An Elastic Load Balancer
 *  + Two security groups
 *      - One for the EC2 instances
 *      - One for the ElastiCache cluster
 *  + A VPC to which the EC2 instances belong (and therefore do NOT use the security group)
 *
 *  Save it out to a template, and then read it back in, asserting that the
 *  loaded template equals the one initially created. Then modify the new
 *  template, and assert that the original values are no longer present in the
 *  modified version.
 **/

var Template = require('../lib/Template.js');
var Validator = require('../lib/Validator.js');
var filePath = '/tmp/modify_cf_test.json';
var t = new Template();

exports.createOriginalTemplate = function(test){

    // Create Parameters
    t.strParam('CacheType', 'cache.m3.medium', 'Type of cache instance to add to the ElastiCache cluster');
    t.strParam('EC2InstanceType', 'c3.large', 'Type of EC2 Instance to launch');
    t.strParam('EC2InstanceAMI', 'ami-6aff5e02', 'AMI Instance ID to use for the EC2 instances');
    t.strParam('KeyName', null, 'Name of the EC2 Keypair to enable SSH access to the boxes');
    t.strParam('OpenCidrIp', '0.0.0.0/0');

    var webServerPort = 80;
    t.numParam('RDPPort', 3389);
    t.numParam('WebServerPort', webServerPort);

    // Create EC2 Instances
    var instanceIds = [];
    for(var i=0; i<2; i++){
        instanceIds.push('loadBalacnedEc2Instance' + i);
    }

    for(var j in instanceIds){
        t.ec2Instance(instanceIds[j])
            .keyName( t.ref('KeyName') )
            .imageId( t.ref('EC2InstanceAMI') )
            .instanceType( t.ref('EC2InstanceType') )
            .securityGroups( t.ref('InstanceSecurityGroup') );
    }

    // Create the load balancer (for the ec2 instances)
    var lbHealth = {
        'Target': 'HTTP:' + webServerPort + '/',
        'HealthyThreshold': '3',
        'UnhealthyThreshold': '5',
        'Interval': '30',
        'Timeout': '5'
    };
    t.loadBalancer('ec2LoadBalancer')
        .availabilityZones()
        .healthCheck(lbHealth)
        .instances(instanceIds)
        .listeners({
          'LoadBalancerPort' : '80',
          'InstancePort' : { 'Ref' : 'WebServerPort' },
          'Protocol' : 'HTTP'
        });

    // Create Elasticache Cluster
    var eccName = 'TestECC';
    t.elastiCacheCluster(eccName)
        .autoMinorVersionUpgrade('true')
        .cacheNodeType( t.ref('CacheType') )
        .clusterName(eccName)
        .engine('memcached')
        .numCacheNodes(1)
        .vpcSecurityGroupIds( t.fnGetAtt('CacheSecurityGroup', 'GroupId') );

    // Create Security Groups
    t.securityGroup('InstanceSecurityGroup', 'Security Group for the EC2 Instances')
        .tcpIn({ 'CidrIp': t.ref('OpenCidrIp')}, t.ref('WebServerPort'), t.ref('RDPPort'));
    t.securityGroup('CacheSecurityGroup', 'Security Group for the ElastiCache Cluster')
        .tcpIn({'SourceSecurityGroupName': t.ref('InstanceSecurityGroup')}, 11211);

    t.save(filePath);

    // Validate the template
    test.expect(2);

    function validationCallback(isValid, message){
        if(!isValid){
            console.log(message);
        }
        test.ok(isValid);
        test.ok(!!message);
        test.done();
    }

    var v = new Validator(filePath);
{'fraction_non_alphanumeric': 0.13173933427097984, 'fraction_numerical': 0.01562744178777934, 'mean_word_length': 2.6823935558112773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2472687', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1948, 'n_words': 522}