Skip to content

Commit b40a94d

Browse files
authored
Merge pull request #2 from Blueshoe/feat/flexbile-cluster-options
feat: enable flexible cluster options
2 parents 321a031 + b8219b1 commit b40a94d

File tree

9 files changed

+29
-8
lines changed

9 files changed

+29
-8
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ It provides the following interface:
4646
- `port_forwarding(...)`: Port forward a target
4747
- `logs(...)`: Get the logs of a pod
4848
- `version()`: Get the Kubernetes version of this cluster
49-
- `create(...)`: Create this cluster
49+
- `create(...)`: Create this cluster (pass special cluster arguments with `options: List[str]` to the CLI command)
5050
- `delete()`: Delete this cluster
5151
- `reset()`: Delete this cluster (if it exists) and create it again
5252

@@ -128,5 +128,13 @@ In this example, the cluster remains active for the entire session and is only d
128128

129129
> Note that `yield` notation that is prefered by pytest to express clean up tasks for this fixture.
130130
131+
#### Special cluster options
132+
You can pass more options using `kwargs['options']: List[str]` to the `create(options=...)` function when creating the cluster like so:
133+
```python
134+
cluster = select_provider_manager("k3d")("my-cluster")
135+
# bind ports of this k3d cluster
136+
cluster.create(options=["--agents", "1", "-p", "8080:80@agent:0", "-p", "31820:31820/UDP@agent:0"])
137+
```
138+
131139
## Examples
132140
Please find more examples in *tests/vendor.py* in this repository. These test cases are written as users of pytest-kubernetes would write test cases in their projects.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
[tool.poetry]
22
name = "pytest-kubernetes"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
description = ""
55
authors = ["Michael Schilonka <michael@blueshoe.io>"]
66
readme = "README.md"
77
packages = [{include = "pytest_kubernetes"}]
8+
repository = "https://github.com/Blueshoe/pytest-kubernetes"
89
classifiers = [
910
"Framework :: Pytest",
1011
"Topic :: Software Development :: Testing",

pytest_kubernetes/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ class ClusterOptions:
88
api_version: str = field(default="1.25.3")
99
# nodes: int = None
1010
kubeconfig_path: Optional[Path] = None
11-
cluster_timeout: int = field(default=120)
11+
cluster_timeout: int = field(default=240)

pytest_kubernetes/providers/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ def delete(self) -> None:
223223
self._on_delete()
224224
if self.kubeconfig:
225225
self.kubeconfig.unlink(missing_ok=True)
226+
self._cluster_options.kubeconfig_path = None
226227
sleep(1)
227228

228229
def reset(self) -> None:

pytest_kubernetes/providers/k3d.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def _translate_version(self, version: str) -> str:
1111
return f"rancher/k3s:v{version}-k3s1"
1212

1313
def _on_create(self, cluster_options: ClusterOptions, **kwargs) -> None:
14+
opts = kwargs.get("options", [])
1415
self._exec(
1516
[
1617
"cluster",
@@ -22,6 +23,7 @@ def _on_create(self, cluster_options: ClusterOptions, **kwargs) -> None:
2223
"--wait",
2324
f"--timeout={cluster_options.cluster_timeout}s",
2425
]
26+
+ opts
2527
)
2628
self._exec(
2729
[

pytest_kubernetes/providers/kind.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def get_binary_name(self) -> str:
88
return "kind"
99

1010
def _on_create(self, cluster_options: ClusterOptions, **kwargs) -> None:
11+
opts = kwargs.get("options", [])
1112
_ = self._exec(
1213
[
1314
"create",
@@ -18,7 +19,7 @@ def _on_create(self, cluster_options: ClusterOptions, **kwargs) -> None:
1819
str(cluster_options.kubeconfig_path),
1920
"--image",
2021
f"kindest/node:v{cluster_options.api_version}",
21-
]
22+
] + opts
2223
)
2324

2425
def _on_delete(self) -> None:

pytest_kubernetes/providers/minikube.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def load_image(self, image: str) -> None:
1616

1717
class MinikubeKVM2Manager(MinikubeManager):
1818
def _on_create(self, cluster_options: ClusterOptions, **kwargs) -> None:
19+
opts = kwargs.get("options", [])
1920
self._exec(
2021
[
2122
"start",
@@ -26,13 +27,14 @@ def _on_create(self, cluster_options: ClusterOptions, **kwargs) -> None:
2627
"--embed-certs",
2728
"--kubernetes-version",
2829
f"v{cluster_options.api_version}",
29-
],
30+
] + opts,
3031
additional_env={"KUBECONFIG": str(cluster_options.kubeconfig_path)},
3132
)
3233

3334

3435
class MinikubeDockerManager(MinikubeManager):
3536
def _on_create(self, cluster_options: ClusterOptions, **kwargs) -> None:
37+
opts = kwargs.get("options", [])
3638
self._exec(
3739
[
3840
"start",
@@ -43,6 +45,6 @@ def _on_create(self, cluster_options: ClusterOptions, **kwargs) -> None:
4345
"--embed-certs",
4446
"--kubernetes-version",
4547
f"v{cluster_options.api_version}",
46-
],
48+
] + opts,
4749
additional_env={"KUBECONFIG": str(cluster_options.kubeconfig_path)},
4850
)

tests/test_providers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ def test_a_create_simple_cluster(self):
2626

2727
def test_b_reset_cluster(self):
2828
self.cluster = self.manager(self.cluster_name)
29+
self.cluster.create()
30+
self.cluster.kubectl(["get", "nodes"])
31+
kubeconfig1 = self.cluster.kubeconfig
2932
self.cluster.reset()
33+
kubeconfig2 = self.cluster.kubeconfig
34+
self.cluster.kubectl(["get", "nodes"])
35+
assert kubeconfig1 != kubeconfig2
3036

3137
def test_c_apply_yaml_file(self):
3238
self.cluster.create()

tests/vendor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_e_kept_cluster_delete(k8s: AClusterManager):
7777
assert configmap["metadata"]["uid"] is not None
7878

7979
k8s.reset()
80-
with pytest.raises(subprocess.CalledProcessError):
80+
with pytest.raises(RuntimeError):
8181
configmap = k8s.kubectl(["get", "configmap", "myconfigmap"])
8282

8383
k8s.delete()
@@ -116,7 +116,7 @@ def test_g_prepopulated_cluster_kept(k8s_with_workload: AClusterManager):
116116
)
117117
assert "k3d-pytest-my-cluster" in process.stdout
118118
k8s.reset()
119-
with pytest.raises(subprocess.CalledProcessError):
119+
with pytest.raises(RuntimeError):
120120
k8s.kubectl(["get", "deployment", "hello-nginxdemo"])
121121

122122

0 commit comments

Comments
 (0)