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

     1  package linux_backend_test
     2  
     3  import (
     4  	"errors"
     5  	"os/exec"
     6  
     7  	"code.cloudfoundry.org/garden-linux/hook"
     8  	"code.cloudfoundry.org/garden-linux/linux_backend"
     9  	"github.com/cloudfoundry/gunk/command_runner/fake_command_runner"
    10  
    11  	"os"
    12  
    13  	"net"
    14  
    15  	networkFakes "code.cloudfoundry.org/garden-linux/network/fakes"
    16  	"code.cloudfoundry.org/garden-linux/process"
    17  	. "github.com/cloudfoundry/gunk/command_runner/fake_command_runner/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("Hooks", func() {
    23  	var hooks hook.HookSet
    24  	var fakeRunner *fake_command_runner.FakeCommandRunner
    25  	var config process.Env
    26  	var fakeNetworkConfigurer *networkFakes.FakeConfigurer
    27  
    28  	BeforeEach(func() {
    29  		hooks = make(hook.HookSet)
    30  		fakeRunner = fake_command_runner.New()
    31  		config = process.Env{
    32  			"id":                      "someID",
    33  			"network_cidr":            "1.2.3.4/8",
    34  			"container_iface_mtu":     "5000",
    35  			"network_container_ip":    "1.6.6.6",
    36  			"network_host_ip":         "1.2.3.5",
    37  			"network_host_iface":      "hostIfc",
    38  			"network_container_iface": "containerIfc",
    39  			"bridge_iface":            "bridgeName",
    40  		}
    41  		fakeNetworkConfigurer = &networkFakes.FakeConfigurer{}
    42  	})
    43  
    44  	Context("After RegisterHooks has been run", func() {
    45  		JustBeforeEach(func() {
    46  			linux_backend.RegisterHooks(hooks, fakeRunner, config, fakeNetworkConfigurer)
    47  		})
    48  
    49  		Context("Inside the host", func() {
    50  			Context("before container creation", func() {
    51  				It("runs the hook-parent-before-clone.sh legacy shell script", func() {
    52  					hooks.Main(hook.PARENT_BEFORE_CLONE)
    53  					Expect(fakeRunner).To(HaveExecutedSerially(fake_command_runner.CommandSpec{
    54  						Path: "hook-parent-before-clone.sh",
    55  					}))
    56  				})
    57  
    58  				Context("when the legacy shell script fails", func() {
    59  					BeforeEach(func() {
    60  						fakeRunner.WhenRunning(fake_command_runner.CommandSpec{
    61  							Path: "hook-parent-before-clone.sh",
    62  						}, func(*exec.Cmd) error {
    63  							return errors.New("o no")
    64  						})
    65  					})
    66  
    67  					It("panics", func() {
    68  						Expect(func() { hooks.Main(hook.PARENT_BEFORE_CLONE) }).To(Panic())
    69  					})
    70  				})
    71  			})
    72  
    73  			Context("after container creation", func() {
    74  				var oldWd, testDir string
    75  
    76  				BeforeEach(func() {
    77  					os.Setenv("PID", "99")
    78  				})
    79  
    80  				AfterEach(func() {
    81  					if oldWd != "" {
    82  						os.Chdir(oldWd)
    83  					}
    84  
    85  					if testDir != "" {
    86  						os.RemoveAll(testDir)
    87  					}
    88  				})
    89  
    90  				It("configures the host's network correctly", func() {
    91  					Expect(func() { hooks.Main(hook.PARENT_AFTER_CLONE) }).ToNot(Panic())
    92  
    93  					Expect(fakeNetworkConfigurer.ConfigureHostCallCount()).To(Equal(1))
    94  					hostConfig := fakeNetworkConfigurer.ConfigureHostArgsForCall(0)
    95  					Expect(hostConfig.HostIntf).To(Equal("hostIfc"))
    96  					Expect(hostConfig.ContainerIntf).To(Equal("containerIfc"))
    97  					Expect(hostConfig.BridgeName).To(Equal("bridgeName"))
    98  					Expect(hostConfig.ContainerPid).To(Equal(99))
    99  					Expect(hostConfig.BridgeIP).To(Equal(net.ParseIP("1.2.3.5")))
   100  					_, expectedSubnet, _ := net.ParseCIDR("1.2.3.4/8")
   101  					Expect(hostConfig.Subnet).To(Equal(expectedSubnet))
   102  					Expect(hostConfig.Mtu).To(Equal(5000))
   103  				})
   104  
   105  				Context("when the network configurer fails", func() {
   106  					BeforeEach(func() {
   107  						fakeNetworkConfigurer.ConfigureHostReturns(errors.New("oh no!"))
   108  					})
   109  
   110  					It("panics", func() {
   111  						Expect(func() { hooks.Main(hook.PARENT_AFTER_CLONE) }).To(Panic())
   112  					})
   113  				})
   114  
   115  				Context("when the network CIDR is badly formatted", func() {
   116  					BeforeEach(func() {
   117  						config["network_cidr"] = "1.2.3.4/8/9"
   118  					})
   119  
   120  					It("panics", func() {
   121  						Expect(func() { hooks.Main(hook.PARENT_AFTER_CLONE) }).To(Panic())
   122  					})
   123  				})
   124  
   125  				Context("when the MTU is invalid", func() {
   126  					BeforeEach(func() {
   127  						config["container_iface_mtu"] = "x"
   128  					})
   129  
   130  					It("panics", func() {
   131  						Expect(func() { hooks.Main(hook.PARENT_AFTER_CLONE) }).To(Panic())
   132  					})
   133  				})
   134  
   135  				It("runs the hook-parent-after-clone.sh legacy shell script", func() {
   136  					Expect(func() { hooks.Main(hook.PARENT_AFTER_CLONE) }).ToNot(Panic())
   137  					Expect(fakeRunner).To(HaveExecutedSerially(fake_command_runner.CommandSpec{
   138  						Path: "hook-parent-after-clone.sh",
   139  					}))
   140  				})
   141  
   142  				Context("when the legacy shell script fails", func() {
   143  					BeforeEach(func() {
   144  						fakeRunner.WhenRunning(fake_command_runner.CommandSpec{
   145  							Path: "hook-parent-after-clone.sh",
   146  						}, func(*exec.Cmd) error {
   147  							return errors.New("o no")
   148  						})
   149  					})
   150  
   151  					It("panics", func() {
   152  						Expect(func() { hooks.Main(hook.PARENT_AFTER_CLONE) }).To(Panic())
   153  					})
   154  				})
   155  			})
   156  		})
   157  	})
   158  })