Пример #1
0
def test_whitelist():
    clear_registry()
    from giblets import Component, ComponentManager, ExtensionPoint, ExtensionInterface, implements
    from giblets.policy import Whitelist
    
    class ICog(ExtensionInterface):
        pass

    class Widget(Component):
        cogs = ExtensionPoint(ICog)

    class GoodCog(Component):
        implements(ICog)

    class BadCog(Component):
        implements(ICog)

    
    policy = Whitelist()
    mgr = ComponentManager()
    mgr.restrict(policy)
    
    # first nothing specified, nothing should show up
    widget = Widget(mgr)
    assert has_exactly(0, GoodCog, widget.cogs)
    assert has_exactly(0, BadCog, widget.cogs)

    # enable only the GoodCog
    policy.enable_component(GoodCog)
    assert has_exactly(1, GoodCog, widget.cogs)
    assert has_exactly(0, BadCog, widget.cogs)

    # re-disable, should show nothing again
    policy.disable_component(GoodCog)
    assert has_exactly(0, GoodCog, widget.cogs)
    assert has_exactly(0, BadCog, widget.cogs)

    # try enable by name...
    policy.enable_component('tests.test_policy.GoodCog')
    assert has_exactly(1, GoodCog, widget.cogs)
    assert has_exactly(0, BadCog, widget.cogs)

    policy.disable_component('tests.test_policy.GoodCog')
    assert has_exactly(0, GoodCog, widget.cogs)
    assert has_exactly(0, BadCog, widget.cogs)
Пример #2
0
def test_patterns():
    clear_registry()
    from giblets import Component, ComponentManager, ExtensionPoint, ExtensionInterface, implements
    from giblets.policy import Patterns

    class ICog(ExtensionInterface):
        pass

    class Widget(Component):
        cogs = ExtensionPoint(ICog)

    class GoodCog(Component):
        implements(ICog)

    class BadCog(Component):
        implements(ICog)

    policy = Patterns()
    mgr = ComponentManager()
    mgr.restrict(policy)
    widget = Widget(mgr)

    # to start with, everything is disabled...
    assert has_exactly(0, GoodCog, widget.cogs)
    assert has_exactly(0, BadCog, widget.cogs)

    # easy to enable everything...
    policy.append_pattern('*', enable=True)
    assert has_exactly(1, GoodCog, widget.cogs)
    assert has_exactly(1, BadCog, widget.cogs)

    # now just disable the bad one
    pat = policy.build_pattern('tests.test_policy.BadCog', enable=False)
    policy.patterns.insert(0, pat)
    assert has_exactly(1, GoodCog, widget.cogs)
    assert has_exactly(0, BadCog, widget.cogs)
    
    # disable all the test core components...
    pat = policy.build_pattern('tests.test_policy.*', enable=False)
    policy.patterns.insert(0, pat)
    assert has_exactly(0, GoodCog, widget.cogs)
    assert has_exactly(0, BadCog, widget.cogs)
Пример #3
0
def test_blacklist():
    clear_registry()
    from giblets import Component, ComponentManager, ExtensionPoint, ExtensionInterface, implements
    from giblets.policy import Blacklist

    class ICog(ExtensionInterface):
        pass
        
    class Widget(Component):
        cogs = ExtensionPoint(ICog)
     
    class GoodCog(Component):
        implements(ICog)
        
    class BadCog(Component):
        implements(ICog)
        
    # first no restrtictions, both should show up
    policy = Blacklist()
    
    mgr = ComponentManager()
    mgr.restrict(policy)
    widget = Widget(mgr)
    assert has_exactly(1, GoodCog, widget.cogs)
    assert has_exactly(1, BadCog, widget.cogs)
    
    policy.disable_component(BadCog)
    assert has_exactly(1, GoodCog, widget.cogs)
    assert has_exactly(0, BadCog, widget.cogs)

    # re-enable
    policy.enable_component(BadCog)
    assert has_exactly(1, GoodCog, widget.cogs)
    assert has_exactly(1, BadCog, widget.cogs)

    policy.disable_component('tests.test_policy.BadCog')
    assert has_exactly(1, GoodCog, widget.cogs)
    assert has_exactly(0, BadCog, widget.cogs)

    policy.enable_component('tests.test_policy.BadCog')
    assert has_exactly(1, GoodCog, widget.cogs)
    assert has_exactly(1, BadCog, widget.cogs)
Пример #4
0
def test_extension_reg():
    clear_registry()
    from giblets import Component, ComponentManager, ExtensionPoint, ExtensionInterface, implements


    # a Train is extended by a bunch of train cars...
    class ITrainCar(ExtensionInterface):
        pass

    class Train(Component):
        cars = ExtensionPoint(ITrainCar)

    class EngineCar(Component):
        implements(ITrainCar)
        
    class PassengerCar(Component):
        implements(ITrainCar)
        
    class CafeCar(Component):
        implements(ITrainCar)
    
    class CabooseCar(Component):
        implements(ITrainCar)
        

    # an irrelevant interface and implementation...
    class IZooAnimal(ExtensionInterface):
        pass
        
    class Zebra(Component):
        implements(IZooAnimal)

    # create a component manager and the train associated with it 
    mgr1 = ComponentManager()
    train1 = Train(mgr1)
    
    # components are singletons w/ respect to manager
    assert id(train1) == id(Train(mgr1))
    
    # check that all of the extensions for the extension point were registered
    for cls in (EngineCar, PassengerCar, CafeCar, CabooseCar):
        assert has_exactly(1, cls, train1.cars)
        assert has_exactly(1, cls, mgr1.get_all(ITrainCar))
    
    # check that the Zebra is not a part of the train
    assert has_exactly(0, Zebra, train1.cars)
    assert has_exactly(0, Zebra, mgr1.get_all(ITrainCar))
    
    # create a different component manager and the train associated with it, 
    # check the same stuff...
    mgr2 = ComponentManager()
    train2 = Train(mgr2)
    assert id(train2) == id(Train(mgr2))
    for cls in (EngineCar, PassengerCar, CafeCar, CabooseCar):
        assert has_exactly(1, cls, train2.cars)
        assert has_exactly(1, cls, mgr2.get_all(ITrainCar))

    assert has_exactly(0, Zebra, train2.cars)
    assert has_exactly(0, Zebra, mgr2.get_all(ITrainCar))

    # now check that the trains and extensions produced by the
    # different component managers are different.
    assert train1 != train2
    for cls in (EngineCar, PassengerCar, CafeCar, CabooseCar):
        i1 = (x for x in train1.cars if isinstance(x, cls)).next()
        i2 = (x for x in train2.cars if isinstance(x, cls)).next()
        assert id(i1) != id(i2)
        
        i1 = (x for x in mgr1.get_all(ITrainCar) if isinstance(x, cls)).next()
        i2 = (x for x in mgr2.get_all(ITrainCar) if isinstance(x, cls)).next()

        assert id(i1) != id(i2)