Preserve repo_type on remote update
This commit is contained in:
@@ -135,12 +135,20 @@ func (h *RemotesHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||
if oldKnown {
|
||||
remote.MutableTTL = existing.MutableTTL
|
||||
remote.QuarantineDays = existing.QuarantineDays
|
||||
remote.RepoType = existing.RepoType
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&remote); err != nil {
|
||||
http.Error(w, "invalid json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
remote.Name = name
|
||||
if remote.RepoType == "" {
|
||||
remote.RepoType = models.RepoTypeRemote
|
||||
}
|
||||
if !remote.RepoType.Valid() {
|
||||
http.Error(w, fmt.Sprintf("invalid repo type: %q", remote.RepoType), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := remote.ValidateMirrorlist(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
|
||||
@@ -172,3 +172,42 @@ func TestQuarantineDaysValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoTypeKeptOnUpdate(t *testing.T) {
|
||||
if testDSN == "" {
|
||||
t.Skip("Docker unavailable")
|
||||
}
|
||||
db, err := database.New(testDSN)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
h := NewRemotesHandler(db, nil, nil).Routes()
|
||||
repoType := func(name string) string {
|
||||
t.Helper()
|
||||
r, err := db.GetRemote(context.Background(), name)
|
||||
if err != nil {
|
||||
t.Fatalf("get %s: %v", name, err)
|
||||
}
|
||||
return string(r.RepoType)
|
||||
}
|
||||
const body = `"package_type":"rpm","base_url":"https://example.com/repo"`
|
||||
for i, s := range []struct {
|
||||
method, path, json string
|
||||
code int
|
||||
name, want string
|
||||
}{
|
||||
{"POST", "/", `{"name":"rt-remote",` + body + `}`, 201, "rt-remote", "remote"},
|
||||
{"PUT", "/rt-remote", `{` + body + `}`, 200, "rt-remote", "remote"},
|
||||
{"POST", "/", `{"name":"rt-local","package_type":"rpm","repo_type":"local"}`, 201, "rt-local", "local"},
|
||||
{"PUT", "/rt-local", `{"package_type":"rpm","description":"x"}`, 200, "rt-local", "local"},
|
||||
{"PUT", "/rt-local", `{"package_type":"rpm","repo_type":"bogus"}`, 400, "rt-local", "local"},
|
||||
} {
|
||||
if c := do(t, h, s.method, s.path, s.json); c != s.code {
|
||||
t.Fatalf("step %d %s %s = %d, want %d", i, s.method, s.path, c, s.code)
|
||||
}
|
||||
if got := repoType(s.name); got != s.want {
|
||||
t.Errorf("step %d: repo_type = %q, want %q", i, got, s.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
UPDATE remotes SET repo_type = 'remote' WHERE repo_type = '' OR repo_type IS NULL;
|
||||
Reference in New Issue
Block a user