...

Source file src/github.com/emissary-ingress/emissary/v3/pkg/acp/ambassador_test.go

Documentation: github.com/emissary-ingress/emissary/v3/pkg/acp

     1  package acp_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/datawire/dlib/dlog"
     8  	"github.com/datawire/dlib/dtime"
     9  	"github.com/emissary-ingress/emissary/v3/pkg/acp"
    10  )
    11  
    12  type awMetadata struct {
    13  	t  *testing.T
    14  	ft *dtime.FakeTime
    15  	aw *acp.AmbassadorWatcher
    16  }
    17  
    18  func (m *awMetadata) check(seq int, clock int, alive bool, ready bool) {
    19  	if m.ft.TimeSinceBoot() != time.Duration(clock)*time.Second {
    20  		m.t.Errorf("%d: FakeTime.TimeSinceBoot should be %ds, not %v", seq, clock, m.ft.TimeSinceBoot())
    21  	}
    22  
    23  	if m.aw.IsAlive() != alive {
    24  		m.t.Errorf("%d: DiagdWatcher.IsAlive %t, wanted %t", seq, m.aw.IsAlive(), alive)
    25  	}
    26  
    27  	if m.aw.IsReady() != ready {
    28  		m.t.Errorf("%d: DiagdWatcher.IsReady %t, wanted %t", seq, m.aw.IsReady(), ready)
    29  	}
    30  }
    31  
    32  func (m *awMetadata) stepSec(step int) {
    33  	m.ft.StepSec(step)
    34  }
    35  
    36  func newAWMetadata(t *testing.T) *awMetadata {
    37  	ft := dtime.NewFakeTime()
    38  	f := &fakeReady{mode: Happy}
    39  
    40  	dw := acp.NewDiagdWatcher()
    41  	dw.SetFetchTime(ft.Now)
    42  
    43  	if dw == nil {
    44  		t.Error("New DiagdWatcher is nil?")
    45  	}
    46  
    47  	ew := acp.NewEnvoyWatcher()
    48  	ew.SetReadyCheck(f.readyCheck)
    49  
    50  	if ew == nil {
    51  		t.Error("New EnvoyWatcher is nil?")
    52  	}
    53  
    54  	aw := acp.NewAmbassadorWatcher(ew, dw)
    55  	aw.SetFetchTime(ft.Now)
    56  
    57  	return &awMetadata{t: t, ft: ft, aw: aw}
    58  }
    59  
    60  func TestAmbassadorHappyPath(t *testing.T) {
    61  	m := newAWMetadata(t)
    62  	m.check(0, 0, true, false)
    63  
    64  	// Advance the clock 10s.
    65  	m.stepSec(10)
    66  	m.check(1, 10, true, false)
    67  
    68  	// Send a snapshot.
    69  	m.stepSec(10)
    70  	m.aw.NoteSnapshotSent()
    71  	m.check(2, 20, true, false)
    72  
    73  	// Mark the snapshot processed.
    74  	m.stepSec(10)
    75  	m.aw.NoteSnapshotProcessed()
    76  	m.check(3, 30, true, false)
    77  
    78  	// Fetch readiness.
    79  	m.stepSec(10)
    80  	m.aw.FetchEnvoyReady(dlog.NewTestContext(t, false))
    81  	m.check(4, 40, true, true)
    82  
    83  	// Make sure it stays happy.
    84  	m.stepSec(10)
    85  	m.check(5, 50, true, true)
    86  }
    87  
    88  func TestAmbassadorUnrealisticallyHappy(t *testing.T) {
    89  	m := newAWMetadata(t)
    90  	m.check(0, 0, true, false)
    91  
    92  	// Advance the clock 10s.
    93  	m.stepSec(10)
    94  	m.check(1, 10, true, false)
    95  
    96  	// Send a snapshot, mark it processed, and have Envoy come up all
    97  	// in the same instant. This is _highly unlikely_ but WTF, let's
    98  	// try it. We expect to see alive and ready here.
    99  	m.stepSec(10)
   100  	m.aw.NoteSnapshotSent()
   101  	m.aw.NoteSnapshotProcessed()
   102  	m.aw.FetchEnvoyReady(dlog.NewTestContext(t, false))
   103  	m.check(2, 20, true, true)
   104  
   105  	// Make sure it stays happy.
   106  	m.stepSec(10)
   107  	m.check(3, 30, true, true)
   108  }
   109  
   110  func TestAmbassadorNoSnapshots(t *testing.T) {
   111  	m := newAWMetadata(t)
   112  	m.check(0, 0, true, false)
   113  
   114  	// Advance the clock halfway through the diagd boot grace period.
   115  	// We should see alive but not ready.
   116  	m.stepSec(300)
   117  	m.check(1, 300, true, false)
   118  
   119  	// Advance nearly to the end of diagd boot grace period.
   120  	// We should see alive but not ready.
   121  	m.stepSec(299)
   122  	m.check(2, 599, true, false)
   123  
   124  	// Advance to the end of diagd boot grace period.
   125  	// We should see neither alive nor ready.
   126  	m.stepSec(1)
   127  	m.check(3, 600, false, false)
   128  
   129  	// Nothing should change after another minute.
   130  	m.stepSec(60)
   131  	m.check(4, 660, false, false)
   132  }
   133  

View as plain text