本文整理汇总了Golang中github.com/cloudfoundry/gunk/command_runner/fake_command_runner.FakeCommandRunner.WhenRunning方法的典型用法代码示例。如果您正苦于以下问题:Golang FakeCommandRunner.WhenRunning方法的具体用法?Golang FakeCommandRunner.WhenRunning怎么用?Golang FakeCommandRunner.WhenRunning使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cloudfoundry/gunk/command_runner/fake_command_runner.FakeCommandRunner
的用法示例。
在下文中一共展示了FakeCommandRunner.WhenRunning方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1:
}
var err error
defaultBaseImage, err = url.Parse("/default/image")
Expect(err).ToNot(HaveOccurred())
externalImageManager = imageplugin.New("/external-image-manager-bin", fakeCommandRunner, defaultBaseImage, idMappings)
baseImage, err = url.Parse("/hello/image")
Expect(err).ToNot(HaveOccurred())
fakeCommandRunner.WhenRunning(fake_command_runner.CommandSpec{
Path: "/external-image-manager-bin",
}, func(cmd *exec.Cmd) error {
if cmd.Stdout != nil {
cmd.Stdout.Write([]byte(fakeCmdRunnerStdout))
}
if cmd.Stderr != nil {
cmd.Stderr.Write([]byte(fakeCmdRunnerStderr))
}
return fakeCmdRunnerErr
})
})
Describe("Create", func() {
BeforeEach(func() {
fakeCmdRunnerStdout = "/this-is/your\n"
})
It("uses the correct external-image-manager binary", func() {
_, _, err := externalImageManager.Create(
logger, "hello", rootfs_provider.Spec{
示例2:
{
Path: "/sbin/iptables",
Args: []string{"--wait", "-A", "prefix-instance-some-id-log", "--jump", "RETURN"},
},
}
})
It("should set up the chain", func() {
Expect(creator.Create(logger, handle, "some-id", bridgeName, ip, network)).To(Succeed())
Expect(fakeRunner).To(HaveExecutedSerially(specs...))
})
DescribeTable("iptables failures",
func(specIndex int, errorString string) {
fakeRunner.WhenRunning(specs[specIndex], func(cmd *exec.Cmd) error {
cmd.Stderr.Write([]byte("iptables failed"))
return errors.New("Exit status blah")
})
Expect(creator.Create(logger, handle, "some-id", bridgeName, ip, network)).To(MatchError(errorString))
},
Entry("create nat instance chain", 0, "iptables: create-instance-chains: iptables failed"),
Entry("bind nat instance chain to nat prerouting chain", 1, "iptables: create-instance-chains: iptables failed"),
Entry("enable NAT for traffic coming from containers", 2, "iptables: create-instance-chains: iptables failed"),
Entry("create logging instance chain", 7, "iptables: create-instance-chains: iptables failed"),
Entry("append logging to instance chain", 8, "iptables: create-instance-chains: iptables failed"),
Entry("return from logging instance chain", 9, "iptables: create-instance-chains: iptables failed"),
)
})
Describe("ContainerTeardown", func() {
var specs []fake_command_runner.CommandSpec
示例3:
})
Context("Inside the host", func() {
Context("before container creation", func() {
It("runs the hook-parent-before-clone.sh legacy shell script", func() {
hooks.Main(hook.PARENT_BEFORE_CLONE)
Expect(fakeRunner).To(HaveExecutedSerially(fake_command_runner.CommandSpec{
Path: "hook-parent-before-clone.sh",
}))
})
Context("when the legacy shell script fails", func() {
BeforeEach(func() {
fakeRunner.WhenRunning(fake_command_runner.CommandSpec{
Path: "hook-parent-before-clone.sh",
}, func(*exec.Cmd) error {
return errors.New("o no")
})
})
It("panics", func() {
Expect(func() { hooks.Main(hook.PARENT_BEFORE_CLONE) }).To(Panic())
})
})
})
Context("after container creation", func() {
var oldWd, testDir string
BeforeEach(func() {
os.Setenv("PID", "99")
示例4:
"some-user",
"some-path",
},
}))
})
It("attaches the tarStream reader to stdin", func() {
Expect(fakeCommandRunner.ExecutedCommands()[0].Stdin).To(Equal(someStream))
})
})
Context("when it fails", func() {
It("returns the contents of stdout and error on failure", func() {
fakeCommandRunner.WhenRunning(fake_command_runner.CommandSpec{}, func(cmd *exec.Cmd) error {
cmd.Stderr.Write([]byte("some error output"))
cmd.Stdout.Write([]byte("some std output"))
return errors.New("someerror")
})
Expect(nstar.StreamIn(lagertest.NewTestLogger("test"), 12, "some-path", "some-user", someStream)).To(
MatchError(ContainSubstring("some error output")),
)
Expect(nstar.StreamIn(lagertest.NewTestLogger("test"), 12, "some-path", "some-user", someStream)).To(
MatchError(ContainSubstring("some std output")),
)
})
})
Context("when no user specified", func() {
It("streams the input to tar as root", func() {
示例5:
Expect(container.State()).To(Equal(linux_backend.StateBorn))
err := container.Start()
Expect(err).ToNot(HaveOccurred())
Expect(container.State()).To(Equal(linux_backend.StateActive))
})
Context("when start.sh fails", func() {
nastyError := errors.New("oh no!")
JustBeforeEach(func() {
fakeRunner.WhenRunning(
fake_command_runner.CommandSpec{
Path: containerDir + "/start.sh",
}, func(*exec.Cmd) error {
return nastyError
},
)
})
It("returns a wrapped error", func() {
err := container.Start()
Expect(err).To(MatchError("container: start: oh no!"))
})
It("does not change the container's state", func() {
Expect(container.State()).To(Equal(linux_backend.StateBorn))
err := container.Start()
Expect(err).To(HaveOccurred())
示例6:
commandRunner.WhenRunning(fake_command_runner.CommandSpec{
Path: "funC-stats",
}, func(cmd *exec.Cmd) error {
cmd.Stdout.Write([]byte(`{
"type": "stats",
"data": {
"cpu": {
"usage": {
"total": 1,
"kernel": 2,
"user": 3
}
},
"memory": {
"raw": {
"active_anon": 1,
"active_file": 2,
"cache": 3,
"hierarchical_memory_limit": 4,
"inactive_anon": 5,
"inactive_file": 6,
"mapped_file": 7,
"pgfault": 8,
"pgmajfault": 9,
"pgpgin": 10,
"pgpgout": 11,
"rss": 12,
"rss_huge": 13,
"total_active_anon": 14,
"total_active_file": 15,
"total_cache": 16,
"total_inactive_anon": 17,
"total_inactive_file": 18,
"total_mapped_file": 19,
"total_pgfault": 20,
"total_pgmajfault": 21,
"total_pgpgin": 22,
"total_pgpgout": 23,
"total_rss": 24,
"total_rss_huge": 25,
"total_unevictable": 26,
"total_writeback": 27,
"unevictable": 28,
"writeback": 29,
"swap": 30,
"hierarchical_memsw_limit": 31,
"total_swap": 32
}
}
}
}`))
return nil
})
示例7:
BeforeEach(func() {
runcBinary = new(fakes.FakeRuncBinary)
commandRunner = fake_command_runner.New()
logger = lagertest.NewTestLogger("test")
runner = runrunc.NewOomWatcher(commandRunner, runcBinary)
runcBinary.EventsCommandStub = func(handle string) *exec.Cmd {
return exec.Command("funC-events", "events", handle)
}
})
It("blows up if `runc events` returns an error", func() {
commandRunner.WhenRunning(fake_command_runner.CommandSpec{
Path: "funC-events",
}, func(cmd *exec.Cmd) error {
return errors.New("boom")
})
Expect(runner.WatchEvents(logger, "some-container", nil)).To(MatchError("start: boom"))
})
Context("when runc events succeeds", func() {
var (
eventsCh chan string
eventsNotifier *fakes.FakeEventsNotifier
)
BeforeEach(func() {
eventsCh = make(chan string, 2)
示例8:
It("runs a shell command", func() {
step := &containerizer.ShellRunnerStep{Runner: runner, Path: path}
err := step.Run()
Expect(err).ToNot(HaveOccurred())
Expect(runner).To(HaveStartedExecuting(
fake_command_runner.CommandSpec{
Path: "sh",
Args: []string{path},
},
))
})
It("returns error if fails to start a shell command", func() {
runner.WhenRunning(fake_command_runner.CommandSpec{}, func(*exec.Cmd) error {
return errors.New("what")
})
step := &containerizer.ShellRunnerStep{Runner: runner, Path: path}
err := step.Run()
Expect(err).To(HaveOccurred())
})
It("returns error if shell command does not exit 0", func() {
runner.WhenWaitingFor(fake_command_runner.CommandSpec{}, func(*exec.Cmd) error {
return errors.New("booo")
})
step := &containerizer.ShellRunnerStep{Runner: runner, Path: path}
err := step.Run()
Expect(err).To(HaveOccurred())
示例9:
It("creates a namespace using 'ip netns add'", func() {
Expect(mgr.Create(logger, "my-namespace")).To(Succeed())
Expect(fakeRunner).To(HaveExecutedSerially(fake_command_runner.CommandSpec{
Path: "ip",
Args: []string{
"netns", "add", "my-namespace",
},
}))
})
Context("when the command fails", func() {
It("returns an error", func() {
fakeRunner.WhenRunning(
fake_command_runner.CommandSpec{},
func(*exec.Cmd) error {
return errors.New("banana")
},
)
Expect(mgr.Create(logger, "my-namespace")).NotTo(Succeed())
})
})
})
Describe("Looking up a Network Namespace path", func() {
It("looks up the Network Namespace path", func() {
Expect(ioutil.WriteFile(path.Join(netnsDir, "banana"), []byte(""), 0700)).To(Succeed())
path, theUnexpected := mgr.Lookup(logger, "banana")
Expect(theUnexpected).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(netnsDir, "banana")))
示例10:
fakeCake = new(fake_cake.FakeCake)
cleaner = &layercake.BtrfsCleaningCake{
Cake: fakeCake,
Runner: runner,
BtrfsMountPoint: btrfsMountPoint,
RemoveAll: func(dir string) error {
removedDirectories = append(removedDirectories, dir)
return nil
},
Logger: lagertest.NewTestLogger("test"),
}
runner.WhenRunning(fake_command_runner.CommandSpec{
Path: "btrfs",
Args: []string{"subvolume", "list", btrfsMountPoint},
}, func(cmd *exec.Cmd) error {
_, err := cmd.Stdout.Write([]byte(listSubvolumesOutput))
Expect(err).NotTo(HaveOccurred())
return listSubVolumeErr
})
fakeCake.PathStub = func(id layercake.ID) (string, error) {
return "/absolute/btrfs_mount/relative/path/to/" + id.GraphID(), graphDriverErr
}
})
Context("when there are no subvolumes", func() {
BeforeEach(func() {
listSubvolumesOutput = "\n"
})
It("does not invoke subvolume delete", func() {
示例11:
Args: []string{"-w", "-A", "foo-bar-baz-log", "-m", "conntrack", "--ctstate", "NEW,UNTRACKED,INVALID", "--protocol", "tcp",
"--jump", "LOG", "--log-prefix", "logPrefix"},
},
fake_command_runner.CommandSpec{
Path: "/sbin/iptables",
Args: []string{"-w", "-A", "foo-bar-baz-log", "--jump", "RETURN"},
}))
})
})
It("ignores failures to flush", func() {
someError := errors.New("y")
fakeRunner.WhenRunning(
fake_command_runner.CommandSpec{
Path: "/sbin/iptables",
Args: []string{"-w", "-F", "foo-bar-baz-log"},
},
func(cmd *exec.Cmd) error {
return someError
})
Expect(subject.Setup("logPrefix")).To(Succeed())
})
It("ignores failures to delete", func() {
someError := errors.New("y")
fakeRunner.WhenRunning(
fake_command_runner.CommandSpec{
Path: "/sbin/iptables",
Args: []string{"-w", "-X", "foo-bar-baz-log"},
},
func(cmd *exec.Cmd) error {
示例12:
Expect(bridgeName).To(Equal("some-bridge"))
Expect(ip.String()).To(Equal("1.2.3.4"))
Expect(network.String()).To(Equal("2.3.4.0/30"))
})
for _, cmd := range []string{"in"} {
command := cmd
Context("when net.sh "+cmd+" fails", func() {
disaster := errors.New("oh no!")
JustBeforeEach(func() {
fakeRunner.WhenRunning(
fake_command_runner.CommandSpec{
Path: containerDir + "/net.sh",
Args: []string{command},
}, func(*exec.Cmd) error {
return disaster
},
)
})
It("returns the error", func() {
err := container.Restore(linux_backend.LinuxContainerSpec{
State: "active",
Events: []string{},
Resources: containerResources,
NetIns: []linux_backend.NetInSpec{
{
HostPort: 1234,
ContainerPort: 5678,
示例13:
Expect(aufsCake.Create(namespacedChildID, parentID, "")).To(Equal(testError))
})
It("should not unmount the parent", func() {
Expect(aufsCake.Create(namespacedChildID, parentID, "")).To(Equal(testError))
Expect(cake.UnmountCallCount()).To(Equal(0))
})
})
Context("when getting parent's path succeeds", func() {
var succeedingRunner *fake_command_runner.FakeCommandRunner
BeforeEach(func() {
succeedingRunner = fake_command_runner.New()
succeedingRunner.WhenRunning(fake_command_runner.CommandSpec{}, func(cmd *exec.Cmd) error {
return nil
})
})
It("should unmount the parentID", func() {
aufsCake.Runner = succeedingRunner
Expect(aufsCake.Create(namespacedChildID, parentID, "")).To(Succeed())
Expect(cake.UnmountCallCount()).To(Equal(1))
Expect(cake.UnmountArgsForCall(0)).To(Equal(parentID))
})
It("should only unmount the parentID after mounting it", func() {
cake.UnmountStub = func(id layercake.ID) error {
Expect(cake.PathCallCount()).Should(BeNumerically(">", 0))
Expect(cake.PathArgsForCall(0)).To(Equal(parentID))
return nil
示例14:
}))
}
itAppendsRule := func(chain string, args ...string) {
Expect(fakeRunner).To(HaveExecutedSerially(fake_command_runner.CommandSpec{
Path: "/sbin/iptables",
Args: append([]string{"-w", "-A", chain}, args...),
}))
}
Describe("Global chains setup", func() {
Context("when the input chain does not exist", func() {
BeforeEach(func() {
fakeRunner.WhenRunning(fake_command_runner.CommandSpec{
Path: "/sbin/iptables",
Args: []string{"-w", "-L", "prefix-input"},
}, func(_ *exec.Cmd) error {
return errors.New("exit status 1")
})
})
It("runs the setup script, passing the environment variables", func() {
Expect(starter.Start()).To(Succeed())
itSetsUpGlobalChains()
})
Context("when running the setup script fails", func() {
BeforeEach(func() {
fakeRunner.WhenRunning(fake_command_runner.CommandSpec{
Path: "bash",
Args: []string{"-c", iptables.SetupScript},
示例15:
Env: []string{
"BURST=256",
fmt.Sprintf("RATE=%d", 128*8),
},
},
))
})
Context("when net_rate.sh fails", func() {
nastyError := errors.New("oh no!")
BeforeEach(func() {
fakeRunner.WhenRunning(
fake_command_runner.CommandSpec{
Path: "/depot/some-id/net_rate.sh",
}, func(*exec.Cmd) error {
return nastyError
},
)
})
It("returns the error", func() {
err := bandwidthManager.SetLimits(warden.BandwidthLimits{
RateInBytesPerSecond: 128,
BurstRateInBytesPerSecond: 256,
})
Expect(err).To(Equal(nastyError))
})
})
})