示例#1
0
    def test_simulate(self):
        ec2_1 = self.__buld_instance({
            'Name': 'Srv1',
            'InstanceType': 't1.micro'
        })
        ec2_2 = self.__buld_instance({
            'Name': 'Srv2',
            'InstanceType': 'r3.large'
        })

        ri1 = {'InstanceType': 't1.micro', 'InstanceCount': 2}

        simulator = EC2ReservedInstanceSimulator()
        simulator.set_ec2([ec2_1, ec2_2])
        simulator.set_ri([ri1])
        simulator.simulate()

        match_ec2 = simulator.list_match_ec2()
        self.assertEqual('t1.micro', match_ec2[0]['InstanceType'])
        self.assertEqual('Linux/UNIX', match_ec2[0]['Platform'])

        unmatch_ec2 = simulator.list_unmatch_ec2()
        self.assertEqual('r3.large', unmatch_ec2[0]['InstanceType'])
        self.assertEqual('Linux/UNIX', unmatch_ec2[0]['Platform'])

        unmatch_ri = simulator.list_unmatch_ri()
        self.assertEqual('t1.micro', unmatch_ri[0]['InstanceType'])
        self.assertEqual('Linux/UNIX', unmatch_ri[0]['ProductDescription'])
        self.assertEqual(1, unmatch_ri[0]['InstanceCount'])
示例#2
0
    def test_simulate_ri_applied_to_running_instance(self):
        ec2_1 = self.__buld_instance({
            'Name': 'Stopped',
            'State': {
                'Code': 80,
                'Name': 'stopped'
            }
        })
        ec2_2 = self.__buld_instance({
            'Name': 'Running',
            'State': {
                'Code': 16,
                'Name': 'running'
            }
        })

        ri = {'InstanceType': 't1.micro', 'InstanceCount': 1}

        simulator = EC2ReservedInstanceSimulator()
        simulator.set_ec2([ec2_1, ec2_2])
        simulator.set_ri([ri])
        simulator.simulate()

        match_ec2 = simulator.list_match_ec2()
        self.assertEqual('Running', match_ec2[0]['Name'])

        unmatch_ec2 = simulator.list_unmatch_ec2()
        self.assertEqual('Stopped', unmatch_ec2[0]['Name'])
示例#3
0
    def test_match_by_instance_is_stopped(self):
        ec2 = {'State': {'Name': 'stopped'}}

        ri = {'InstanceType': 't1.micro'}
        simulator = EC2ReservedInstanceSimulator()

        result = simulator.match_by_instance_state(ec2, ri)
        self.assertIsNone(result)
示例#4
0
    def test_match_by_instance_state(self):
        ec2 = {'State': {'Name': 'running'}}

        ri = {'InstanceType': 't1.micro'}
        simulator = EC2ReservedInstanceSimulator()

        result = simulator.match_by_instance_state(ec2, ri)
        self.assertEqual(ri, result)
示例#5
0
    def test_match_by_instance_type_not_match(self):
        ec2 = {'InstanceType': 'm1.large', 'State': {'Name': 'running'}}

        ri = {'InstanceType': 't1.micro'}
        simulator = EC2ReservedInstanceSimulator()

        result = simulator.match_by_instance_type(ec2, ri)
        self.assertIsNone(result)
示例#6
0
    def test_instances_is_empty_after_apply_ri(self):
        ri = {'InstanceType': 't1.micro'}
        ri['InstanceCount'] = 1

        simulator = EC2ReservedInstanceSimulator()
        simulator.set_ri([ri])
        simulator.apply_ri(ri)

        self.assertListEqual([], simulator.reserved_instances)
示例#7
0
    def test_instancecount_is_1_after_apply_ri(self):
        ri = {'InstanceType': 't1.micro'}
        ri['InstanceCount'] = 2

        simulator = EC2ReservedInstanceSimulator()
        simulator.set_ri([ri])
        simulator.apply_ri(ri)

        result = simulator.reserved_instances[0]
        self.assertEqual(1, result['InstanceCount'])
示例#8
0
    def test_instances_not_change_after_apply_ri(self):
        ri = {'InstanceType': 't1.micro'}
        ri['InstanceCount'] = 1

        another_ri = {'InstanceType': 'm1.large'}
        another_ri['InstanceCount'] = 1

        simulator = EC2ReservedInstanceSimulator()
        simulator.set_ri([ri])
        simulator.apply_ri(another_ri)

        result = simulator.reserved_instances[0]
        self.assertEqual(1, result['InstanceCount'])
        self.assertEqual('t1.micro', result['InstanceType'])
示例#9
0
    def test_match_by_platform_not_match(self):
        ec2 = {
            'InstanceType': 't1.micro',
            'Platform': 'Windows',
            'State': {
                'Name': 'running'
            }
        }

        ri = {'InstanceType': 't1.micro', 'ProductDescription': 'Linux/UNIX'}
        simulator = EC2ReservedInstanceSimulator()

        result = simulator.match_by_platform(ec2, ri)
        self.assertIsNone(result)
示例#10
0
    def test_simulate_ri_applied_to_older_instance(self):
        ec2_1 = self.__buld_instance({'Name': 'Newer'})
        ec2_2 = self.__buld_instance({'Name': 'Older'})

        ec2_1['LaunchTime'] = datetime.datetime.now()
        ec2_2['LaunchTime'] = datetime.datetime.now() - datetime.timedelta(
            days=1)

        ri = {'InstanceType': 't1.micro', 'InstanceCount': 1}

        simulator = EC2ReservedInstanceSimulator()
        simulator.set_ec2([ec2_1, ec2_2])
        simulator.set_ri([ri])
        simulator.simulate()

        match_ec2 = simulator.list_match_ec2()
        self.assertEqual('Older', match_ec2[0]['Name'])

        unmatch_ec2 = simulator.list_unmatch_ec2()
        self.assertEqual('Newer', unmatch_ec2[0]['Name'])
示例#11
0
 def test_set_ri(self):
     simulator = EC2ReservedInstanceSimulator()
     self.assertListEqual([], simulator.reserved_instances)
     simulator.set_ri([])
     self.assertIsNotNone(simulator.reserved_instances)
示例#12
0
 def test_list_unmatch_ri(self):
     simulator = EC2ReservedInstanceSimulator()
     result = simulator.list_unmatch_ri()
     self.assertListEqual([], result)
示例#13
0
 def test_set_ec2(self):
     simulator = EC2ReservedInstanceSimulator()
     self.assertListEqual(simulator.ec2_instances, [])
     simulator.set_ec2([])
     self.assertIsNotNone(simulator.ec2_instances)