|
32 | 32 | KubeconfigInaccessableError)
|
33 | 33 | from tests.functional.eks.test_util import (describe_cluster_response,
|
34 | 34 | describe_cluster_creating_response,
|
35 |
| - get_testdata) |
| 35 | + get_testdata, |
| 36 | + assume_role_response) |
36 | 37 |
|
37 | 38 | def sanitize_output(output):
|
38 | 39 | """
|
@@ -66,6 +67,15 @@ def setUp(self):
|
66 | 67 | self.client.describe_cluster.return_value = describe_cluster_response()
|
67 | 68 | self.mock_create_client.return_value = self.client
|
68 | 69 |
|
| 70 | + # Set up the sts_client_mock |
| 71 | + self.sts_client_mock = mock.Mock() |
| 72 | + self.sts_client_mock.assume_role.return_value = assume_role_response() |
| 73 | + |
| 74 | + # Ensure the mock_create_client correctly returns the appropriate mock |
| 75 | + self.mock_create_client.side_effect = lambda service_name, **kwargs: ( |
| 76 | + self.sts_client_mock if service_name == "sts" else self.client |
| 77 | + ) |
| 78 | + |
69 | 79 | self.command = UpdateKubeconfigCommand(self.session)
|
70 | 80 | self.maxDiff = None
|
71 | 81 |
|
@@ -422,3 +432,59 @@ def test_update_old_api_version(self):
|
422 | 432 |
|
423 | 433 | self.assert_cmd(configs, passed, environment)
|
424 | 434 | self.assert_config_state("valid_old_api_version", "valid_old_api_version_updated")
|
| 435 | + |
| 436 | + def test_assume_role(self): |
| 437 | + """ |
| 438 | + Test that assume_role_arn is handled correctly when provided. |
| 439 | + """ |
| 440 | + configs = ["valid_existing"] |
| 441 | + self.initialize_tempfiles(configs) |
| 442 | + |
| 443 | + # Include the --assume-role-arn argument |
| 444 | + args = [ |
| 445 | + "--name", "ExampleCluster", |
| 446 | + "--assume-role-arn", "arn:aws:iam::123456789012:role/test-role" |
| 447 | + ] |
| 448 | + |
| 449 | + # Mock environment variables and paths |
| 450 | + kubeconfig_path = self._get_temp_config("valid_existing") |
| 451 | + default_path = self._get_temp_config("default_temp") |
| 452 | + |
| 453 | + with mock.patch.dict(os.environ, {'KUBECONFIG': kubeconfig_path}): |
| 454 | + with mock.patch("awscli.customizations.eks.update_kubeconfig.DEFAULT_PATH", default_path): |
| 455 | + self.command(args, None) |
| 456 | + |
| 457 | + # Verify that assume_role was called with the correct parameters |
| 458 | + self.sts_client_mock.assume_role.assert_called_once_with( |
| 459 | + RoleArn="arn:aws:iam::123456789012:role/test-role", |
| 460 | + RoleSessionName="EKSDescribeClusterSession" |
| 461 | + ) |
| 462 | + |
| 463 | + # Verify that the EKS client was created with the assumed credentials |
| 464 | + self.mock_create_client.assert_any_call( |
| 465 | + "eks", |
| 466 | + aws_access_key_id="test-access-key", |
| 467 | + aws_secret_access_key="test-secret-key", |
| 468 | + aws_session_token="test-session-token" |
| 469 | + ) |
| 470 | + |
| 471 | + # Verify that the cluster was described |
| 472 | + self.client.describe_cluster.assert_called_once_with(name="ExampleCluster") |
| 473 | + |
| 474 | + # Assert the configuration state |
| 475 | + self.assert_config_state("valid_existing", "output_combined") |
| 476 | + |
| 477 | + def test_no_assume_role(self): |
| 478 | + """ |
| 479 | + Test that assume_role_arn is not used when not provided. |
| 480 | + """ |
| 481 | + configs = ["valid_existing"] |
| 482 | + passed = "valid_existing" |
| 483 | + environment = [] |
| 484 | + |
| 485 | + self.client.describe_cluster = mock.Mock(return_value=describe_cluster_response()) |
| 486 | + self.assert_cmd(configs, passed, environment) |
| 487 | + |
| 488 | + # Verify that assume_role was not called |
| 489 | + self.mock_create_client.assert_called_once_with("eks") |
| 490 | + self.client.describe_cluster.assert_called_once_with(name="ExampleCluster") |
0 commit comments