Пример #1
0
func TestSystemdInitManager(t *testing.T) {
	Convey("Given a systemd implementation", t, func() {
		exec := sys.NewStubExecutor("", nil)
		sysd := &systemd.Implementation{
			UnitDir: os.TempDir(),
			Exec:    exec,
		}

		Convey("Given a systemd unit", func() {
			testUnit := systemd.NewUnit("testUnit", "here")

			Convey("It should enable the component", func() {
				sysd.Install(testUnit)

				So(<-exec.Exec, ShouldEqual, "systemctl")
				So(<-exec.Args, ShouldConsistOf, "enable", "--system", testUnit.Name())
			})

			Convey("It should disable the component", func() {
				sysd.Disable(testUnit)

				So(<-exec.Exec, ShouldEqual, "systemctl")
				So(<-exec.Args, ShouldConsistOf, "disable", testUnit.Name())
			})

			Convey("It should start the component", func() {
				sysd.Start(testUnit)

				So(<-exec.Exec, ShouldEqual, "systemctl")
				So(<-exec.Args, ShouldConsistOf, "start", testUnit.Name())
			})

			Convey("It should stop the component", func() {
				sysd.Stop(testUnit)

				So(<-exec.Exec, ShouldEqual, "systemctl")
				So(<-exec.Args, ShouldConsistOf, "stop", testUnit.Name())
			})
		})
	})
}
Пример #2
0
func TestAddingFirewallRules(t *testing.T) {
	Convey("Given an action, a target chain and a package filtering rule", t, func() {
		rule := &iptables.Rule{
			Source:        []string{"192.168.1.1", "192.168.1.2"},
			Destination:   []string{"192.168.1.3", "192.168.1.4"},
			FromInterface: "eth0",
			ToInterface:   "eth1",
			Protocol:      "tcp",
			IsSyncPackage: true,
			Target:        iptables.DropTarget,
		}

		action := iptables.Append

		chain := firewall.Chain{
			Name:  "INPUT",
			Table: firewall.Filter,
		}

		Convey("The firewall manager implementation should perform the action", func() {
			exec := sys.NewStubExecutor("", nil)
			fwllmgr := iptables.Implementation{exec}

			err := fwllmgr.Perform(action, chain, rule)
			So(err, ShouldBeNil)

			So(<-exec.Exec, ShouldEqual, "iptables")
			So(<-exec.Args, ShouldBeSuperSetOf, []string{
				"--table=filter",
				"--append=INPUT",
				"--source=192.168.1.1,192.168.1.2",
				"--destination=192.168.1.3,192.168.1.4",
				"--in-interface=eth0",
				"--out-interface=eth1",
				"--protocol=tcp",
				"--jump=DROP",
				"--syn",
			})
		})
	})

	Convey("When performing an operation", t, func() {
		exec := sys.NewStubExecutor("", nil)
		fwllmgr := iptables.Implementation{exec}

		chain := firewall.Chain{
			Name:  "INPUT",
			Table: firewall.Filter,
		}

		rule := &iptables.Rule{
			Protocol: "tcp",
			Target:   iptables.DropTarget,
		}

		Convey("The xtables lock should be acquired to prevent multiple updates at the same time", func() {
			err := fwllmgr.Perform(iptables.Append, chain, rule)
			So(err, ShouldBeNil)

			So(<-exec.Exec, ShouldEqual, "iptables")
			So(<-exec.Args, ShouldContain, "--wait")
		})
	})
}