github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/kustomize/dependencies_test.go (about)

     1  package kustomize
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/tilt-dev/tilt/internal/testutils/tempdir"
    11  )
    12  
    13  func TestNoFile(t *testing.T) {
    14  	f := newKustomizeFixture(t)
    15  
    16  	f.assertErrorContains("unable to find one of [kustomization.yaml kustomization.yml Kustomization] in directory ")
    17  }
    18  
    19  func TestTooManyFiles(t *testing.T) {
    20  	f := newKustomizeFixture(t)
    21  	f.tempdir.WriteFile("kustomization.yml", "")
    22  	f.tempdir.WriteFile("kustomization.yaml", "")
    23  
    24  	f.assertErrorContains("Found multiple kustomization files under")
    25  }
    26  
    27  func TestEmpty(t *testing.T) {
    28  	f := newKustomizeFixture(t)
    29  	kustomizeFile := ""
    30  	f.writeRootKustomize(kustomizeFile)
    31  
    32  	expected := []string{"kustomization.yaml"}
    33  
    34  	f.assertDeps(expected)
    35  }
    36  
    37  func TestSimple(t *testing.T) {
    38  	f := newKustomizeFixture(t)
    39  	kustomizeFile := `# Example configuration for the webserver
    40  # at https://github.com/monopole/hello
    41  commonLabels:
    42    app: my-hello
    43  
    44  resources:
    45    - deployment.yaml
    46    - service.yaml
    47    - configMap.yaml`
    48  	f.writeRootKustomize(kustomizeFile)
    49  
    50  	expected := []string{"kustomization.yaml", "deployment.yaml", "service.yaml", "configMap.yaml"}
    51  	f.assertDeps(expected)
    52  }
    53  
    54  func TestComplex(t *testing.T) {
    55  	f := newKustomizeFixture(t)
    56  	kustomizeFile := `
    57  # declare ConfigMap as a resource
    58  resources:
    59  - configmap.yaml
    60  
    61  # declare ConfigMap from a ConfigMapGenerator
    62  configMapGenerator:
    63  - name: a-configmap
    64    files:
    65      - configs/configfile
    66      - configs/another_configfile
    67  
    68  patchesJson6902:
    69    - target:
    70        group: extensions
    71        version: v1beta1
    72        kind: Ingress
    73        name: my-ingress
    74      path: ingress_patch.yaml`
    75  	f.writeRootKustomize(kustomizeFile)
    76  
    77  	expected := []string{"kustomization.yaml", "configmap.yaml", "ingress_patch.yaml", "configs/configfile", "configs/another_configfile"}
    78  	f.assertDeps(expected)
    79  }
    80  
    81  func TestRecursive(t *testing.T) {
    82  	f := newKustomizeFixture(t)
    83  
    84  	// these used to be only specified under "bases", but now that's deprecated and "resources"
    85  	// that purpose. for now, test both.
    86  	// https://github.com/tilt-dev/tilt/blob/15d0c94ccc08230d3a528b14cb0a3455b947d13c/vendor/sigs.k8s.io/kustomize/api/types/kustomization.go#L102
    87  	kustomize := `bases:
    88  - ./dev
    89  resources:
    90  - ./staging
    91  - ./production
    92  
    93  namePrefix: cluster-a-`
    94  
    95  	f.writeRootKustomize(kustomize)
    96  
    97  	base := `resources:
    98  - pod.yaml`
    99  	f.writeBaseKustomize("base", base)
   100  	basePod := `apiVersion: v1
   101    kind: Pod
   102    metadata:
   103      name: myapp-pod
   104      labels:
   105        app: myapp
   106    spec:
   107      containers:
   108      - name: nginx
   109        image: nginx:1.7.9`
   110  	f.writeBaseFile("base", "pod.yaml", basePod)
   111  
   112  	dev := `bases:
   113  - ./../base
   114  namePrefix: dev-`
   115  	f.writeBaseKustomize("dev", dev)
   116  
   117  	staging := `bases:
   118  - ./../base
   119  namePrefix: stag-`
   120  	f.writeBaseKustomize("staging", staging)
   121  
   122  	production := `bases:
   123  - ./../base
   124  namePrefix: prod-`
   125  	f.writeBaseKustomize("production", production)
   126  
   127  	expected := []string{
   128  		"base/kustomization.yaml",
   129  		"base/pod.yaml",
   130  		"dev/kustomization.yaml",
   131  		"staging/kustomization.yaml",
   132  		"production/kustomization.yaml",
   133  		"kustomization.yaml",
   134  	}
   135  	f.assertDeps(expected)
   136  }
   137  
   138  // patches was deprecated and then re-added with a different meaning
   139  // https://github.com/tilt-dev/tilt/issues/4081
   140  func TestPatches(t *testing.T) {
   141  	f := newKustomizeFixture(t)
   142  	kustomizeFile := `# Example configuration for the webserver
   143  # at https://github.com/monopole/hello
   144  commonLabels:
   145    app: my-hello
   146  
   147  resources:
   148    - deployment.yaml
   149    - service.yaml
   150    - configMap.yaml
   151  
   152  patches:
   153    - path: patch.yaml
   154      target:
   155        kind: Deployment
   156        name: foo
   157  `
   158  	f.writeRootKustomize(kustomizeFile)
   159  
   160  	expected := []string{"kustomization.yaml", "deployment.yaml", "service.yaml", "configMap.yaml", "patch.yaml"}
   161  	f.assertDeps(expected)
   162  }
   163  
   164  type kustomizeFixture struct {
   165  	t       *testing.T
   166  	tempdir *tempdir.TempDirFixture
   167  }
   168  
   169  func newKustomizeFixture(t *testing.T) *kustomizeFixture {
   170  
   171  	return &kustomizeFixture{
   172  		t:       t,
   173  		tempdir: tempdir.NewTempDirFixture(t),
   174  	}
   175  }
   176  
   177  func (f *kustomizeFixture) writeRootKustomize(contents string) {
   178  	f.tempdir.WriteFile("kustomization.yaml", contents)
   179  }
   180  
   181  func (f *kustomizeFixture) writeBaseKustomize(pathToContainingDirectory, contents string) {
   182  	f.tempdir.WriteFile(filepath.Join(pathToContainingDirectory, "kustomization.yaml"), contents)
   183  }
   184  
   185  func (f *kustomizeFixture) writeBaseFile(pathToContainingDirectory, name, contents string) {
   186  	f.tempdir.WriteFile(filepath.Join(pathToContainingDirectory, name), contents)
   187  }
   188  
   189  func (f *kustomizeFixture) getDeps() []string {
   190  	deps, err := Deps(f.tempdir.Path())
   191  	if err != nil {
   192  		f.t.Fatal(err)
   193  	}
   194  
   195  	return deps
   196  }
   197  
   198  func (f *kustomizeFixture) assertDeps(expected []string) {
   199  	fullExpected := f.tempdir.JoinPaths(expected)
   200  
   201  	actual := f.getDeps()
   202  
   203  	require.ElementsMatch(f.t, fullExpected, actual)
   204  }
   205  
   206  func (f *kustomizeFixture) assertErrorContains(expected string) {
   207  	_, err := Deps(f.tempdir.Path())
   208  	if err == nil {
   209  		f.t.Fatal("Expected an error, got nil")
   210  	}
   211  
   212  	if !strings.Contains(err.Error(), expected) {
   213  		f.t.Errorf("Expected %s to contain %s", err.Error(), expected)
   214  	}
   215  }