mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-10 18:07:42 +01:00
Add unit test cases for replication codes
This commit is contained in:
parent
ce8a102af1
commit
8e4e5e8b54
43
src/replication/source/match_test.go
Normal file
43
src/replication/source/match_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
26
src/replication/target/target_test.go
Normal file
26
src/replication/target/target_test.go
Normal file
@ -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)
|
||||
}
|
57
src/replication/trigger/immediate_test.go
Normal file
57
src/replication/trigger/immediate_test.go
Normal file
@ -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))
|
||||
}
|
96
src/replication/trigger/manager_test.go
Normal file
96
src/replication/trigger/manager_test.go
Normal file
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user