github.com/tilt-dev/tilt@v0.36.0/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  components:
    90  - ./component
    91  resources:
    92  - ./staging
    93  - ./production
    94  
    95  namePrefix: cluster-a-`
    96  
    97  	f.writeRootKustomize(kustomize)
    98  
    99  	base := `resources:
   100  - pod.yaml`
   101  	f.writeBaseKustomize("base", base)
   102  	basePod := `apiVersion: v1
   103    kind: Pod
   104    metadata:
   105      name: myapp-pod
   106      labels:
   107        app: myapp
   108    spec:
   109      containers:
   110      - name: nginx
   111        image: nginx:1.7.9`
   112  	f.writeBaseFile("base", "pod.yaml", basePod)
   113  
   114  	dev := `bases:
   115  - ./../base
   116  namePrefix: dev-`
   117  	f.writeBaseKustomize("dev", dev)
   118  
   119  	component := `labels:
   120    - pairs:
   121        instance: myapp`
   122  	f.writeBaseKustomize("component", component)
   123  
   124  	staging := `bases:
   125  - ./../base
   126  namePrefix: stag-`
   127  	f.writeBaseKustomize("staging", staging)
   128  
   129  	production := `bases:
   130  - ./../base
   131  namePrefix: prod-`
   132  	f.writeBaseKustomize("production", production)
   133  
   134  	expected := []string{
   135  		"base/kustomization.yaml",
   136  		"base/pod.yaml",
   137  		"component/kustomization.yaml",
   138  		"dev/kustomization.yaml",
   139  		"staging/kustomization.yaml",
   140  		"production/kustomization.yaml",
   141  		"kustomization.yaml",
   142  	}
   143  	f.assertDeps(expected)
   144  }
   145  
   146  // patches was deprecated and then re-added with a different meaning
   147  // https://github.com/tilt-dev/tilt/issues/4081
   148  func TestPatches(t *testing.T) {
   149  	f := newKustomizeFixture(t)
   150  	kustomizeFile := `# Example configuration for the webserver
   151  # at https://github.com/monopole/hello
   152  commonLabels:
   153    app: my-hello
   154  
   155  resources:
   156    - deployment.yaml
   157    - service.yaml
   158    - configMap.yaml
   159  
   160  patches:
   161    - path: patch.yaml
   162      target:
   163        kind: Deployment
   164        name: foo
   165  `
   166  	f.writeRootKustomize(kustomizeFile)
   167  
   168  	expected := []string{"kustomization.yaml", "deployment.yaml", "service.yaml", "configMap.yaml", "patch.yaml"}
   169  	f.assertDeps(expected)
   170  }
   171  
   172  type kustomizeFixture struct {
   173  	t       *testing.T
   174  	tempdir *tempdir.TempDirFixture
   175  }
   176  
   177  func newKustomizeFixture(t *testing.T) *kustomizeFixture {
   178  
   179  	return &kustomizeFixture{
   180  		t:       t,
   181  		tempdir: tempdir.NewTempDirFixture(t),
   182  	}
   183  }
   184  
   185  func (f *kustomizeFixture) writeRootKustomize(contents string) {
   186  	f.tempdir.WriteFile("kustomization.yaml", contents)
   187  }
   188  
   189  func (f *kustomizeFixture) writeBaseKustomize(pathToContainingDirectory, contents string) {
   190  	f.tempdir.WriteFile(filepath.Join(pathToContainingDirectory, "kustomization.yaml"), contents)
   191  }
   192  
   193  func (f *kustomizeFixture) writeBaseFile(pathToContainingDirectory, name, contents string) {
   194  	f.tempdir.WriteFile(filepath.Join(pathToContainingDirectory, name), contents)
   195  }
   196  
   197  func (f *kustomizeFixture) getDeps() []string {
   198  	deps, err := Deps(f.tempdir.Path())
   199  	if err != nil {
   200  		f.t.Fatal(err)
   201  	}
   202  
   203  	return deps
   204  }
   205  
   206  func (f *kustomizeFixture) assertDeps(expected []string) {
   207  	fullExpected := f.tempdir.JoinPaths(expected)
   208  
   209  	actual := f.getDeps()
   210  
   211  	require.ElementsMatch(f.t, fullExpected, actual)
   212  }
   213  
   214  func (f *kustomizeFixture) assertErrorContains(expected string) {
   215  	_, err := Deps(f.tempdir.Path())
   216  	if err == nil {
   217  		f.t.Fatal("Expected an error, got nil")
   218  	}
   219  
   220  	if !strings.Contains(err.Error(), expected) {
   221  		f.t.Errorf("Expected %s to contain %s", err.Error(), expected)
   222  	}
   223  }