Merge pull request #3907 from ywk253100/180103_ut

Add unit test cases for replication codes
This commit is contained in:
Wenkai Yin 2018-01-04 10:34:10 +08:00 committed by GitHub
commit 7639834c96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 222 additions and 0 deletions

View 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)
}
}

View 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)
}

View 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))
}

View 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)
}