add tests for filters
parent
6c12aee975
commit
6d4e7cffaf
@ -0,0 +1,152 @@
|
|||||||
|
package container
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/v2tec/watchtower/container/mocks"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWatchtowerContainersFilter(t *testing.T) {
|
||||||
|
container := new(mocks.FilterableContainer)
|
||||||
|
|
||||||
|
container.On("IsWatchtower").Return(true)
|
||||||
|
|
||||||
|
assert.True(t, WatchtowerContainersFilter(container))
|
||||||
|
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNoFilter(t *testing.T) {
|
||||||
|
container := new(mocks.FilterableContainer)
|
||||||
|
|
||||||
|
assert.True(t, noFilter(container))
|
||||||
|
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterByNames(t *testing.T) {
|
||||||
|
filter := filterByNames(make([]string, 0), nil)
|
||||||
|
assert.Nil(t, filter)
|
||||||
|
|
||||||
|
names := make([]string, 0)
|
||||||
|
names = append(names, "test")
|
||||||
|
|
||||||
|
filter = filterByNames(names, noFilter)
|
||||||
|
assert.NotNil(t, filter)
|
||||||
|
|
||||||
|
container := new(mocks.FilterableContainer)
|
||||||
|
container.On("Name").Return("test")
|
||||||
|
assert.True(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Name").Return("NoTest")
|
||||||
|
assert.False(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterByEnableLabel(t *testing.T) {
|
||||||
|
filter := filterByEnableLabel(noFilter)
|
||||||
|
assert.NotNil(t, filter)
|
||||||
|
|
||||||
|
container := new(mocks.FilterableContainer)
|
||||||
|
container.On("Enabled").Return(true, true)
|
||||||
|
assert.True(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Enabled").Return(false, true)
|
||||||
|
assert.True(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Enabled").Return(false, false)
|
||||||
|
assert.False(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterByDisabledLabel(t *testing.T) {
|
||||||
|
filter := filterByDisabledLabel(noFilter)
|
||||||
|
assert.NotNil(t, filter)
|
||||||
|
|
||||||
|
container := new(mocks.FilterableContainer)
|
||||||
|
container.On("Enabled").Return(true, true)
|
||||||
|
assert.True(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Enabled").Return(false, true)
|
||||||
|
assert.False(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Enabled").Return(false, false)
|
||||||
|
assert.True(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildFilter(t *testing.T) {
|
||||||
|
names := make([]string, 0)
|
||||||
|
names = append(names, "test")
|
||||||
|
|
||||||
|
filter := BuildFilter(names, false)
|
||||||
|
|
||||||
|
container := new(mocks.FilterableContainer)
|
||||||
|
container.On("Name").Return("Invalid")
|
||||||
|
container.On("Enabled").Return(false, false)
|
||||||
|
assert.False(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Name").Return("test")
|
||||||
|
container.On("Enabled").Return(false, false)
|
||||||
|
assert.True(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Name").Return("Invalid")
|
||||||
|
container.On("Enabled").Return(true, true)
|
||||||
|
assert.False(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Name").Return("test")
|
||||||
|
container.On("Enabled").Return(true, true)
|
||||||
|
assert.True(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Enabled").Return(false, true)
|
||||||
|
assert.False(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildFilterEnableLabel(t *testing.T) {
|
||||||
|
names := make([]string, 0)
|
||||||
|
names = append(names, "test")
|
||||||
|
|
||||||
|
filter := BuildFilter(names, true)
|
||||||
|
|
||||||
|
container := new(mocks.FilterableContainer)
|
||||||
|
container.On("Enabled").Return(false, false)
|
||||||
|
assert.False(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Name").Return("Invalid")
|
||||||
|
container.On("Enabled").Twice().Return(true, true)
|
||||||
|
assert.False(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Name").Return("test")
|
||||||
|
container.On("Enabled").Twice().Return(true, true)
|
||||||
|
assert.True(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
|
||||||
|
container = new(mocks.FilterableContainer)
|
||||||
|
container.On("Enabled").Return(false, true)
|
||||||
|
assert.False(t, filter(container))
|
||||||
|
container.AssertExpectations(t)
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
// Code generated by mockery v1.0.0
|
||||||
|
package mocks
|
||||||
|
|
||||||
|
import mock "github.com/stretchr/testify/mock"
|
||||||
|
|
||||||
|
// FilterableContainer is an autogenerated mock type for the FilterableContainer type
|
||||||
|
type FilterableContainer struct {
|
||||||
|
mock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enabled provides a mock function with given fields:
|
||||||
|
func (_m *FilterableContainer) Enabled() (bool, bool) {
|
||||||
|
ret := _m.Called()
|
||||||
|
|
||||||
|
var r0 bool
|
||||||
|
if rf, ok := ret.Get(0).(func() bool); ok {
|
||||||
|
r0 = rf()
|
||||||
|
} else {
|
||||||
|
r0 = ret.Get(0).(bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
var r1 bool
|
||||||
|
if rf, ok := ret.Get(1).(func() bool); ok {
|
||||||
|
r1 = rf()
|
||||||
|
} else {
|
||||||
|
r1 = ret.Get(1).(bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0, r1
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsWatchtower provides a mock function with given fields:
|
||||||
|
func (_m *FilterableContainer) IsWatchtower() bool {
|
||||||
|
ret := _m.Called()
|
||||||
|
|
||||||
|
var r0 bool
|
||||||
|
if rf, ok := ret.Get(0).(func() bool); ok {
|
||||||
|
r0 = rf()
|
||||||
|
} else {
|
||||||
|
r0 = ret.Get(0).(bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name provides a mock function with given fields:
|
||||||
|
func (_m *FilterableContainer) Name() string {
|
||||||
|
ret := _m.Called()
|
||||||
|
|
||||||
|
var r0 string
|
||||||
|
if rf, ok := ret.Get(0).(func() string); ok {
|
||||||
|
r0 = rf()
|
||||||
|
} else {
|
||||||
|
r0 = ret.Get(0).(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0
|
||||||
|
}
|
Loading…
Reference in New Issue