github.com/grahambrereton-form3/tilt@v0.10.18/internal/engine/configs/configs_controller_test.go (about) 1 package configs 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "reflect" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 13 "github.com/windmilleng/tilt/internal/docker" 14 "github.com/windmilleng/tilt/internal/k8s/testyaml" 15 "github.com/windmilleng/tilt/internal/store" 16 "github.com/windmilleng/tilt/internal/testutils" 17 "github.com/windmilleng/tilt/internal/testutils/manifestbuilder" 18 "github.com/windmilleng/tilt/internal/testutils/tempdir" 19 "github.com/windmilleng/tilt/internal/tiltfile" 20 "github.com/windmilleng/tilt/pkg/model" 21 ) 22 23 func TestConfigsController(t *testing.T) { 24 f := newCCFixture(t) 25 defer f.TearDown() 26 27 assert.Equal(t, model.OrchestratorUnknown, f.docker.Orchestrator) 28 f.addManifest("fe") 29 30 bar := manifestbuilder.New(f, "bar").WithK8sYAML(testyaml.SanchoYAML).Build() 31 a := f.run(bar) 32 33 expected := ConfigsReloadedAction{ 34 Manifests: []model.Manifest{bar}, 35 FinishTime: f.fc.Times[1], 36 } 37 38 assert.Equal(t, expected, a) 39 assert.Equal(t, model.OrchestratorK8s, f.docker.Orchestrator) 40 } 41 42 func TestConfigsControllerDockerNotConnected(t *testing.T) { 43 f := newCCFixture(t) 44 defer f.TearDown() 45 46 assert.Equal(t, model.OrchestratorUnknown, f.docker.Orchestrator) 47 f.addManifest("fe") 48 f.docker.CheckConnectedErr = fmt.Errorf("connection-error") 49 50 bar := manifestbuilder.New(f, "bar").WithK8sYAML(testyaml.SanchoYAML).Build() 51 a := f.run(bar) 52 53 if assert.Error(t, a.Err) { 54 assert.Equal(t, "Failed to connect to Docker: connection-error", a.Err.Error()) 55 } 56 } 57 58 type ccFixture struct { 59 *tempdir.TempDirFixture 60 ctx context.Context 61 cc *ConfigsController 62 st *store.Store 63 getActions func() []store.Action 64 tfl *tiltfile.FakeTiltfileLoader 65 fc *testutils.FakeClock 66 docker *docker.FakeClient 67 } 68 69 func newCCFixture(t *testing.T) *ccFixture { 70 f := tempdir.NewTempDirFixture(t) 71 st, getActions := store.NewStoreForTesting() 72 tfl := tiltfile.NewFakeTiltfileLoader() 73 d := docker.NewFakeClient() 74 cc := NewConfigsController(tfl, d) 75 fc := testutils.NewRandomFakeClock() 76 cc.clock = fc.Clock() 77 ctx, _, _ := testutils.CtxAndAnalyticsForTest() 78 st.AddSubscriber(ctx, cc) 79 go st.Loop(ctx) 80 return &ccFixture{ 81 TempDirFixture: f, 82 ctx: ctx, 83 cc: cc, 84 st: st, 85 getActions: getActions, 86 tfl: tfl, 87 fc: fc, 88 docker: d, 89 } 90 } 91 92 func (f *ccFixture) addManifest(name model.ManifestName) { 93 state := f.st.LockMutableStateForTesting() 94 m := manifestbuilder.New(f, name).WithK8sYAML(testyaml.SanchoYAML).Build() 95 mt := store.NewManifestTarget(m) 96 state.UpsertManifestTarget(mt) 97 state.PendingConfigFileChanges["Tiltfile"] = time.Now() 98 state.TiltfilePath = f.JoinPath("Tiltfile") 99 f.st.UnlockMutableState() 100 } 101 102 func (f *ccFixture) run(m model.Manifest) ConfigsReloadedAction { 103 // configs_controller uses state.RelativeTiltfilePath, which is relative to wd 104 // sometimes the original directory was invalid (e.g., it was another test's temp dir, which was deleted, 105 // but not changed out of), and if it was already invalid, then let's not worry about it. 106 origDir, _ := os.Getwd() 107 err := os.Chdir(f.Path()) 108 if err != nil { 109 f.T().Fatalf("error changing dir: %v", err) 110 } 111 defer func() { 112 if origDir != "" { 113 err = os.Chdir(origDir) 114 if err != nil { 115 f.T().Fatalf("unable to restore original wd: '%v'", err) 116 } 117 } 118 }() 119 120 f.tfl.Result = tiltfile.TiltfileLoadResult{ 121 Manifests: []model.Manifest{m}, 122 } 123 f.st.NotifySubscribers(f.ctx) 124 125 a := store.WaitForAction(f.T(), reflect.TypeOf(ConfigsReloadedAction{}), f.getActions) 126 cra, ok := a.(ConfigsReloadedAction) 127 if !ok { 128 f.T().Fatalf("didn't get an action of type %T", ConfigsReloadedAction{}) 129 } 130 131 return cra 132 }