github.com/tilt-dev/tilt@v0.36.0/integration/analytics_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  
    14  	"github.com/tilt-dev/tilt/internal/testutils/tempdir"
    15  	"github.com/tilt-dev/wmclient/pkg/analytics"
    16  )
    17  
    18  const WindmillDirEnvVarName = "WINDMILL_DIR"
    19  const AnalyticsUrlEnvVarName = "TILT_ANALYTICS_URL"
    20  
    21  type analyticsFixture struct {
    22  	*k8sFixture
    23  	tempDir *tempdir.TempDirFixture
    24  	mss     *MemoryStatsServer
    25  }
    26  
    27  func newAnalyticsFixture(t *testing.T) *analyticsFixture {
    28  	td := tempdir.NewTempDirFixture(t)
    29  	af := &analyticsFixture{
    30  		k8sFixture: newK8sFixture(t, "analytics"),
    31  		tempDir:    td,
    32  	}
    33  	af.tilt.Environ[WindmillDirEnvVarName] = td.Path()
    34  
    35  	af.SetupAnalyticsServer()
    36  
    37  	t.Cleanup(af.TearDown)
    38  	return af
    39  }
    40  
    41  func (af *analyticsFixture) SetupAnalyticsServer() {
    42  	mss, port, err := StartMemoryStatsServer()
    43  	if !assert.NoError(af.t, err) {
    44  		af.t.FailNow()
    45  	}
    46  	af.mss = mss
    47  	af.tilt.Environ["TILT_DISABLE_ANALYTICS"] = ""
    48  	af.tilt.Environ["CI"] = ""
    49  	af.tilt.Environ[AnalyticsUrlEnvVarName] = fmt.Sprintf("http://localhost:%d/report", port)
    50  }
    51  
    52  func (af *analyticsFixture) TearDown() {
    53  	err := af.mss.TearDown()
    54  	if err != nil {
    55  		af.t.Fatal(err)
    56  	}
    57  }
    58  
    59  func (af *analyticsFixture) SetOpt(opt analytics.Opt) {
    60  	af.t.Setenv(WindmillDirEnvVarName, af.tempDir.Path())
    61  	err := analytics.SetOpt(opt)
    62  	if err != nil {
    63  		af.t.Fatal(err)
    64  	}
    65  }
    66  
    67  func TestOptedIn(t *testing.T) {
    68  	f := newAnalyticsFixture(t)
    69  
    70  	f.SetOpt(analytics.OptIn)
    71  
    72  	f.TiltCI("analytics")
    73  
    74  	ctx, cancel := context.WithTimeout(f.ctx, time.Minute)
    75  	defer cancel()
    76  	f.WaitForAllPodsReady(ctx, "app=analytics")
    77  
    78  	var observedEventNames []string
    79  	for _, c := range f.mss.ma.Counts {
    80  		observedEventNames = append(observedEventNames, c.Name)
    81  	}
    82  
    83  	var observedTimerNames []string
    84  	for _, c := range f.mss.ma.Timers {
    85  		observedTimerNames = append(observedTimerNames, c.Name)
    86  	}
    87  
    88  	// just check that a couple metrics were successfully reported rather than asserting an exhaustive list
    89  	// the goal is to ensure that analytics is working in general, not to test which specific metrics are reported
    90  	// and we don't want to have to update this every time we change which metrics we report
    91  	assert.Contains(t, observedEventNames, "tilt.cmd.ci")
    92  	assert.Contains(t, observedEventNames, "tilt.tiltfile.loaded")
    93  	assert.Contains(t, observedTimerNames, "tilt.tiltfile.load")
    94  }
    95  
    96  func TestOptedOut(t *testing.T) {
    97  	f := newAnalyticsFixture(t)
    98  
    99  	f.SetOpt(analytics.OptOut)
   100  
   101  	f.TiltCI("analytics")
   102  
   103  	ctx, cancel := context.WithTimeout(f.ctx, time.Minute)
   104  	defer cancel()
   105  	f.WaitForAllPodsReady(ctx, "app=analytics")
   106  
   107  	assert.Equal(t, 0, len(f.mss.ma.Counts))
   108  }
   109  
   110  func TestOptDefault(t *testing.T) {
   111  	f := newAnalyticsFixture(t)
   112  
   113  	f.SetOpt(analytics.OptDefault)
   114  
   115  	f.TiltCI("analytics")
   116  
   117  	ctx, cancel := context.WithTimeout(f.ctx, time.Minute)
   118  	defer cancel()
   119  	f.WaitForAllPodsReady(ctx, "app=analytics")
   120  
   121  	var observedEventNames []string
   122  	for _, c := range f.mss.ma.Counts {
   123  		observedEventNames = append(observedEventNames, c.Name)
   124  	}
   125  
   126  	var observedTimerNames []string
   127  	for _, c := range f.mss.ma.Timers {
   128  		observedTimerNames = append(observedTimerNames, c.Name)
   129  	}
   130  
   131  	// just check that a couple metrics were successfully reported rather than asserting an exhaustive list
   132  	// the goal is to ensure that analytics is working in general, not to test which specific metrics are reported
   133  	// and we don't want to have to update this every time we change which metrics we report
   134  	assert.Contains(t, observedEventNames, "tilt.cmd.ci")
   135  	assert.Contains(t, observedEventNames, "tilt.tiltfile.loaded")
   136  	assert.Contains(t, observedTimerNames, "tilt.tiltfile.load")
   137  }