From ddb06df1d860836111ff72357c603702e6ee483d Mon Sep 17 00:00:00 2001 From: William Bezuidenhout Date: Sat, 17 Oct 2020 12:57:12 +0200 Subject: [PATCH 1/6] add methods to add torrents in stopped state --- rtorrent/rtorrent.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/rtorrent/rtorrent.go b/rtorrent/rtorrent.go index c55890d..67348b2 100644 --- a/rtorrent/rtorrent.go +++ b/rtorrent/rtorrent.go @@ -82,7 +82,16 @@ func (r *RTorrent) WithHTTPClient(client *http.Client) *RTorrent { return r } -// Add adds a new torrent by URL +// AddStopped adds a new torrent by URL in a stopped state +func (r *RTorrent) AddStopped(url string) error { + _, err := r.xmlrpcClient.Call("load.normal", "", url) + if err != nil { + return errors.Wrap(err, "load.normal XMLRPC call failed") + } + return nil +} + +// Add adds a new torrent by URL and starts the torrent func (r *RTorrent) Add(url string) error { _, err := r.xmlrpcClient.Call("load.start", "", url) if err != nil { @@ -91,7 +100,16 @@ func (r *RTorrent) Add(url string) error { return nil } -// AddTorrent adds a new torrent by the torrent files data +// AddTorrentStopped adds a new torrent by the torrent files data but does not start the torrent +func (r *RTorrent) AddTorrentStopped(data []byte) error { + _, err := r.xmlrpcClient.Call("load.raw", "", data) + if err != nil { + return errors.Wrap(err, "load.raw XMLRPC call failed") + } + return nil +} + +// AddTorrent adds a new torrent by the torrent files data and starts the torrent func (r *RTorrent) AddTorrent(data []byte) error { _, err := r.xmlrpcClient.Call("load.raw_start", "", data) if err != nil { From e100c8ae0bd94d01b0d5cd790daabc769ed7c88d Mon Sep 17 00:00:00 2001 From: William Bezuidenhout Date: Sat, 17 Oct 2020 13:10:10 +0200 Subject: [PATCH 2/6] add test --- rtorrent/rtorrent_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/rtorrent/rtorrent_test.go b/rtorrent/rtorrent_test.go index e691d37..e07cb55 100644 --- a/rtorrent/rtorrent_test.go +++ b/rtorrent/rtorrent_test.go @@ -186,6 +186,39 @@ func TestRTorrent(t *testing.T) { require.Empty(t, torrents) }) }) + + }) + }) + + t.Run("by url (stopped)", func(t *testing.T) { + err := client.AddStopped("http://releases.ubuntu.com/19.04/ubuntu-19.04-live-server-amd64.iso.torrent") + require.NoError(t, err) + + t.Run("get torrent", func(t *testing.T) { + // It will take some time to appear, so retry a few times + tries := 0 + var torrents []Torrent + var err error + for { + <-time.After(time.Second) + torrents, err = client.GetTorrents(ViewStopped) + require.NoError(t, err) + if len(torrents) > 0 { + break + } + if tries > 10 { + require.NoError(t, errors.Errorf("torrent did not show up in time")) + } + tries++ + } + require.NotEmpty(t, torrents) + require.Len(t, torrents, 1) + require.Equal(t, "B7B0FBAB74A85D4AC170662C645982A862826455", torrents[0].Hash) + require.Equal(t, "ubuntu-19.04-live-server-amd64.iso", torrents[0].Name) + require.Equal(t, "", torrents[0].Label) + require.Equal(t, 784334848, torrents[0].Size) + require.Equal(t, "/downloads/incoming/ubuntu-19.04-live-server-amd64.iso", torrents[0].Path) + require.False(t, torrents[0].Completed) }) }) From 57bef1432701f640fc77950eb69f6c20e0e8fa2f Mon Sep 17 00:00:00 2001 From: William Bezuidenhout Date: Tue, 20 Oct 2020 21:31:01 +0200 Subject: [PATCH 3/6] update tests to not check for path for stopped torrents --- rtorrent/rtorrent_test.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/rtorrent/rtorrent_test.go b/rtorrent/rtorrent_test.go index e07cb55..33f08ba 100644 --- a/rtorrent/rtorrent_test.go +++ b/rtorrent/rtorrent_test.go @@ -217,7 +217,8 @@ func TestRTorrent(t *testing.T) { require.Equal(t, "ubuntu-19.04-live-server-amd64.iso", torrents[0].Name) require.Equal(t, "", torrents[0].Label) require.Equal(t, 784334848, torrents[0].Size) - require.Equal(t, "/downloads/incoming/ubuntu-19.04-live-server-amd64.iso", torrents[0].Path) + //no path yet since the torrent is stopped + require.Equal(t, "", torrents[0].Path) require.False(t, torrents[0].Completed) }) }) @@ -253,20 +254,10 @@ func TestRTorrent(t *testing.T) { require.Equal(t, "ubuntu-20.04.1-live-server-amd64.iso", torrents[0].Name) require.Equal(t, "", torrents[0].Label) require.Equal(t, 958398464, torrents[0].Size) - require.Equal(t, "/downloads/incoming/ubuntu-20.04.1-live-server-amd64.iso", torrents[0].Path) + //no path yet since the torrent is stopped + require.Equal(t, "", torrents[0].Path) require.False(t, torrents[0].Completed) - t.Run("get files", func(t *testing.T) { - files, err := client.GetFiles(torrents[0]) - require.NoError(t, err) - require.NotEmpty(t, files) - require.Len(t, files, 1) - for _, f := range files { - require.NotEmpty(t, f.Path) - require.NotZero(t, f.Size) - } - }) - t.Run("delete torrent", func(t *testing.T) { err := client.Delete(torrents[0]) require.NoError(t, err) From 186a983c1990c9b7444eae543dfa5d5bfac37d52 Mon Sep 17 00:00:00 2001 From: William Bezuidenhout Date: Tue, 20 Oct 2020 21:41:53 +0200 Subject: [PATCH 4/6] add fetch and deletion of stopped torrent in tests --- rtorrent/rtorrent_test.go | 62 ++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/rtorrent/rtorrent_test.go b/rtorrent/rtorrent_test.go index 33f08ba..ab0dd9d 100644 --- a/rtorrent/rtorrent_test.go +++ b/rtorrent/rtorrent_test.go @@ -220,6 +220,49 @@ func TestRTorrent(t *testing.T) { //no path yet since the torrent is stopped require.Equal(t, "", torrents[0].Path) require.False(t, torrents[0].Completed) + + t.Run("get status", func(t *testing.T) { + var status Status + var err error + <-time.After(time.Second) + status, err = client.GetStatus(torrents[0]) + require.NoError(t, err) + t.Logf("Status = %+v", status) + + require.False(t, status.Completed) + require.Zero(t, status.CompletedBytes) + require.Zero(t, status.DownRate) + require.NotZero(t, status.Size) + }) + + t.Run("delete torrent", func(t *testing.T) { + err := client.Delete(torrents[0]) + require.NoError(t, err) + + torrents, err := client.GetTorrents(ViewMain) + require.NoError(t, err) + require.Empty(t, torrents) + + t.Run("get torrent", func(t *testing.T) { + // It will take some time to disappear, so retry a few times + tries := 0 + var torrents []Torrent + var err error + for { + <-time.After(time.Second) + torrents, err = client.GetTorrents(ViewMain) + require.NoError(t, err) + if len(torrents) == 0 { + break + } + if tries > 10 { + require.NoError(t, errors.Errorf("torrent did not delete in time")) + } + tries++ + } + require.Empty(t, torrents) + }) + }) }) }) @@ -232,22 +275,13 @@ func TestRTorrent(t *testing.T) { require.NoError(t, err) t.Run("get torrent", func(t *testing.T) { - // It will take some time to appear, so retry a few times - tries := 0 var torrents []Torrent var err error - for { - <-time.After(time.Second) - torrents, err = client.GetTorrents(ViewMain) - require.NoError(t, err) - if len(torrents) > 0 { - break - } - if tries > 10 { - require.NoError(t, errors.Errorf("torrent did not show up in time")) - } - tries++ - } + + <-time.After(time.Second) + torrents, err = client.GetTorrents(ViewMain) + require.NoError(t, err) + require.NotEmpty(t, torrents) require.Len(t, torrents, 1) require.Equal(t, "36C67464C37A83478CEFF54932B5A9BDDEA636F3", torrents[0].Hash) From ca1f0cd63d34f5e8fafdb0352bfdf7fbc824f658 Mon Sep 17 00:00:00 2001 From: William Bezuidenhout Date: Tue, 20 Oct 2020 21:54:18 +0200 Subject: [PATCH 5/6] restore deleted test code and add data stopped tests --- rtorrent/rtorrent_test.go | 81 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/rtorrent/rtorrent_test.go b/rtorrent/rtorrent_test.go index ab0dd9d..595f34c 100644 --- a/rtorrent/rtorrent_test.go +++ b/rtorrent/rtorrent_test.go @@ -222,10 +222,8 @@ func TestRTorrent(t *testing.T) { require.False(t, torrents[0].Completed) t.Run("get status", func(t *testing.T) { - var status Status - var err error <-time.After(time.Second) - status, err = client.GetStatus(torrents[0]) + status, err := client.GetStatus(torrents[0]) require.NoError(t, err) t.Logf("Status = %+v", status) @@ -275,9 +273,83 @@ func TestRTorrent(t *testing.T) { require.NoError(t, err) t.Run("get torrent", func(t *testing.T) { + // It will take some time to appear, so retry a few times + tries := 0 var torrents []Torrent var err error + for { + <-time.After(time.Second) + torrents, err = client.GetTorrents(ViewMain) + require.NoError(t, err) + if len(torrents) > 0 { + break + } + if tries > 10 { + require.NoError(t, errors.Errorf("torrent did not show up in time")) + } + tries++ + } + require.NotEmpty(t, torrents) + require.Len(t, torrents, 1) + require.Equal(t, "36C67464C37A83478CEFF54932B5A9BDDEA636F3", torrents[0].Hash) + require.Equal(t, "ubuntu-20.04.1-live-server-amd64.iso", torrents[0].Name) + require.Equal(t, "", torrents[0].Label) + require.Equal(t, 958398464, torrents[0].Size) + require.Equal(t, "/downloads/incoming/ubuntu-20.04.1-live-server-amd64.iso", torrents[0].Path) + require.False(t, torrents[0].Completed) + + t.Run("get files", func(t *testing.T) { + files, err := client.GetFiles(torrents[0]) + require.NoError(t, err) + require.NotEmpty(t, files) + require.Len(t, files, 1) + for _, f := range files { + require.NotEmpty(t, f.Path) + require.NotZero(t, f.Size) + } + }) + + t.Run("delete torrent", func(t *testing.T) { + err := client.Delete(torrents[0]) + require.NoError(t, err) + torrents, err := client.GetTorrents(ViewMain) + require.NoError(t, err) + require.Empty(t, torrents) + + t.Run("get torrent", func(t *testing.T) { + // It will take some time to disappear, so retry a few times + tries := 0 + var torrents []Torrent + var err error + for { + <-time.After(time.Second) + torrents, err = client.GetTorrents(ViewMain) + require.NoError(t, err) + if len(torrents) == 0 { + break + } + if tries > 10 { + require.NoError(t, errors.Errorf("torrent did not delete in time")) + } + tries++ + } + require.Empty(t, torrents) + }) + }) + }) + }) + + t.Run("with data (stopped)", func(t *testing.T) { + b, err := ioutil.ReadFile("testdata/ubuntu-20.04.1-live-server-amd64.iso.torrent") + require.NoError(t, err) + require.NotEmpty(t, b) + + err = client.AddTorrentStopped(b) + require.NoError(t, err) + + t.Run("get torrent", func(t *testing.T) { + // It will take some time to appear, so retry a few times <-time.After(time.Second) torrents, err = client.GetTorrents(ViewMain) require.NoError(t, err) @@ -288,9 +360,6 @@ func TestRTorrent(t *testing.T) { require.Equal(t, "ubuntu-20.04.1-live-server-amd64.iso", torrents[0].Name) require.Equal(t, "", torrents[0].Label) require.Equal(t, 958398464, torrents[0].Size) - //no path yet since the torrent is stopped - require.Equal(t, "", torrents[0].Path) - require.False(t, torrents[0].Completed) t.Run("delete torrent", func(t *testing.T) { err := client.Delete(torrents[0]) From 8f10fe283100d63af704746e8e6abee4e4509630 Mon Sep 17 00:00:00 2001 From: William Bezuidenhout Date: Tue, 20 Oct 2020 21:56:56 +0200 Subject: [PATCH 6/6] fix silly uninitialized var --- rtorrent/rtorrent_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtorrent/rtorrent_test.go b/rtorrent/rtorrent_test.go index 595f34c..bdacbed 100644 --- a/rtorrent/rtorrent_test.go +++ b/rtorrent/rtorrent_test.go @@ -351,7 +351,7 @@ func TestRTorrent(t *testing.T) { t.Run("get torrent", func(t *testing.T) { // It will take some time to appear, so retry a few times <-time.After(time.Second) - torrents, err = client.GetTorrents(ViewMain) + torrents, err := client.GetTorrents(ViewMain) require.NoError(t, err) require.NotEmpty(t, torrents)