|
7 | 7 | "testing"
|
8 | 8 | "time"
|
9 | 9 |
|
| 10 | + "github.com/golang/protobuf/ptypes/timestamp" |
10 | 11 | "github.com/stretchr/testify/assert"
|
11 | 12 | "github.com/stretchr/testify/require"
|
12 | 13 |
|
@@ -205,8 +206,110 @@ func TestLocalResource(t *testing.T) {
|
205 | 206 | r := v.Resources[1]
|
206 | 207 | assert.Equal(t, "test", r.Name)
|
207 | 208 | assert.Equal(t, model.RuntimeStatusNotApplicable, model.RuntimeStatus(r.RuntimeStatus))
|
| 209 | + require.Len(t, r.Specs, 1) |
| 210 | + spec := r.Specs[0] |
| 211 | + require.Equal(t, proto_webview.TargetType_TT_LOCAL, spec.Type) |
| 212 | + require.False(t, spec.HasLiveUpdate) |
208 | 213 | }
|
209 | 214 |
|
| 215 | +func TestBuildHistory(t *testing.T) { |
| 216 | + br1 := model.BuildRecord{ |
| 217 | + StartTime: time.Now().Add(-1 * time.Hour), |
| 218 | + FinishTime: time.Now().Add(-50 * time.Minute), |
| 219 | + Reason: model.BuildReasonFlagInit, |
| 220 | + BuildTypes: []model.BuildType{model.BuildTypeImage, model.BuildTypeK8s}, |
| 221 | + } |
| 222 | + br2 := model.BuildRecord{ |
| 223 | + Edits: []string{"a.txt", "b.txt"}, |
| 224 | + StartTime: time.Now().Add(-45 * time.Minute), |
| 225 | + FinishTime: time.Now().Add(-44 * time.Minute), |
| 226 | + Reason: model.BuildReasonFlagChangedFiles, |
| 227 | + BuildTypes: []model.BuildType{model.BuildTypeLiveUpdate}, |
| 228 | + } |
| 229 | + br3 := model.BuildRecord{ |
| 230 | + StartTime: time.Now().Add(-20 * time.Minute), |
| 231 | + FinishTime: time.Now().Add(-19 * time.Minute), |
| 232 | + Reason: model.BuildReasonFlagCrash, |
| 233 | + BuildTypes: []model.BuildType{model.BuildTypeImage, model.BuildTypeK8s}, |
| 234 | + } |
| 235 | + buildRecords := []model.BuildRecord{br1, br2, br3} |
| 236 | + expectedUpdateTypes := [][]proto_webview.UpdateType{ |
| 237 | + []proto_webview.UpdateType{proto_webview.UpdateType_UT_IMAGE, proto_webview.UpdateType_UT_K8S}, |
| 238 | + []proto_webview.UpdateType{proto_webview.UpdateType_UT_LIVE_UPDATE}, |
| 239 | + []proto_webview.UpdateType{proto_webview.UpdateType_UT_IMAGE, proto_webview.UpdateType_UT_K8S}, |
| 240 | + } |
| 241 | + |
| 242 | + m := model.Manifest{ |
| 243 | + Name: "foo", |
| 244 | + }.WithDeployTarget(model.K8sTarget{}) |
| 245 | + state := newState([]model.Manifest{m}) |
| 246 | + state.ManifestTargets[m.Name].State.BuildHistory = buildRecords |
| 247 | + |
| 248 | + v := stateToProtoView(t, *state) |
| 249 | + require.Equal(t, 2, len(v.Resources)) |
| 250 | + r := v.Resources[1] |
| 251 | + require.Equal(t, "foo", r.Name) |
| 252 | + require.Len(t, r.BuildHistory, 3) |
| 253 | + |
| 254 | + for i, actual := range r.BuildHistory { |
| 255 | + expected := buildRecords[i] |
| 256 | + require.Equal(t, expected.Edits, actual.Edits) |
| 257 | + require.Equal(t, mustTimeToProto(expected.StartTime), actual.StartTime) |
| 258 | + require.Equal(t, mustTimeToProto(expected.FinishTime), actual.FinishTime) |
| 259 | + require.Equal(t, i == 2, actual.IsCrashRebuild) |
| 260 | + require.ElementsMatch(t, expectedUpdateTypes[i], actual.UpdateTypes) |
| 261 | + } |
| 262 | +} |
| 263 | + |
| 264 | +func TestSpecs(t *testing.T) { |
| 265 | + lu, err := model.NewLiveUpdate( |
| 266 | + []model.LiveUpdateStep{model.LiveUpdateSyncStep{Source: "foo", Dest: "bar"}}, ".") |
| 267 | + require.NoError(t, err) |
| 268 | + luTarg := model.ImageTarget{}.WithBuildDetails(model.DockerBuild{LiveUpdate: lu}) |
| 269 | + |
| 270 | + mNoLiveUpd := model.Manifest{Name: "noLiveUpd"}.WithImageTarget(model.ImageTarget{}).WithDeployTarget(model.K8sTarget{}) |
| 271 | + mLiveUpd := model.Manifest{Name: "liveUpd"}.WithImageTarget(luTarg).WithDeployTarget(model.K8sTarget{}) |
| 272 | + mLocal := model.Manifest{Name: "local"}.WithDeployTarget(model.LocalTarget{}) |
| 273 | + |
| 274 | + expected := []struct { |
| 275 | + name string |
| 276 | + targetTypes []proto_webview.TargetType |
| 277 | + hasLiveUpdate bool |
| 278 | + }{ |
| 279 | + {"noLiveUpd", []proto_webview.TargetType{proto_webview.TargetType_TT_IMAGE, proto_webview.TargetType_TT_K8S}, false}, |
| 280 | + {"liveUpd", []proto_webview.TargetType{proto_webview.TargetType_TT_IMAGE, proto_webview.TargetType_TT_K8S}, true}, |
| 281 | + {"local", []proto_webview.TargetType{proto_webview.TargetType_TT_LOCAL}, false}, |
| 282 | + } |
| 283 | + state := newState([]model.Manifest{mNoLiveUpd, mLiveUpd, mLocal}) |
| 284 | + v := stateToProtoView(t, *state) |
| 285 | + |
| 286 | + require.Equal(t, 4, len(v.Resources)) |
| 287 | + for i, r := range v.Resources { |
| 288 | + if i == 0 { |
| 289 | + continue // skip Tiltfile |
| 290 | + } |
| 291 | + expected := expected[i-1] |
| 292 | + require.Equal(t, expected.name, r.Name, "name mismatch for resource at index %d", i) |
| 293 | + observedTypes := []proto_webview.TargetType{} |
| 294 | + var iTargHasLU bool |
| 295 | + for _, spec := range r.Specs { |
| 296 | + observedTypes = append(observedTypes, spec.Type) |
| 297 | + if spec.Type == proto_webview.TargetType_TT_IMAGE { |
| 298 | + iTargHasLU = spec.HasLiveUpdate |
| 299 | + } |
| 300 | + } |
| 301 | + require.ElementsMatch(t, expected.targetTypes, observedTypes, "for resource %q", r.Name) |
| 302 | + require.Equal(t, expected.hasLiveUpdate, iTargHasLU, "for resource %q", r.Name) |
| 303 | + } |
| 304 | +} |
| 305 | + |
| 306 | +func mustTimeToProto(t time.Time) *timestamp.Timestamp { |
| 307 | + ts, err := timeToProto(t) |
| 308 | + if err != nil { |
| 309 | + panic(err) |
| 310 | + } |
| 311 | + return ts |
| 312 | +} |
210 | 313 | func findResource(n model.ManifestName, view *proto_webview.View) (*proto_webview.Resource, bool) {
|
211 | 314 | for _, res := range view.Resources {
|
212 | 315 | if res.Name == n.String() {
|
|
0 commit comments