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

serve/restic: Disallow overwriting files in append-only mode #2196

Merged
merged 5 commits into from
Apr 4, 2018

Conversation

fd0
Copy link
Contributor

@fd0 fd0 commented Apr 2, 2018

This commits add tests for the HTTP handler for rclone serve restic in append-only mode. It is tested that files cannot be overwritten or deleted. See #2186 for some background.

Closes #2195

@fd0
Copy link
Contributor Author

fd0 commented Apr 2, 2018

Hm. The failing test for AppVeyor on Windows seems to not be caused by my changes. @ncw can you please restart it?

@ncw
Copy link
Member

ncw commented Apr 2, 2018

Very nice, and particularly the tests - not familiar with httptest.ResponseRecorder - it is a very nice way of testing http servers I'll add to my toolkit! I like your functional testing style for testing lots of optional things too.

I had to add this

diff --git a/cmd/serve/restic/restic_appendonly_test.go b/cmd/serve/restic/restic_appendonly_test.go
index c7ae4116..cef033e1 100644
--- a/cmd/serve/restic/restic_appendonly_test.go
+++ b/cmd/serve/restic/restic_appendonly_test.go
@@ -175,6 +175,9 @@ func TestResticHandler(t *testing.T) {
 
 	// globally set append-only mode
 	appendOnly = true
+	defer func() {
+		appendOnly = false // reset when done
+	}()
 
 	// make a new file system in the temp dir
 	f := cmd.NewFsSrc([]string{tempdir})

To make the integration tests pass too (ie where rclone calls restics REST tests). Everyone loves global variables :-(

I normally use assert and require to check results from tests which are a very thin wrapper about the if / log, just saving a bit of typing and making consistent log outputs.

So

	if err != nil {
		t.Fatal(err)
	}

Could become

        require.NoError(err)

And

		if res.Code != code {
			t.Errorf("wrong response code, want %v, got %v", code, res.Code)
		}

could become (note params are t, want, got)

                assert.Equal(t, code, res.Code)

Anyway that is a minor thing and if you hate assert/require then don't bother changing them :-)

@fd0
Copy link
Contributor Author

fd0 commented Apr 2, 2018

Thanks for the comments! I'll change the few minor things you mentioned. I'm fine with whatever testing style is preferred for the project :)

@fd0
Copy link
Contributor Author

fd0 commented Apr 2, 2018

One thing that bugs me every time I see assert is that I lose the compile-time type checking. if uint64(0) == int(0) fails to compile, whereas for assert.Equal(t, uint64(0), int(0)) is only a runtime error (hopefully).

@fd0
Copy link
Contributor Author

fd0 commented Apr 2, 2018

done, feel free to squash the commits if you like.

@fd0
Copy link
Contributor Author

fd0 commented Apr 2, 2018

FYI, the testing style is not my idea, but is derived from some tests in stdlib (obviously), in this case here:

https://github.com/golang/go/blob/0250ef910f9a979a2151bd8e02b2641b74cf2f27/src/net/http/httptest/recorder_test.go#L114-L123

@ncw
Copy link
Member

ncw commented Apr 2, 2018

One thing that bugs me every time I see assert is that I lose the compile-time type checking. if uint64(0) == int(0) fails to compile, whereas for assert.Equal(t, uint64(0), int(0)) is only a runtime error (hopefully).

Yes that is certainly a slight annoyance of assert and one I've come across a few times. There are a few other little niggles too. For me, they outweight the repetitive typing though! Conceptually assert/require are very close to the standard go testing style, unlike goconvey (for instance) which always hurts my head when I have to contribute to a project using it!

Thanksf or the updates - will review/merge later today when I have more time!

@ncw ncw merged commit 5b8977a into rclone:master Apr 4, 2018
@ncw
Copy link
Member

ncw commented Apr 4, 2018

Thank you very much for that - I've merged it now :-)

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