本文整理匯總了Golang中bosh/system/fakes.FakeCmdRunner.AddCmdResult方法的典型用法代碼示例。如果您正苦於以下問題:Golang FakeCmdRunner.AddCmdResult方法的具體用法?Golang FakeCmdRunner.AddCmdResult怎麽用?Golang FakeCmdRunner.AddCmdResult使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類bosh/system/fakes.FakeCmdRunner
的用法示例。
在下文中一共展示了FakeCmdRunner.AddCmdResult方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1:
runner *fakesys.FakeCmdRunner
searcher RoutesSearcher
)
BeforeEach(func() {
runner = fakesys.NewFakeCmdRunner()
searcher = NewCmdRoutesSearcher(runner)
})
Describe("SearchRoutes", func() {
Context("when running command succeeds", func() {
It("returns parsed routes information", func() {
runner.AddCmdResult("route -n", fakesys.FakeCmdResult{
Stdout: `Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.16.79.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 172.16.79.1 0.0.0.0 UG 0 0 0 eth0
`,
})
routes, err := searcher.SearchRoutes()
Expect(err).ToNot(HaveOccurred())
Expect(routes).To(Equal([]Route{
Route{Destination: "172.16.79.0", Gateway: "0.0.0.0", InterfaceName: "eth0"},
Route{Destination: "169.254.0.0", Gateway: "0.0.0.0", InterfaceName: "eth0"},
Route{Destination: "0.0.0.0", Gateway: "172.16.79.1", InterfaceName: "eth0"},
}))
})
It("ignores empty lines", func() {
runner.AddCmdResult("route -n", fakesys.FakeCmdResult{
示例2: init
func init() {
const expectedUbuntuDHCPConfig = `# Generated by bosh-agent
option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;
send host-name "<hostname>";
request subnet-mask, broadcast-address, time-offset, routers,
domain-name, domain-name-servers, domain-search, host-name,
netbios-name-servers, netbios-scope, interface-mtu,
rfc3442-classless-static-routes, ntp-servers;
prepend domain-name-servers zz.zz.zz.zz, yy.yy.yy.yy, xx.xx.xx.xx;
`
Describe("ubuntuNetManager", func() {
var (
fs *fakesys.FakeFileSystem
cmdRunner *fakesys.FakeCmdRunner
defaultNetworkResolver *fakenet.FakeDefaultNetworkResolver
netManager NetManager
)
BeforeEach(func() {
fs = fakesys.NewFakeFileSystem()
cmdRunner = fakesys.NewFakeCmdRunner()
defaultNetworkResolver = &fakenet.FakeDefaultNetworkResolver{}
logger := boshlog.NewLogger(boshlog.LevelNone)
netManager = NewUbuntuNetManager(fs, cmdRunner, defaultNetworkResolver, 1*time.Millisecond, logger)
})
Describe("SetupDhcp", func() {
networks := boshsettings.Networks{
"bosh": boshsettings.Network{
Default: []string{"dns"},
DNS: []string{"xx.xx.xx.xx", "yy.yy.yy.yy", "zz.zz.zz.zz"},
},
"vip": boshsettings.Network{
Default: []string{},
DNS: []string{"aa.aa.aa.aa"},
},
}
ItRestartsDhcp := func() {
Context("when ifconfig version is 0.7", func() {
BeforeEach(func() {
cmdRunner.AddCmdResult("ifup --version", fakesys.FakeCmdResult{
Stdout: "ifup version 0.7.47",
})
})
It("restarts dhclient", func() {
err := netManager.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
Expect(len(cmdRunner.RunCommands)).To(Equal(3))
Expect(cmdRunner.RunCommands[1]).To(Equal([]string{"ifdown", "-a", "--no-loopback"}))
Expect(cmdRunner.RunCommands[2]).To(Equal([]string{"ifup", "-a", "--no-loopback"}))
})
})
Context("when ifconfig version is 0.6", func() {
BeforeEach(func() {
cmdRunner.AddCmdResult("ifup --version", fakesys.FakeCmdResult{
Stdout: "ifup version 0.6.0",
})
})
It("restarts dhclient", func() {
err := netManager.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
Expect(len(cmdRunner.RunCommands)).To(Equal(3))
Expect(cmdRunner.RunCommands[1]).To(Equal([]string{"ifdown", "-a", "--exclude=lo"}))
Expect(cmdRunner.RunCommands[2]).To(Equal([]string{"ifup", "-a", "--exclude=lo"}))
})
})
}
ItUpdatesDhcp3Config := func() {
It("updates /etc/dhcp3/dhclient.conf", func() {
err := netManager.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
dhcpConfig := fs.GetFileTestStat("/etc/dhcp3/dhclient.conf")
Expect(dhcpConfig).ToNot(BeNil())
Expect(dhcpConfig.StringContents()).To(Equal(expectedUbuntuDHCPConfig))
})
}
ItUpdatesDhcpConfig := func() {
It("updates /etc/dhcp/dhclient.conf", func() {
err := netManager.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
dhcpConfig := fs.GetFileTestStat("/etc/dhcp/dhclient.conf")
Expect(dhcpConfig).ToNot(BeNil())
Expect(dhcpConfig.StringContents()).To(Equal(expectedUbuntuDHCPConfig))
})
}
//.........這裏部分代碼省略.........
示例3:
Expect(err).To(HaveOccurred())
})
})
})
Describe("Mount", func() {
var (
mountResults fakesys.FakeCmdResult
)
BeforeEach(func() {
mountResults = fakesys.FakeCmdResult{}
})
JustBeforeEach(func() {
runner.AddCmdResult("mount /dev/sr0 /fake/settings/path", mountResults)
})
It("runs the mount command", func() {
err := cd.Mount("/fake/settings/path")
Expect(err).NotTo(HaveOccurred())
Expect(runner.RunCommands).To(Equal([][]string{{"mount", "/dev/sr0", "/fake/settings/path"}}))
})
Context("when mount command errors", func() {
BeforeEach(func() {
mountResults = fakesys.FakeCmdResult{
Stderr: "failed to mount",
Error: errors.New("exit 1"),
}
})
示例4:
Expect(actualCmd.WorkingDir).To(Equal("/fake-working-dir"))
})
It("returns an error if it fails to save output", func() {
fs.OpenFileErr = errors.New("fake-open-file-error")
_, err := runner.RunCommand("fake-log-dir-name", "fake-log-file-name", cmd)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-open-file-error"))
})
Context("when command succeeds", func() {
BeforeEach(func() {
cmdRunner.AddCmdResult("fake-cmd fake-args", fakesys.FakeCmdResult{
Stdout: "fake-stdout",
Stderr: "fake-stderr",
ExitStatus: 0,
})
})
It("returns correct result", func() {
expectedResult := &CmdResult{
IsStdoutTruncated: false,
Stdout: []byte("fake-stdout"),
Stderr: []byte("fake-stderr"),
ExitStatus: 0,
}
result, err := runner.RunCommand("fake-log-dir-name", "fake-log-file-name", cmd)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(expectedResult))
示例5:
Expect(runner.RunCommands[1]).To(Equal([]string{"mount", "/dev/baz", "/mnt/bar"}))
})
It("returns error and does not try to unmount/mount anything when searching mounts fails", func() {
mountsSearcher.SearchMountsErr = errors.New("fake-search-mounts-err")
err := mounter.Remount("/mnt/foo", "/mnt/bar")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-search-mounts-err"))
Expect(0).To(Equal(len(runner.RunCommands)))
})
})
Describe("SwapOn", func() {
It("linux swap on", func() {
runner.AddCmdResult("swapon -s", fakesys.FakeCmdResult{Stdout: "Filename Type Size Used Priority\n"})
mounter.SwapOn("/dev/swap")
Expect(2).To(Equal(len(runner.RunCommands)))
Expect(runner.RunCommands[1]).To(Equal([]string{"swapon", "/dev/swap"}))
})
It("linux swap on when already on", func() {
runner.AddCmdResult("swapon -s", fakesys.FakeCmdResult{Stdout: swaponUsageOutput})
mounter.SwapOn("/dev/swap")
Expect(1).To(Equal(len(runner.RunCommands)))
Expect(runner.RunCommands[0]).To(Equal([]string{"swapon", "-s"}))
})
It("linux swap on when already on other device", func() {
示例6:
Expect(fileName).To(BeEmpty())
})
It("external get errs when external cli errs", func() {
tempFile, err := fs.TempFile("bosh-blobstore-external-TestGetErrsWhenExternalCliErrs")
Expect(err).ToNot(HaveOccurred())
fs.ReturnTempFile = tempFile
defer fs.RemoveAll(tempFile.Name())
expectedCmd := []string{
"bosh-blobstore-fake-provider", "-c", configPath, "get",
"fake-blob-id",
tempFile.Name(),
}
runner.AddCmdResult(strings.Join(expectedCmd, " "), fakesys.FakeCmdResult{Error: errors.New("fake-error")})
fileName, err := blobstore.Get("fake-blob-id", "")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-error"))
Expect(fileName).To(BeEmpty())
Expect(fs.FileExists(tempFile.Name())).To(BeFalse())
})
})
Describe("CleanUp", func() {
It("external clean up", func() {
file, err := fs.TempFile("bosh-blobstore-external-TestCleanUp")
Expect(err).ToNot(HaveOccurred())
fileName := file.Name()
示例7:
var (
runner *fakesys.FakeCmdRunner
searcher MountsSearcher
)
BeforeEach(func() {
runner = fakesys.NewFakeCmdRunner()
searcher = NewCmdMountsSearcher(runner)
})
Describe("SearchMounts", func() {
Context("when running command succeeds", func() {
It("returns parsed mount information", func() {
runner.AddCmdResult("mount", fakesys.FakeCmdResult{
Stdout: `devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
/dev/sda1 on /boot type ext2 (rw)
none on /tmp/warden/cgroup type tmpfs (rw)`,
})
mounts, err := searcher.SearchMounts()
Expect(err).ToNot(HaveOccurred())
Expect(mounts).To(Equal([]Mount{
Mount{PartitionPath: "devpts", MountPoint: "/dev/pts"},
Mount{PartitionPath: "tmpfs", MountPoint: "/run"},
Mount{PartitionPath: "/dev/sda1", MountPoint: "/boot"},
Mount{PartitionPath: "none", MountPoint: "/tmp/warden/cgroup"},
}))
})
It("ignores empty lines", func() {
runner.AddCmdResult("mount", fakesys.FakeCmdResult{
示例8:
Describe("Run", func() {
var (
params fakeDrainParams
)
BeforeEach(func() {
params = fakeDrainParams{
jobChange: "job_shutdown",
hashChange: "hash_unchanged",
updatedPackages: []string{"foo", "bar"},
}
})
It("runs drain script", func() {
commandResult := fakesys.FakeCmdResult{Stdout: "1"}
runner.AddCmdResult("/fake/script job_shutdown hash_unchanged foo bar", commandResult)
_, err := drainScript.Run(params)
Expect(err).ToNot(HaveOccurred())
expectedCmd := boshsys.Command{
Name: "/fake/script",
Args: []string{"job_shutdown", "hash_unchanged", "foo", "bar"},
Env: map[string]string{
"PATH": "/usr/sbin:/usr/bin:/sbin:/bin",
},
}
Expect(len(runner.RunComplexCommands)).To(Equal(1))
Expect(runner.RunComplexCommands[0]).To(Equal(expectedCmd))
})
示例9:
"bosh": boshsettings.Network{
Default: []string{"dns"},
DNS: []string{"xx.xx.xx.xx", "yy.yy.yy.yy", "zz.zz.zz.zz"},
},
"vip": boshsettings.Network{
Default: []string{},
DNS: []string{"aa.aa.aa.aa"},
},
}
})
ItRestartsDhcp := func() {
Context("when ifconfig version is 0.7", func() {
BeforeEach(func() {
cmdRunner.AddCmdResult("ifup --version", fakesys.FakeCmdResult{
Stdout: "ifup version 0.7.47",
})
})
It("restarts dhclient", func() {
err := netManager.SetupDhcp(networks, nil)
Expect(err).ToNot(HaveOccurred())
Expect(len(cmdRunner.RunCommands)).To(Equal(3))
Expect(cmdRunner.RunCommands[1]).To(Equal([]string{"ifdown", "-a", "--no-loopback"}))
Expect(cmdRunner.RunCommands[2]).To(Equal([]string{"ifup", "-a", "--no-loopback"}))
})
})
Context("when ifconfig version is 0.6", func() {
BeforeEach(func() {