func (s *PortsDocSuite) SetUpTest(c *gc.C) { s.ConnSuite.SetUpTest(c) f := factory.NewFactory(s.State) s.charm = f.MakeCharm(c, &factory.CharmParams{Name: "wordpress"}) s.service = f.MakeService(c, &factory.ServiceParams{Name: "wordpress", Charm: s.charm}) s.machine = f.MakeMachine(c, &factory.MachineParams{Series: "quantal"}) s.unit1 = f.MakeUnit(c, &factory.UnitParams{Service: s.service, Machine: s.machine}) s.unit2 = f.MakeUnit(c, &factory.UnitParams{Service: s.service, Machine: s.machine}) var err error s.subnet, err = s.State.AddSubnet(state.SubnetInfo{CIDR: "0.1.2.0/24"}) c.Assert(err, jc.ErrorIsNil) s.portsOnSubnet, err = state.GetOrCreatePorts(s.State, s.machine.Id(), s.subnet.CIDR()) c.Assert(err, jc.ErrorIsNil) c.Assert(s.portsOnSubnet, gc.NotNil) s.portsWithoutSubnet, err = state.GetOrCreatePorts(s.State, s.machine.Id(), "") c.Assert(err, jc.ErrorIsNil) c.Assert(s.portsWithoutSubnet, gc.NotNil) }
func (s *PortsDocSuite) SetUpTest(c *gc.C) { s.ConnSuite.SetUpTest(c) f := factory.NewFactory(s.State) s.charm = f.MakeCharm(c, &factory.CharmParams{Name: "wordpress"}) s.service = f.MakeService(c, &factory.ServiceParams{Name: "wordpress", Charm: s.charm}) s.machine = f.MakeMachine(c, &factory.MachineParams{Series: "quantal"}) s.unit1 = f.MakeUnit(c, &factory.UnitParams{Service: s.service, Machine: s.machine}) s.unit2 = f.MakeUnit(c, &factory.UnitParams{Service: s.service, Machine: s.machine}) var err error s.ports, err = state.GetOrCreatePorts(s.State, s.machine.Id(), network.DefaultPublic) c.Assert(err, gc.IsNil) c.Assert(s.ports, gc.NotNil) }
func (s *PortsDocSuite) SetUpTest(c *gc.C) { s.ConnSuite.SetUpTest(c) s.charm = s.AddTestingCharm(c, "wordpress") var err error s.service = s.AddTestingService(c, "wordpress", s.charm) c.Assert(err, gc.IsNil) s.unit, err = s.service.AddUnit() c.Assert(err, gc.IsNil) s.machine, err = s.State.AddMachine("quantal", state.JobHostUnits) c.Assert(err, gc.IsNil) err = s.unit.AssignToMachine(s.machine) c.Assert(err, gc.IsNil) s.ports, err = state.GetOrCreatePorts(s.State, s.machine.Id()) c.Assert(err, gc.IsNil) c.Assert(s.ports, gc.NotNil) }
func (s *PortsDocSuite) TestCreatePorts(c *gc.C) { ports, err := state.GetOrCreatePorts(s.State, s.machine.Id(), network.DefaultPublic) c.Assert(err, gc.IsNil) c.Assert(ports, gc.NotNil) err = ports.OpenPorts(state.PortRange{ FromPort: 100, ToPort: 200, UnitName: s.unit1.Name(), Protocol: "TCP", }) c.Assert(err, gc.IsNil) ports, err = state.GetPorts(s.State, s.machine.Id(), network.DefaultPublic) c.Assert(err, gc.IsNil) c.Assert(ports, gc.NotNil) c.Assert(ports.PortsForUnit(s.unit1.Name()), gc.HasLen, 1) }
func (s *PortsDocSuite) testCreatePortsWithSubnetID(c *gc.C, subnetID string) { ports, err := state.GetOrCreatePorts(s.State, s.machine.Id(), subnetID) c.Assert(err, jc.ErrorIsNil) c.Assert(ports, gc.NotNil) err = ports.OpenPorts(state.PortRange{ FromPort: 100, ToPort: 200, UnitName: s.unit1.Name(), Protocol: "TCP", }) c.Assert(err, jc.ErrorIsNil) ports, err = state.GetPorts(s.State, s.machine.Id(), subnetID) c.Assert(err, jc.ErrorIsNil) c.Assert(ports, gc.NotNil) c.Assert(ports.PortsForUnit(s.unit1.Name()), gc.HasLen, 1) }
func (s *PortsDocSuite) TestOpenAndClosePorts(c *gc.C) { testCases := []struct { about string existing []state.PortRange open *state.PortRange close *state.PortRange expected string }{{ about: "open and close same port range", existing: nil, open: &state.PortRange{ FromPort: 100, ToPort: 200, UnitName: s.unit1.Name(), Protocol: "TCP", }, close: &state.PortRange{ FromPort: 100, ToPort: 200, UnitName: s.unit1.Name(), Protocol: "TCP", }, expected: "", }, { about: "try to close part of a port range", existing: []state.PortRange{{ FromPort: 100, ToPort: 200, UnitName: s.unit1.Name(), Protocol: "TCP", }}, open: nil, close: &state.PortRange{ FromPort: 100, ToPort: 150, UnitName: s.unit1.Name(), Protocol: "TCP", }, expected: "mismatched port ranges: port ranges 100-200/tcp \\(wordpress/0\\) and 100-150/tcp \\(wordpress/0\\) conflict", }, { about: "close an unopened port range with existing clash from other unit", existing: []state.PortRange{{ FromPort: 100, ToPort: 150, UnitName: s.unit1.Name(), Protocol: "TCP", }}, open: nil, close: &state.PortRange{ FromPort: 100, ToPort: 150, UnitName: s.unit1.Name(), Protocol: "TCP", }, expected: "", }, { about: "close an unopened port range", existing: nil, open: nil, close: &state.PortRange{ FromPort: 100, ToPort: 150, UnitName: s.unit1.Name(), Protocol: "TCP", }, expected: "", }, { about: "try to close an overlapping port range", existing: []state.PortRange{{ FromPort: 100, ToPort: 200, UnitName: s.unit1.Name(), Protocol: "TCP", }}, open: nil, close: &state.PortRange{ FromPort: 100, ToPort: 300, UnitName: s.unit1.Name(), Protocol: "TCP", }, expected: "mismatched port ranges: port ranges 100-200/tcp \\(wordpress/0\\) and 100-300/tcp \\(wordpress/0\\) conflict", }, { about: "try to open an overlapping port range with different unit", existing: []state.PortRange{{ FromPort: 100, ToPort: 200, UnitName: s.unit1.Name(), Protocol: "TCP", }}, open: &state.PortRange{ FromPort: 100, ToPort: 300, UnitName: s.unit2.Name(), Protocol: "TCP", }, expected: "cannot open ports 100-300/tcp on machine 0: port ranges 100-200/tcp \\(wordpress/0\\) and 100-300/tcp \\(wordpress/1\\) conflict", }, { about: "try to open an identical port range with different unit", existing: []state.PortRange{{ FromPort: 100, ToPort: 200, UnitName: s.unit1.Name(), Protocol: "TCP", }}, open: &state.PortRange{ FromPort: 100, ToPort: 200, UnitName: s.unit2.Name(), Protocol: "TCP", }, expected: "cannot open ports 100-200/tcp on machine 0: port ranges 100-200/tcp \\(wordpress/0\\) and 100-200/tcp \\(wordpress/1\\) conflict", }, { about: "try to open a port range with different protocol with different unit", existing: []state.PortRange{{ FromPort: 100, ToPort: 200, UnitName: s.unit1.Name(), Protocol: "TCP", }}, open: &state.PortRange{ FromPort: 100, ToPort: 200, UnitName: s.unit2.Name(), Protocol: "UDP", }, expected: "", }, { about: "try to open a non-overlapping port range with different unit", existing: []state.PortRange{{ FromPort: 100, ToPort: 200, UnitName: s.unit1.Name(), Protocol: "TCP", }}, open: &state.PortRange{ FromPort: 300, ToPort: 400, UnitName: s.unit2.Name(), Protocol: "TCP", }, expected: "", }} for i, t := range testCases { c.Logf("test %d: %s", i, t.about) ports, err := state.GetOrCreatePorts(s.State, s.machine.Id(), network.DefaultPublic) c.Assert(err, gc.IsNil) c.Assert(ports, gc.NotNil) // open ports that should exist for the test case for _, portRange := range t.existing { err := ports.OpenPorts(portRange) c.Check(err, gc.IsNil) } if t.existing != nil { err = ports.Refresh() c.Check(err, gc.IsNil) } if t.open != nil { err = ports.OpenPorts(*t.open) if t.expected == "" { c.Check(err, gc.IsNil) } else { c.Check(err, gc.ErrorMatches, t.expected) } err = ports.Refresh() c.Check(err, gc.IsNil) } if t.close != nil { err := ports.ClosePorts(*t.close) if t.expected == "" { c.Check(err, gc.IsNil) } else { c.Check(err, gc.ErrorMatches, t.expected) } } err = ports.Remove() c.Check(err, gc.IsNil) } }