github.com/google/cloudprober@v0.11.3/surfacers/common/compress/compress_test.go (about)

     1  // Copyright 2017 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package compress
    16  
    17  /*
    18  These tests wait 100 milliseconds in between a write command and the read of the
    19  file that it writes to to allow for the write to disk to complete. This could
    20  lead to flakiness in the future.
    21  */
    22  
    23  import (
    24  	"bytes"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/google/cloudprober/logger"
    29  )
    30  
    31  // Test that compress bytes is compressing bytes appropriately.
    32  func TestCompressBytes(t *testing.T) {
    33  	testString := "string that is about to be compressed"
    34  	compressed, err := Compress([]byte(testString))
    35  	if err != nil {
    36  		t.Errorf("Got error while compressing the test string (%s): %v", testString, err)
    37  	}
    38  	// Verified that following is a good string using:
    39  	// in="H4sIAAAAAAAA/youKcrMS1coyUgsUcgsVkhMyi8tUSjJV0hKVUjOzy0oSi0uTk0BBAAA//+2oNy3JQAAAA=="
    40  	// echo -n $in | base64 -d | gunzip -c
    41  	expectedCompressed := "H4sIAAAAAAAA/youKcrMS1coyUgsUcgsVkhMyi8tUSjJV0hKVUjOzy0oSi0uTk0BBAAA//+2oNy3JQAAAA=="
    42  	if string(compressed) != expectedCompressed {
    43  		t.Errorf("compressBytes(), got=%s, expected=%s", string(compressed), expectedCompressed)
    44  	}
    45  }
    46  
    47  func TestCompressionBufferFlush(t *testing.T) {
    48  	var outbuf bytes.Buffer
    49  
    50  	c := CompressionBuffer{
    51  		buf:       new(bytes.Buffer),
    52  		batchSize: 1000,
    53  		l:         &logger.Logger{},
    54  		callback:  func(b []byte) { outbuf.Write(b) },
    55  	}
    56  
    57  	testStr := "test string\n"
    58  	// We write without newline as new line is added by WriteLineToBuffer.
    59  	c.WriteLineToBuffer(strings.TrimSpace(testStr))
    60  
    61  	if c.buf.Len() == 0 || c.lines == 0 {
    62  		t.Errorf("compressionBuffer unexpectedly empty")
    63  	}
    64  	if !bytes.Equal(c.buf.Bytes(), []byte(testStr)) {
    65  		t.Errorf("Buffer: %v, expected: %v", c.buf.Bytes(), []byte(testStr))
    66  	}
    67  
    68  	c.compressAndCallback()
    69  
    70  	if c.buf.Len() != 0 && c.lines != 0 {
    71  		t.Errorf("flush() didn't empty the compressionBuffer")
    72  	}
    73  
    74  	b, _ := Compress([]byte(testStr))
    75  	if !bytes.Equal(outbuf.Bytes(), b) {
    76  		t.Errorf("Got compressed data: %v, expected: %v", outbuf.Bytes(), b)
    77  	}
    78  }