def test_nextAWSSlave_AWS(self):
        """Test that we pick between ondemand and spot properly"""
        f = _nextAWSSlave()
        inhouse, ondemand, spot = _classifyAWSSlaves(self.slaves)
        # We need to mock out _get_pending so that we don't have to create a db
        # for these tests
        with mock.patch.object(buildbotcustom.misc, "_get_pending") as \
                _get_pending:
            request = mock.Mock()
            request.submittedAt = 0
            _get_pending.return_value = [request]

            # Sanity check - can't choose any slave if none are available!
            self.assertEquals(None,
                              f(self.builder, []))

            # _get_pending shouldn't get called either
            self.assertEquals(_get_pending.called, 0)

            # _get_pending shouldn't get called if we only have ondemand
            # instances either
            self.assertEquals("slave-ec2",
                              f(self.builder, ondemand).slave.slavename)
            self.assertEquals(_get_pending.called, 0)

            # Spot instances should be preferred
            self.assertEquals("slave-spot-001",
                              f(self.builder, spot + ondemand).slave.slavename)
    def test_nextAWSSlave_noRequests(self):
        """Test that everything works if there are no pending requests to
        getRetries"""
        f = _nextAWSSlave()
        inhouse, ondemand, spot = _classifyAWSSlaves(self.slaves)
        # We need to mock out _get_pending so that we don't have to create a db
        # for these tests
        with mock.patch.object(buildbotcustom.misc, "_get_pending") as \
                _get_pending:
            _get_pending.return_value = []

            # Sanity check - can't choose any slave if none are available!
            self.assertEquals(None,
                              f(self.builder, []))

            # _get_pending shouldn't get called either
            self.assertEquals(_get_pending.called, 0)

            # _get_pending shouldn't get called if we only have ondemand
            # instances either
            self.assertEquals("slave-ec2",
                              f(self.builder, ondemand).slave.slavename)
            self.assertEquals(_get_pending.called, 0)

            # Spot instances should be preferred if there are no retries
            self.assertEquals("slave-spot-001",
                              f(self.builder, spot + ondemand).slave.slavename)
    def test_nextAWSSlave_AWS_wait(self):
        """Test that we'll wait up to aws_wait for inhouse instances to become
        available"""
        f = _nextAWSSlave(aws_wait=60)
        inhouse, ondemand, spot = _classifyAWSSlaves(self.slaves)
        # We need to mock out _get_pending so that we don't have to create a db
        # for these tests
        with mock.patch.object(buildbotcustom.misc, "_get_pending") as \
                _get_pending:
            # Also need to mock time
            with mock.patch.object(buildbotcustom.misc, "now") as t:
                request = mock.Mock()
                request.submittedAt = 0
                _get_pending.return_value = [request]

                # at t=1, we shouldn't use an ondemand or spot intance
                t.return_value = 1
                self.assertEquals(None, f(self.builder, spot + ondemand))

                # at t=61, we shoue use an ondemand or spot intance
                t.return_value = 61
                self.assertEquals("slave-spot-001",
                                  f(self.builder,
                                    spot + ondemand).slave.slavename)
                self.assertEquals("slave-ec2",
                                  f(self.builder,
                                    ondemand).slave.slavename)
    def test_nextAWSSlave_AWS(self):
        """Test that we pick between ondemand and spot properly"""
        f = _nextAWSSlave()
        inhouse, ondemand, spot = _classifyAWSSlaves(self.slaves)
        # We need to mock out _get_pending so that we don't have to create a db
        # for these tests
        with mock.patch.object(buildbotcustom.misc, "_get_pending") as \
                _get_pending:
            request = mock.Mock()
            request.submittedAt = 0
            _get_pending.return_value = [request]

            # Sanity check - can't choose any slave if none are available!
            self.assertEquals(None, f(self.builder, []))

            # _get_pending shouldn't get called either
            self.assertEquals(_get_pending.called, 0)

            # _get_pending shouldn't get called if we only have ondemand
            # instances either
            self.assertEquals("slave-ec2",
                              f(self.builder, ondemand).slave.slavename)
            self.assertEquals(_get_pending.called, 0)

            # Spot instances should be preferred
            self.assertEquals("slave-spot-001",
                              f(self.builder, spot + ondemand).slave.slavename)
    def test_nextAWSSlave_noRequests(self):
        """Test that everything works if there are no pending requests to
        getRetries"""
        f = _nextAWSSlave()
        inhouse, ondemand, spot = _classifyAWSSlaves(self.slaves)
        # We need to mock out _get_pending so that we don't have to create a db
        # for these tests
        with mock.patch.object(buildbotcustom.misc, "_get_pending") as \
                _get_pending:
            _get_pending.return_value = []

            # Sanity check - can't choose any slave if none are available!
            self.assertEquals(None, f(self.builder, []))

            # _get_pending shouldn't get called either
            self.assertEquals(_get_pending.called, 0)

            # _get_pending shouldn't get called if we only have ondemand
            # instances either
            self.assertEquals("slave-ec2",
                              f(self.builder, ondemand).slave.slavename)
            self.assertEquals(_get_pending.called, 0)

            # Spot instances should be preferred if there are no retries
            self.assertEquals("slave-spot-001",
                              f(self.builder, spot + ondemand).slave.slavename)
    def test_nextAWSSlave_AWS_wait(self):
        """Test that we'll wait up to aws_wait for inhouse instances to become
        available"""
        f = _nextAWSSlave(aws_wait=60)
        inhouse, ondemand, spot = _classifyAWSSlaves(self.slaves)
        # We need to mock out _get_pending so that we don't have to create a db
        # for these tests
        with mock.patch.object(buildbotcustom.misc, "_get_pending") as \
                _get_pending:
            # Also need to mock time
            with mock.patch.object(buildbotcustom.misc, "now") as t:
                request = mock.Mock()
                request.submittedAt = 0
                _get_pending.return_value = [request]

                # at t=1, we shouldn't use an ondemand or spot intance
                t.return_value = 1
                self.assertEquals(None, f(self.builder, spot + ondemand))

                # at t=61, we shoue use an ondemand or spot intance
                t.return_value = 61
                self.assertEquals(
                    "slave-spot-001",
                    f(self.builder, spot + ondemand).slave.slavename)
                self.assertEquals("slave-ec2",
                                  f(self.builder, ondemand).slave.slavename)
    def test_nextAWSSlave_inhouse(self):
        """Test that _nextAWSSlave returns the correct slave in different
        situations"""
        f = _nextAWSSlave()
        inhouse, ondemand, spot = _classifyAWSSlaves(self.slaves)

        # Always choose inhouse if available
        self.assertEquals("slave-hw", f(self.builder,
                                        self.slaves).slave.slavename)
        self.assertEquals("slave-hw", f(self.builder,
                                        inhouse + ondemand).slave.slavename)
        self.assertEquals("slave-hw", f(self.builder,
                                        inhouse + spot).slave.slavename)
    def test_nextAWSSlave_inhouse(self):
        """Test that _nextAWSSlave returns the correct slave in different
        situations"""
        f = _nextAWSSlave()
        inhouse, ondemand, spot = _classifyAWSSlaves(self.slaves)

        # Always choose inhouse if available
        self.assertEquals("slave-hw",
                          f(self.builder, self.slaves).slave.slavename)
        self.assertEquals("slave-hw",
                          f(self.builder, inhouse + ondemand).slave.slavename)
        self.assertEquals("slave-hw",
                          f(self.builder, inhouse + spot).slave.slavename)