mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-24 09:38:09 +01:00
add test for dao metod
This commit is contained in:
parent
4fbe18cb03
commit
6788f891f1
@ -766,6 +766,78 @@ func TestAddRepTarget(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetRepTargetByName(t *testing.T) {
|
||||||
|
target, err := GetRepTarget(targetID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get target %d: %v", targetID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
target2, err := GetRepTargetByName(target.Name)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get target %s: %v", target.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if target.Name != target2.Name {
|
||||||
|
t.Errorf("unexpected target name: %s, expected: %s", target2.Name, target.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdateRepTarget(t *testing.T) {
|
||||||
|
target := &models.RepTarget{
|
||||||
|
Name: "name",
|
||||||
|
URL: "http://url",
|
||||||
|
Username: "username",
|
||||||
|
Password: "password",
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := AddRepTarget(*target)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to add target: %v", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := DeleteRepTarget(id); err != nil {
|
||||||
|
t.Logf("failed to delete target %d: %v", id, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
target.ID = id
|
||||||
|
target.Name = "new_name"
|
||||||
|
target.URL = "http://new_url"
|
||||||
|
target.Username = "new_username"
|
||||||
|
target.Password = "new_password"
|
||||||
|
|
||||||
|
if err = UpdateRepTarget(*target); err != nil {
|
||||||
|
t.Fatalf("failed to update target: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
target, err = GetRepTarget(id)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get target %d: %v", id, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if target.Name != "new_name" {
|
||||||
|
t.Errorf("unexpected name: %s, expected: %s", target.Name, "new_name")
|
||||||
|
}
|
||||||
|
|
||||||
|
if target.URL != "http://new_url" {
|
||||||
|
t.Errorf("unexpected url: %s, expected: %s", target.URL, "http://new_url")
|
||||||
|
}
|
||||||
|
|
||||||
|
if target.Username != "new_username" {
|
||||||
|
t.Errorf("unexpected username: %s, expected: %s", target.Username, "new_username")
|
||||||
|
}
|
||||||
|
|
||||||
|
if target.Password != "new_password" {
|
||||||
|
t.Errorf("unexpected password: %s, expected: %s", target.Password, "new_password")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetAllRepTargets(t *testing.T) {
|
||||||
|
if _, err := GetAllRepTargets(); err != nil {
|
||||||
|
t.Fatalf("failed to get all targets: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestAddRepPolicy(t *testing.T) {
|
func TestAddRepPolicy(t *testing.T) {
|
||||||
policy := models.RepPolicy{
|
policy := models.RepPolicy{
|
||||||
ProjectID: 1,
|
ProjectID: 1,
|
||||||
@ -800,6 +872,23 @@ func TestAddRepPolicy(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetRepPolicyByName(t *testing.T) {
|
||||||
|
policy, err := GetRepPolicy(policyID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get policy %d: %v", policyID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
policy2, err := GetRepPolicyByName(policy.Name)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get policy %s: %v", policy.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if policy.Name != policy2.Name {
|
||||||
|
t.Errorf("unexpected name: %s, expected: %s", policy2.Name, policy.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestDisableRepPolicy(t *testing.T) {
|
func TestDisableRepPolicy(t *testing.T) {
|
||||||
err := DisableRepPolicy(policyID)
|
err := DisableRepPolicy(policyID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -11,13 +11,13 @@ import (
|
|||||||
|
|
||||||
// AddRepTarget ...
|
// AddRepTarget ...
|
||||||
func AddRepTarget(target models.RepTarget) (int64, error) {
|
func AddRepTarget(target models.RepTarget) (int64, error) {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
return o.Insert(&target)
|
return o.Insert(&target)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRepTarget ...
|
// GetRepTarget ...
|
||||||
func GetRepTarget(id int64) (*models.RepTarget, error) {
|
func GetRepTarget(id int64) (*models.RepTarget, error) {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
t := models.RepTarget{ID: id}
|
t := models.RepTarget{ID: id}
|
||||||
err := o.Read(&t)
|
err := o.Read(&t)
|
||||||
if err == orm.ErrNoRows {
|
if err == orm.ErrNoRows {
|
||||||
@ -28,7 +28,7 @@ func GetRepTarget(id int64) (*models.RepTarget, error) {
|
|||||||
|
|
||||||
// GetRepTargetByName ...
|
// GetRepTargetByName ...
|
||||||
func GetRepTargetByName(name string) (*models.RepTarget, error) {
|
func GetRepTargetByName(name string) (*models.RepTarget, error) {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
t := models.RepTarget{Name: name}
|
t := models.RepTarget{Name: name}
|
||||||
err := o.Read(&t, "Name")
|
err := o.Read(&t, "Name")
|
||||||
if err == orm.ErrNoRows {
|
if err == orm.ErrNoRows {
|
||||||
@ -39,26 +39,21 @@ func GetRepTargetByName(name string) (*models.RepTarget, error) {
|
|||||||
|
|
||||||
// DeleteRepTarget ...
|
// DeleteRepTarget ...
|
||||||
func DeleteRepTarget(id int64) error {
|
func DeleteRepTarget(id int64) error {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
_, err := o.Delete(&models.RepTarget{ID: id})
|
_, err := o.Delete(&models.RepTarget{ID: id})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRepTarget ...
|
// UpdateRepTarget ...
|
||||||
func UpdateRepTarget(target models.RepTarget) error {
|
func UpdateRepTarget(target models.RepTarget) error {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
if len(target.Password) != 0 {
|
_, err := o.Update(&target, "URL", "Name", "Username", "Password")
|
||||||
_, err := o.Update(&target)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := o.Update(&target, "URL", "Name", "Username")
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllRepTargets ...
|
// GetAllRepTargets ...
|
||||||
func GetAllRepTargets() ([]*models.RepTarget, error) {
|
func GetAllRepTargets() ([]*models.RepTarget, error) {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
qs := o.QueryTable(&models.RepTarget{})
|
qs := o.QueryTable(&models.RepTarget{})
|
||||||
var targets []*models.RepTarget
|
var targets []*models.RepTarget
|
||||||
_, err := qs.All(&targets)
|
_, err := qs.All(&targets)
|
||||||
@ -67,7 +62,7 @@ func GetAllRepTargets() ([]*models.RepTarget, error) {
|
|||||||
|
|
||||||
// AddRepPolicy ...
|
// AddRepPolicy ...
|
||||||
func AddRepPolicy(policy models.RepPolicy) (int64, error) {
|
func AddRepPolicy(policy models.RepPolicy) (int64, error) {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
sqlTpl := `insert into replication_policy (name, project_id, target_id, enabled, description, cron_str, start_time, creation_time, update_time ) values (?, ?, ?, ?, ?, ?, %s, NOW(), NOW())`
|
sqlTpl := `insert into replication_policy (name, project_id, target_id, enabled, description, cron_str, start_time, creation_time, update_time ) values (?, ?, ?, ?, ?, ?, %s, NOW(), NOW())`
|
||||||
var sql string
|
var sql string
|
||||||
if policy.Enabled == 1 {
|
if policy.Enabled == 1 {
|
||||||
@ -89,7 +84,7 @@ func AddRepPolicy(policy models.RepPolicy) (int64, error) {
|
|||||||
|
|
||||||
// GetRepPolicy ...
|
// GetRepPolicy ...
|
||||||
func GetRepPolicy(id int64) (*models.RepPolicy, error) {
|
func GetRepPolicy(id int64) (*models.RepPolicy, error) {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
p := models.RepPolicy{ID: id}
|
p := models.RepPolicy{ID: id}
|
||||||
err := o.Read(&p)
|
err := o.Read(&p)
|
||||||
if err == orm.ErrNoRows {
|
if err == orm.ErrNoRows {
|
||||||
@ -100,7 +95,7 @@ func GetRepPolicy(id int64) (*models.RepPolicy, error) {
|
|||||||
|
|
||||||
// GetRepPolicyByName ...
|
// GetRepPolicyByName ...
|
||||||
func GetRepPolicyByName(name string) (*models.RepPolicy, error) {
|
func GetRepPolicyByName(name string) (*models.RepPolicy, error) {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
p := models.RepPolicy{Name: name}
|
p := models.RepPolicy{Name: name}
|
||||||
err := o.Read(&p, "Name")
|
err := o.Read(&p, "Name")
|
||||||
if err == orm.ErrNoRows {
|
if err == orm.ErrNoRows {
|
||||||
@ -112,21 +107,21 @@ func GetRepPolicyByName(name string) (*models.RepPolicy, error) {
|
|||||||
// GetRepPolicyByProject ...
|
// GetRepPolicyByProject ...
|
||||||
func GetRepPolicyByProject(projectID int64) ([]*models.RepPolicy, error) {
|
func GetRepPolicyByProject(projectID int64) ([]*models.RepPolicy, error) {
|
||||||
var res []*models.RepPolicy
|
var res []*models.RepPolicy
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
_, err := o.QueryTable("replication_policy").Filter("project_id", projectID).All(&res)
|
_, err := o.QueryTable("replication_policy").Filter("project_id", projectID).All(&res)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteRepPolicy ...
|
// DeleteRepPolicy ...
|
||||||
func DeleteRepPolicy(id int64) error {
|
func DeleteRepPolicy(id int64) error {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
_, err := o.Delete(&models.RepPolicy{ID: id})
|
_, err := o.Delete(&models.RepPolicy{ID: id})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRepPolicyEnablement ...
|
// UpdateRepPolicyEnablement ...
|
||||||
func UpdateRepPolicyEnablement(id int64, enabled int) error {
|
func UpdateRepPolicyEnablement(id int64, enabled int) error {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
p := models.RepPolicy{
|
p := models.RepPolicy{
|
||||||
ID: id,
|
ID: id,
|
||||||
Enabled: enabled}
|
Enabled: enabled}
|
||||||
@ -147,7 +142,7 @@ func DisableRepPolicy(id int64) error {
|
|||||||
|
|
||||||
// AddRepJob ...
|
// AddRepJob ...
|
||||||
func AddRepJob(job models.RepJob) (int64, error) {
|
func AddRepJob(job models.RepJob) (int64, error) {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
if len(job.Status) == 0 {
|
if len(job.Status) == 0 {
|
||||||
job.Status = models.JobPending
|
job.Status = models.JobPending
|
||||||
}
|
}
|
||||||
@ -159,7 +154,7 @@ func AddRepJob(job models.RepJob) (int64, error) {
|
|||||||
|
|
||||||
// GetRepJob ...
|
// GetRepJob ...
|
||||||
func GetRepJob(id int64) (*models.RepJob, error) {
|
func GetRepJob(id int64) (*models.RepJob, error) {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
j := models.RepJob{ID: id}
|
j := models.RepJob{ID: id}
|
||||||
err := o.Read(&j)
|
err := o.Read(&j)
|
||||||
if err == orm.ErrNoRows {
|
if err == orm.ErrNoRows {
|
||||||
@ -186,20 +181,20 @@ func GetRepJobToStop(policyID int64) ([]*models.RepJob, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func repJobPolicyIDQs(policyID int64) orm.QuerySeter {
|
func repJobPolicyIDQs(policyID int64) orm.QuerySeter {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
return o.QueryTable("replication_job").Filter("policy_id", policyID)
|
return o.QueryTable("replication_job").Filter("policy_id", policyID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteRepJob ...
|
// DeleteRepJob ...
|
||||||
func DeleteRepJob(id int64) error {
|
func DeleteRepJob(id int64) error {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
_, err := o.Delete(&models.RepJob{ID: id})
|
_, err := o.Delete(&models.RepJob{ID: id})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRepJobStatus ...
|
// UpdateRepJobStatus ...
|
||||||
func UpdateRepJobStatus(id int64, status string) error {
|
func UpdateRepJobStatus(id int64, status string) error {
|
||||||
o := orm.NewOrm()
|
o := GetOrmer()
|
||||||
j := models.RepJob{
|
j := models.RepJob{
|
||||||
ID: id,
|
ID: id,
|
||||||
Status: status,
|
Status: status,
|
||||||
|
Loading…
Reference in New Issue
Block a user