Skip to content

Commit 4986a38

Browse files
committed
add waiting for psql operator to be ready, working #34
1 parent bba7f23 commit 4986a38

File tree

3 files changed

+216
-2
lines changed

3 files changed

+216
-2
lines changed

k8s/k8ssetup/database_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func Test_isDatabaseRunning(t *testing.T) {
168168
}
169169
})
170170

171-
t.Run("must return false if database creation fails", func(t *testing.T) {
171+
t.Run("must return false if database check fails", func(t *testing.T) {
172172
var errInvalid = errors.New("invalid")
173173
k8sImpl.executeCommand = func(cmdName string, params ...string) (string, error) {
174174
return "", errInvalid

k8s/k8ssetup/k8ssetup.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"os/exec"
1010
"path/filepath"
11+
"strings"
1112

1213
"github.com/go-git/go-git/v5"
1314
)
@@ -40,13 +41,42 @@ func (k k8sSetUpImpl) InstallPostgresqlOperator() error {
4041
if err := k.doPsqlOperatorInstallation(); err != nil {
4142
return fmt.Errorf("error installing PostgreSQL operator: %v", err)
4243
}
44+
k.waitPsqlOperatorRunning()
45+
4346
} else {
4447
log.Println("PostgreSQL operator is installed ...")
4548
}
4649

4750
return nil
4851
}
4952

53+
func (k k8sSetUpImpl) waitPsqlOperatorRunning() {
54+
cnt := true
55+
for cnt {
56+
running, err := k.isPsqlOperatorRunning()
57+
cnt = !(err == nil && running)
58+
}
59+
log.Print("Psql operator is running")
60+
}
61+
62+
func (k k8sSetUpImpl) isPsqlOperatorRunning() (running bool, err error) {
63+
log.Printf("Checking if postgres operator is already running ...")
64+
65+
var podNames, output string
66+
if podNames, err = k.kubectl("get", "pod", "-o", "name"); err == nil {
67+
for _, name := range strings.Split(podNames, "\n") {
68+
if strings.Contains(name, "postgres-operator") {
69+
if output, err = k.kubectl("get", name, "-o", "jsonpath='{.status.phase}'"); err != nil {
70+
return false, err
71+
}
72+
running = output == "'Running'"
73+
break
74+
}
75+
}
76+
}
77+
return
78+
}
79+
5080
func (k k8sSetUpImpl) isPostgreSQLOperatorInstalled() bool {
5181
log.Println("Checking if postgresql operator is already installed ...")
5282
if _, err := k.kubectl("describe", "service/postgres-operator"); err != nil {
@@ -107,10 +137,10 @@ func (k k8sSetUpImpl) defaultExecuteCommand(cmdName string, params ...string) (o
107137

108138
err = cmd.Run()
109139
output = stdBuffer.String()
140+
log.Println(output)
110141
if err != nil {
111142
err = fmt.Errorf("error '%v' in %q %q", err, cmdName, output)
112143
}
113-
114144
return
115145
}
116146

k8s/k8ssetup/k8ssetup_test.go

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ func Test_InstallPostgresqlOperator(t *testing.T) {
140140
if params[0] == "describe" {
141141
return "error", errors.New("some error")
142142
}
143+
if params[0] == "get" {
144+
if params[1] == "pod" {
145+
return "pod/postgres-operator-59bb89464c-dx2rs", nil
146+
} else if params[1] == "pod/postgres-operator-59bb89464c-dx2rs" {
147+
return "'Running'", nil
148+
}
149+
}
143150
return "", nil
144151
}
145152
var expect error = nil
@@ -171,3 +178,180 @@ func Test_InstallPostgresqlOperator(t *testing.T) {
171178
}
172179
})
173180
}
181+
182+
func Test_waitPsqlOperatorRunning(t *testing.T) {
183+
k8sImpl := NewK8sSetUp().(*k8sSetUpImpl)
184+
185+
t.Run("must run until database is running", func(t *testing.T) {
186+
count := 0
187+
k8sImpl.executeCommand = func(cmdName string, params ...string) (string, error) {
188+
if params[0] == "get" {
189+
if params[1] == "pod" {
190+
return "pod/postgres-operator-59bb89464c-dx2rs", nil
191+
} else if params[1] == "pod/postgres-operator-59bb89464c-dx2rs" {
192+
count++
193+
if count == 3 {
194+
return "'Running'", nil
195+
}
196+
return "'Waiting'", nil
197+
}
198+
}
199+
return "", nil
200+
}
201+
202+
k8sImpl.waitPsqlOperatorRunning()
203+
expect := 3
204+
got := count
205+
if got != expect {
206+
t.Fatalf("Got %v, expect %v", got, expect)
207+
}
208+
})
209+
210+
t.Run("must run until there is no error and running", func(t *testing.T) {
211+
var errInvalid = errors.New("invalid")
212+
count := 0
213+
k8sImpl.executeCommand = func(cmdName string, params ...string) (string, error) {
214+
if params[0] == "get" {
215+
if params[1] == "pod" {
216+
return "pod/postgres-operator-59bb89464c-dx2rs", nil
217+
} else if params[1] == "pod/postgres-operator-59bb89464c-dx2rs" {
218+
count++
219+
if count == 2 {
220+
return "'Running'", nil
221+
}
222+
return "", errInvalid
223+
}
224+
}
225+
return "", nil
226+
}
227+
228+
k8sImpl.waitPsqlOperatorRunning()
229+
expect := 2
230+
got := count
231+
if got != expect {
232+
t.Fatalf("Got %v, expect %v", got, expect)
233+
}
234+
})
235+
}
236+
237+
func Test_isPsqlOperatorRunning(t *testing.T) {
238+
k8sImpl := NewK8sSetUp().(*k8sSetUpImpl)
239+
240+
t.Run("must return true if postgres operator is running", func(t *testing.T) {
241+
k8sImpl.executeCommand = func(cmdName string, params ...string) (string, error) {
242+
if params[0] == "get" {
243+
if params[1] == "pod" {
244+
return "pod/postgres-operator-59bb89464c-dx2rs", nil
245+
} else if params[1] == "pod/postgres-operator-59bb89464c-dx2rs" {
246+
return "'Running'", nil
247+
}
248+
}
249+
return "", nil
250+
}
251+
252+
expect := true
253+
var expectErr error = nil
254+
got, gotErr := k8sImpl.isPsqlOperatorRunning()
255+
256+
if gotErr != expectErr {
257+
t.Fatalf("Got error %v, expect error %v", gotErr, expectErr)
258+
}
259+
if got != expect {
260+
t.Fatalf("Got %v, expect %v", got, expect)
261+
}
262+
})
263+
264+
t.Run("must return false if postgres operator does not exist", func(t *testing.T) {
265+
k8sImpl.executeCommand = func(cmdName string, params ...string) (string, error) {
266+
if params[0] == "get" {
267+
if params[1] == "pod" {
268+
return "pod/test-1234", nil
269+
}
270+
}
271+
return "", nil
272+
}
273+
274+
expect := false
275+
var expectErr error = nil
276+
got, gotErr := k8sImpl.isPsqlOperatorRunning()
277+
278+
if gotErr != expectErr {
279+
t.Fatalf("Got error %v, expect error %v", gotErr, expectErr)
280+
}
281+
if got != expect {
282+
t.Fatalf("Got %v, expect %v", got, expect)
283+
}
284+
})
285+
286+
t.Run("must return false if postgres operator is in progress", func(t *testing.T) {
287+
k8sImpl.executeCommand = func(cmdName string, params ...string) (string, error) {
288+
if params[0] == "get" {
289+
if params[1] == "pod" {
290+
return "pod/postgres-operator-59bb89464c-dx2rs", nil
291+
} else if params[1] == "pod/postgres-operator-59bb89464c-dx2rs" {
292+
return "'Waiting'", nil
293+
}
294+
}
295+
return "", nil
296+
}
297+
298+
expect := false
299+
var expectErr error = nil
300+
got, gotErr := k8sImpl.isPsqlOperatorRunning()
301+
302+
if gotErr != expectErr {
303+
t.Fatalf("Got error %v, expect error %v", gotErr, expectErr)
304+
}
305+
if got != expect {
306+
t.Fatalf("Got %v, expect %v", got, expect)
307+
}
308+
})
309+
310+
t.Run("must return false if get pods fails", func(t *testing.T) {
311+
var errInvalid = errors.New("invalid")
312+
k8sImpl.executeCommand = func(cmdName string, params ...string) (string, error) {
313+
if params[0] == "get" {
314+
if params[1] == "pod" {
315+
return "", errInvalid
316+
}
317+
}
318+
return "", nil
319+
}
320+
321+
expect := false
322+
expectErr := errInvalid
323+
got, gotErr := k8sImpl.isPsqlOperatorRunning()
324+
325+
if gotErr != expectErr {
326+
t.Fatalf("Got error %v, expect error %v", gotErr, expectErr)
327+
}
328+
if got != expect {
329+
t.Fatalf("Got %v, expect %v", got, expect)
330+
}
331+
})
332+
333+
t.Run("must return false if postgres operator status check fails", func(t *testing.T) {
334+
var errInvalid = errors.New("invalid")
335+
k8sImpl.executeCommand = func(cmdName string, params ...string) (string, error) {
336+
if params[0] == "get" {
337+
if params[1] == "pod" {
338+
return "pod/postgres-operator-59bb89464c-dx2rs", nil
339+
} else if params[1] == "pod/postgres-operator-59bb89464c-dx2rs" {
340+
return "", errInvalid
341+
}
342+
}
343+
return "", nil
344+
}
345+
346+
expect := false
347+
expectErr := errInvalid
348+
got, gotErr := k8sImpl.isPsqlOperatorRunning()
349+
350+
if gotErr != expectErr {
351+
t.Fatalf("Got error %v, expect error %v", gotErr, expectErr)
352+
}
353+
if got != expect {
354+
t.Fatalf("Got %v, expect %v", got, expect)
355+
}
356+
})
357+
}

0 commit comments

Comments
 (0)