github.com/grahambrereton-form3/tilt@v0.10.18/internal/store/engine_state_test.go (about)

     1  package store
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"sort"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/windmilleng/tilt/internal/hud/view"
    13  	"github.com/windmilleng/tilt/internal/k8s/testyaml"
    14  
    15  	"github.com/windmilleng/tilt/internal/k8s"
    16  	"github.com/windmilleng/tilt/pkg/model"
    17  )
    18  
    19  func TestStateToViewMultipleSyncs(t *testing.T) {
    20  	m := model.Manifest{
    21  		Name: "foo",
    22  	}.WithImageTarget(model.ImageTarget{}.
    23  		WithBuildDetails(model.FastBuild{
    24  			Syncs: []model.Sync{
    25  				{LocalPath: "/a/b"},
    26  				{LocalPath: "/a/b/c"},
    27  			},
    28  		}),
    29  	)
    30  	state := newState([]model.Manifest{m})
    31  	ms := state.ManifestTargets[m.Name].State
    32  	ms.CurrentBuild.Edits = []string{"/a/b/d", "/a/b/c/d/e"}
    33  	ms.BuildHistory = []model.BuildRecord{
    34  		{Edits: []string{"/a/b/d", "/a/b/c/d/e"}},
    35  	}
    36  	ms.MutableBuildStatus(m.ImageTargets[0].ID()).PendingFileChanges =
    37  		map[string]time.Time{"/a/b/d": time.Now(), "/a/b/c/d/e": time.Now()}
    38  	v := StateToView(*state)
    39  
    40  	if !assert.Equal(t, 2, len(v.Resources)) {
    41  		return
    42  	}
    43  
    44  	r, _ := v.Resource(m.Name)
    45  	assert.Equal(t, []string{"d", "d/e"}, r.LastBuild().Edits)
    46  
    47  	sort.Strings(r.CurrentBuild.Edits)
    48  	assert.Equal(t, []string{"d", "d/e"}, r.CurrentBuild.Edits)
    49  	assert.Equal(t, []string{"d", "d/e"}, r.PendingBuildEdits)
    50  }
    51  
    52  func TestStateToViewPortForwards(t *testing.T) {
    53  	m := model.Manifest{
    54  		Name: "foo",
    55  	}.WithDeployTarget(model.K8sTarget{
    56  		PortForwards: []model.PortForward{
    57  			{LocalPort: 8000, ContainerPort: 5000},
    58  			{LocalPort: 7000, ContainerPort: 5001},
    59  		},
    60  	})
    61  	state := newState([]model.Manifest{m})
    62  	v := StateToView(*state)
    63  	res, _ := v.Resource(m.Name)
    64  	assert.Equal(t,
    65  		[]string{"http://localhost:7000/", "http://localhost:8000/"},
    66  		res.Endpoints)
    67  }
    68  
    69  func TestStateToViewUnresourcedYAMLManifest(t *testing.T) {
    70  	m, err := k8s.NewK8sOnlyManifestFromYAML(testyaml.SanchoYAML)
    71  	assert.NoError(t, err)
    72  	state := newState([]model.Manifest{m})
    73  	v := StateToView(*state)
    74  
    75  	assert.Equal(t, 2, len(v.Resources))
    76  
    77  	r, _ := v.Resource(m.Name)
    78  	assert.Equal(t, nil, r.LastBuild().Error)
    79  
    80  	expectedInfo := view.YAMLResourceInfo{K8sResources: []string{"sancho:deployment"}}
    81  	assert.Equal(t, expectedInfo, r.ResourceInfo)
    82  }
    83  
    84  func TestMostRecentPod(t *testing.T) {
    85  	podA := Pod{PodID: "pod-a", StartedAt: time.Now()}
    86  	podB := Pod{PodID: "pod-b", StartedAt: time.Now().Add(time.Minute)}
    87  	podC := Pod{PodID: "pod-c", StartedAt: time.Now().Add(-time.Minute)}
    88  	podSet := NewK8sRuntimeState(podA, podB, podC)
    89  	assert.Equal(t, "pod-b", podSet.MostRecentPod().PodID.String())
    90  }
    91  
    92  func TestRelativeTiltfilePath(t *testing.T) {
    93  	es := newState([]model.Manifest{})
    94  	wd, err := os.Getwd()
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	es.TiltfilePath = filepath.Join(wd, "Tiltfile")
    99  
   100  	actual, err := es.RelativeTiltfilePath()
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	assert.Equal(t, "Tiltfile", actual)
   105  }
   106  
   107  func newState(manifests []model.Manifest) *EngineState {
   108  	ret := NewState()
   109  	for _, m := range manifests {
   110  		ret.ManifestTargets[m.Name] = NewManifestTarget(m)
   111  		ret.ManifestDefinitionOrder = append(ret.ManifestDefinitionOrder, m.Name)
   112  	}
   113  
   114  	return ret
   115  }