github.com/grahambrereton-form3/tilt@v0.10.18/internal/hud/webview/convert_test.go (about) 1 package webview 2 3 import ( 4 "os" 5 "path/filepath" 6 "sort" 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/windmilleng/tilt/internal/k8s" 14 "github.com/windmilleng/tilt/internal/k8s/testyaml" 15 "github.com/windmilleng/tilt/internal/store" 16 "github.com/windmilleng/tilt/pkg/model" 17 proto_webview "github.com/windmilleng/tilt/pkg/webview" 18 ) 19 20 var fooManifest = model.Manifest{Name: "foo"}.WithDeployTarget(model.K8sTarget{}) 21 22 func stateToProtoView(t *testing.T, s store.EngineState) *proto_webview.View { 23 v, err := StateToProtoView(s) 24 if err != nil { 25 t.Fatal(err) 26 } 27 28 return v 29 } 30 31 func TestStateToWebViewMultipleSyncs(t *testing.T) { 32 m := model.Manifest{ 33 Name: "foo", 34 }.WithDeployTarget(model.K8sTarget{}).WithImageTarget(model.ImageTarget{}. 35 WithBuildDetails(model.FastBuild{ 36 Syncs: []model.Sync{ 37 {LocalPath: "/a/b"}, 38 {LocalPath: "/a/b/c"}, 39 }, 40 }), 41 ) 42 43 state := newState([]model.Manifest{m}) 44 ms := state.ManifestTargets[m.Name].State 45 ms.CurrentBuild.Edits = []string{"/a/b/d", "/a/b/c/d/e"} 46 ms.BuildHistory = []model.BuildRecord{ 47 {Edits: []string{"/a/b/d", "/a/b/c/d/e"}}, 48 } 49 ms.MutableBuildStatus(m.ImageTargets[0].ID()).PendingFileChanges = 50 map[string]time.Time{"/a/b/d": time.Now(), "/a/b/c/d/e": time.Now()} 51 v := stateToProtoView(t, *state) 52 53 if !assert.Equal(t, 2, len(v.Resources)) { 54 return 55 } 56 57 r, _ := findResource(m.Name, v) 58 assert.Equal(t, []string{"d", "d/e"}, lastBuild(r).Edits) 59 60 sort.Strings(r.CurrentBuild.Edits) 61 assert.Equal(t, []string{"d", "d/e"}, r.CurrentBuild.Edits) 62 assert.Equal(t, []string{"d", "d/e"}, r.PendingBuildEdits) 63 } 64 65 func TestStateToWebViewPortForwards(t *testing.T) { 66 m := model.Manifest{ 67 Name: "foo", 68 }.WithDeployTarget(model.K8sTarget{ 69 PortForwards: []model.PortForward{ 70 {LocalPort: 8000, ContainerPort: 5000}, 71 {LocalPort: 7000, ContainerPort: 5001}, 72 }, 73 }) 74 state := newState([]model.Manifest{m}) 75 v := stateToProtoView(t, *state) 76 res, _ := findResource(m.Name, v) 77 assert.Equal(t, 78 []string{"http://localhost:7000/", "http://localhost:8000/"}, 79 res.Endpoints) 80 } 81 82 func TestStateToViewUnresourcedYAMLManifest(t *testing.T) { 83 m, err := k8s.NewK8sOnlyManifestFromYAML(testyaml.SanchoYAML) 84 assert.NoError(t, err) 85 state := newState([]model.Manifest{m}) 86 v := stateToProtoView(t, *state) 87 88 assert.Equal(t, 2, len(v.Resources)) 89 90 r, _ := findResource(m.Name, v) 91 assert.Equal(t, "", lastBuild(r).Error) 92 93 expectedInfo := &proto_webview.YAMLResourceInfo{K8SResources: []string{"sancho:deployment"}} 94 assert.Equal(t, expectedInfo, r.YamlResourceInfo) 95 } 96 97 func TestStateToViewTiltfileLog(t *testing.T) { 98 es := newState([]model.Manifest{}) 99 es.TiltfileState.CombinedLog = model.AppendLog( 100 es.TiltfileState.CombinedLog, 101 store.NewLogEvent("Tiltfile", []byte("hello")), 102 false, 103 "", 104 nil) 105 v := stateToProtoView(t, *es) 106 r, ok := findResource("(Tiltfile)", v) 107 require.True(t, ok, "no resource named (Tiltfile) found") 108 assert.Equal(t, "hello", r.CombinedLog) 109 } 110 111 func TestRelativeTiltfilePath(t *testing.T) { 112 es := newState([]model.Manifest{}) 113 wd, err := os.Getwd() 114 if err != nil { 115 t.Fatal(err) 116 } 117 es.TiltfilePath = filepath.Join(wd, "Tiltfile") 118 119 actual, err := es.RelativeTiltfilePath() 120 if err != nil { 121 t.Fatal(err) 122 } 123 assert.Equal(t, "Tiltfile", actual) 124 } 125 126 func TestNeedsNudgeSet(t *testing.T) { 127 state := newState(nil) 128 129 m := fooManifest 130 targ := store.NewManifestTarget(m) 131 targ.State = &store.ManifestState{} 132 state.UpsertManifestTarget(targ) 133 134 v := stateToProtoView(t, *state) 135 136 assert.False(t, v.NeedsAnalyticsNudge, 137 "LastSuccessfulDeployTime not set, so NeedsNudge should not be set") 138 139 targ.State = &store.ManifestState{LastSuccessfulDeployTime: time.Now()} 140 state.UpsertManifestTarget(targ) 141 142 v = stateToProtoView(t, *state) 143 assert.True(t, v.NeedsAnalyticsNudge) 144 } 145 146 func TestTriggerMode(t *testing.T) { 147 state := newState(nil) 148 m := fooManifest 149 targ := store.NewManifestTarget(m) 150 targ.Manifest.TriggerMode = model.TriggerModeManualAfterInitial 151 targ.State = &store.ManifestState{} 152 state.UpsertManifestTarget(targ) 153 154 v := stateToProtoView(t, *state) 155 assert.Equal(t, 2, len(v.Resources)) 156 157 newM, _ := findResource(model.ManifestName("foo"), v) 158 assert.Equal(t, model.TriggerModeManualAfterInitial, model.TriggerMode(newM.TriggerMode)) 159 } 160 161 func TestFeatureFlags(t *testing.T) { 162 state := newState(nil) 163 state.Features = map[string]bool{"foo_feature": true} 164 165 v := stateToProtoView(t, *state) 166 assert.Equal(t, v.FeatureFlags, map[string]bool{"foo_feature": true}) 167 } 168 169 func TestReadinessCheckFailing(t *testing.T) { 170 m := model.Manifest{ 171 Name: "foo", 172 }.WithDeployTarget(model.K8sTarget{}) 173 state := newState([]model.Manifest{m}) 174 state.ManifestTargets[m.Name].State.RuntimeState = store.K8sRuntimeState{ 175 Pods: map[k8s.PodID]*store.Pod{ 176 "pod id": { 177 Status: "Running", 178 Phase: "Running", 179 Containers: []store.Container{ 180 { 181 Ready: false, 182 }, 183 }, 184 }, 185 }, 186 } 187 188 v := stateToProtoView(t, *state) 189 rv, ok := findResource(m.Name, v) 190 require.True(t, ok) 191 require.Equal(t, RuntimeStatusPending, RuntimeStatus(rv.RuntimeStatus)) 192 } 193 194 func TestLocalResource(t *testing.T) { 195 cmd := model.Cmd{ 196 Argv: []string{"make", "test"}, 197 } 198 lt := model.NewLocalTarget("my-local", cmd, "path/to/tiltfile", []string{"/foo/bar", "/baz/qux"}) 199 m := model.Manifest{ 200 Name: "test", 201 }.WithDeployTarget(lt) 202 203 state := newState([]model.Manifest{m}) 204 v := stateToProtoView(t, *state) 205 206 assert.Equal(t, 2, len(v.Resources)) 207 r := v.Resources[1] 208 assert.Equal(t, "test", r.Name) 209 assert.Equal(t, RuntimeStatusNotApplicable, RuntimeStatus(r.RuntimeStatus)) 210 } 211 212 func findResource(n model.ManifestName, view *proto_webview.View) (*proto_webview.Resource, bool) { 213 for _, res := range view.Resources { 214 if res.Name == n.String() { 215 return res, true 216 } 217 } 218 219 return nil, false 220 } 221 222 func lastBuild(r *proto_webview.Resource) *proto_webview.BuildRecord { 223 if len(r.BuildHistory) == 0 { 224 return &proto_webview.BuildRecord{} 225 } 226 227 return r.BuildHistory[0] 228 } 229 230 func newState(manifests []model.Manifest) *store.EngineState { 231 ret := store.NewState() 232 for _, m := range manifests { 233 ret.ManifestTargets[m.Name] = store.NewManifestTarget(m) 234 ret.ManifestDefinitionOrder = append(ret.ManifestDefinitionOrder, m.Name) 235 } 236 237 return ret 238 }