def test_scale_invalid_quantity(self, stderr): with self.assertRaises(SystemExit) as cm: plugin.scale(["-s", "myservice", "-i", "abc", "-n", "0"]) exc = cm.exception self.assertEqual(2, exc.code) expected_msg = "quantity must be a positive integer\n" stderr.write.assert_called_with(expected_msg)
def test_scale_missing_service(self, stderr): with self.assertRaises(SystemExit) as cm: plugin.scale(["-n", "1"]) exc = cm.exception self.assertEqual(2, exc.code) expected_msg = "scale: error: argument -s/--service is required\n" stderr.write.assert_called_with(expected_msg)
def test_scale_missing_quantity(self, stderr): with self.assertRaises(SystemExit) as cm: plugin.scale(["-s", "myservice", "-i", "abc"]) exc = cm.exception self.assertEqual(2, exc.code) expected_msg = "scale: error: argument -n/--quantity is required\n" stderr.write.assert_called_with(expected_msg)
def test_scale_no_token(self, stderr): self.set_envs() self.addCleanup(self.delete_envs) del os.environ["TSURU_TOKEN"] self.addCleanup(self.set_envs) with self.assertRaises(SystemExit) as cm: plugin.scale(["-s", "myservice", "-i", "myinstance", "-n", "10"]) exc = cm.exception self.assertEqual(2, exc.code) stderr.write.assert_called_with("ERROR: missing TSURU_TOKEN\n")
def test_scale(self, stdout, Request, urlopen): request = Request.return_value self.set_envs() self.addCleanup(self.delete_envs) result = mock.Mock() result.getcode.return_value = 201 urlopen.return_value = result plugin.scale(["-s", "myservice", "-i", "myinstance", "-n", "10"]) Request.assert_called_with(self.target + "services/myservice/proxy/myinstance?" + "callback=/resources/myinstance/scale") request.add_header.assert_called_with("Authorization", "bearer " + self.token) request.add_data.assert_called_with("quantity=10") urlopen.assert_called_with(request) stdout.write.assert_called_with("Instance successfully scaled to 10 units\n")
def test_scale_failure_invalid_value(self, stderr, Request, urlopen): request = mock.Mock() Request.return_value = request self.set_envs() self.addCleanup(self.delete_envs) urlopen.return_value = HTTPError(None, 400, None, None, StringIO(u"Invalid quantity")) with self.assertRaises(SystemExit) as cm: plugin.scale(["-s", "myservice", "-i", "myinstance", "-n", "10"]) exc = cm.exception self.assertEqual(1, exc.code) Request.assert_called_with(self.target + "services/myservice/proxy/myinstance?" + "callback=/resources/myinstance/scale") request.add_header.assert_called_with("Authorization", "bearer " + self.token) request.add_data.assert_called_with("quantity=10") urlopen.assert_called_with(request) stderr.write.assert_called_with("ERROR: Invalid quantity\n")
def test_scale_no_target(self, stderr): with self.assertRaises(SystemExit) as cm: plugin.scale(["-s", "myservice", "-i", "myinstance", "-n", "10"]) exc = cm.exception self.assertEqual(2, exc.code) stderr.write.assert_called_with("ERROR: missing TSURU_TARGET\n")