5
5
use RenokiCo \PhpK8s \Exceptions \KubeConfigClusterNotFound ;
6
6
use RenokiCo \PhpK8s \Exceptions \KubeConfigContextNotFound ;
7
7
use RenokiCo \PhpK8s \Exceptions \KubeConfigUserNotFound ;
8
+ use RenokiCo \PhpK8s \Kinds \K8sResource ;
8
9
use RenokiCo \PhpK8s \KubernetesCluster ;
9
10
10
11
class KubeConfigTest extends TestCase
@@ -21,13 +22,15 @@ public function setUp(): void
21
22
22
23
public function test_kube_config_from_yaml_file_with_base64_encoded_ssl ()
23
24
{
24
- $ this ->cluster ->fromKubeConfigYamlFile (__DIR__ .'/cluster/kubeconfig.yaml ' , 'minikube ' );
25
+ $ cluster = new KubernetesCluster ('http://127.0.0.1:8080 ' );
26
+
27
+ $ cluster ->fromKubeConfigYamlFile (__DIR__ .'/cluster/kubeconfig.yaml ' , 'minikube ' );
25
28
26
29
[
27
30
'verify ' => $ caPath ,
28
31
'cert ' => $ certPath ,
29
32
'ssl_key ' => $ keyPath ,
30
- ] = $ this -> cluster ->getClient ()->getConfig ();
33
+ ] = $ cluster ->getClient ()->getConfig ();
31
34
32
35
$ this ->assertEquals ("some-ca \n" , file_get_contents ($ caPath ));
33
36
$ this ->assertEquals ("some-cert \n" , file_get_contents ($ certPath ));
@@ -36,13 +39,15 @@ public function test_kube_config_from_yaml_file_with_base64_encoded_ssl()
36
39
37
40
public function test_kube_config_from_yaml_file_with_paths_to_ssl ()
38
41
{
39
- $ this ->cluster ->fromKubeConfigYamlFile (__DIR__ .'/cluster/kubeconfig.yaml ' , 'minikube-2 ' );
42
+ $ cluster = new KubernetesCluster ('http://127.0.0.1:8080 ' );
43
+
44
+ $ cluster ->fromKubeConfigYamlFile (__DIR__ .'/cluster/kubeconfig.yaml ' , 'minikube-2 ' );
40
45
41
46
[
42
47
'verify ' => $ caPath ,
43
48
'cert ' => $ certPath ,
44
49
'ssl_key ' => $ keyPath ,
45
- ] = $ this -> cluster ->getClient ()->getConfig ();
50
+ ] = $ cluster ->getClient ()->getConfig ();
46
51
47
52
$ this ->assertEquals ('/path/to/.minikube/ca.crt ' , $ caPath );
48
53
$ this ->assertEquals ('/path/to/.minikube/client.crt ' , $ certPath );
@@ -51,40 +56,68 @@ public function test_kube_config_from_yaml_file_with_paths_to_ssl()
51
56
52
57
public function test_kube_config_from_yaml_cannot_load_if_no_cluster ()
53
58
{
59
+ $ cluster = new KubernetesCluster ('http://127.0.0.1:8080 ' );
60
+
54
61
$ this ->expectException (KubeConfigClusterNotFound::class);
55
62
56
- $ this -> cluster ->fromKubeConfigYamlFile (__DIR__ .'/cluster/kubeconfig.yaml ' , 'minikube-without-cluster ' );
63
+ $ cluster ->fromKubeConfigYamlFile (__DIR__ .'/cluster/kubeconfig.yaml ' , 'minikube-without-cluster ' );
57
64
}
58
65
59
66
public function test_kube_config_from_yaml_cannot_load_if_no_user ()
60
67
{
68
+ $ cluster = new KubernetesCluster ('http://127.0.0.1:8080 ' );
69
+
61
70
$ this ->expectException (KubeConfigUserNotFound::class);
62
71
63
- $ this -> cluster ->fromKubeConfigYamlFile (__DIR__ .'/cluster/kubeconfig.yaml ' , 'minikube-without-user ' );
72
+ $ cluster ->fromKubeConfigYamlFile (__DIR__ .'/cluster/kubeconfig.yaml ' , 'minikube-without-user ' );
64
73
}
65
74
66
75
public function test_kube_config_from_yaml_cannot_load_if_wrong_context ()
67
76
{
77
+ $ cluster = new KubernetesCluster ('http://127.0.0.1:8080 ' );
78
+
68
79
$ this ->expectException (KubeConfigContextNotFound::class);
69
80
70
- $ this -> cluster ->fromKubeConfigYamlFile (__DIR__ .'/cluster/kubeconfig.yaml ' , 'inexistent-context ' );
81
+ $ cluster ->fromKubeConfigYamlFile (__DIR__ .'/cluster/kubeconfig.yaml ' , 'inexistent-context ' );
71
82
}
72
83
73
84
public function test_http_authentication ()
74
85
{
75
- $ this -> cluster -> httpAuthentication ( ' some-user ' , ' some-password ' );
86
+ $ cluster = new KubernetesCluster ( ' http://127.0.0.1:8080 ' );
76
87
77
- ['auth ' => $ auth ] = $ this ->cluster ->getClient ()->getConfig ();
88
+ $ cluster ->httpAuthentication ('some-user ' , 'some-password ' );
89
+
90
+ ['auth ' => $ auth ] = $ cluster ->getClient ()->getConfig ();
78
91
79
92
$ this ->assertEquals (['some-user ' , 'some-password ' ], $ auth );
80
93
}
81
94
82
95
public function test_bearer_token_authentication ()
83
96
{
84
- $ this ->cluster ->loadTokenFromFile (__DIR__ .'/cluster/token.txt ' );
97
+ $ cluster = new KubernetesCluster ('http://127.0.0.1:8080 ' );
98
+
99
+ $ cluster ->loadTokenFromFile (__DIR__ .'/cluster/token.txt ' );
85
100
86
- ['headers ' => ['authorization ' => $ token ]] = $ this -> cluster ->getClient ()->getConfig ();
101
+ ['headers ' => ['authorization ' => $ token ]] = $ cluster ->getClient ()->getConfig ();
87
102
88
103
$ this ->assertEquals ('Bearer some-token ' , $ token );
89
104
}
105
+
106
+ public function test_in_cluster_config ()
107
+ {
108
+ $ cluster = new KubernetesCluster ('http://127.0.0.1:8080 ' );
109
+
110
+ $ cluster ->inClusterConfiguration ();
111
+
112
+ [
113
+ 'headers ' => ['authorization ' => $ token ],
114
+ 'verify ' => $ caPath ,
115
+ ] = $ cluster ->getClient ()->getConfig ();
116
+
117
+ $ this ->assertEquals ('Bearer some-token ' , $ token );
118
+ $ this ->assertEquals ('/var/run/secrets/kubernetes.io/serviceaccount/ca.crt ' , $ caPath );
119
+ $ this ->assertEquals ('some-namespace ' , K8sResource::$ defaultNamespace );
120
+
121
+ K8sResource::setDefaultNamespace ('default ' );
122
+ }
90
123
}
0 commit comments