Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AUT-350 - Adding more permissions for unit test database container. #1058

Merged
merged 2 commits into from
Nov 12, 2024

Conversation

alexcottner
Copy link
Contributor

@alexcottner alexcottner commented Nov 5, 2024

AUT-350

Problem: When unit tests run, they fail to truncate the database table as the postgres user account doesn't have permission to do that. But it fails silently without an explicit Fatalf call, so we didn't realize.

This solution: We mount an additional initdb file for the database container in docker-compose. This means we don't include it in the Dockerfile or modify schema.sql so it's clear that this is for unit tests only.

Other solutions:

  1. Modify schema.sql - This could grant our user account more permissions than intended if somebody runs it without checking.
  2. Modify the database Dockerfile - This means it would be included whenever the container is built, which again could accidentally grant our user account more permissions than intended if we decide to use a container DB in the future.
  3. Modify the postgres startup command (grant more permissions on the fly) - This feels less intuitive.
  4. Make an admin user account for the unit tests to clear data - More code just for unit tests makes me itchy.

@alexcottner alexcottner marked this pull request as ready for review November 5, 2024 21:55
@alexcottner alexcottner requested review from a team as code owners November 5, 2024 21:55
@alexcottner alexcottner requested review from jmhodges and removed request for a team November 5, 2024 21:55
@jmhodges
Copy link
Contributor

jmhodges commented Nov 5, 2024

Oh, I just approved it, but could we change our handlers_test.go to do this truncate before the test runs? I think we need to do that anyway, but would also demonstrate the problem is solved.

Or we could panic in Cleanup or something

@jmhodges
Copy link
Contributor

jmhodges commented Nov 5, 2024

I could be told nah, tho. We can do it as follow ups

@jmhodges
Copy link
Contributor

jmhodges commented Nov 7, 2024

You wanna merge this or are you thinking about doing that work? (I would like to post my PR that uses this work to require a database in contentsignaturepki)

@jmhodges
Copy link
Contributor

jmhodges commented Nov 7, 2024

Hrm, I'm not seeing this allow me to use truncate in the tests in my branch. Have you confirmed it works? Or is this me PEBKAC'ing my docker compose work?

@alexcottner
Copy link
Contributor Author

Hrm, I'm not seeing this allow me to use truncate in the tests in my branch. Have you confirmed it works? Or is this me PEBKAC'ing my docker compose work?

To double check myself, I made this change.

diff --git a/database/queries_test.go b/database/queries_test.go
index 80a84de7..39b02006 100644
--- a/database/queries_test.go
+++ b/database/queries_test.go
@@ -2,6 +2,7 @@ package database
 
 import (
        "fmt"
+       "log"
        "reflect"
        "sync"
        "testing"
@@ -17,6 +18,12 @@ func TestConcurrentEndEntityOperations(t *testing.T) {
                Host:                host + ":5432",
                MonitorPollInterval: 10 * time.Second,
        })
+
+       _, err = db.Exec("truncate table endentities;")
+       if err != nil {
+               log.Fatalf("Failed to truncate endentities;")
+       }
+
        if err != nil {
                t.Fatal(err)
        }

And then ran:

make build
docker compose run unit-test

And it worked fine for me. Wanna pair tomorrow?

@jmhodges
Copy link
Contributor

jmhodges commented Nov 8, 2024

I missed this comment in reply to me! I apologize for the delay. Let me investigate my situation. Perhaps the same problem that caused my debian GPG problems also caused my local problem.

@jmhodges
Copy link
Contributor

It was PEBKAC! I had left in code using a different user

@alexcottner alexcottner merged commit 63a724f into main Nov 12, 2024
14 checks passed
@alexcottner alexcottner deleted the aut-350 branch November 12, 2024 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants