github.com/google/cloudprober@v0.11.3/surfacers/file/file_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 file
    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  	"context"
    25  	"fmt"
    26  	"io/ioutil"
    27  	"os"
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/golang/protobuf/proto"
    32  	"github.com/kylelemons/godebug/pretty"
    33  
    34  	"github.com/google/cloudprober/metrics"
    35  	"github.com/google/cloudprober/surfacers/common/compress"
    36  	"github.com/google/cloudprober/surfacers/common/options"
    37  	configpb "github.com/google/cloudprober/surfacers/file/proto"
    38  )
    39  
    40  func TestWrite(t *testing.T) {
    41  	testWrite(t, false)
    42  }
    43  
    44  func TestWriteWithCompression(t *testing.T) {
    45  	testWrite(t, true)
    46  }
    47  
    48  func testWrite(t *testing.T, compressionEnabled bool) {
    49  	t.Helper()
    50  
    51  	tests := []struct {
    52  		description string
    53  		em          *metrics.EventMetrics
    54  	}{
    55  		{
    56  			description: "file write of float data",
    57  			em:          metrics.NewEventMetrics(time.Now()).AddMetric("float-test", metrics.NewInt(123456)),
    58  		},
    59  		{
    60  			description: "file write of string data",
    61  			em:          metrics.NewEventMetrics(time.Now()).AddMetric("string-test", metrics.NewString("test-string")),
    62  		},
    63  	}
    64  
    65  	for _, tt := range tests {
    66  
    67  		// Create temporary file so we don't accidentally overwrite
    68  		// another file during test.
    69  		f, err := ioutil.TempFile("", "file_test")
    70  		if err != nil {
    71  			t.Errorf("Unable to create a new file for testing: %v\ntest description: %s", err, tt.description)
    72  		}
    73  		defer os.Remove(f.Name())
    74  
    75  		s := &Surfacer{
    76  			c: &configpb.SurfacerConf{
    77  				FilePath:           proto.String(f.Name()),
    78  				CompressionEnabled: proto.Bool(compressionEnabled),
    79  			},
    80  			opts: &options.Options{
    81  				MetricsBufferSize: 1000,
    82  			},
    83  		}
    84  		id := time.Now().UnixNano()
    85  		err = s.init(context.Background(), id)
    86  		if err != nil {
    87  			t.Errorf("Unable to create a new file surfacer: %v\ntest description: %s", err, tt.description)
    88  		}
    89  
    90  		s.Write(context.Background(), tt.em)
    91  		s.close()
    92  
    93  		dat, err := ioutil.ReadFile(f.Name())
    94  		if err != nil {
    95  			t.Errorf("Unable to open test output file for reading: %v\ntest description: %s", err, tt.description)
    96  		}
    97  
    98  		expectedStr := fmt.Sprintf("%s %d %s\n", s.c.GetPrefix(), id, tt.em.String())
    99  		if compressionEnabled {
   100  			compressed, err := compress.Compress([]byte(expectedStr))
   101  			if err != nil {
   102  				t.Errorf("Unexpected error while compressing bytes: %v, Input: %s", err, expectedStr)
   103  			}
   104  			expectedStr = string(compressed) + "\n"
   105  		}
   106  
   107  		if diff := pretty.Compare(expectedStr, string(dat)); diff != "" {
   108  			t.Errorf("Message written does not match expected output (-want +got):\n%s\ntest description: %s", diff, tt.description)
   109  		}
   110  	}
   111  }