func createManagerAndAddContainers( driver *stest.MockStorageDriver, containers []string, f func(*container.MockContainerHandler), t *testing.T, ) *manager { if driver == nil { driver = &stest.MockStorageDriver{} } container.ClearContainerHandlerFactories() mif, err := New(driver) if err != nil { t.Fatal(err) } if ret, ok := mif.(*manager); ok { for _, name := range containers { mockHandler := container.NewMockContainerHandler(name) ret.containers[name], err = newContainerData(name, driver, mockHandler, false) if err != nil { t.Fatal(err) } f(mockHandler) } return ret } t.Fatal("Wrong type") return nil }
func createContainerDataAndSetHandler( driver storage.StorageDriver, f func(*container.MockContainerHandler), t *testing.T, ) *containerData { factory := &container.FactoryForMockContainerHandler{ Name: "factoryForMockContainer", PrepareContainerHandlerFunc: func(name string, handler *container.MockContainerHandler) { handler.Name = name f(handler) }, } container.ClearContainerHandlerFactories() container.RegisterContainerHandlerFactory(factory) if driver == nil { driver = &stest.MockStorageDriver{} } ret, err := NewContainerData("/container", driver) if err != nil { t.Fatal(err) } return ret }
func TestNewContainerHandler_SecondMatches(t *testing.T) { container.ClearContainerHandlerFactories() // Register one allways no and one always yes factory. allwaysNo := &mockContainerHandlerFactory{ Name: "no", CanHandleValue: false, CanAcceptValue: true, } container.RegisterContainerHandlerFactory(allwaysNo, []watcher.ContainerWatchSource{watcher.Raw}) allwaysYes := &mockContainerHandlerFactory{ Name: "yes", CanHandleValue: true, CanAcceptValue: true, } container.RegisterContainerHandlerFactory(allwaysYes, []watcher.ContainerWatchSource{watcher.Raw}) // The yes factory should be asked to create the ContainerHandler. mockContainer, err := mockFactory.NewContainerHandler(testContainerName, true) if err != nil { t.Error(err) } allwaysYes.On("NewContainerHandler", testContainerName).Return(mockContainer, nil) cont, _, err := container.NewContainerHandler(testContainerName, watcher.Raw, true) if err != nil { t.Error(err) } if cont == nil { t.Error("Expected container to not be nil") } }
func TestNewContainerHandler_Accept(t *testing.T) { container.ClearContainerHandlerFactories() // Register handler that can handle the container, but can't accept it. cannotHandle := &mockContainerHandlerFactory{ Name: "no", CanHandleValue: false, CanAcceptValue: true, } container.RegisterContainerHandlerFactory(cannotHandle, []watcher.ContainerWatchSource{watcher.Raw}) cannotAccept := &mockContainerHandlerFactory{ Name: "no", CanHandleValue: true, CanAcceptValue: false, } container.RegisterContainerHandlerFactory(cannotAccept, []watcher.ContainerWatchSource{watcher.Raw}) _, accept, err := container.NewContainerHandler(testContainerName, watcher.Raw, true) if err != nil { t.Error("Expected NewContainerHandler to succeed") } if accept == true { t.Error("Expected NewContainerHandler to ignore the container.") } }
func createManagerAndAddContainers( driver *stest.MockStorageDriver, containers []string, f func(*container.MockContainerHandler), t *testing.T, ) *manager { if driver == nil { driver = &stest.MockStorageDriver{} } factory := &container.FactoryForMockContainerHandler{ Name: "factoryForManager", PrepareContainerHandlerFunc: func(name string, handler *container.MockContainerHandler) { handler.Name = name found := false for _, c := range containers { if c == name { found = true } } if !found { t.Errorf("Asked to create a container with name %v, which is unknown.", name) } f(handler) }, } container.ClearContainerHandlerFactories() container.RegisterContainerHandlerFactory(factory) mif, err := New(driver) if err != nil { t.Fatal(err) } if ret, ok := mif.(*manager); ok { for _, container := range containers { ret.containers[container], err = NewContainerData(container, driver) if err != nil { t.Fatal(err) } } return ret } t.Fatal("Wrong type") return nil }
func createManagerAndAddContainers( driver *stest.MockStorageDriver, sysfs *fakesysfs.FakeSysFs, containers []string, f func(*container.MockContainerHandler), t *testing.T, ) *manager { if driver == nil { driver = &stest.MockStorageDriver{} } container.ClearContainerHandlerFactories() mif, err := New(driver, sysfs) if err != nil { t.Fatal(err) } if ret, ok := mif.(*manager); ok { for _, name := range containers { mockHandler := container.NewMockContainerHandler(name) cont, err := newContainerData(name, driver, mockHandler, nil, false) if err != nil { t.Fatal(err) } ret.containers[namespacedContainerName{ Name: name, }] = cont // Add Docker containers under their namespace. if strings.HasPrefix(name, "/docker") { ret.containers[namespacedContainerName{ Namespace: docker.DockerNamespace, Name: strings.TrimPrefix(name, "/docker/"), }] = cont } f(mockHandler) } return ret } t.Fatal("Wrong type") return nil }
func TestNewContainerHandler_NoneMatch(t *testing.T) { container.ClearContainerHandlerFactories() // Register two allways no factories. allwaysNo1 := &mockContainerHandlerFactory{ Name: "no", CanHandleValue: false, CanAcceptValue: true, } container.RegisterContainerHandlerFactory(allwaysNo1, []watcher.ContainerWatchSource{watcher.Raw}) allwaysNo2 := &mockContainerHandlerFactory{ Name: "no", CanHandleValue: false, CanAcceptValue: true, } container.RegisterContainerHandlerFactory(allwaysNo2, []watcher.ContainerWatchSource{watcher.Raw}) _, _, err := container.NewContainerHandler(testContainerName, watcher.Raw, true) if err == nil { t.Error("Expected NewContainerHandler to fail") } }
func createManagerAndAddContainers( memoryStorage *memory.InMemoryStorage, sysfs *fakesysfs.FakeSysFs, containers []string, f func(*container.MockContainerHandler), t *testing.T, ) *manager { container.ClearContainerHandlerFactories() mif := &manager{ containers: make(map[namespacedContainerName]*containerData), quitChannels: make([]chan error, 0, 2), memoryStorage: memoryStorage, } for _, name := range containers { mockHandler := container.NewMockContainerHandler(name) spec := itest.GenerateRandomContainerSpec(4) mockHandler.On("GetSpec").Return( spec, nil, ).Once() cont, err := newContainerData(name, memoryStorage, mockHandler, nil, false) if err != nil { t.Fatal(err) } mif.containers[namespacedContainerName{ Name: name, }] = cont // Add Docker containers under their namespace. if strings.HasPrefix(name, "/docker") { mif.containers[namespacedContainerName{ Namespace: docker.DockerNamespace, Name: strings.TrimPrefix(name, "/docker/"), }] = cont } f(mockHandler) } return mif }