Merge pull request #3693 from ywk253100/171127_schedual_trigger

Implement schedual trigger for replication
This commit is contained in:
Wenkai Yin 2017-11-28 13:43:01 +08:00 committed by GitHub
commit 34f70ff3b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 3 deletions

View File

@ -1,8 +1,11 @@
package trigger
import (
"errors"
"fmt"
"github.com/vmware/harbor/src/common/scheduler"
"github.com/vmware/harbor/src/common/scheduler/policy"
"github.com/vmware/harbor/src/common/scheduler/task"
"github.com/vmware/harbor/src/replication"
)
@ -25,10 +28,30 @@ func (st *ScheduleTrigger) Kind() string {
//Setup is the implementation of same method defined in Trigger interface
func (st *ScheduleTrigger) Setup() error {
return errors.New("Not implemented")
config := &policy.AlternatePolicyConfiguration{}
switch st.params.Type {
case replication.TriggerScheduleDaily:
config.Duration = 24 * 3600
config.OffsetTime = st.params.Offtime
case replication.TriggerScheduleWeekly:
config.Duration = 7 * 24 * 3600
config.OffsetTime = st.params.Offtime
config.Weekday = st.params.Weekday
default:
return fmt.Errorf("unsupported schedual trigger type: %s", st.params.Type)
}
schedulePolicy := policy.NewAlternatePolicy(assembleName(st.params.PolicyID), config)
attachTask := task.NewReplicationTask()
schedulePolicy.AttachTasks(attachTask)
return scheduler.DefaultScheduler.Schedule(schedulePolicy)
}
//Unset is the implementation of same method defined in Trigger interface
func (st *ScheduleTrigger) Unset() error {
return errors.New("Not implemented")
return scheduler.DefaultScheduler.UnSchedule(assembleName(st.params.PolicyID))
}
func assembleName(policyID int64) string {
return fmt.Sprintf("replication_policy_%d", policyID)
}

View File

@ -0,0 +1,63 @@
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package trigger
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vmware/harbor/src/common/scheduler"
"github.com/vmware/harbor/src/replication"
)
func TestAssembleName(t *testing.T) {
assert.Equal(t, "replication_policy_1", assembleName(1))
}
func TestKindOfScheduleTrigger(t *testing.T) {
trigger := NewScheduleTrigger(ScheduleParam{})
assert.Equal(t, replication.TriggerKindSchedule, trigger.Kind())
}
func TestSetupAndUnSetOfScheduleTrigger(t *testing.T) {
// invalid schedule param
trigger := NewScheduleTrigger(ScheduleParam{})
assert.NotNil(t, trigger.Setup())
// valid schedule param
var policyID int64 = 1
trigger = NewScheduleTrigger(ScheduleParam{
BasicParam: BasicParam{
PolicyID: policyID,
},
Type: replication.TriggerScheduleWeekly,
Weekday: (int8(time.Now().Weekday()) + 1) % 7,
Offtime: 0,
})
count := scheduler.DefaultScheduler.PolicyCount()
require.Nil(t, scheduler.DefaultScheduler.GetPolicy(assembleName(policyID)))
require.Nil(t, trigger.Setup())
assert.Equal(t, count+1, scheduler.DefaultScheduler.PolicyCount())
assert.NotNil(t, scheduler.DefaultScheduler.GetPolicy(assembleName(policyID)))
require.Nil(t, trigger.Unset())
assert.Equal(t, count, scheduler.DefaultScheduler.PolicyCount())
assert.Nil(t, scheduler.DefaultScheduler.GetPolicy(assembleName(policyID)))
}