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

Add post-{commit,merge} #857

Merged
merged 6 commits into from
Oct 22, 2020
Merged

Add post-{commit,merge} #857

merged 6 commits into from
Oct 22, 2020

Conversation

arielshaqed
Copy link
Contributor

Part of #534 but will be useful elsewhere. Allows loosely-coupled components, will be used to
support export.

@codecov-io
Copy link

codecov-io commented Oct 21, 2020

Codecov Report

Merging #857 into master will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #857      +/-   ##
==========================================
+ Coverage   45.05%   45.09%   +0.03%     
==========================================
  Files         140      140              
  Lines       10842    10853      +11     
==========================================
+ Hits         4885     4894       +9     
- Misses       5325     5326       +1     
- Partials      632      633       +1     
Impacted Files Coverage Δ
catalog/cataloger.go 69.28% <100.00%> (+1.13%) ⬆️
catalog/cataloger_commit.go 76.81% <100.00%> (-1.98%) ⬇️
catalog/cataloger_merge.go 62.96% <100.00%> (+0.69%) ⬆️
catalog/cataloger_create_entry.go 94.73% <0.00%> (-5.27%) ⬇️
catalog/cataloger_create_repository.go 65.38% <0.00%> (+3.84%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7a8ca25...5000139. Read the comment docs.

{Path: newFilename},
{Path: overFilename, Seed: "seed1"},
{Path: delFilename, Deleted: true},
func validateMergeWithHooks(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit too much, no? a simple test of any simple merge use case should verify that hooks works - looking for an issue on a failed test that is written like that will make it hard on the developer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the simple merge case? It requires creating 2 branches, creating an entry on one, merging, and then verifying that the hook was called (first case) and also that the hook can fail merging.

Rewriting as a separate pair of tests. It keeps this test shorter, but means much more test code to understand. Worst case you can ask for the old code back.

c := testCataloger(t)

t.Run("commit hooks run and see commit", func(t *testing.T) {
repository := testCatalogerRepo(t, ctx, c, "repository", "master")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can take out of the repo creation out of both tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every other test does that too. We can refactor in a separate PR.

catalog/cataloger_commit_test.go Outdated Show resolved Hide resolved
@@ -152,6 +152,10 @@ type Merger interface {
Merge(ctx context.Context, repository, leftBranch, rightBranch, committer, message string, metadata Metadata) (*MergeResult, error)
}

type Hookser interface {
GetHooks() *CatalogerHooks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
GetHooks() *CatalogerHooks
Hooks() *CatalogerHooks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

// called in a current transaction context; if they return an error the transaction is rolled
// back. Because these transactions are current, the hook can see the effect the operation only
// on the passed transaction.
type CatalogerHooks struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a branch filter by name/regex?
o.w. you're relying on the hook itself to filter by it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall not do this. If needed in >1 hooks, we could add a wrapper ("decorator") function that filters a hook implementation for regexp. The added complexity of doing it here would include special semantics for merges -- there are two branches here.
Finally, I'm not sure it will be useful. For exports it's not, because it is hard to precompute the regexp (changes transactionally with the DB) and easy to check directly on the DB.

Copy link
Contributor Author

@arielshaqed arielshaqed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I think I covered everything. Let me know if you would like me to roll back the new post-merge hook tests, I don't particularly like them either way (old or new).

@@ -152,6 +152,10 @@ type Merger interface {
Merge(ctx context.Context, repository, leftBranch, rightBranch, committer, message string, metadata Metadata) (*MergeResult, error)
}

type Hookser interface {
GetHooks() *CatalogerHooks
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

// called in a current transaction context; if they return an error the transaction is rolled
// back. Because these transactions are current, the hook can see the effect the operation only
// on the passed transaction.
type CatalogerHooks struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall not do this. If needed in >1 hooks, we could add a wrapper ("decorator") function that filters a hook implementation for regexp. The added complexity of doing it here would include special semantics for merges -- there are two branches here.
Finally, I'm not sure it will be useful. For exports it's not, because it is hard to precompute the regexp (changes transactionally with the DB) and easy to check directly on the DB.

c := testCataloger(t)

t.Run("commit hooks run and see commit", func(t *testing.T) {
repository := testCatalogerRepo(t, ctx, c, "repository", "master")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every other test does that too. We can refactor in a separate PR.

{Path: newFilename},
{Path: overFilename, Seed: "seed1"},
{Path: delFilename, Deleted: true},
func validateMergeWithHooks(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the simple merge case? It requires creating 2 branches, creating an entry on one, merging, and then verifying that the hook was called (first case) and also that the hook can fail merging.

Rewriting as a separate pair of tests. It keeps this test shorter, but means much more test code to understand. Worst case you can ask for the old code back.

arielshaqed and others added 4 commits October 22, 2020 14:13
Part of #534 but will be useful elsewhere.  Allows loosely-coupled components, will be used to
support export.
Co-authored-by: Barak Amar <barak.amar@treeverse.io>
Also explicitly take a test name in `testCreateEntryCalcChecksum` rather than manually appending
it to a seed.
Copy link
Contributor

@nopcoder nopcoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just suggested an alternative code for the tests, all the rest is cool.

@@ -83,9 +85,9 @@ func testCatalogerGetEntry(t testing.TB, ctx context.Context, c Cataloger, repos
}
}

func testCreateEntryCalcChecksum(key string, seed string) string {
func testCreateEntryCalcChecksum(key string, testName string, seed string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the plan was to pass the test name as seed if needed and call this function.
possibly calling this function again for verification.
there is no need to contact the test name when we have seed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that if any 2 tests use the same key and seed then they clash. It caused an actual problem when I copied calls around.

@@ -1200,3 +1201,60 @@ func TestCataloger_MergeFromChildAfterMergeFromParent(t *testing.T) {
t.Fatalf("Merge err=%s, expected none", err)
}
}

func TestCataloger_Merge_Hooks(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me know what you think about the diff test code - if you think it is easier to read, update this one - max I can live with both cases or give you the code for this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will copy that code over here, you're correct that it's a bit nicer.

Copy link
Contributor Author

@arielshaqed arielshaqed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Pulling once tests pass.

@@ -1200,3 +1201,60 @@ func TestCataloger_MergeFromChildAfterMergeFromParent(t *testing.T) {
t.Fatalf("Merge err=%s, expected none", err)
}
}

func TestCataloger_Merge_Hooks(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will copy that code over here, you're correct that it's a bit nicer.

@@ -83,9 +85,9 @@ func testCatalogerGetEntry(t testing.TB, ctx context.Context, c Cataloger, repos
}
}

func testCreateEntryCalcChecksum(key string, seed string) string {
func testCreateEntryCalcChecksum(key string, testName string, seed string) string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that if any 2 tests use the same key and seed then they clash. It caused an actual problem when I copied calls around.

@arielshaqed arielshaqed merged commit cf0d129 into master Oct 22, 2020
@arielshaqed arielshaqed deleted the feature/534-hooks branch October 22, 2020 13:16
@arielshaqed arielshaqed restored the feature/534-hooks branch October 25, 2020 11:04
@arielshaqed arielshaqed deleted the feature/534-hooks branch October 25, 2020 11:07
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.

None yet

4 participants