github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/trace/logger_provider_test.go (about)

     1  package trace_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path"
     6  	"runtime"
     7  
     8  	. "code.cloudfoundry.org/cli/cf/trace"
     9  	"code.cloudfoundry.org/gofileutils/fileutils"
    10  
    11  	"os"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	"github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("NewLogger", func() {
    19  	var buffer *gbytes.Buffer
    20  	BeforeEach(func() {
    21  		buffer = gbytes.NewBuffer()
    22  	})
    23  
    24  	It("returns a logger that doesn't write anywhere when nothing is set", func() {
    25  		logger := NewLogger(buffer, false, "", "")
    26  
    27  		logger.Print("Hello World")
    28  
    29  		Expect(buffer).NotTo(gbytes.Say("Hello World"))
    30  	})
    31  
    32  	It("returns a logger that only writes to STDOUT when verbose is set", func() {
    33  		logger := NewLogger(buffer, true, "", "")
    34  
    35  		logger.Print("Hello World")
    36  
    37  		Expect(buffer).To(gbytes.Say("Hello World"))
    38  	})
    39  
    40  	It("returns a logger that only writes to STDOUT when CF_TRACE=true", func() {
    41  		logger := NewLogger(buffer, false, "true", "")
    42  
    43  		logger.Print("Hello World")
    44  
    45  		Expect(buffer).To(gbytes.Say("Hello World"))
    46  
    47  		_, err := os.Open("true")
    48  		Expect(err).To(HaveOccurred())
    49  	})
    50  
    51  	It("returns a logger that only writes to STDOUT when config.trace=true", func() {
    52  		logger := NewLogger(buffer, false, "", "true")
    53  
    54  		logger.Print("Hello World")
    55  
    56  		Expect(buffer).To(gbytes.Say("Hello World"))
    57  
    58  		_, err := os.Open("true")
    59  		Expect(err).To(HaveOccurred())
    60  	})
    61  
    62  	It("returns a logger that only writes to STDOUT when verbose is set and CF_TRACE=false", func() {
    63  		logger := NewLogger(buffer, true, "false", "")
    64  
    65  		logger.Print("Hello World")
    66  
    67  		Expect(buffer).To(gbytes.Say("Hello World"))
    68  
    69  		_, err := os.Open("false")
    70  		Expect(err).To(HaveOccurred())
    71  	})
    72  
    73  	It("returns a logger that only writes to STDOUT when verbose is set and config.trace=false", func() {
    74  		logger := NewLogger(buffer, true, "", "false")
    75  
    76  		logger.Print("Hello World")
    77  
    78  		Expect(buffer).To(gbytes.Say("Hello World"))
    79  
    80  		_, err := os.Open("false")
    81  		Expect(err).To(HaveOccurred())
    82  	})
    83  
    84  	It("returns a logger that writes to STDOUT and a file when verbose is set and CF_TRACE is a path", func() {
    85  		fileutils.TempFile("trace_test", func(file *os.File, err error) {
    86  			logger := NewLogger(buffer, true, file.Name(), "")
    87  
    88  			logger.Print("Hello World")
    89  
    90  			Expect(buffer).To(gbytes.Say("Hello World"))
    91  
    92  			fileContents, _ := ioutil.ReadAll(file)
    93  			Expect(fileContents).To(ContainSubstring("Hello World"))
    94  		})
    95  	})
    96  
    97  	It("creates the file with 0600 permission", func() {
    98  		// cannot use fileutils.TempFile because it sets the permissions to 0600
    99  		// itself
   100  		fileutils.TempDir("trace_test", func(tmpDir string, err error) {
   101  			Expect(err).ToNot(HaveOccurred())
   102  
   103  			fileName := path.Join(tmpDir, "trace_test")
   104  			logger := NewLogger(buffer, true, fileName, "")
   105  			logger.Print("Hello World")
   106  
   107  			stat, err := os.Stat(fileName)
   108  			Expect(err).ToNot(HaveOccurred())
   109  			if runtime.GOOS == "windows" {
   110  				Expect(stat.Mode().String()).To(Equal(os.FileMode(0666).String()))
   111  			} else {
   112  				Expect(stat.Mode().String()).To(Equal(os.FileMode(0600).String()))
   113  			}
   114  		})
   115  	})
   116  
   117  	It("returns a logger that writes to STDOUT and a file when verbose is set and config.trace is a path", func() {
   118  		fileutils.TempFile("trace_test", func(file *os.File, err error) {
   119  			logger := NewLogger(buffer, true, "", file.Name())
   120  
   121  			logger.Print("Hello World")
   122  
   123  			Expect(buffer).To(gbytes.Say("Hello World"))
   124  
   125  			fileContents, _ := ioutil.ReadAll(file)
   126  			Expect(fileContents).To(ContainSubstring("Hello World"))
   127  		})
   128  	})
   129  
   130  	It("returns a logger that writes to a file when CF_TRACE is a path", func() {
   131  		fileutils.TempFile("trace_test", func(file *os.File, err error) {
   132  			logger := NewLogger(buffer, false, file.Name(), "")
   133  
   134  			logger.Print("Hello World")
   135  
   136  			Expect(buffer).NotTo(gbytes.Say("Hello World"))
   137  
   138  			fileContents, _ := ioutil.ReadAll(file)
   139  			Expect(fileContents).To(ContainSubstring("Hello World"))
   140  		})
   141  	})
   142  
   143  	It("returns a logger that writes to a file when config.trace is a path", func() {
   144  		fileutils.TempFile("trace_test", func(file *os.File, err error) {
   145  			logger := NewLogger(buffer, false, "", file.Name())
   146  
   147  			logger.Print("Hello World")
   148  
   149  			Expect(buffer).NotTo(gbytes.Say("Hello World"))
   150  
   151  			fileContents, _ := ioutil.ReadAll(file)
   152  			Expect(fileContents).To(ContainSubstring("Hello World"))
   153  		})
   154  	})
   155  
   156  	It("returns a logger that writes to multiple files when CF_TRACE and config.trace are both paths", func() {
   157  		fileutils.TempFile("cf_trace_test", func(cfTraceFile *os.File, err error) {
   158  			fileutils.TempFile("config_trace_test", func(configTraceFile *os.File, err error) {
   159  				logger := NewLogger(buffer, false, cfTraceFile.Name(), configTraceFile.Name())
   160  
   161  				logger.Print("Hello World")
   162  
   163  				Expect(buffer).NotTo(gbytes.Say("Hello World"))
   164  
   165  				cfTraceFileContents, _ := ioutil.ReadAll(cfTraceFile)
   166  				Expect(cfTraceFileContents).To(ContainSubstring("Hello World"))
   167  
   168  				configTraceFileContents, _ := ioutil.ReadAll(configTraceFile)
   169  				Expect(configTraceFileContents).To(ContainSubstring("Hello World"))
   170  			})
   171  		})
   172  	})
   173  
   174  	It("returns a logger that writes to STDOUT when CF_TRACE is a path that cannot be opened", func() {
   175  		if runtime.GOOS != "windows" {
   176  			logger := NewLogger(buffer, false, "/dev/null/whoops", "")
   177  
   178  			logger.Print("Hello World")
   179  
   180  			Expect(buffer).To(gbytes.Say("Hello World"))
   181  		}
   182  	})
   183  
   184  	It("returns a logger that writes to STDOUT when config.trace is a path that cannot be opened", func() {
   185  		if runtime.GOOS != "windows" {
   186  			logger := NewLogger(buffer, false, "", "/dev/null/whoops")
   187  
   188  			logger.Print("Hello World")
   189  
   190  			Expect(buffer).To(gbytes.Say("CF_TRACE ERROR CREATING LOG FILE /dev/null/whoops"))
   191  			Expect(buffer).To(gbytes.Say("Hello World"))
   192  		}
   193  	})
   194  
   195  	It("returns a logger that writes to STDOUT when verbose is set and CF_TRACE is a path that cannot be opened", func() {
   196  		if runtime.GOOS != "windows" {
   197  			logger := NewLogger(buffer, true, "", "/dev/null/whoops")
   198  
   199  			logger.Print("Hello World")
   200  
   201  			Expect(buffer).To(gbytes.Say("CF_TRACE ERROR CREATING LOG FILE /dev/null/whoops"))
   202  			Expect(buffer).To(gbytes.Say("Hello World"))
   203  		}
   204  	})
   205  })