func (s *mockStore) SaveJob(job *model.Job) (created bool, err error) { if _, ok := s.remotes[job.RemoteID]; !ok { return false, errors.New("Remote not found") } if job.ID == 0 { s.idCounter++ job.ID = s.idCounter created = true } s.jobs[job.ID] = *job return created, nil }
func (s *mockStore) DeleteJob(job *model.Job) (deleted bool, err error) { backups, err := s.BackupsForJob(job) if err != nil { return false, err } for _, b := range backups { s.DeleteBackup(&b) } if _, ok := s.jobs[job.ID]; ok { delete(s.jobs, job.ID) deleted = true } job.ID = 0 return deleted, nil }
func TestJobs(t *testing.T) { remote := &model.Remote{Driver: "ssh", Location: "foo"} store.SaveRemote(remote) jobsUrl := fmt.Sprintf("%s/jobs", server.URL) jJson := `{"remote_id": 123, "job_name": "mysql", "interval": 1200000000000, "preferred_time": "03:00"}` request, err := http.NewRequest("POST", jobsUrl, strings.NewReader(jJson)) res, err := http.DefaultClient.Do(request) if err != nil { t.Error(err) } assert.Equal(t, 500, res.StatusCode) j := model.Job{ RemoteID: remote.ID, JobName: "mysql", Interval: time.Duration(20 * time.Minute), PreferredTime: model.ClockTime{Hour: 3, Minute: 0}, } jJson = fmt.Sprintf(`{"remote_id": %d, "job_name": "mysql", "interval": 1200000000000, "preferred_time": "03:00"}`, remote.ID) request, err = http.NewRequest("POST", jobsUrl, strings.NewReader(jJson)) res, err = http.DefaultClient.Do(request) if err != nil { t.Error(err) } assert.Equal(t, 201, res.StatusCode) jslice, err := store.Jobs() assert.Equal(t, 1, len(jslice)) assert.Equal(t, "mysql", jslice[0].JobName) assert.Equal(t, remote.ID, jslice[0].RemoteID) assert.Equal(t, 3, jslice[0].PreferredTime.Hour) assert.Equal(t, 0, jslice[0].PreferredTime.Minute) j.ID = jslice[0].ID request, err = http.NewRequest("GET", jobsUrl, 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.Jobs json.NewDecoder(res.Body).Decode(&target) assert.Equal(t, 1, len(target)) assert.Equal(t, j, target[0]) jJson = `{"job_name": "rsync"}` request, err = http.NewRequest("PUT", jobsUrl+"/"+strconv.Itoa(int(j.ID)), strings.NewReader(jJson)) res, err = http.DefaultClient.Do(request) if err != nil { t.Error(err) } assert.Equal(t, 200, res.StatusCode) jslice, err = store.Jobs() assert.Equal(t, 1, len(jslice)) assert.Equal(t, "rsync", jslice[0].JobName) request, err = http.NewRequest("DELETE", jobsUrl+"/"+strconv.Itoa(int(j.ID)), nil) res, err = http.DefaultClient.Do(request) if err != nil { t.Error(err) } assert.Equal(t, 200, res.StatusCode) jslice, err = store.Jobs() assert.Equal(t, 0, len(jslice)) store.DeleteRemote(remote) }