本文整理汇总了Golang中github.com/cloudfoundry-incubator/garden.Container.Run方法的典型用法代码示例。如果您正苦于以下问题:Golang Container.Run方法的具体用法?Golang Container.Run怎么用?Golang Container.Run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cloudfoundry-incubator/garden.Container
的用法示例。
在下文中一共展示了Container.Run方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: run
func (c *checker) run(logger lager.Logger, container garden.Container) (garden.Process, error) {
logger = logger.Session("run", lager.Data{
"processPath": c.healthcheckSpec.Path,
"processArgs": c.healthcheckSpec.Args,
"processUser": c.healthcheckSpec.User,
"processEnv": c.healthcheckSpec.Env,
"processDir": c.healthcheckSpec.Dir,
})
logger.Debug("starting")
defer logger.Debug("finished")
var proc garden.Process
err := retryOnFail(c.retryInterval, func(attempt uint) (runErr error) {
proc, runErr = container.Run(c.healthcheckSpec, garden.ProcessIO{})
if runErr != nil {
logger.Error("failed", runErr, lager.Data{"attempt": attempt})
return runErr
}
logger.Debug("succeeded", lager.Data{"attempt": attempt})
return nil
})
return proc, err
}
示例2: runCommand
func runCommand(container garden.Container, path string, args []string) {
proc, err := container.Run(
garden.ProcessSpec{
Path: path,
Args: args,
},
ginkgoIO)
Expect(err).NotTo(HaveOccurred())
exitCode, err := proc.Wait()
Expect(err).NotTo(HaveOccurred())
Expect(exitCode).To(Equal(0))
}
示例3: listenInContainer
func listenInContainer(container garden.Container, containerPort uint32) error {
_, err := container.Run(garden.ProcessSpec{
User: "alice",
Path: "sh",
Args: []string{"-c", fmt.Sprintf("echo %d | nc -l -p %d", containerPort, containerPort)},
}, garden.ProcessIO{
Stdout: GinkgoWriter,
Stderr: GinkgoWriter,
})
Expect(err).ToNot(HaveOccurred())
time.Sleep(2 * time.Second)
return err
}
示例4: runInContainer
func runInContainer(container garden.Container, script string) (garden.Process, *gbytes.Buffer) {
out := gbytes.NewBuffer()
process, err := container.Run(garden.ProcessSpec{
User: "alice",
Path: "sh",
Args: []string{"-c", script},
}, garden.ProcessIO{
Stdout: io.MultiWriter(out, GinkgoWriter),
Stderr: GinkgoWriter,
})
Expect(err).ToNot(HaveOccurred())
return process, out
}
示例5: AssertMemoryLimits
func AssertMemoryLimits(container garden.Container) {
buf := make([]byte, 0, 1024*1024)
stdout := bytes.NewBuffer(buf)
process, err := container.Run(garden.ProcessSpec{
Path: "bin/consume.exe",
Args: []string{"memory", "128"},
}, garden.ProcessIO{Stdout: stdout})
Expect(err).ShouldNot(HaveOccurred())
exitCode, err := process.Wait()
Expect(err).ShouldNot(HaveOccurred())
// consume script will exit 42 if it is not killed
Expect(exitCode).ToNot(Equal(42), "process did not get OOM killed")
Expect(stdout.String()).To(ContainSubstring("Consumed: 3 mb"))
}
示例6: canCreateAndUseFuseFileSystem
func canCreateAndUseFuseFileSystem(container garden.Container, user string) {
mountpoint := "/tmp/fuse-test"
process, err := container.Run(garden.ProcessSpec{
User: user,
Path: "mkdir",
Args: []string{"-p", mountpoint},
}, garden.ProcessIO{Stdout: GinkgoWriter, Stderr: GinkgoWriter})
Expect(err).ToNot(HaveOccurred())
Expect(process.Wait()).To(Equal(0), "Could not make temporary directory!")
process, err = container.Run(garden.ProcessSpec{
User: user,
Path: "/usr/bin/hellofs",
Args: []string{mountpoint},
}, garden.ProcessIO{Stdout: GinkgoWriter, Stderr: GinkgoWriter})
Expect(err).ToNot(HaveOccurred())
Expect(process.Wait()).To(Equal(0), "Failed to mount hello filesystem.")
stdout := gbytes.NewBuffer()
process, err = container.Run(garden.ProcessSpec{
User: user,
Path: "cat",
Args: []string{filepath.Join(mountpoint, "hello")},
}, garden.ProcessIO{Stdout: stdout, Stderr: GinkgoWriter})
Expect(err).ToNot(HaveOccurred())
Expect(process.Wait()).To(Equal(0), "Failed to find hello file.")
Expect(stdout).To(gbytes.Say("Hello World!"))
process, err = container.Run(garden.ProcessSpec{
User: user,
Path: "fusermount",
Args: []string{"-u", mountpoint},
}, garden.ProcessIO{Stdout: GinkgoWriter, Stderr: GinkgoWriter})
Expect(err).ToNot(HaveOccurred())
Expect(process.Wait()).To(Equal(0), "Failed to unmount user filesystem.")
stdout2 := gbytes.NewBuffer()
process, err = container.Run(garden.ProcessSpec{
User: user,
Path: "ls",
Args: []string{mountpoint},
}, garden.ProcessIO{Stdout: stdout2, Stderr: GinkgoWriter})
Expect(err).ToNot(HaveOccurred())
Expect(process.Wait()).To(Equal(0))
Expect(stdout2).ToNot(gbytes.Say("hello"), "Fuse filesystem appears still to be visible after being unmounted.")
}
示例7: ethInterfaceName
func ethInterfaceName(container garden.Container) string {
buffer := gbytes.NewBuffer()
proc, err := container.Run(
garden.ProcessSpec{
Path: "sh",
Args: []string{"-c", "ifconfig | grep 'Ethernet' | cut -f 1 -d ' '"},
User: "root",
},
garden.ProcessIO{
Stdout: buffer,
Stderr: GinkgoWriter,
},
)
Expect(err).NotTo(HaveOccurred())
Expect(proc.Wait()).To(Equal(0))
contIfaceName := string(buffer.Contents()) // w3-abc-1
return contIfaceName[:len(contIfaceName)-2] + "0" // w3-abc-0
}
示例8: checkConnection
func checkConnection(container garden.Container, ip string, port int) error {
process, err := container.Run(garden.ProcessSpec{
User: "alice",
Path: "sh",
Args: []string{"-c", fmt.Sprintf("echo hello | nc -w1 %s %d", ip, port)},
}, garden.ProcessIO{Stdout: GinkgoWriter, Stderr: GinkgoWriter})
if err != nil {
return err
}
exitCode, err := process.Wait()
if err != nil {
return err
}
if exitCode == 0 {
return nil
} else {
return fmt.Errorf("Request failed. Process exited with code %d", exitCode)
}
}
示例9: createContainerTestFileIn
func createContainerTestFileIn(container garden.Container, dir string) string {
fileName := "bind-mount-test-file"
filePath := filepath.Join(dir, fileName)
process, err := container.Run(garden.ProcessSpec{
Path: "touch",
Args: []string{filePath},
User: "root",
}, garden.ProcessIO{nil, os.Stdout, os.Stderr})
Expect(err).ToNot(HaveOccurred())
Expect(process.Wait()).To(Equal(0))
process, err = container.Run(garden.ProcessSpec{
Path: "chmod",
Args: []string{"0777", filePath},
User: "root",
}, garden.ProcessIO{nil, os.Stdout, os.Stderr})
Expect(err).ToNot(HaveOccurred())
Expect(process.Wait()).To(Equal(0))
return fileName
}
示例10: checkFileAccess
func checkFileAccess(container garden.Container, bindMountMode garden.BindMountMode, bindMountOrigin garden.BindMountOrigin, dstPath string, fileName string, privCtr, privReq bool) {
readOnly := (garden.BindMountModeRO == bindMountMode)
ctrOrigin := (garden.BindMountOriginContainer == bindMountOrigin)
realRoot := (privReq && privCtr)
// can we read a file?
filePath := filepath.Join(dstPath, fileName)
var user string
if privReq {
user = "root"
} else {
user = "alice"
}
process, err := container.Run(garden.ProcessSpec{
Path: "cat",
Args: []string{filePath},
User: user,
}, garden.ProcessIO{})
Expect(err).ToNot(HaveOccurred())
Expect(process.Wait()).To(Equal(0))
// try to write a new file
filePath = filepath.Join(dstPath, "checkFileAccess-file")
process, err = container.Run(garden.ProcessSpec{
Path: "touch",
Args: []string{filePath},
User: user,
}, garden.ProcessIO{
Stderr: GinkgoWriter,
Stdout: GinkgoWriter,
})
Expect(err).ToNot(HaveOccurred())
if readOnly || (!realRoot && !ctrOrigin) {
Expect(process.Wait()).ToNot(Equal(0))
} else {
Expect(process.Wait()).To(Equal(0))
}
// try to delete an existing file
filePath = filepath.Join(dstPath, fileName)
process, err = container.Run(garden.ProcessSpec{
Path: "rm",
Args: []string{filePath},
User: user,
}, garden.ProcessIO{})
Expect(err).ToNot(HaveOccurred())
if readOnly || (!realRoot && !ctrOrigin) {
Expect(process.Wait()).ToNot(Equal(0))
} else {
Expect(process.Wait()).To(Equal(0))
}
}
示例11:
Expect(err).ShouldNot(HaveOccurred())
defer tarFile.Close()
err = c.StreamIn(garden.StreamInSpec{Path: "bin", TarStream: tarFile})
Expect(err).ShouldNot(HaveOccurred())
})
AfterEach(func() {
err := client.Destroy(c.Handle())
Expect(err).ShouldNot(HaveOccurred())
})
testConnection := func(protocol, address string, port uint16) (garden.Process, error) {
return c.Run(garden.ProcessSpec{
Path: "bin/connect_to_remote_url.exe",
Env: []string{
fmt.Sprintf("PROTOCOL=%v", protocol),
fmt.Sprintf("ADDRESS=%v:%v", address, port),
},
}, garden.ProcessIO{})
}
openPort := func(proto garden.Protocol, port uint16, ip string) {
rule := garden.NetOutRule{
Protocol: proto,
}
if ip != "" {
parsedIP := net.ParseIP(ip)
Expect(parsedIP).ToNot(BeNil())
rule.Networks = []garden.IPRange{
{
Start: parsedIP,
示例12:
It("is successfully created with correct privileges for root in container", func() {
checkFileAccess(container, bindMountMode, bindMountOrigin, dstPath, testFileName, privilegedContainer, true)
})
Context("and the parents of the dstPath don't yet exist", func() {
BeforeEach(func() {
dstPath = "/home/alice/has/a/restaurant/readonly"
})
It("successfully creates the parents of the dstPath with correct ownership for root in the container", func() {
out := gbytes.NewBuffer()
proc, err := container.Run(garden.ProcessSpec{
User: "root",
Path: "ls",
Args: []string{"-l", "/home/alice/has"},
}, garden.ProcessIO{
Stdout: io.MultiWriter(out, GinkgoWriter),
Stderr: GinkgoWriter,
})
Expect(err).NotTo(HaveOccurred())
Expect(proc.Wait()).To(Equal(0))
Expect(out).To(gbytes.Say(`root`))
})
})
})
})
Context("which is read-write", func() {
BeforeEach(func() {
bindMountMode = garden.BindMountModeRW
dstPath = "/home/alice/readwrite"
示例13: startV1DockerRegistry
})
AfterEach(func() {
if container2 != nil {
Expect(client.Destroy(container2.Handle())).To(Succeed())
}
})
Context("with a non-privileged container", func() {
BeforeEach(func() {
privilegedContainer = false
})
It("should use the updated rootfs when creating a new container", func() {
process, err := container2.Run(garden.ProcessSpec{
Path: "/ls",
User: "root",
}, garden.ProcessIO{Stdout: GinkgoWriter, Stderr: GinkgoWriter})
Expect(err).NotTo(HaveOccurred())
exitStatus, err := process.Wait()
Expect(err).NotTo(HaveOccurred())
Expect(exitStatus).To(Equal(0))
})
})
})
})
func startV1DockerRegistry(dockerRegistryIP string, dockerRegistryPort string) garden.Container {
dockerRegistry, err := client.Create(
garden.ContainerSpec{
示例14:
JustBeforeEach(func() {
client = startGarden()
var err error
container, err = client.Create(garden.ContainerSpec{
RootFSPath: fuseRootFSPath,
Privileged: privilegedContainer,
})
Expect(err).ToNot(HaveOccurred())
})
Describe("/dev/fuse", func() {
It("is a character special device file", func() {
process, err := container.Run(garden.ProcessSpec{
User: user,
Path: "/usr/bin/test",
Args: []string{"-c", "/dev/fuse"},
}, garden.ProcessIO{Stdout: GinkgoWriter, Stderr: GinkgoWriter})
Expect(err).ToNot(HaveOccurred())
Expect(process.Wait()).To(Equal(0), "/dev/fuse cannot be found or is not a character special device.")
})
Context("in a privileged Container", func() {
BeforeEach(func() {
privilegedContainer = true
})
Context("a privileged process", func() {
BeforeEach(func() {
user = "root"
示例15:
})
It("overrides the container's internal port with it's external port", func() {
By("Creating two NetIn mappings")
const externalPort1, internalPort1 uint32 = 1000, 1001
_, _, err := c.NetIn(externalPort1, internalPort1)
Expect(err).ShouldNot(HaveOccurred())
const externalPort2, internalPort2 uint32 = 2000, 2001
_, _, err = c.NetIn(externalPort2, internalPort2)
Expect(err).ShouldNot(HaveOccurred())
By("Mapping 1's container port is substituted for it's external port")
stdout := bytes.NewBuffer(make([]byte, 0, 1024*1024))
process, err := c.Run(garden.ProcessSpec{
Path: "bin/show_port.bat",
Env: []string{fmt.Sprintf("PORT=%v", internalPort1)},
}, garden.ProcessIO{Stdout: stdout})
Expect(err).ShouldNot(HaveOccurred())
_, err = process.Wait()
Expect(err).ShouldNot(HaveOccurred())
Expect(stdout).Should(ContainSubstring(fmt.Sprintf("PORT=%v", externalPort1)))
By("Mapping 2's container port is substituted for it's external port")
stdout = bytes.NewBuffer(make([]byte, 0, 1024*1024))
process, err = c.Run(garden.ProcessSpec{
Path: "bin/show_port.bat",
Env: []string{fmt.Sprintf("PORT=%v", internalPort2)},
}, garden.ProcessIO{Stdout: stdout})
Expect(err).ShouldNot(HaveOccurred())
_, err = process.Wait()
Expect(err).ShouldNot(HaveOccurred())