github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/thirdparty/cmdconfig/commands/cmdsink/cmdsink_test.go (about)

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package cmdsink
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/GoogleContainerTools/kpt/internal/printer"
    14  	"github.com/GoogleContainerTools/kpt/internal/printer/fake"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestSinkCommand(t *testing.T) {
    19  	d, err := os.MkdirTemp("", "sink-test")
    20  	if !assert.NoError(t, err) {
    21  		t.FailNow()
    22  	}
    23  	// delete the dir as we just want the temp dir path
    24  	// directory should be created by the command
    25  	os.RemoveAll(d)
    26  
    27  	r := GetSinkRunner(fake.CtxWithDefaultPrinter(), "")
    28  	r.Command.SetIn(bytes.NewBufferString(`apiVersion: config.kubernetes.io/v1
    29  kind: ResourceList
    30  items:
    31  - kind: Deployment
    32    metadata:
    33      labels:
    34        app: nginx2
    35      name: foo
    36      annotations:
    37        app: nginx2
    38        config.kubernetes.io/index: '0'
    39        config.kubernetes.io/path: 'f1.yaml'
    40    spec:
    41      replicas: 1
    42  - kind: Service
    43    metadata:
    44      name: foo
    45      annotations:
    46        app: nginx
    47        config.kubernetes.io/index: '1'
    48        config.kubernetes.io/path: 'f1.yaml'
    49    spec:
    50      selector:
    51        app: nginx
    52  - apiVersion: v1
    53    kind: Abstraction
    54    metadata:
    55      name: foo
    56      annotations:
    57        config.kubernetes.io/function: |
    58          container:
    59            image: gcr.io/example/reconciler:v1
    60        config.kubernetes.io/local-config: "true"
    61        config.kubernetes.io/index: '0'
    62        config.kubernetes.io/path: 'f2.yaml'
    63    spec:
    64      replicas: 3
    65  - apiVersion: apps/v1
    66    kind: Deployment
    67    metadata:
    68      labels:
    69        app: nginx
    70      name: bar
    71      annotations:
    72        app: nginx
    73        config.kubernetes.io/index: '1'
    74        config.kubernetes.io/path: 'f2.yaml'
    75    spec:
    76      replicas: 3
    77  `))
    78  	r.Command.SetArgs([]string{d})
    79  	if !assert.NoError(t, r.Command.Execute()) {
    80  		t.FailNow()
    81  	}
    82  	defer os.RemoveAll(d)
    83  
    84  	actual, err := os.ReadFile(filepath.Join(d, "f1.yaml"))
    85  	if !assert.NoError(t, err) {
    86  		t.FailNow()
    87  	}
    88  	expected := `kind: Deployment
    89  metadata:
    90    labels:
    91      app: nginx2
    92    name: foo
    93    annotations:
    94      app: nginx2
    95  spec:
    96    replicas: 1
    97  ---
    98  kind: Service
    99  metadata:
   100    name: foo
   101    annotations:
   102      app: nginx
   103  spec:
   104    selector:
   105      app: nginx
   106  `
   107  	if !assert.Equal(t, expected, string(actual)) {
   108  		t.FailNow()
   109  	}
   110  
   111  	actual, err = os.ReadFile(filepath.Join(d, "f2.yaml"))
   112  	if !assert.NoError(t, err) {
   113  		t.FailNow()
   114  	}
   115  	expected = `apiVersion: v1
   116  kind: Abstraction
   117  metadata:
   118    name: foo
   119    annotations:
   120      config.kubernetes.io/function: |
   121        container:
   122          image: gcr.io/example/reconciler:v1
   123      config.kubernetes.io/local-config: "true"
   124  spec:
   125    replicas: 3
   126  ---
   127  apiVersion: apps/v1
   128  kind: Deployment
   129  metadata:
   130    labels:
   131      app: nginx
   132    name: bar
   133    annotations:
   134      app: nginx
   135  spec:
   136    replicas: 3
   137  `
   138  	if !assert.Equal(t, expected, string(actual)) {
   139  		t.FailNow()
   140  	}
   141  }
   142  
   143  func TestSinkCommand_Error(t *testing.T) {
   144  	d, err := os.MkdirTemp("", "sink-test")
   145  	if !assert.NoError(t, err) {
   146  		t.FailNow()
   147  	}
   148  
   149  	r := GetSinkRunner(fake.CtxWithDefaultPrinter(), "")
   150  	r.Command.SetIn(bytes.NewBufferString(`apiVersion: config.kubernetes.io/v1
   151  kind: ResourceList
   152  items:
   153  - kind: Deployment
   154    metadata:
   155      labels:
   156        app: nginx2
   157      name: foo
   158      annotations:
   159        app: nginx2
   160        config.kubernetes.io/index: '0'
   161        config.kubernetes.io/path: 'f1.yaml'
   162    spec:
   163      replicas: 1
   164  `))
   165  	r.Command.SetArgs([]string{d})
   166  	logs := &bytes.Buffer{}
   167  	r.Ctx = printer.WithContext(r.Ctx, printer.New(nil, logs))
   168  	err = r.Command.Execute()
   169  	if !assert.Error(t, err) {
   170  		t.FailNow()
   171  	}
   172  
   173  	if !assert.Equal(t, fmt.Sprintf(`directory %q already exists, please delete the directory and retry`, d), err.Error()) {
   174  		t.FailNow()
   175  	}
   176  }
   177  
   178  func TestSinkCommandJSON(t *testing.T) {
   179  	d, err := os.MkdirTemp("", "sink-test")
   180  	if !assert.NoError(t, err) {
   181  		t.FailNow()
   182  	}
   183  	// delete the dir as we just want the temp dir path
   184  	// directory should be created by the command
   185  	os.RemoveAll(d)
   186  
   187  	r := GetSinkRunner(fake.CtxWithDefaultPrinter(), "")
   188  	r.Command.SetIn(bytes.NewBufferString(`apiVersion: config.kubernetes.io/v1
   189  kind: ResourceList
   190  items:
   191  - {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo",
   192      "annotations": {"app": "nginx2", config.kubernetes.io/index: '0',
   193        config.kubernetes.io/path: 'f1.json'}}, "spec": {"replicas": 1}}
   194  `))
   195  	r.Command.SetArgs([]string{d})
   196  	if !assert.NoError(t, r.Command.Execute()) {
   197  		t.FailNow()
   198  	}
   199  	defer os.RemoveAll(d)
   200  
   201  	actual, err := os.ReadFile(filepath.Join(d, "f1.json"))
   202  	if !assert.NoError(t, err) {
   203  		t.FailNow()
   204  	}
   205  	expected := `{
   206    "kind": "Deployment",
   207    "metadata": {
   208      "annotations": {
   209        "app": "nginx2"
   210      },
   211      "labels": {
   212        "app": "nginx2"
   213      },
   214      "name": "foo"
   215    },
   216    "spec": {
   217      "replicas": 1
   218    }
   219  }
   220  `
   221  	if !assert.Equal(t, expected, string(actual)) {
   222  		t.FailNow()
   223  	}
   224  }