@@ -4,145 +4,157 @@ import nock from 'nock';
4
4
import { KubeConfig } from './config.js' ;
5
5
import { Health } from './health.js' ;
6
6
import { Cluster , User } from './config_types.js' ;
7
+ import { RequestOptions } from 'node:https' ;
7
8
8
9
describe ( 'Health' , ( ) => {
9
- describe ( 'livez' , ( ) => {
10
- it ( 'should throw an error if no current active cluster' , async ( ) => {
11
- const kc = new KubeConfig ( ) ;
12
- const health = new Health ( kc ) ;
13
- await rejects ( health . livez ( { } ) , { message : 'No currently active cluster' } ) ;
14
- } ) ;
15
-
16
- it ( 'should return true if /livez returns with status 200' , async ( ) => {
17
- const kc = new KubeConfig ( ) ;
18
- const cluster = {
19
- name : 'foo' ,
20
- server : 'https://server.com' ,
21
- } as Cluster ;
22
-
23
- const user = {
24
- name : 'my-user' ,
25
- password : 'some-password' ,
26
- } as User ;
27
- kc . loadFromClusterAndUser ( cluster , user ) ;
28
-
29
- const scope = nock ( 'https://server.com' ) . get ( '/livez' ) . reply ( 200 ) ;
30
- const health = new Health ( kc ) ;
31
-
32
- const r = await health . livez ( { } ) ;
33
- strictEqual ( r , true ) ;
34
- scope . done ( ) ;
35
- } ) ;
36
-
37
- it ( 'should return false if /livez returns with status 500' , async ( ) => {
38
- const kc = new KubeConfig ( ) ;
39
- const cluster = {
40
- name : 'foo' ,
41
- server : 'https://server.com' ,
42
- } as Cluster ;
43
-
44
- const user = {
45
- name : 'my-user' ,
46
- password : 'some-password' ,
47
- } as User ;
48
- kc . loadFromClusterAndUser ( cluster , user ) ;
49
-
50
- const scope = nock ( 'https://server.com' ) . get ( '/livez' ) . reply ( 500 ) ;
51
- const health = new Health ( kc ) ;
52
-
53
- const r = await health . livez ( { } ) ;
54
- strictEqual ( r , false ) ;
55
- scope . done ( ) ;
56
- } ) ;
57
-
58
- it ( 'should return true if /livez returns status 404 and /healthz returns status 200' , async ( ) => {
59
- const kc = new KubeConfig ( ) ;
60
- const cluster = {
61
- name : 'foo' ,
62
- server : 'https://server.com' ,
63
- } as Cluster ;
64
-
65
- const user = {
66
- name : 'my-user' ,
67
- password : 'some-password' ,
68
- } as User ;
69
- kc . loadFromClusterAndUser ( cluster , user ) ;
70
-
71
- const scope = nock ( 'https://server.com' ) ;
72
- scope . get ( '/livez' ) . reply ( 404 ) ;
73
- scope . get ( '/healthz' ) . reply ( 200 ) ;
74
- const health = new Health ( kc ) ;
75
-
76
- const r = await health . livez ( { } ) ;
77
- strictEqual ( r , true ) ;
78
- scope . done ( ) ;
79
- } ) ;
80
-
81
- it ( 'should return false if /livez returns status 404 and /healthz returns status 500' , async ( ) => {
82
- const kc = new KubeConfig ( ) ;
83
- const cluster = {
84
- name : 'foo' ,
85
- server : 'https://server.com' ,
86
- } as Cluster ;
87
-
88
- const user = {
89
- name : 'my-user' ,
90
- password : 'some-password' ,
91
- } as User ;
92
- kc . loadFromClusterAndUser ( cluster , user ) ;
93
-
94
- const scope = nock ( 'https://server.com' ) ;
95
- scope . get ( '/livez' ) . reply ( 404 ) ;
96
- scope . get ( '/healthz' ) . reply ( 500 ) ;
97
- const health = new Health ( kc ) ;
98
-
99
- const r = await health . livez ( { } ) ;
100
- strictEqual ( r , false ) ;
101
- scope . done ( ) ;
102
- } ) ;
103
-
104
- it ( 'should return true if both /livez and /healthz return status 404' , async ( ) => {
105
- const kc = new KubeConfig ( ) ;
106
- const cluster = {
107
- name : 'foo' ,
108
- server : 'https://server.com' ,
109
- } as Cluster ;
110
-
111
- const user = {
112
- name : 'my-user' ,
113
- password : 'some-password' ,
114
- } as User ;
115
- kc . loadFromClusterAndUser ( cluster , user ) ;
116
-
117
- const scope = nock ( 'https://server.com' ) ;
118
- scope . get ( '/livez' ) . reply ( 404 ) ;
119
- scope . get ( '/healthz' ) . reply ( 200 ) ;
120
- const health = new Health ( kc ) ;
121
-
122
- const r = await health . livez ( { } ) ;
123
- strictEqual ( r , true ) ;
124
- scope . done ( ) ;
125
- } ) ;
126
-
127
- it ( 'should throw an error when fetch throws an error' , async ( ) => {
128
- const kc = new KubeConfig ( ) ;
129
- const cluster = {
130
- name : 'foo' ,
131
- server : 'https://server.com' ,
132
- } as Cluster ;
133
-
134
- const user = {
135
- name : 'my-user' ,
136
- password : 'some-password' ,
137
- } as User ;
138
- kc . loadFromClusterAndUser ( cluster , user ) ;
139
-
140
- const scope = nock ( 'https://server.com' ) ;
141
- scope . get ( '/livez' ) . replyWithError ( new Error ( 'an error' ) ) ;
142
- const health = new Health ( kc ) ;
143
-
144
- await rejects ( health . livez ( { } ) , { message : 'Error occurred in health request' } ) ;
145
- scope . done ( ) ;
10
+ [
11
+ {
12
+ path : '/livez' ,
13
+ method : async ( health : Health , opts : RequestOptions ) => health . livez ( opts ) ,
14
+ } ,
15
+ {
16
+ path : '/readyz' ,
17
+ method : async ( health : Health , opts : RequestOptions ) => health . readyz ( opts ) ,
18
+ } ,
19
+ ] . forEach ( ( test ) => {
20
+ describe ( test . path , ( ) => {
21
+ it ( 'should throw an error if no current active cluster' , async ( ) => {
22
+ const kc = new KubeConfig ( ) ;
23
+ const health = new Health ( kc ) ;
24
+ await rejects ( test . method ( health , { } ) , { message : 'No currently active cluster' } ) ;
25
+ } ) ;
26
+
27
+ it ( `should return true if ${ test . path } returns with status 200` , async ( ) => {
28
+ const kc = new KubeConfig ( ) ;
29
+ const cluster = {
30
+ name : 'foo' ,
31
+ server : 'https://server.com' ,
32
+ } as Cluster ;
33
+
34
+ const user = {
35
+ name : 'my-user' ,
36
+ password : 'some-password' ,
37
+ } as User ;
38
+ kc . loadFromClusterAndUser ( cluster , user ) ;
39
+
40
+ const scope = nock ( 'https://server.com' ) . get ( test . path ) . reply ( 200 ) ;
41
+ const health = new Health ( kc ) ;
42
+
43
+ const r = await test . method ( health , { } ) ;
44
+ strictEqual ( r , true ) ;
45
+ scope . done ( ) ;
46
+ } ) ;
47
+
48
+ it ( `should return false if ${ test . path } returns with status 500` , async ( ) => {
49
+ const kc = new KubeConfig ( ) ;
50
+ const cluster = {
51
+ name : 'foo' ,
52
+ server : 'https://server.com' ,
53
+ } as Cluster ;
54
+
55
+ const user = {
56
+ name : 'my-user' ,
57
+ password : 'some-password' ,
58
+ } as User ;
59
+ kc . loadFromClusterAndUser ( cluster , user ) ;
60
+
61
+ const scope = nock ( 'https://server.com' ) . get ( test . path ) . reply ( 500 ) ;
62
+ const health = new Health ( kc ) ;
63
+
64
+ const r = await test . method ( health , { } ) ;
65
+ strictEqual ( r , false ) ;
66
+ scope . done ( ) ;
67
+ } ) ;
68
+
69
+ it ( `should return true if ${ test . path } returns status 404 and /healthz returns status 200` , async ( ) => {
70
+ const kc = new KubeConfig ( ) ;
71
+ const cluster = {
72
+ name : 'foo' ,
73
+ server : 'https://server.com' ,
74
+ } as Cluster ;
75
+
76
+ const user = {
77
+ name : 'my-user' ,
78
+ password : 'some-password' ,
79
+ } as User ;
80
+ kc . loadFromClusterAndUser ( cluster , user ) ;
81
+
82
+ const scope = nock ( 'https://server.com' ) ;
83
+ scope . get ( test . path ) . reply ( 404 ) ;
84
+ scope . get ( '/healthz' ) . reply ( 200 ) ;
85
+ const health = new Health ( kc ) ;
86
+
87
+ const r = await test . method ( health , { } ) ;
88
+ strictEqual ( r , true ) ;
89
+ scope . done ( ) ;
90
+ } ) ;
91
+
92
+ it ( `should return false if ${ test . path } returns status 404 and /healthz returns status 500` , async ( ) => {
93
+ const kc = new KubeConfig ( ) ;
94
+ const cluster = {
95
+ name : 'foo' ,
96
+ server : 'https://server.com' ,
97
+ } as Cluster ;
98
+
99
+ const user = {
100
+ name : 'my-user' ,
101
+ password : 'some-password' ,
102
+ } as User ;
103
+ kc . loadFromClusterAndUser ( cluster , user ) ;
104
+
105
+ const scope = nock ( 'https://server.com' ) ;
106
+ scope . get ( test . path ) . reply ( 404 ) ;
107
+ scope . get ( '/healthz' ) . reply ( 500 ) ;
108
+ const health = new Health ( kc ) ;
109
+
110
+ const r = await test . method ( health , { } ) ;
111
+ strictEqual ( r , false ) ;
112
+ scope . done ( ) ;
113
+ } ) ;
114
+
115
+ it ( `should return true if both ${ test . path } and /healthz return status 404` , async ( ) => {
116
+ const kc = new KubeConfig ( ) ;
117
+ const cluster = {
118
+ name : 'foo' ,
119
+ server : 'https://server.com' ,
120
+ } as Cluster ;
121
+
122
+ const user = {
123
+ name : 'my-user' ,
124
+ password : 'some-password' ,
125
+ } as User ;
126
+ kc . loadFromClusterAndUser ( cluster , user ) ;
127
+
128
+ const scope = nock ( 'https://server.com' ) ;
129
+ scope . get ( test . path ) . reply ( 404 ) ;
130
+ scope . get ( '/healthz' ) . reply ( 404 ) ;
131
+ const health = new Health ( kc ) ;
132
+
133
+ const r = await test . method ( health , { } ) ;
134
+ strictEqual ( r , true ) ;
135
+ scope . done ( ) ;
136
+ } ) ;
137
+
138
+ it ( 'should throw an error when fetch throws an error' , async ( ) => {
139
+ const kc = new KubeConfig ( ) ;
140
+ const cluster = {
141
+ name : 'foo' ,
142
+ server : 'https://server.com' ,
143
+ } as Cluster ;
144
+
145
+ const user = {
146
+ name : 'my-user' ,
147
+ password : 'some-password' ,
148
+ } as User ;
149
+ kc . loadFromClusterAndUser ( cluster , user ) ;
150
+
151
+ const scope = nock ( 'https://server.com' ) ;
152
+ scope . get ( test . path ) . replyWithError ( new Error ( 'an error' ) ) ;
153
+ const health = new Health ( kc ) ;
154
+
155
+ await rejects ( test . method ( health , { } ) , { message : 'Error occurred in health request' } ) ;
156
+ scope . done ( ) ;
157
+ } ) ;
146
158
} ) ;
147
159
} ) ;
148
160
} ) ;
0 commit comments