github.com/cptmikhailov/conmon@v2.0.20+incompatible/runner/conmon_test/conmon_test.go (about)

     1  package conmon_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/containers/conmon/runner/conmon"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var (
    16  	conmonPath  = "/usr/bin/conmon"
    17  	ctrID       = "abcdefghijklm"
    18  	validPath   = "/tmp"
    19  	invalidPath = "/not/a/path"
    20  )
    21  
    22  func getConmonOutputGivenOptions(options ...conmon.ConmonOption) (string, string) {
    23  	var stdout bytes.Buffer
    24  	var stderr bytes.Buffer
    25  
    26  	options = append(options, conmon.WithStdout(&stdout), conmon.WithStderr(&stderr))
    27  
    28  	ci, err := conmon.CreateAndExecConmon(options...)
    29  	Expect(err).To(BeNil())
    30  
    31  	ci.Wait()
    32  
    33  	return stdout.String(), stderr.String()
    34  }
    35  
    36  var _ = Describe("conmon", func() {
    37  	Describe("version", func() {
    38  		It("Should return conmon version", func() {
    39  			out, _ := getConmonOutputGivenOptions(
    40  				conmon.WithVersion(),
    41  				conmon.WithPath(conmonPath),
    42  			)
    43  			Expect(out).To(ContainSubstring("conmon version"))
    44  			Expect(out).To(ContainSubstring("commit"))
    45  		})
    46  	})
    47  	Describe("no container ID", func() {
    48  		It("should fail", func() {
    49  			_, err := getConmonOutputGivenOptions(
    50  				conmon.WithPath(conmonPath),
    51  			)
    52  			Expect(err).To(ContainSubstring("conmon: Container ID not provided. Use --cid"))
    53  		})
    54  	})
    55  	Describe("no container UUID", func() {
    56  		It("should fail", func() {
    57  			_, err := getConmonOutputGivenOptions(
    58  				conmon.WithPath(conmonPath),
    59  				conmon.WithContainerID(ctrID),
    60  			)
    61  			Expect(err).To(ContainSubstring("Container UUID not provided. Use --cuuid"))
    62  		})
    63  	})
    64  	Describe("runtime path", func() {
    65  		It("no path should fail", func() {
    66  			_, err := getConmonOutputGivenOptions(
    67  				conmon.WithPath(conmonPath),
    68  				conmon.WithContainerID(ctrID),
    69  				conmon.WithContainerUUID(ctrID),
    70  			)
    71  			Expect(err).To(ContainSubstring("Runtime path not provided. Use --runtime"))
    72  		})
    73  		It("invalid path should fail", func() {
    74  			_, err := getConmonOutputGivenOptions(
    75  				conmon.WithPath(conmonPath),
    76  				conmon.WithContainerID(ctrID),
    77  				conmon.WithContainerUUID(ctrID),
    78  				conmon.WithRuntimePath(invalidPath),
    79  			)
    80  			Expect(err).To(ContainSubstring(fmt.Sprintf("Runtime path %s is not valid", invalidPath)))
    81  		})
    82  	})
    83  	Describe("ctr logs", func() {
    84  		var tmpDir string
    85  		var tmpLogPath string
    86  		var origCwd string
    87  		BeforeEach(func() {
    88  			d, err := ioutil.TempDir(os.TempDir(), "conmon-")
    89  			Expect(err).To(BeNil())
    90  			tmpDir = d
    91  			tmpLogPath = filepath.Join(tmpDir, "log")
    92  			origCwd, err = os.Getwd()
    93  			Expect(err).To(BeNil())
    94  		})
    95  		AfterEach(func() {
    96  			Expect(os.RemoveAll(tmpDir)).To(BeNil())
    97  			err := os.Chdir(origCwd)
    98  			Expect(err).To(BeNil())
    99  		})
   100  		It("no log driver should fail", func() {
   101  			_, stderr := getConmonOutputGivenOptions(
   102  				conmon.WithPath(conmonPath),
   103  				conmon.WithContainerID(ctrID),
   104  				conmon.WithContainerUUID(ctrID),
   105  				conmon.WithRuntimePath(validPath),
   106  			)
   107  			Expect(stderr).To(ContainSubstring("Log driver not provided. Use --log-path"))
   108  		})
   109  		It("empty log driver should fail", func() {
   110  			_, stderr := getConmonOutputGivenOptions(
   111  				conmon.WithPath(conmonPath),
   112  				conmon.WithContainerID(ctrID),
   113  				conmon.WithContainerUUID(ctrID),
   114  				conmon.WithRuntimePath(validPath),
   115  				conmon.WithLogPath(""),
   116  			)
   117  			Expect(stderr).To(ContainSubstring("log-path must not be empty"))
   118  		})
   119  		It("empty log driver and path should fail", func() {
   120  			_, stderr := getConmonOutputGivenOptions(
   121  				conmon.WithPath(conmonPath),
   122  				conmon.WithContainerID(ctrID),
   123  				conmon.WithContainerUUID(ctrID),
   124  				conmon.WithRuntimePath(validPath),
   125  				conmon.WithLogPath(":"),
   126  			)
   127  			Expect(stderr).To(ContainSubstring("log-path must not be empty"))
   128  		})
   129  		It("k8s-file requires a filename", func() {
   130  			_, stderr := getConmonOutputGivenOptions(
   131  				conmon.WithPath(conmonPath),
   132  				conmon.WithContainerID(ctrID),
   133  				conmon.WithContainerUUID(ctrID),
   134  				conmon.WithRuntimePath(validPath),
   135  				conmon.WithLogPath("k8s-file"),
   136  			)
   137  			Expect(stderr).To(ContainSubstring("k8s-file requires a filename"))
   138  		})
   139  		It("k8s-file: requires a filename", func() {
   140  			_, stderr := getConmonOutputGivenOptions(
   141  				conmon.WithPath(conmonPath),
   142  				conmon.WithContainerID(ctrID),
   143  				conmon.WithContainerUUID(ctrID),
   144  				conmon.WithRuntimePath(validPath),
   145  				conmon.WithLogPath("k8s-file:"),
   146  			)
   147  			Expect(stderr).To(ContainSubstring("k8s-file requires a filename"))
   148  		})
   149  		It("log driver as path should pass", func() {
   150  			_, stderr := getConmonOutputGivenOptions(
   151  				conmon.WithPath(conmonPath),
   152  				conmon.WithContainerID(ctrID),
   153  				conmon.WithContainerUUID(ctrID),
   154  				conmon.WithRuntimePath(validPath),
   155  				conmon.WithLogDriver("", tmpLogPath),
   156  			)
   157  			Expect(stderr).To(BeEmpty())
   158  
   159  			_, err := os.Stat(tmpLogPath)
   160  			Expect(err).To(BeNil())
   161  		})
   162  		It("log driver as k8s-file:path should pass", func() {
   163  			_, stderr := getConmonOutputGivenOptions(
   164  				conmon.WithPath(conmonPath),
   165  				conmon.WithContainerID(ctrID),
   166  				conmon.WithContainerUUID(ctrID),
   167  				conmon.WithRuntimePath(validPath),
   168  				conmon.WithLogDriver("k8s-file", tmpLogPath),
   169  			)
   170  			Expect(stderr).To(BeEmpty())
   171  
   172  			_, err := os.Stat(tmpLogPath)
   173  			Expect(err).To(BeNil())
   174  		})
   175  		It("log driver as :path should pass", func() {
   176  			_, stderr := getConmonOutputGivenOptions(
   177  				conmon.WithPath(conmonPath),
   178  				conmon.WithContainerID(ctrID),
   179  				conmon.WithContainerUUID(ctrID),
   180  				conmon.WithRuntimePath(validPath),
   181  				conmon.WithLogPath(":"+tmpLogPath),
   182  			)
   183  			Expect(stderr).To(BeEmpty())
   184  
   185  			_, err := os.Stat(tmpLogPath)
   186  			Expect(err).To(BeNil())
   187  		})
   188  		It("log driver as none should pass", func() {
   189  			direrr := os.Chdir(tmpDir)
   190  			Expect(direrr).To(BeNil())
   191  
   192  			_, stderr := getConmonOutputGivenOptions(
   193  				conmon.WithPath(conmonPath),
   194  				conmon.WithContainerID(ctrID),
   195  				conmon.WithContainerUUID(ctrID),
   196  				conmon.WithRuntimePath(validPath),
   197  				conmon.WithLogDriver("none", ""),
   198  			)
   199  			Expect(stderr).To(BeEmpty())
   200  
   201  			_, err := os.Stat("none")
   202  			Expect(err).NotTo(BeNil())
   203  		})
   204  		It("log driver as off should pass", func() {
   205  			direrr := os.Chdir(tmpDir)
   206  			Expect(direrr).To(BeNil())
   207  
   208  			_, stderr := getConmonOutputGivenOptions(
   209  				conmon.WithPath(conmonPath),
   210  				conmon.WithContainerID(ctrID),
   211  				conmon.WithContainerUUID(ctrID),
   212  				conmon.WithRuntimePath(validPath),
   213  				conmon.WithLogDriver("off", ""),
   214  			)
   215  			Expect(stderr).To(BeEmpty())
   216  
   217  			_, err := os.Stat("off")
   218  			Expect(err).NotTo(BeNil())
   219  		})
   220  		It("log driver as null should pass", func() {
   221  			direrr := os.Chdir(tmpDir)
   222  			Expect(direrr).To(BeNil())
   223  
   224  			_, stderr := getConmonOutputGivenOptions(
   225  				conmon.WithPath(conmonPath),
   226  				conmon.WithContainerID(ctrID),
   227  				conmon.WithContainerUUID(ctrID),
   228  				conmon.WithRuntimePath(validPath),
   229  				conmon.WithLogDriver("null", ""),
   230  			)
   231  			Expect(stderr).To(BeEmpty())
   232  
   233  			_, err := os.Stat("none")
   234  			Expect(err).NotTo(BeNil())
   235  		})
   236  		It("log driver as journald should pass", func() {
   237  			direrr := os.Chdir(tmpDir)
   238  			Expect(direrr).To(BeNil())
   239  
   240  			_, stderr := getConmonOutputGivenOptions(
   241  				conmon.WithPath(conmonPath),
   242  				conmon.WithContainerID(ctrID),
   243  				conmon.WithContainerUUID(ctrID),
   244  				conmon.WithRuntimePath(validPath),
   245  				conmon.WithLogDriver("journald", ""),
   246  			)
   247  			Expect(stderr).To(BeEmpty())
   248  
   249  			_, err := os.Stat("journald")
   250  			Expect(err).NotTo(BeNil())
   251  		})
   252  		It("log driver as :journald should pass", func() {
   253  			direrr := os.Chdir(tmpDir)
   254  			Expect(direrr).To(BeNil())
   255  
   256  			_, stderr := getConmonOutputGivenOptions(
   257  				conmon.WithPath(conmonPath),
   258  				conmon.WithContainerID(ctrID),
   259  				conmon.WithContainerUUID(ctrID),
   260  				conmon.WithRuntimePath(validPath),
   261  				conmon.WithLogPath(":journald"),
   262  			)
   263  			Expect(stderr).To(BeEmpty())
   264  
   265  			_, err := os.Stat("journald")
   266  			Expect(err).To(BeNil())
   267  		})
   268  		It("log driver as journald with short cid should fail", func() {
   269  			// conmon requires a cid of len > 12
   270  			shortCtrID := "abcdefghijkl"
   271  			_, stderr := getConmonOutputGivenOptions(
   272  				conmon.WithPath(conmonPath),
   273  				conmon.WithContainerID(shortCtrID),
   274  				conmon.WithContainerUUID(shortCtrID),
   275  				conmon.WithRuntimePath(validPath),
   276  				conmon.WithLogDriver("journald", ""),
   277  			)
   278  			Expect(stderr).To(ContainSubstring("Container ID must be longer than 12 characters"))
   279  		})
   280  		It("log driver as k8s-file with path should pass", func() {
   281  			_, stderr := getConmonOutputGivenOptions(
   282  				conmon.WithPath(conmonPath),
   283  				conmon.WithContainerID(ctrID),
   284  				conmon.WithContainerUUID(ctrID),
   285  				conmon.WithRuntimePath(validPath),
   286  				conmon.WithLogDriver("k8s-file", tmpLogPath),
   287  			)
   288  			Expect(stderr).To(BeEmpty())
   289  
   290  			_, err := os.Stat(tmpLogPath)
   291  			Expect(err).To(BeNil())
   292  		})
   293  		It("log driver as k8s-file with invalid path should fail", func() {
   294  			_, stderr := getConmonOutputGivenOptions(
   295  				conmon.WithPath(conmonPath),
   296  				conmon.WithContainerID(ctrID),
   297  				conmon.WithContainerUUID(ctrID),
   298  				conmon.WithRuntimePath(validPath),
   299  				conmon.WithLogDriver("k8s-file", invalidPath),
   300  			)
   301  			Expect(stderr).To(ContainSubstring("Failed to open log file"))
   302  		})
   303  		It("log driver as invalid driver should fail", func() {
   304  			invalidLogDriver := "invalid"
   305  			_, stderr := getConmonOutputGivenOptions(
   306  				conmon.WithPath(conmonPath),
   307  				conmon.WithContainerID(ctrID),
   308  				conmon.WithContainerUUID(ctrID),
   309  				conmon.WithRuntimePath(validPath),
   310  				conmon.WithLogDriver(invalidLogDriver, tmpLogPath),
   311  			)
   312  			Expect(stderr).To(ContainSubstring("No such log driver " + invalidLogDriver))
   313  		})
   314  		It("log driver as invalid driver with a blank path should fail", func() {
   315  			invalidLogDriver := "invalid"
   316  			_, stderr := getConmonOutputGivenOptions(
   317  				conmon.WithPath(conmonPath),
   318  				conmon.WithContainerID(ctrID),
   319  				conmon.WithContainerUUID(ctrID),
   320  				conmon.WithRuntimePath(validPath),
   321  				conmon.WithLogDriver(invalidLogDriver, ""),
   322  			)
   323  			Expect(stderr).To(ContainSubstring("No such log driver " + invalidLogDriver))
   324  		})
   325  		It("multiple log drivers should pass", func() {
   326  			_, stderr := getConmonOutputGivenOptions(
   327  				conmon.WithPath(conmonPath),
   328  				conmon.WithContainerID(ctrID),
   329  				conmon.WithContainerUUID(ctrID),
   330  				conmon.WithRuntimePath(validPath),
   331  				conmon.WithLogDriver("k8s-file", tmpLogPath),
   332  				conmon.WithLogDriver("journald", ""),
   333  			)
   334  			Expect(stderr).To(BeEmpty())
   335  
   336  			_, err := os.Stat(tmpLogPath)
   337  			Expect(err).To(BeNil())
   338  		})
   339  		It("multiple log drivers with one invalid should fail", func() {
   340  			invalidLogDriver := "invalid"
   341  			_, stderr := getConmonOutputGivenOptions(
   342  				conmon.WithPath(conmonPath),
   343  				conmon.WithContainerID(ctrID),
   344  				conmon.WithContainerUUID(ctrID),
   345  				conmon.WithRuntimePath(validPath),
   346  				conmon.WithLogDriver("k8s-file", tmpLogPath),
   347  				conmon.WithLogDriver(invalidLogDriver, tmpLogPath),
   348  			)
   349  			Expect(stderr).To(ContainSubstring("No such log driver " + invalidLogDriver))
   350  		})
   351  	})
   352  })