本文整理匯總了Golang中bosh/system/fakes.FakeFileSystem.WriteFile方法的典型用法代碼示例。如果您正苦於以下問題:Golang FakeFileSystem.WriteFile方法的具體用法?Golang FakeFileSystem.WriteFile怎麽用?Golang FakeFileSystem.WriteFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類bosh/system/fakes.FakeFileSystem
的用法示例。
在下文中一共展示了FakeFileSystem.WriteFile方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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;
prepend domain-name-servers yy.yy.yy.yy;
prepend domain-name-servers xx.xx.xx.xx;
`
Describe("ubuntu", func() {
var (
collector *fakestats.FakeStatsCollector
fs *fakesys.FakeFileSystem
cmdRunner *fakesys.FakeCmdRunner
diskManager *fakedisk.FakeDiskManager
dirProvider boshdirs.DirectoriesProvider
platform Platform
cdutil *fakecd.FakeCdUtil
compressor boshcmd.Compressor
copier boshcmd.Copier
vitalsService boshvitals.Service
logger boshlog.Logger
)
BeforeEach(func() {
collector = &fakestats.FakeStatsCollector{}
fs = fakesys.NewFakeFileSystem()
cmdRunner = fakesys.NewFakeCmdRunner()
diskManager = fakedisk.NewFakeDiskManager()
dirProvider = boshdirs.NewDirectoriesProvider("/fake-dir")
cdutil = fakecd.NewFakeCdUtil()
compressor = boshcmd.NewTarballCompressor(cmdRunner, fs)
copier = boshcmd.NewCpCopier(cmdRunner, fs)
vitalsService = boshvitals.NewService(collector, dirProvider)
logger = boshlog.NewLogger(boshlog.LevelNone)
})
JustBeforeEach(func() {
netManager := boshnet.NewUbuntuNetManager(fs, cmdRunner, 1*time.Millisecond)
platform = NewLinuxPlatform(
fs,
cmdRunner,
collector,
compressor,
copier,
dirProvider,
vitalsService,
cdutil,
diskManager,
netManager,
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"},
},
}
Context("when dhcp was not previously configured", func() {
It("updates dhclient.conf", func() {
err := platform.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
dhcpConfig := fs.GetFileTestStat("/etc/dhcp3/dhclient.conf")
Expect(dhcpConfig).ToNot(BeNil())
Expect(dhcpConfig.StringContents()).To(Equal(expectedUbuntuDHCPConfig))
})
It("restarts dhclient", func() {
err := platform.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
Expect(len(cmdRunner.RunCommands)).To(Equal(2))
Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"pkill", "dhclient3"}))
Expect(cmdRunner.RunCommands[1]).To(Equal([]string{"/etc/init.d/networking", "restart"}))
})
})
Context("when dhcp was previously configured with different configuration", func() {
BeforeEach(func() {
fs.WriteFileString("/etc/dhcp3/dhclient.conf", "fake-other-configuration")
})
//.........這裏部分代碼省略.........
示例2: init
//.........這裏部分代碼省略.........
"/fake-tmp-dir/fake-path-in-archive/bin/test",
})
fs.ChmodErr = errors.New("fake-chmod-error")
err := act()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-chmod-error"))
})
It("installs bundle from decompressed tmp path of a job template", func() {
fs.TempDirDir = "/fake-tmp-dir"
var installedBeforeDecompression bool
compressor.DecompressFileToDirCallBack = func() {
installedBeforeDecompression = bundle.Installed
}
err := act()
Expect(err).ToNot(HaveOccurred())
// bundle installation did not happen before decompression
Expect(installedBeforeDecompression).To(BeFalse())
// make sure that bundle install happened after decompression
Expect(bundle.InstallSourcePath).To(Equal("/fake-tmp-dir/fake-path-in-archive"))
})
It("sets executable bit for files in bin", func() {
fs.TempDirDir = "/fake-tmp-dir"
compressor.DecompressFileToDirCallBack = func() {
fs.WriteFile("/fake-tmp-dir/fake-path-in-archive/bin/test1", []byte{})
fs.WriteFile("/fake-tmp-dir/fake-path-in-archive/bin/test2", []byte{})
fs.WriteFile("/fake-tmp-dir/fake-path-in-archive/config/test", []byte{})
}
fs.SetGlob("/fake-tmp-dir/fake-path-in-archive/bin/*", []string{
"/fake-tmp-dir/fake-path-in-archive/bin/test1",
"/fake-tmp-dir/fake-path-in-archive/bin/test2",
})
var binTest1Stats, binTest2Stats, configTestStats *fakesys.FakeFileStats
bundle.InstallCallBack = func() {
binTest1Stats = fs.GetFileTestStat("/fake-tmp-dir/fake-path-in-archive/bin/test1")
binTest2Stats = fs.GetFileTestStat("/fake-tmp-dir/fake-path-in-archive/bin/test2")
configTestStats = fs.GetFileTestStat("/fake-tmp-dir/fake-path-in-archive/config/test")
}
err := act()
Expect(err).ToNot(HaveOccurred())
// bin files are executable
Expect(int(binTest1Stats.FileMode)).To(Equal(0755))
Expect(int(binTest2Stats.FileMode)).To(Equal(0755))
// non-bin files are not made executable
Expect(int(configTestStats.FileMode)).ToNot(Equal(0755))
})
}
ItUpdatesPackages := func(act func() error) {
var packageApplier *fakepa.FakePackageApplier
示例3: init
//.........這裏部分代碼省略.........
})
}
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))
})
}
ItDoesNotRestartDhcp := func() {
It("does not restart dhclient", func() {
err := netManager.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
Expect(len(cmdRunner.RunCommands)).To(Equal(0))
})
}
Context("when dhclient3 is installed on the system", func() {
BeforeEach(func() { cmdRunner.CommandExistsValue = true })
Context("when dhcp was not previously configured", func() {
ItUpdatesDhcp3Config()
ItRestartsDhcp()
})
Context("when dhcp was previously configured with different configuration", func() {
BeforeEach(func() {
fs.WriteFileString("/etc/dhcp3/dhclient.conf", "fake-other-configuration")
})
ItUpdatesDhcp3Config()
ItRestartsDhcp()
})
Context("when dhcp was previously configured with the same configuration", func() {
BeforeEach(func() {
fs.WriteFileString("/etc/dhcp3/dhclient.conf", expectedUbuntuDHCPConfig)
})
ItUpdatesDhcp3Config()
ItDoesNotRestartDhcp()
})
})
Context("when dhclient3 is not installed on the system", func() {
BeforeEach(func() { cmdRunner.CommandExistsValue = false })
Context("when dhcp was not previously configured", func() {
ItUpdatesDhcpConfig()
ItRestartsDhcp()
})
Context("when dhcp was previously configured with different configuration", func() {
BeforeEach(func() {
fs.WriteFileString("/etc/dhcp/dhclient.conf", "fake-other-configuration")
})
ItUpdatesDhcpConfig()
ItRestartsDhcp()
})
示例4:
var (
fs *fakesys.FakeFileSystem
cmdRunner *fakesys.FakeCmdRunner
arping AddressBroadcaster
)
BeforeEach(func() {
fs = fakesys.NewFakeFileSystem()
cmdRunner = fakesys.NewFakeCmdRunner()
logger := boshlog.NewLogger(boshlog.LevelNone)
arping = NewArping(cmdRunner, fs, logger, arpingIterations, 0, 0)
})
Describe("BroadcastMACAddresses", func() {
BeforeEach(func() {
fs.WriteFile("/sys/class/net/eth0", []byte{})
fs.WriteFile("/sys/class/net/eth1", []byte{})
})
It("runs arping commands for each interface", func() {
addresses := []boship.InterfaceAddress{
boship.NewSimpleInterfaceAddress("eth0", "192.168.195.6"),
boship.NewSimpleInterfaceAddress("eth1", "127.0.0.1"),
}
arping.BroadcastMACAddresses(addresses)
countA := 0
countB := 0
a := []string{"arping", "-c", "1", "-U", "-I", "eth0", "192.168.195.6"}
示例5: init
//.........這裏部分代碼省略.........
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"},
},
}
Context("when dhcp was not previously configured", func() {
It("writes dhcp configuration", func() {
err := platform.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
dhcpConfig := fs.GetFileTestStat("/etc/dhcp/dhclient.conf")
Expect(dhcpConfig).ToNot(BeNil())
Expect(dhcpConfig.StringContents()).To(Equal(expectedCentosDHCPConfig))
})
It("restarts network", func() {
err := platform.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
Expect(len(cmdRunner.RunCommands)).To(Equal(1))
Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"service", "network", "restart"}))
})
})
Context("when dhcp was previously configured with different configuration", func() {
BeforeEach(func() {
fs.WriteFileString("/etc/dhcp/dhclient.conf", "fake-other-configuration")
})
It("updates dhcp configuration", func() {
err := platform.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
dhcpConfig := fs.GetFileTestStat("/etc/dhcp/dhclient.conf")
Expect(dhcpConfig).ToNot(BeNil())
Expect(dhcpConfig.StringContents()).To(Equal(expectedCentosDHCPConfig))
})
It("restarts network", func() {
err := platform.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
Expect(len(cmdRunner.RunCommands)).To(Equal(1))
Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"service", "network", "restart"}))
})
})
Context("when dhcp was previously configured with same configuration", func() {
BeforeEach(func() {
fs.WriteFileString("/etc/dhcp/dhclient.conf", expectedCentosDHCPConfig)
})
It("keeps dhcp configuration", func() {
err := platform.SetupDhcp(networks)
Expect(err).ToNot(HaveOccurred())
dhcpConfig := fs.GetFileTestStat("/etc/dhcp/dhclient.conf")
Expect(dhcpConfig).ToNot(BeNil())
Expect(dhcpConfig.StringContents()).To(Equal(expectedCentosDHCPConfig))
示例6: init
func init() {
Describe("prepareNetworkChange", func() {
var (
action PrepareNetworkChangeAction
fs *fakesys.FakeFileSystem
settingsService *fakesettings.FakeSettingsService
)
BeforeEach(func() {
fs = fakesys.NewFakeFileSystem()
settingsService = &fakesettings.FakeSettingsService{}
action = NewPrepareNetworkChange(fs, settingsService)
})
It("is synchronous", func() {
Expect(action.IsAsynchronous()).To(BeFalse())
})
It("is not persistent", func() {
Expect(action.IsPersistent()).To(BeFalse())
})
It("invalidates settings so that load settings cannot fall back on old settings", func() {
resp, err := action.Run()
Expect(err).NotTo(HaveOccurred())
Expect(resp).To(Equal("ok"))
Expect(settingsService.SettingsWereInvalidated).To(BeTrue())
})
Context("when settings invalidation succeeds", func() {
Context("when the network rules file can be removed", func() {
It("removes the network rules file", func() {
fs.WriteFile("/etc/udev/rules.d/70-persistent-net.rules", []byte{})
resp, err := action.Run()
Expect(err).NotTo(HaveOccurred())
Expect(resp).To(Equal("ok"))
Expect(fs.FileExists("/etc/udev/rules.d/70-persistent-net.rules")).To(BeFalse())
})
})
Context("when the network rules file cannot be removed", func() {
BeforeEach(func() {
fs.RemoveAllError = errors.New("fake-remove-all-error")
})
It("returns error from removing the network rules file", func() {
resp, err := action.Run()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-remove-all-error"))
Expect(resp).To(BeNil())
})
})
})
Context("when settings invalidation fails", func() {
BeforeEach(func() {
settingsService.InvalidateSettingsError = errors.New("fake-invalidate-error")
})
It("returns error early if settings err invalidating", func() {
resp, err := action.Run()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-invalidate-error"))
Expect(resp).To(BeNil())
})
It("does not remove the network rules file", func() {
fs.WriteFile("/etc/udev/rules.d/70-persistent-net.rules", []byte{})
action.Run()
Expect(fs.FileExists("/etc/udev/rules.d/70-persistent-net.rules")).To(BeTrue())
})
})
})
}
示例7:
})
It("get monit credentials leaves colons in password intact", func() {
fs.WriteFileString("/fake-dir/monit/monit.user", "fake-user:fake:random:password")
username, password, err := platform.GetMonitCredentials()
Expect(err).NotTo(HaveOccurred())
Expect(username).To(Equal("fake-user"))
Expect(password).To(Equal("fake:random:password"))
})
})
Describe("PrepareForNetworkingChange", func() {
It("removes the network persistent rules file", func() {
fs.WriteFile("/etc/udev/rules.d/70-persistent-net.rules", []byte{})
err := platform.PrepareForNetworkingChange()
Expect(err).NotTo(HaveOccurred())
Expect(fs.FileExists("/etc/udev/rules.d/70-persistent-net.rules")).To(BeFalse())
})
It("returns error if removing persistent rules file fails", func() {
fs.RemoveAllError = errors.New("fake-remove-all-error")
err := platform.PrepareForNetworkingChange()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-remove-all-error"))
})
})
示例8:
Expect(len(cmdRunner.RunCommands)).To(Equal(0))
ntpConfig := fs.GetFileTestStat("/fake-dir/bosh/etc/ntpserver")
Expect(ntpConfig).To(BeNil())
})
})
Describe("SetupEphemeralDiskWithPath", func() {
It("sets up ephemeral disk with path", func() {
fakeFormatter := diskManager.FakeFormatter
fakePartitioner := diskManager.FakePartitioner
fakeMounter := diskManager.FakeMounter
fakePartitioner.GetDeviceSizeInMbSizes = map[string]uint64{"/dev/xvda": uint64(1024 * 1024 * 1024)}
fs.WriteFile("/dev/xvda", []byte{})
err := platform.SetupEphemeralDiskWithPath("/dev/xvda")
Expect(err).NotTo(HaveOccurred())
dataDir := fs.GetFileTestStat("/fake-dir/data")
Expect(dataDir.FileType).To(Equal(fakesys.FakeFileTypeDir))
Expect(dataDir.FileMode).To(Equal(os.FileMode(0750)))
Expect(fakePartitioner.PartitionDevicePath).To(Equal("/dev/xvda"))
Expect(len(fakePartitioner.PartitionPartitions)).To(Equal(2))
swapPartition := fakePartitioner.PartitionPartitions[0]
ext4Partition := fakePartitioner.PartitionPartitions[1]
Expect(swapPartition.Type).To(Equal(boshdisk.PartitionTypeSwap))
示例9:
runner = NewFileLoggingCmdRunner(fs, cmdRunner, "/fake-base-dir", 15)
cmd = boshsys.Command{
Name: "fake-cmd",
Args: []string{"fake-args"},
Env: map[string]string{"fake-env-key": "fake-env-var"},
WorkingDir: "/fake-working-dir",
}
})
Describe("RunCommand", func() {
It("cleans logs directory", func() {
err := fs.MkdirAll("/fake-base-dir/fake-log-dir-name/", os.FileMode(0750))
Expect(err).ToNot(HaveOccurred())
err = fs.WriteFile("/fake-base-dir/fake-log-dir-name/old-file", []byte("test-data"))
Expect(err).ToNot(HaveOccurred())
_, err = runner.RunCommand("fake-log-dir-name", "fake-log-file-name", cmd)
Expect(err).ToNot(HaveOccurred())
Expect(fs.FileExists("/fake-base-dir/fake-log-dir-name/old-file")).To(BeFalse())
})
It("returns an error if it fails to remove previous logs directory", func() {
fs.RemoveAllError = errors.New("fake-remove-all-error")
_, err := runner.RunCommand("fake-log-dir-name", "fake-log-file-name", cmd)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-remove-all-error"))
})
示例10: init
//.........這裏部分代碼省略.........
It("returns any error from writing to the setting file", func() {
fs.WriteToFileError = errors.New("fs-write-file-error")
err := service.LoadSettings()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fs-write-file-error"))
})
})
Context("when settings contain multiple dynamic networks", func() {
BeforeEach(func() {
fetchedSettings.Networks = Networks{
"fake-net-1": Network{Type: NetworkTypeDynamic},
"fake-net-2": Network{Type: NetworkTypeDynamic},
}
})
It("returns error because multiple dynamic networks are not supported", func() {
err := service.LoadSettings()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Multiple dynamic networks are not supported"))
})
})
})
Context("when settings fetcher fails fetching settings", func() {
BeforeEach(func() {
fetcherFuncErr = errors.New("fake-fetch-error")
})
Context("when a settings file exists", func() {
Context("when settings contain at most one dynamic network", func() {
BeforeEach(func() {
fs.WriteFile("/setting/path", []byte(`{
"agent_id":"some-agent-id",
"networks": {"fake-net-1": {"type": "dynamic"}}
}`))
})
It("returns settings from the settings file", func() {
err := service.LoadSettings()
Expect(err).ToNot(HaveOccurred())
Expect(service.GetSettings()).To(Equal(Settings{
AgentID: "some-agent-id",
Networks: Networks{
"fake-net-1": Network{Type: NetworkTypeDynamic},
},
}))
})
})
Context("when settings contain multiple dynamic networks", func() {
BeforeEach(func() {
fs.WriteFile("/setting/path", []byte(`{
"agent_id":"some-agent-id",
"networks": {
"fake-net-1": {"type": "dynamic"},
"fake-net-2": {"type": "dynamic"}
}
}`))
})
It("returns error because multiple dynamic networks are not supported", func() {
err := service.LoadSettings()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Multiple dynamic networks are not supported"))
示例11:
Context("when infrastructure settings file is found", func() {
BeforeEach(func() {
settingsPath := filepath.Join(dirProvider.BoshDir(), "dummy-cpi-agent-env.json")
expectedSettings := boshsettings.Settings{
AgentID: "123-456-789",
Blobstore: boshsettings.Blobstore{
Type: boshsettings.BlobstoreTypeDummy,
},
Mbus: "nats://127.0.0.1:4222",
}
existingSettingsBytes, err := json.Marshal(expectedSettings)
Expect(err).ToNot(HaveOccurred())
fs.WriteFile(settingsPath, existingSettingsBytes)
})
It("returns settings", func() {
settings, err := inf.GetSettings()
Expect(err).ToNot(HaveOccurred())
assert.Equal(GinkgoT(), settings, boshsettings.Settings{
AgentID: "123-456-789",
Blobstore: boshsettings.Blobstore{Type: boshsettings.BlobstoreTypeDummy},
Mbus: "nats://127.0.0.1:4222",
})
})
})
Context("when infrastructure settings file is not found", func() {
It("returns error", func() {
示例12:
expectedNetwork := boshsettings.Network{
Default: []string{"fake-default"},
DNS: []string{"fake-dns-name"},
IP: "fake-ip-address",
Netmask: "fake-netmask",
Gateway: "fake-gateway",
Mac: "fake-mac-address",
}
BeforeEach(func() {
settingsPath := filepath.Join(dirProvider.BoshDir(), "dummy-default-network-settings.json")
expectedNetworkBytes, err := json.Marshal(expectedNetwork)
Expect(err).ToNot(HaveOccurred())
fs.WriteFile(settingsPath, expectedNetworkBytes)
})
It("returns network", func() {
network, err := platform.GetDefaultNetwork()
Expect(err).ToNot(HaveOccurred())
Expect(network).To(Equal(expectedNetwork))
})
})
Context("when default networks settings file is not found", func() {
It("does not return error because dummy configuration allows no dynamic IP", func() {
_, err := platform.GetDefaultNetwork()
Expect(err).ToNot(HaveOccurred())
})
})
示例13:
Expect(err).To(HaveOccurred())
Expect(len(runner.RunComplexCommands)).To(Equal(1))
Expect(runner.RunComplexCommands[0].Env).ToNot(HaveKey("BOSH_JOB_NEXT_STATE"))
})
It("returns error when cannot get the job next state and does not run drain script", func() {
params.jobNextStateErr = errors.New("fake-job-next-state-err")
_, err := drainScript.Run(params)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-job-next-state-err"))
Expect(len(runner.RunComplexCommands)).To(Equal(0))
})
})
})
Describe("Exists", func() {
It("returns bool", func() {
commandResult := fakesys.FakeCmdResult{Stdout: "1"}
runner.AddCmdResult("/fake/script job_shutdown hash_unchanged foo bar", commandResult)
Expect(drainScript.Exists()).To(BeFalse())
fs.WriteFile("/fake/script", []byte{})
Expect(drainScript.Exists()).To(BeTrue())
})
})
})