github.com/rigado/snapd@v2.42.5-go-mod+incompatible/strutil/matchcounter_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package strutil_test
    21  
    22  import (
    23  	"regexp"
    24  
    25  	"gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/strutil"
    28  )
    29  
    30  type mcSuite struct{}
    31  
    32  var _ = check.Suite(&mcSuite{})
    33  
    34  const out = `
    35  
    36  Failed to write /tmp/1/modules/4.4.0-112-generic/modules.symbols, skipping
    37  
    38  Write on output file failed because No space left on device
    39  
    40  Hello I am a happy line that does not mention failure.
    41  
    42  writer: failed to write data block 0
    43  
    44  Failed to write /tmp/1/modules/4.4.0-112-generic/modules.symbols.bin, skipping
    45  
    46  Write on output file failed because No space left on device
    47  
    48  writer: failed to write data block 0
    49  
    50  Failed to write /tmp/1/modules/4.4.0-112-generic/vdso/vdso32.so, skipping
    51  
    52  Write on output file failed because No space left on device
    53  
    54  La la la.
    55  
    56  writer: failed to write data block 0
    57  
    58  Failed to write /tmp/1/modules/4.4.0-112-generic/vdso/vdso64.so, skipping
    59  
    60  Write on output file failed because No space left on device
    61  
    62  writer: failed to write data block 0
    63  
    64  Failed to write /tmp/1/modules/4.4.0-112-generic/vdso/vdsox32.so, skipping
    65  
    66  Write on output file failed because No space left on device
    67  
    68  writer: failed to write data block 0
    69  
    70  Failed to write /tmp/1/snap/manifest.yaml, skipping
    71  
    72  🦄🌈💩
    73  
    74  Write on output file failed because No space left on device
    75  
    76  writer: failed to write data block 0
    77  
    78  Failed to write /tmp/1/snap/snapcraft.yaml, skipping
    79  `
    80  
    81  var thisRegexp = regexp.MustCompile("(?m).*[Ff]ailed.*")
    82  var nilRegexpEquiv = regexp.MustCompile("(?m).+")
    83  
    84  func (mcSuite) TestMatchCounterFull(c *check.C) {
    85  	// check a single write
    86  	expected := thisRegexp.FindAllString(out, 3)
    87  	w := &strutil.MatchCounter{Regexp: thisRegexp, N: 3}
    88  	_, err := w.Write([]byte(out))
    89  	c.Assert(err, check.IsNil)
    90  	matches, count := w.Matches()
    91  	c.Check(count, check.Equals, 19)
    92  	c.Assert(matches, check.DeepEquals, expected)
    93  }
    94  
    95  func (mcSuite) TestMatchCounterPartials(c *check.C) {
    96  	// now we know the whole thing matches expected, we check partials
    97  	buf := []byte(out)
    98  	expected := []string{
    99  		"Failed to write /tmp/1/modules/4.4.0-112-generic/modules.symbols, skipping",
   100  		"Write on output file failed because No space left on device",
   101  		"writer: failed to write data block 0",
   102  	}
   103  
   104  	for step := 1; step < 100; step++ {
   105  		w := &strutil.MatchCounter{Regexp: thisRegexp, N: 3}
   106  		var i int
   107  		for i = 0; i+step < len(buf); i += step {
   108  			_, err := w.Write(buf[i : i+step])
   109  			c.Assert(err, check.IsNil, check.Commentf("step:%d i:%d", step, i))
   110  		}
   111  		_, err := w.Write(buf[i:])
   112  		c.Assert(err, check.IsNil, check.Commentf("step:%d tail", step))
   113  		matches, count := w.Matches()
   114  		c.Check(count, check.Equals, 19, check.Commentf("step:%d", step))
   115  		c.Check(matches, check.DeepEquals, expected, check.Commentf("step:%d", step))
   116  	}
   117  }
   118  
   119  func (mcSuite) TestMatchCounterPartialsReusingBuffer(c *check.C) {
   120  	// now we know the whole thing matches expected, we check partials
   121  	buf := []byte(out)
   122  	expected := []string{
   123  		"Failed to write /tmp/1/modules/4.4.0-112-generic/modules.symbols, skipping",
   124  		"Write on output file failed because No space left on device",
   125  		"writer: failed to write data block 0",
   126  	}
   127  
   128  	for step := 1; step < 100; step++ {
   129  		wbuf := make([]byte, step)
   130  		w := &strutil.MatchCounter{Regexp: thisRegexp, N: 3}
   131  		var i int
   132  		for i = 0; i+step < len(buf); i += step {
   133  			copy(wbuf, buf[i:])
   134  			_, err := w.Write(wbuf)
   135  			c.Assert(err, check.IsNil, check.Commentf("step:%d i:%d", step, i))
   136  		}
   137  		wbuf = wbuf[:len(buf[i:])]
   138  		copy(wbuf, buf[i:])
   139  		_, err := w.Write(wbuf)
   140  		c.Assert(err, check.IsNil, check.Commentf("step:%d tail", step))
   141  		matches, count := w.Matches()
   142  		c.Assert(count, check.Equals, 19, check.Commentf("step:%d", step))
   143  		c.Assert(matches, check.DeepEquals, expected, check.Commentf("step:%d", step))
   144  	}
   145  }
   146  
   147  func (mcSuite) TestMatchCounterZero(c *check.C) {
   148  	w := &strutil.MatchCounter{Regexp: thisRegexp, N: 0}
   149  	_, err := w.Write([]byte(out))
   150  	c.Assert(err, check.IsNil)
   151  	matches, count := w.Matches()
   152  	c.Check(count, check.Equals, 19)
   153  	c.Assert(matches, check.HasLen, 0)
   154  }
   155  
   156  func (mcSuite) TestMatchCounterNegative(c *check.C) {
   157  	expected := thisRegexp.FindAllString(out, -1)
   158  
   159  	w := &strutil.MatchCounter{Regexp: thisRegexp, N: -1}
   160  	_, err := w.Write([]byte(out))
   161  	c.Assert(err, check.IsNil)
   162  	matches, count := w.Matches()
   163  	c.Check(count, check.Equals, 19)
   164  	c.Check(count, check.Equals, len(matches))
   165  	c.Assert(matches, check.DeepEquals, expected)
   166  }
   167  
   168  func (mcSuite) TestMatchCounterNilRegexpFull(c *check.C) {
   169  	expected := nilRegexpEquiv.FindAllString(out, -1)
   170  	w := &strutil.MatchCounter{N: -1}
   171  	_, err := w.Write([]byte(out))
   172  	c.Assert(err, check.IsNil)
   173  	matches, count := w.Matches()
   174  	c.Check(count, check.Equals, len(matches))
   175  	c.Check(len(matches), check.Equals, len(expected))
   176  	c.Assert(matches, check.DeepEquals, expected)
   177  }
   178  
   179  func (mcSuite) TestMatchCounterNilRegexpLimited(c *check.C) {
   180  	expected := nilRegexpEquiv.FindAllString(out, 10)
   181  	w := &strutil.MatchCounter{N: 10}
   182  	_, err := w.Write([]byte(out))
   183  	c.Assert(err, check.IsNil)
   184  	matches, count := w.Matches()
   185  	c.Check(count, check.Equals, 22)
   186  	c.Check(len(matches), check.Equals, len(expected))
   187  	c.Assert(matches, check.DeepEquals, expected)
   188  }
   189  
   190  func (mcSuite) TestMatchCounterNilRegexpPartials(c *check.C) {
   191  	expected := nilRegexpEquiv.FindAllString(out, 3)
   192  
   193  	buf := []byte(out)
   194  	for step := 1; step < 100; step++ {
   195  		w := &strutil.MatchCounter{N: 3}
   196  		var i int
   197  		for i = 0; i+step < len(buf); i += step {
   198  			_, err := w.Write(buf[i : i+step])
   199  			c.Assert(err, check.IsNil, check.Commentf("step:%d i:%d", step, i))
   200  		}
   201  		_, err := w.Write(buf[i:])
   202  		c.Assert(err, check.IsNil, check.Commentf("step:%d tail", step))
   203  		matches, count := w.Matches()
   204  		c.Check(count, check.Equals, 22, check.Commentf("step:%d", step))
   205  		c.Check(matches, check.DeepEquals, expected, check.Commentf("step:%d", step))
   206  	}
   207  }