diff --git a/src/replication/source/match_test.go b/src/replication/source/match_test.go new file mode 100644 index 000000000..b4d581b74 --- /dev/null +++ b/src/replication/source/match_test.go @@ -0,0 +1,43 @@ +// 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 source + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMatch(t *testing.T) { + cases := []struct { + pattern string + str string + matched bool + }{ + {"", "", true}, + {"*", "library", true}, + {"library/*", "library/mysql", true}, + {"library/*", "library/mysql/5.6", false}, + {"library/mysq?", "library/mysql", true}, + {"library/mysq?", "library/mysqld", false}, + } + + for _, c := range cases { + matched, err := match(c.pattern, c.str) + require.Nil(t, err) + assert.Equal(t, c.matched, matched) + } +} diff --git a/src/replication/target/target_test.go b/src/replication/target/target_test.go new file mode 100644 index 000000000..42a8f59e7 --- /dev/null +++ b/src/replication/target/target_test.go @@ -0,0 +1,26 @@ +// 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 target + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewDefaultManager(t *testing.T) { + mgr := NewDefaultManager() + assert.NotNil(t, mgr) +} diff --git a/src/replication/trigger/immediate_test.go b/src/replication/trigger/immediate_test.go new file mode 100644 index 000000000..a14dfe252 --- /dev/null +++ b/src/replication/trigger/immediate_test.go @@ -0,0 +1,57 @@ +// 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" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/utils/test" + "github.com/vmware/harbor/src/replication" +) + +func TestKindOfImmediateTrigger(t *testing.T) { + trigger := NewImmediateTrigger(ImmediateParam{}) + assert.Equal(t, replication.TriggerKindImmediate, trigger.Kind()) +} + +func TestSetupAndUnsetOfImmediateTrigger(t *testing.T) { + dao.DefaultDatabaseWatchItemDAO = &test.FakeWatchItemDAO{} + + param := ImmediateParam{} + param.PolicyID = 1 + param.OnDeletion = true + param.Namespaces = []string{"library"} + trigger := NewImmediateTrigger(param) + + err := trigger.Setup() + require.Nil(t, err) + + items, err := DefaultWatchList.Get("library", "push") + require.Nil(t, err) + assert.Equal(t, 1, len(items)) + + items, err = DefaultWatchList.Get("library", "delete") + require.Nil(t, err) + assert.Equal(t, 1, len(items)) + + err = trigger.Unset() + require.Nil(t, err) + items, err = DefaultWatchList.Get("library", "delete") + require.Nil(t, err) + assert.Equal(t, 0, len(items)) +} diff --git a/src/replication/trigger/manager_test.go b/src/replication/trigger/manager_test.go new file mode 100644 index 000000000..d02d9ee28 --- /dev/null +++ b/src/replication/trigger/manager_test.go @@ -0,0 +1,96 @@ +// 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" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" +) + +func TestCreateTrigger(t *testing.T) { + // nil policy + _, err := createTrigger(nil) + require.NotNil(t, err) + + // nil trigger + _, err = createTrigger(&models.ReplicationPolicy{}) + require.NotNil(t, err) + + // schedule trigger + trigger, err := createTrigger(&models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindSchedule, + ScheduleParam: &models.ScheduleParam{ + Type: replication.TriggerScheduleWeekly, + Weekday: 1, + Offtime: 1, + }, + }, + }) + require.Nil(t, err) + assert.NotNil(t, trigger) + + // immediate trigger + trigger, err = createTrigger(&models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindImmediate, + }, + }) + require.Nil(t, err) + assert.NotNil(t, trigger) + + // manual trigger + trigger, err = createTrigger(&models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindManual, + }, + }) + require.Nil(t, err) + assert.Nil(t, trigger) +} + +func TestSetupTrigger(t *testing.T) { + mgr := NewManager(1) + + err := mgr.SetupTrigger(&models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindSchedule, + ScheduleParam: &models.ScheduleParam{ + Type: replication.TriggerScheduleDaily, + Offtime: 1, + }, + }, + }) + assert.Nil(t, err) +} + +func TestUnsetTrigger(t *testing.T) { + mgr := NewManager(1) + + err := mgr.UnsetTrigger(&models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindSchedule, + ScheduleParam: &models.ScheduleParam{ + Type: replication.TriggerScheduleDaily, + Offtime: 1, + }, + }, + }) + assert.Nil(t, err) +}