github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/integration/lifecycle/logging_test.go (about)

     1  package lifecycle_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path"
     7  
     8  	"github.com/cloudfoundry-incubator/garden"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	"github.com/onsi/gomega/gbytes"
    12  )
    13  
    14  var _ = Describe("Logging", func() {
    15  	var container garden.Container
    16  	var containerSpec garden.ContainerSpec
    17  
    18  	BeforeEach(func() {
    19  		containerSpec = garden.ContainerSpec{}
    20  	})
    21  
    22  	JustBeforeEach(func() {
    23  		var err error
    24  		client = startGarden()
    25  		container, err = client.Create(containerSpec)
    26  		Expect(err).ToNot(HaveOccurred())
    27  	})
    28  
    29  	Context("when container is created", func() {
    30  		BeforeEach(func() {
    31  			containerSpec = garden.ContainerSpec{
    32  				Handle: "kumquat",
    33  				Env:    []string{"PASSWORD=MY_SECRET"},
    34  				Properties: garden.Properties{
    35  					"super": "banana",
    36  				},
    37  			}
    38  		})
    39  
    40  		It("should log before and after starting with the container handle", func() {
    41  			Eventually(client).Should(gbytes.Say(`container.start.starting","log_level":0,"data":{"handle":"kumquat"`))
    42  			Eventually(client).Should(gbytes.Say(`container.start.ended","log_level":0,"data":{"handle":"kumquat"`))
    43  		})
    44  
    45  		It("should not log any environment variables", func() {
    46  			Consistently(client).ShouldNot(gbytes.Say("PASSWORD"))
    47  			Consistently(client).ShouldNot(gbytes.Say("MY_SECRET"))
    48  		})
    49  
    50  		It("should not log any properties", func() {
    51  			Consistently(client).ShouldNot(gbytes.Say("super"))
    52  			Consistently(client).ShouldNot(gbytes.Say("banana"))
    53  		})
    54  
    55  		Context("from a docker url", func() {
    56  			BeforeEach(func() {
    57  				containerSpec.RootFSPath = "docker:///cfgarden/with-volume"
    58  			})
    59  
    60  			It("should not log any environment variables", func() {
    61  				Consistently(client).ShouldNot(gbytes.Say("test-from-dockerfile"))
    62  			})
    63  		})
    64  
    65  	})
    66  
    67  	Context("when container spawn a new process", func() {
    68  		It("should not log any environment variables and command line arguments", func() {
    69  			process, err := container.Run(garden.ProcessSpec{
    70  				User: "alice",
    71  				Path: "echo",
    72  				Args: []string{"-username", "banana"},
    73  				Env:  []string{"PASSWORD=MY_SECRET"},
    74  			}, garden.ProcessIO{
    75  				Stdout: GinkgoWriter,
    76  				Stderr: GinkgoWriter,
    77  			})
    78  			Expect(err).ToNot(HaveOccurred())
    79  			exitStatus, err := process.Wait()
    80  			Expect(err).ToNot(HaveOccurred())
    81  			Expect(exitStatus).To(Equal(0))
    82  
    83  			Consistently(client).ShouldNot(gbytes.Say("PASSWORD"))
    84  			Consistently(client).ShouldNot(gbytes.Say("MY_SECRET"))
    85  			Consistently(client).ShouldNot(gbytes.Say("-username"))
    86  			Consistently(client).ShouldNot(gbytes.Say("banana"))
    87  		})
    88  	})
    89  
    90  	Context("and iodaemon fails to start wsh", func() {
    91  		JustBeforeEach(func() {
    92  			info, err := container.Info()
    93  			Expect(err).NotTo(HaveOccurred())
    94  
    95  			Expect(os.Remove(path.Join(info.ContainerPath, "bin", "wsh"))).To(Succeed())
    96  			Expect(ioutil.WriteFile(path.Join(info.ContainerPath, "bin", "wsh"), []byte("fail-to-exec"), 0755)).To(Succeed())
    97  		})
    98  
    99  		It("logs iodaemon's error", func() {
   100  			_, err := container.Run(garden.ProcessSpec{
   101  				Path: "echo",
   102  				Args: []string{"hello world"},
   103  				User: "root",
   104  			}, garden.ProcessIO{})
   105  			Expect(err).To(HaveOccurred())
   106  			Eventually(client).Should(gbytes.Say("wsh failed to start"))
   107  		})
   108  	})
   109  
   110  	Context("when working with properties", func() {
   111  		BeforeEach(func() {
   112  			containerSpec = garden.ContainerSpec{
   113  				Properties: garden.Properties{
   114  					"super": "banana",
   115  				},
   116  			}
   117  		})
   118  
   119  		It("should not log the properties when we are getting them", func() {
   120  			_, err := container.Properties()
   121  			Expect(err).ToNot(HaveOccurred())
   122  
   123  			Consistently(client).ShouldNot(gbytes.Say("super"))
   124  			Consistently(client).ShouldNot(gbytes.Say("banana"))
   125  		})
   126  
   127  		It("should not log the properties when we are setting them", func() {
   128  			err := container.SetProperty("super", "banana")
   129  			Expect(err).ToNot(HaveOccurred())
   130  
   131  			Consistently(client).ShouldNot(gbytes.Say("super"))
   132  			Consistently(client).ShouldNot(gbytes.Say("banana"))
   133  		})
   134  	})
   135  })