func (s *mockStore) SaveRemote(remote *model.Remote) (created bool, err error) { if remote.ID == 0 { s.idCounter++ remote.ID = s.idCounter created = true } s.remotes[remote.ID] = *remote return created, nil }
func TestRemotes(t *testing.T) { // Fetch list of remotes remotesUrl := fmt.Sprintf("%s/remotes", server.URL) r := model.Remote{Driver: "ssh", Location: "foo"} rJson := `{"driver": "ssh", "location": "foo"}` request, err := http.NewRequest("POST", remotesUrl, strings.NewReader(rJson)) res, err := http.DefaultClient.Do(request) if err != nil { t.Error(err) } assert.Equal(t, 201, res.StatusCode) rslice, err := store.Remotes() assert.Equal(t, 1, len(rslice)) assert.Equal(t, "ssh", rslice[0].Driver) assert.Equal(t, "foo", rslice[0].Location) r.ID = rslice[0].ID request, err = http.NewRequest("GET", remotesUrl, nil) res, err = http.DefaultClient.Do(request) if err != nil { t.Error(err) } assert.Equal(t, 200, res.StatusCode) defer res.Body.Close() var target model.Remotes json.NewDecoder(res.Body).Decode(&target) assert.Equal(t, 1, len(target)) assert.Equal(t, r, target[0]) rJson = `{"driver": "ssh", "location": "bar"}` request, err = http.NewRequest("PUT", remotesUrl+"/"+strconv.Itoa(int(r.ID)), strings.NewReader(rJson)) res, err = http.DefaultClient.Do(request) if err != nil { t.Error(err) } assert.Equal(t, 200, res.StatusCode) rslice, err = store.Remotes() assert.Equal(t, 1, len(rslice)) assert.Equal(t, "ssh", rslice[0].Driver) assert.Equal(t, "bar", rslice[0].Location) request, err = http.NewRequest("DELETE", remotesUrl+"/"+strconv.Itoa(int(r.ID)), nil) res, err = http.DefaultClient.Do(request) if err != nil { t.Error(err) } assert.Equal(t, 200, res.StatusCode) rslice, err = store.Remotes() assert.Equal(t, 0, len(rslice)) }
func (s *mockStore) DeleteRemote(remote *model.Remote) (deleted bool, err error) { jobs, err := s.JobsForRemote(remote) if err != nil { return false, err } for _, j := range jobs { s.DeleteJob(&j) } if _, ok := s.remotes[remote.ID]; ok { delete(s.remotes, remote.ID) deleted = true } remote.ID = 0 return deleted, nil }