github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/engine/buildcontrol/testdata_test.go (about)

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