func TestSetupHandlerUpdateFuncUsesFilter(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	mockRC := NewMockResourceController(mockCtrl)
	cw := &CRWatcher{
		Config: &Config{
			Filter: "io.nicolerenee.lostromos.filter",
		},
	}
	r1 := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing1",
			},
		},
	}
	r1Filtered := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing1",
				"annotations": map[string]interface{}{
					"io.nicolerenee.lostromos.filter": "true",
				},
			},
		},
	}
	r2 := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing2",
			},
		},
	}
	r2Filtered := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing2",
				"annotations": map[string]interface{}{
					"io.nicolerenee.lostromos.filter": "true",
				},
			},
		},
	}
	cw.setupHandler(mockRC)
	mockRC.EXPECT().ResourceUpdated(r1, r2).MinTimes(0).MaxTimes(0)
	mockRC.EXPECT().ResourceDeleted(r1Filtered)
	mockRC.EXPECT().ResourceAdded(r2Filtered)
	mockRC.EXPECT().ResourceUpdated(r1Filtered, r2Filtered)
	cw.handler.OnUpdate(r1, r2)
	cw.handler.OnUpdate(r1Filtered, r2)
	cw.handler.OnUpdate(r1, r2Filtered)
	cw.handler.OnUpdate(r1Filtered, r2Filtered)
}
Test to ensure that if we are given filter criteria we call ResourceUpdated, ResourceDeleted, and ResourceAdded
appropriately for filtered and unfiltered resources.
package crwatcher
import (
	"errors"
	"fmt"
	"testing"
	"github.com/golang/mock/gomock"
	"github.com/stretchr/testify/assert"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/util/wait"
	restclient "k8s.io/client-go/rest"
	"github.com/lostromos/lostromos/printctlr"
)
type logResult struct {
	msg string
}
type testLogger struct {
	res *logResult
}
func (c testLogger) Error(err error) {
	c.res.msg = fmt.Sprintf("error: %s", err)
}
func TestNewCRWatcher(t *testing.T) {
	kubeCfg := &restclient.Config{}
	cfg := &Config{PluralName: "test"}
	cw, err := NewCRWatcher(cfg, kubeCfg, printctlr.Controller{}, testLogger{})
	assert.Nil(t, err)
	assert.Equal(t, cfg, cw.Config)
	assert.NotNil(t, cw.resource)
	assert.NotNil(t, cw.handler)
	assert.NotNil(t, cw.store)
	assert.NotNil(t, cw.controller)
	assert.NotNil(t, cw.logger)
}
func TestNewCRWatcherReturnsNilOnError(t *testing.T) {
	kubeCfg := &restclient.Config{}
	kubeCfg.Host = "http:///"
	cfg := &Config{PluralName: "test"}
	cw, err := NewCRWatcher(cfg, kubeCfg, printctlr.Controller{}, testLogger{})
	assert.Nil(t, cw)
	assert.NotNil(t, err)
	assert.Equal(t, "host must be a URL or a host:port pair: \"http:///\"", err.Error())
}
func TestLogKubeError(t *testing.T) {
	kubeCfg := &restclient.Config{}
	cfg := &Config{PluralName: "test"}
	res := &logResult{}
	lgr := &testLogger{res: res}
	cw, err := NewCRWatcher(cfg, kubeCfg, printctlr.Controller{}, lgr)
	assert.Nil(t, err)
	assert.NotNil(t, cw.logger)
	cw.logKubeError(errors.New("test"))
	assert.Equal(t, "error: test", lgr.res.msg)
}
func TestSetupHandlerAddFunc(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	mockRC := NewMockResourceController(mockCtrl)
	cw := &CRWatcher{
		Config: &Config{},
	}
	r := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing1",
			},
		},
	}
	cw.setupHandler(mockRC)
	mockRC.EXPECT().ResourceAdded(r)
	cw.handler.OnAdd(r)
}
func TestSetupHandlerAddFuncUsesFilter(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	mockRC := NewMockResourceController(mockCtrl)
	cw := &CRWatcher{
		Config: &Config{
			Filter: "io.nicolerenee.lostromos.filter",
		},
	}
	r1 := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing1",
			},
		},
	}
	r2 := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing2",
				"annotations": map[string]interface{}{
					"io.nicolerenee.lostromos.filter": "true",
				},
			},
		},
	}
	cw.setupHandler(mockRC)
	mockRC.EXPECT().ResourceAdded(r1).MinTimes(0).MaxTimes(0)
	mockRC.EXPECT().ResourceAdded(r2)
	cw.handler.OnAdd(r1)
	cw.handler.OnAdd(r2)
}
func TestSetupHandlerDeleteFunc(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	mockRC := NewMockResourceController(mockCtrl)
	cw := &CRWatcher{
		Config: &Config{},
	}
	r := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing1",
			},
		},
	}
	cw.setupHandler(mockRC)
	mockRC.EXPECT().ResourceDeleted(r)
	cw.handler.OnDelete(r)
}
func TestSetupHandlerDeleteFuncUsesFilter(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	mockRC := NewMockResourceController(mockCtrl)
	cw := &CRWatcher{
		Config: &Config{
			Filter: "io.nicolerenee.lostromos.filter",
		},
	}
	r1 := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing1",
			},
		},
	}
	r2 := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing2",
				"annotations": map[string]interface{}{
					"io.nicolerenee.lostromos.filter": "true",
				},
			},
		},
	}
	cw.setupHandler(mockRC)
	mockRC.EXPECT().ResourceDeleted(r1).MinTimes(0).MaxTimes(0)
	mockRC.EXPECT().ResourceDeleted(r2)
	cw.handler.OnDelete(r1)
	cw.handler.OnDelete(r2)
}
func TestSetupHandlerUpdateFunc(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	mockRC := NewMockResourceController(mockCtrl)
	cw := &CRWatcher{
		Config: &Config{},
	}
	r1 := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing1",
			},
		},
	}
	r2 := &unstructured.Unstructured{
		Object: map[string]interface{}{
			"metadata": map[string]interface{}{
				"name": "Thing2",
			},
		},
	}
	cw.setupHandler(mockRC)
	mockRC.EXPECT().ResourceUpdated(r1, r2)
	cw.handler.OnUpdate(r1, r2)
}