github.com/geofffranks/garden-linux@v0.0.0-20160715111146-26c893169cfa/network/filter_test.go (about)

     1  package network_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/garden"
     7  	"code.cloudfoundry.org/garden-linux/network"
     8  	"code.cloudfoundry.org/garden-linux/network/iptables/fakes"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Filter", func() {
    14  	var (
    15  		fakeChain *fakes.FakeChain
    16  		filter    network.Filter
    17  	)
    18  
    19  	BeforeEach(func() {
    20  		fakeChain = new(fakes.FakeChain)
    21  		filter = network.NewFilter(fakeChain)
    22  		Expect(filter).ToNot(BeNil())
    23  	})
    24  
    25  	Context("Setup", func() {
    26  		It("sets up the chain", func() {
    27  			Expect(filter.Setup("logPrefix")).To(Succeed())
    28  			Expect(fakeChain.SetupCallCount()).To(Equal(1))
    29  			Expect(fakeChain.SetupArgsForCall(0)).To(Equal("logPrefix"))
    30  		})
    31  
    32  		Context("when chain setup returns an error", func() {
    33  			It("Setup wraps the error and returns it", func() {
    34  				fakeChain.SetupReturns(errors.New("x"))
    35  				err := filter.Setup("logPrefix")
    36  				Expect(err).To(MatchError("network: log chain setup: x"))
    37  			})
    38  		})
    39  	})
    40  
    41  	Context("TearDown", func() {
    42  		It("tears down the chain", func() {
    43  			filter.TearDown()
    44  			Expect(fakeChain.TearDownCallCount()).To(Equal(1))
    45  		})
    46  	})
    47  
    48  	Context("NetOut", func() {
    49  		It("should mutate IP tables", func() {
    50  			rule := garden.NetOutRule{}
    51  			err := filter.NetOut(rule)
    52  			Expect(err).ToNot(HaveOccurred())
    53  
    54  			Expect(fakeChain.PrependFilterRuleCallCount()).To(Equal(1))
    55  			passedRule := fakeChain.PrependFilterRuleArgsForCall(0)
    56  			Expect(passedRule).To(Equal(passedRule))
    57  		})
    58  
    59  		It("returns an error if one occurs", func() {
    60  			fakeChain.PrependFilterRuleReturns(errors.New("iptables says no"))
    61  			Expect(filter.NetOut(garden.NetOutRule{})).To(MatchError("iptables says no"))
    62  		})
    63  	})
    64  })