(t *testing.T)
| 37 | } |
| 38 | |
| 39 | func TestWriteUsersInPostgreSQL(t *testing.T) { |
| 40 | ctx := context.Background() |
| 41 | |
| 42 | t.Run("Arguments", func(t *testing.T) { |
| 43 | expected := errors.New("pass-through") |
| 44 | exec := func( |
| 45 | _ context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string, |
| 46 | ) error { |
| 47 | assert.Assert(t, stdout != nil, "should capture stdout") |
| 48 | assert.Assert(t, stderr != nil, "should capture stderr") |
| 49 | return expected |
| 50 | } |
| 51 | |
| 52 | cluster := new(v1beta1.PostgresCluster) |
| 53 | assert.Equal(t, expected, WriteUsersInPostgreSQL(ctx, cluster, exec, nil, nil)) |
| 54 | }) |
| 55 | |
| 56 | t.Run("Empty", func(t *testing.T) { |
| 57 | calls := 0 |
| 58 | exec := func( |
| 59 | _ context.Context, stdin io.Reader, _, _ io.Writer, command ...string, |
| 60 | ) error { |
| 61 | calls++ |
| 62 | |
| 63 | b, err := io.ReadAll(stdin) |
| 64 | assert.NilError(t, err) |
| 65 | assert.Equal(t, string(b), strings.TrimSpace(` |
| 66 | SET synchronous_commit = LOCAL;SET search_path TO ''; |
| 67 | CREATE TEMPORARY TABLE input (id serial, data json); |
| 68 | \copy input (data) from stdin with (format text) |
| 69 | \. |
| 70 | BEGIN; |
| 71 | SELECT pg_catalog.format('CREATE USER %I', |
| 72 | pg_catalog.json_extract_path_text(input.data, 'username')) |
| 73 | FROM input |
| 74 | WHERE NOT EXISTS ( |
| 75 | SELECT 1 FROM pg_catalog.pg_roles |
| 76 | WHERE rolname = pg_catalog.json_extract_path_text(input.data, 'username')) |
| 77 | ORDER BY input.id |
| 78 | \gexec |
| 79 | |
| 80 | SELECT pg_catalog.format('ALTER ROLE %I WITH %s PASSWORD %L', |
| 81 | pg_catalog.json_extract_path_text(input.data, 'username'), |
| 82 | pg_catalog.json_extract_path_text(input.data, 'options'), |
| 83 | pg_catalog.json_extract_path_text(input.data, 'verifier')) |
| 84 | FROM input ORDER BY input.id |
| 85 | \gexec |
| 86 | |
| 87 | SELECT pg_catalog.format('GRANT ALL PRIVILEGES ON DATABASE %I TO %I', |
| 88 | pg_catalog.json_array_elements_text( |
| 89 | pg_catalog.json_extract_path( |
| 90 | pg_catalog.json_strip_nulls(input.data), 'databases')), |
| 91 | pg_catalog.json_extract_path_text(input.data, 'username')) |
| 92 | FROM input ORDER BY input.id |
| 93 | \gexec |
| 94 | COMMIT;`)) |
| 95 | return nil |
| 96 | } |
nothing calls this directly
no test coverage detected