github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/search/proptest/issue865_test.go (about)

     1  // +build big
     2  
     3  // Copyright (c) 2018 Uber Technologies, Inc.
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  //
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  //
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  package proptest
    24  
    25  import (
    26  	"math/rand"
    27  	"os"
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/m3db/m3/src/m3ninx/doc"
    32  	"github.com/m3db/m3/src/m3ninx/index"
    33  	"github.com/m3db/m3/src/m3ninx/search"
    34  	"github.com/m3db/m3/src/m3ninx/search/executor"
    35  	"github.com/m3db/m3/src/m3ninx/search/query"
    36  	"github.com/m3db/m3/src/x/context"
    37  
    38  	"github.com/leanovate/gopter"
    39  	"github.com/leanovate/gopter/prop"
    40  	"github.com/stretchr/testify/require"
    41  )
    42  
    43  // NB(prateek): this test simulates the issues described in issue: https://github.com/m3db/m3/issues/865
    44  
    45  var (
    46  	doc1 = doc.Metadata{
    47  		ID: []byte("__name__=node_cpu_seconds_total,cpu=1,instance=m3db-node01:9100,job=node-exporter,mode=system,"),
    48  		Fields: []doc.Field{
    49  			{[]byte("cpu"), []byte("1")},
    50  			{[]byte("__name__"), []byte("node_cpu_seconds_total")},
    51  			{[]byte("instance"), []byte("m3db-node01:9100")},
    52  			{[]byte("job"), []byte("node-exporter")},
    53  			{[]byte("mode"), []byte("system")},
    54  		},
    55  	}
    56  	doc2 = doc.Metadata{
    57  		ID: []byte("__name__=node_memory_SwapTotal_bytes,instance=m3db-node01:9100,job=node-exporter,"),
    58  		Fields: []doc.Field{
    59  			{[]byte("__name__"), []byte("node_memory_SwapTotal_bytes")},
    60  			{[]byte("instance"), []byte("m3db-node01:9100")},
    61  			{[]byte("job"), []byte("node-exporter")},
    62  		},
    63  	}
    64  	doc3 = doc.Metadata{
    65  		ID: []byte("__name__=node_memory_SwapTotal_bytes,instance=alertmanager03:9100,job=node-exporter,"),
    66  		Fields: []doc.Field{
    67  			{[]byte("__name__"), []byte("node_memory_SwapTotal_bytes")},
    68  			{[]byte("instance"), []byte("alertmanager03:9100")},
    69  			{[]byte("job"), []byte("node-exporter")},
    70  		},
    71  	}
    72  	doc4 = doc.Metadata{
    73  		ID: []byte("__name__=node_memory_SwapTotal_bytes,instance=prometheus01:9100,job=node-exporter,"),
    74  		Fields: []doc.Field{
    75  			{[]byte("__name__"), []byte("node_memory_SwapTotal_bytes")},
    76  			{[]byte("instance"), []byte("prometheus01:9100")},
    77  			{[]byte("job"), []byte("node-exporter")},
    78  		},
    79  	}
    80  	simpleTestDocs = []doc.Metadata{doc1, doc2, doc3, doc4}
    81  )
    82  
    83  func TestAnyDistributionOfDocsDoesNotAffectQuery(t *testing.T) {
    84  	parameters := gopter.DefaultTestParameters()
    85  	seed := time.Now().UnixNano()
    86  	parameters.MinSuccessfulTests = 100
    87  	parameters.MaxSize = 20
    88  	parameters.Rng = rand.New(rand.NewSource(seed))
    89  	properties := gopter.NewProperties(parameters)
    90  
    91  	docMatcher, err := newDocumentIteratorMatcher(t, doc.NewDocumentFromMetadata(doc2))
    92  	require.NoError(t, err)
    93  	properties.Property("Any distribution of simple documents does not affect query results", prop.ForAll(
    94  		func(i propTestInput) (bool, error) {
    95  			ctx := context.NewBackground()
    96  			segments := i.generate(t, simpleTestDocs)
    97  			readers := make([]index.Reader, 0, len(segments))
    98  			for _, s := range segments {
    99  				r, err := s.Reader()
   100  				if err != nil {
   101  					return false, err
   102  				}
   103  				readers = append(readers, r)
   104  			}
   105  
   106  			q := query.NewConjunctionQuery([]search.Query{
   107  				query.NewTermQuery([]byte("__name__"), []byte("node_memory_SwapTotal_bytes")),
   108  				query.NewTermQuery([]byte("instance"), []byte("m3db-node01:9100")),
   109  			})
   110  
   111  			e := executor.NewExecutor(readers)
   112  			d, err := e.Execute(ctx, q)
   113  			if err != nil {
   114  				return false, err
   115  			}
   116  
   117  			if err := docMatcher.Matches(d); err != nil {
   118  				return false, err
   119  			}
   120  
   121  			return true, nil
   122  		},
   123  		genPropTestInput(len(simpleTestDocs)),
   124  	))
   125  
   126  	reporter := gopter.NewFormatedReporter(true, 160, os.Stdout)
   127  	if !properties.Run(reporter) {
   128  		t.Errorf("failed with initial seed: %d", seed)
   129  	}
   130  }