- .github
- devel
- doc
-
examples
-
dynamic-client
-
notebooks
-
watch
-
yaml_dir
- README.md
- __init__.py
- annotate_deployment.py
- api_discovery.py
- apply_from_dict.py
- apply_from_directory.py
- apply_from_single_file.py
- cluster_scoped_custom_object.py
- cronjob_crud.py
- deployment_create.py
- deployment_crud.py
- duration-gep2257.py
- in_cluster_config.py
- ingress_create.py
- job_crud.py
- multiple_clusters.py
- namespaced_custom_object.py
- node_labels.py
- out_of_cluster_config.py
- pick_kube_config_context.py
- pod_config_list.py
- pod_exec.py
- pod_portforward.py
- remote_cluster.py
- rollout-daemonset.py
- rollout-statefulset.py
-
dynamic-client
- kubernetes
- scripts
- .gitignore
- CHANGELOG.md
- CONTRIBUTING.md
- LICENSE
- MANIFEST.in
- OWNERS
- README.md
- SECURITY_CONTACTS
- code-of-conduct.md
- codecov.yml
- requirements.txt
- setup.cfg
- setup.py
- test-requirements.txt
- tox.ini
Explanation
This code tests the InClusterConfigLoader
class which is responsible for loading Kubernetes configuration from within a cluster. Here’s a breakdown:
1. Setup
- Environment Variables:
SERVICE_HOST_ENV_NAME
andSERVICE_PORT_ENV_NAME
are constants representing environment variables used for discovering the Kubernetes API server address.- Test Variables:
_TEST_TOKEN
,_TEST_NEW_TOKEN
,_TEST_CERT
are sample token and certificate data._TEST_HOST
,_TEST_PORT
are test values for the API server hostname and port._TEST_ENVIRON
and_TEST_IPV6_ENVIRON
: Dictionaries representing environment variables to simulate an in-cluster environment.InClusterConfigTest
class: This class contains unit tests.setUp
andtearDown
: These methods set up and clean up temporary files used in the tests._create_file_with_temp_content
: A helper method to create temporary files with specified content.
2. Tests
test_join_host_port
: Verifies the correct formation of the API server address string from host and port using the_join_host_port
function.test_load_config
:- Loads the configuration from the
InClusterConfigLoader
with a test certificate file. - Assertions ensure the correct API server URL (
host
), certificate filename (ssl_ca_cert
), and token (token
) are loaded. test_refresh_token
:- Creates an
InClusterConfigLoader
and loads configuration into aConfiguration
object. - Checks that the token is correctly set in the
Configuration
andInClusterConfigLoader
objects. - Replaces the token file with a new one to simulate a token refresh.
- Verifies that the
Configuration
andInClusterConfigLoader
now contain the new token and that thetoken_expires_at
timestamp has been updated. _should_fail_load
: A helper method that asserts that loading configuration should fail given a specific reason.- Error Scenario Tests:
- Tests various error conditions such as missing environment variables, missing or empty configuration files, and invalid file paths.
- Each test calls
_should_fail_load
to ensure the configuration loading process throws an exception.
3. InClusterConfigLoader
class
- Constructor: Initializes the class with the path to the token file, certificate file, and the environment variables.
_load_config
: Loads the API server address, token, and certificate from the environment and configuration files.load_and_set
: Loads the configuration and sets it in the givenConfiguration
object.try_refresh_token
: Attempts to refresh the token if the current token is about to expire.
Code Overview
This code thoroughly tests the InClusterConfigLoader
class, covering different aspects of configuration loading, token refresh, and error handling. It ensures that the class correctly retrieves configuration information from within a Kubernetes cluster and sets up the Configuration
object for interacting with the Kubernetes API.
Graph
The graph shows the usage of functions within the codebase.
Select a code symbol to view it's graph