github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/engine/testdata_test.go (about) 1 package engine 2 3 import ( 4 "fmt" 5 6 "github.com/tilt-dev/tilt/internal/container" 7 "github.com/tilt-dev/tilt/internal/k8s/testyaml" 8 "github.com/tilt-dev/tilt/internal/testutils/manifestbuilder" 9 "github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1" 10 "github.com/tilt-dev/tilt/pkg/model" 11 ) 12 13 type Fixture = manifestbuilder.Fixture 14 15 const SanchoYAML = testyaml.SanchoYAML 16 17 const SanchoDockerfile = ` 18 FROM go:1.10 19 ADD . . 20 RUN go install github.com/tilt-dev/sancho 21 ENTRYPOINT /go/bin/sancho 22 ` 23 24 var SanchoRef = container.MustParseSelector(testyaml.SanchoImage) 25 var SanchoBaseRef = container.MustParseSelector("sancho-base") 26 var SanchoSidecarRef = container.MustParseSelector(testyaml.SanchoSidecarImage) 27 28 func SyncStepsForApp(app string, fixture Fixture) []v1alpha1.LiveUpdateSync { 29 return []v1alpha1.LiveUpdateSync{ 30 { 31 LocalPath: ".", 32 ContainerPath: fmt.Sprintf("/go/src/github.com/tilt-dev/%s", app), 33 }, 34 } 35 } 36 func SanchoSyncSteps(fixture Fixture) []v1alpha1.LiveUpdateSync { 37 return SyncStepsForApp("sancho", fixture) 38 } 39 40 func RunStepsForApp(app string) []v1alpha1.LiveUpdateExec { 41 return []v1alpha1.LiveUpdateExec{ 42 v1alpha1.LiveUpdateExec{Args: []string{"go", "install", fmt.Sprintf("github.com/tilt-dev/%s", app)}}, 43 } 44 } 45 46 var SanchoRunSteps = RunStepsForApp("sancho") 47 48 func NewSanchoLiveUpdateManifest(f Fixture) model.Manifest { 49 return manifestbuilder.New(f, "sancho"). 50 WithK8sYAML(SanchoYAML). 51 WithImageTarget(NewSanchoLiveUpdateImageTarget(f)). 52 Build() 53 } 54 55 func NewSanchoLiveUpdateDCManifest(f Fixture) model.Manifest { 56 return manifestbuilder.New(f, "sancho"). 57 WithDockerCompose(). 58 WithImageTarget(NewSanchoLiveUpdateImageTarget(f)). 59 Build() 60 } 61 62 func NewSanchoCustomBuildManifest(fixture Fixture) model.Manifest { 63 return NewSanchoCustomBuildManifestWithTag(fixture, "") 64 } 65 66 func NewSanchoCustomBuildImageTarget(fixture Fixture) model.ImageTarget { 67 return NewSanchoCustomBuildImageTargetWithTag(fixture, "") 68 } 69 70 func NewSanchoCustomBuildImageTargetWithTag(fixture Fixture, tag string) model.ImageTarget { 71 cb := model.CustomBuild{ 72 CmdImageSpec: v1alpha1.CmdImageSpec{Args: model.ToHostCmd("exit 0").Argv, OutputTag: tag}, 73 Deps: []string{fixture.JoinPath("app")}, 74 } 75 return model.MustNewImageTarget(SanchoRef).WithBuildDetails(cb) 76 } 77 78 func NewSanchoCustomBuildManifestWithTag(fixture Fixture, tag string) model.Manifest { 79 return manifestbuilder.New(fixture, "sancho"). 80 WithK8sYAML(SanchoYAML). 81 WithImageTarget(NewSanchoCustomBuildImageTargetWithTag(fixture, tag)). 82 Build() 83 } 84 85 func NewSanchoDockerBuildImageTarget(f Fixture) model.ImageTarget { 86 return model.MustNewImageTarget(SanchoRef). 87 WithDockerImage(v1alpha1.DockerImageSpec{ 88 DockerfileContents: SanchoDockerfile, 89 Context: f.Path(), 90 }) 91 } 92 93 func NewSanchoLiveUpdate(f Fixture) v1alpha1.LiveUpdateSpec { 94 syncs := []v1alpha1.LiveUpdateSync{ 95 { 96 97 LocalPath: ".", 98 ContainerPath: "/go/src/github.com/tilt-dev/sancho", 99 }, 100 } 101 runs := []v1alpha1.LiveUpdateExec{ 102 { 103 Args: []string{"go", "install", "github.com/tilt-dev/sancho"}, 104 }, 105 } 106 107 return assembleLiveUpdate(syncs, runs, true, []string{}, f) 108 } 109 110 func NewSanchoLiveUpdateImageTarget(f Fixture) model.ImageTarget { 111 return NewSanchoDockerBuildImageTarget(f). 112 WithLiveUpdateSpec("sancho:sancho", NewSanchoLiveUpdate(f)) 113 } 114 115 func NewSanchoSidecarDockerBuildImageTarget(f Fixture) model.ImageTarget { 116 return NewSanchoDockerBuildImageTarget(f).MustWithRef(SanchoSidecarRef) 117 } 118 119 func NewSanchoSidecarLiveUpdateImageTarget(f Fixture) model.ImageTarget { 120 return NewSanchoLiveUpdateImageTarget(f).MustWithRef(SanchoSidecarRef) 121 } 122 123 func NewSanchoDockerBuildManifest(f Fixture) model.Manifest { 124 return NewSanchoDockerBuildManifestWithYaml(f, SanchoYAML) 125 } 126 127 func NewSanchoDockerBuildManifestWithYaml(f Fixture, yaml string) model.Manifest { 128 return manifestbuilder.New(f, "sancho"). 129 WithK8sYAML(yaml). 130 WithImageTarget(NewSanchoDockerBuildImageTarget(f)). 131 Build() 132 } 133 134 func NewSanchoMultiStageImages(fixture Fixture) []model.ImageTarget { 135 baseImage := model.MustNewImageTarget(SanchoBaseRef). 136 WithDockerImage(v1alpha1.DockerImageSpec{ 137 DockerfileContents: `FROM golang:1.10`, 138 Context: fixture.JoinPath("sancho-base"), 139 }) 140 141 srcImage := model.MustNewImageTarget(SanchoRef). 142 WithDockerImage(v1alpha1.DockerImageSpec{ 143 DockerfileContents: ` 144 FROM sancho-base 145 ADD . . 146 RUN go install github.com/tilt-dev/sancho 147 ENTRYPOINT /go/bin/sancho 148 `, 149 Context: fixture.JoinPath("sancho"), 150 }).WithImageMapDeps([]string{baseImage.ImageMapName()}) 151 return []model.ImageTarget{baseImage, srcImage} 152 } 153 154 func NewManifestsWithCommonAncestor(fixture Fixture) (model.Manifest, model.Manifest) { 155 refCommon := container.MustParseSelector("gcr.io/common") 156 ref1 := container.MustParseSelector("gcr.io/image-1") 157 ref2 := container.MustParseSelector("gcr.io/image-2") 158 159 fixture.MkdirAll("common") 160 fixture.MkdirAll("image-1") 161 fixture.MkdirAll("image-2") 162 163 targetCommon := model.MustNewImageTarget(refCommon). 164 WithDockerImage(v1alpha1.DockerImageSpec{ 165 DockerfileContents: `FROM golang:1.10`, 166 Context: fixture.JoinPath("common"), 167 }) 168 target1 := model.MustNewImageTarget(ref1). 169 WithDockerImage(v1alpha1.DockerImageSpec{ 170 DockerfileContents: `FROM ` + refCommon.String(), 171 Context: fixture.JoinPath("image-1"), 172 }).WithImageMapDeps([]string{targetCommon.ImageMapName()}) 173 target2 := model.MustNewImageTarget(ref2). 174 WithDockerImage(v1alpha1.DockerImageSpec{ 175 DockerfileContents: `FROM ` + refCommon.String(), 176 Context: fixture.JoinPath("image-2"), 177 }).WithImageMapDeps([]string{targetCommon.ImageMapName()}) 178 179 m1 := manifestbuilder.New(fixture, "image-1"). 180 WithK8sYAML(testyaml.Deployment("image-1", ref1.String())). 181 WithImageTargets(targetCommon, target1). 182 Build() 183 m2 := manifestbuilder.New(fixture, "image-2"). 184 WithK8sYAML(testyaml.Deployment("image-2", ref2.String())). 185 WithImageTargets(targetCommon, target2). 186 Build() 187 return m1, m2 188 } 189 190 func NewManifestsWithTwoCommonAncestors(fixture Fixture) (model.Manifest, model.Manifest) { 191 refBase := container.MustParseSelector("gcr.io/base") 192 refCommon := container.MustParseSelector("gcr.io/common") 193 ref1 := container.MustParseSelector("gcr.io/image-1") 194 ref2 := container.MustParseSelector("gcr.io/image-2") 195 196 fixture.MkdirAll("base") 197 fixture.MkdirAll("common") 198 fixture.MkdirAll("image-1") 199 fixture.MkdirAll("image-2") 200 201 targetBase := model.MustNewImageTarget(refBase). 202 WithDockerImage(v1alpha1.DockerImageSpec{ 203 DockerfileContents: `FROM golang:1.10`, 204 Context: fixture.JoinPath("base"), 205 }) 206 targetCommon := model.MustNewImageTarget(refCommon). 207 WithDockerImage(v1alpha1.DockerImageSpec{ 208 DockerfileContents: `FROM ` + refBase.String(), 209 Context: fixture.JoinPath("common"), 210 }).WithImageMapDeps([]string{targetBase.ImageMapName()}) 211 target1 := model.MustNewImageTarget(ref1). 212 WithDockerImage(v1alpha1.DockerImageSpec{ 213 DockerfileContents: `FROM ` + refCommon.String(), 214 Context: fixture.JoinPath("image-1"), 215 }).WithImageMapDeps([]string{targetCommon.ImageMapName()}) 216 target2 := model.MustNewImageTarget(ref2). 217 WithDockerImage(v1alpha1.DockerImageSpec{ 218 DockerfileContents: `FROM ` + refCommon.String(), 219 Context: fixture.JoinPath("image-2"), 220 }).WithImageMapDeps([]string{targetCommon.ImageMapName()}) 221 222 m1 := manifestbuilder.New(fixture, "image-1"). 223 WithK8sYAML(testyaml.Deployment("image-1", ref1.String())). 224 WithImageTargets(targetBase, targetCommon, target1). 225 Build() 226 m2 := manifestbuilder.New(fixture, "image-2"). 227 WithK8sYAML(testyaml.Deployment("image-2", ref2.String())). 228 WithImageTargets(targetBase, targetCommon, target2). 229 Build() 230 return m1, m2 231 } 232 233 func NewManifestsWithSameTwoImages(fixture Fixture) (model.Manifest, model.Manifest) { 234 refCommon := container.MustParseSelector("gcr.io/common") 235 ref1 := container.MustParseSelector("gcr.io/image-1") 236 237 fixture.MkdirAll("common") 238 fixture.MkdirAll("image-1") 239 240 targetCommon := model.MustNewImageTarget(refCommon). 241 WithDockerImage(v1alpha1.DockerImageSpec{ 242 DockerfileContents: `FROM golang:1.10`, 243 Context: fixture.JoinPath("common"), 244 }) 245 target1 := model.MustNewImageTarget(ref1). 246 WithDockerImage(v1alpha1.DockerImageSpec{ 247 DockerfileContents: `FROM ` + refCommon.String(), 248 Context: fixture.JoinPath("image-1"), 249 }).WithImageMapDeps([]string{targetCommon.ImageMapName()}) 250 251 m1 := manifestbuilder.New(fixture, "dep-1"). 252 WithK8sYAML(testyaml.Deployment("dep-1", ref1.String())). 253 WithImageTargets(targetCommon, target1). 254 Build() 255 m2 := manifestbuilder.New(fixture, "dep-2"). 256 WithK8sYAML(testyaml.Deployment("dep-2", ref1.String())). 257 WithImageTargets(targetCommon, target1). 258 Build() 259 return m1, m2 260 } 261 262 func assembleLiveUpdate(syncs []v1alpha1.LiveUpdateSync, runs []v1alpha1.LiveUpdateExec, shouldRestart bool, fallBackOn []string, f Fixture) v1alpha1.LiveUpdateSpec { 263 restart := v1alpha1.LiveUpdateRestartStrategyNone 264 if shouldRestart { 265 restart = v1alpha1.LiveUpdateRestartStrategyAlways 266 } 267 return v1alpha1.LiveUpdateSpec{ 268 BasePath: f.Path(), 269 Syncs: syncs, 270 Execs: runs, 271 StopPaths: fallBackOn, 272 Restart: restart, 273 } 274 }