...

Source file src/sigs.k8s.io/kustomize/kyaml/comments/comments_test.go

Documentation: sigs.k8s.io/kustomize/kyaml/comments

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package comments
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"sigs.k8s.io/kustomize/kyaml/yaml"
    12  )
    13  
    14  func TestCopyComments(t *testing.T) {
    15  	testCases := []struct {
    16  		name     string
    17  		from     string
    18  		to       string
    19  		expected string
    20  	}{
    21  		{
    22  			name: "copy_comments",
    23  			from: `# A
    24  #
    25  # B
    26  
    27  # C
    28  apiVersion: apps/v1
    29  kind: Deployment
    30  spec: # comment 1
    31    # comment 2
    32    replicas: 3 # comment 3
    33    # comment 4
    34  `,
    35  			to: `apiVersion: apps/v1
    36  kind: Deployment
    37  spec:
    38    replicas: 4
    39  `,
    40  			expected: `# A
    41  #
    42  # B
    43  
    44  # C
    45  apiVersion: apps/v1
    46  kind: Deployment
    47  spec: # comment 1
    48    # comment 2
    49    replicas: 4 # comment 3
    50    # comment 4
    51  `,
    52  		},
    53  
    54  		{
    55  			name: "associative_list",
    56  			from: `
    57  apiVersion: apps/v1
    58  kind: Deployment
    59  spec:
    60    template:
    61      spec:
    62        containers:
    63        - name: foo
    64          image: bar # comment 1
    65  `,
    66  			to: `
    67  apiVersion: apps/v1
    68  kind: Deployment
    69  spec:
    70    template:
    71      spec:
    72        containers:
    73        - name: foo
    74          image: bar
    75  `,
    76  			expected: `
    77  apiVersion: apps/v1
    78  kind: Deployment
    79  spec:
    80    template:
    81      spec:
    82        containers:
    83        - name: foo
    84          image: bar # comment 1
    85  `,
    86  		},
    87  
    88  		{
    89  			name: "associative_list_2",
    90  			from: `
    91  apiVersion: constraints.gatekeeper.sh/v1beta1
    92  kind: EnforceFoo
    93  metadata:
    94    name: enforce-foo
    95  spec:
    96    parameters:
    97      naming_rules:
    98      - kind: Bar
    99        patterns:
   100        # comment 1
   101        - ^(dev|prod|staging|qa|shared)$
   102  `,
   103  			to: `
   104  apiVersion: constraints.gatekeeper.sh/v1beta1
   105  kind: EnforceFoo
   106  metadata:
   107    name: enforce-foo
   108  spec:
   109    parameters:
   110      naming_rules:
   111      - kind: Bar
   112        patterns:
   113        - ^(dev|prod|staging|qa|shared)$
   114  `,
   115  			expected: `
   116  apiVersion: constraints.gatekeeper.sh/v1beta1
   117  kind: EnforceFoo
   118  metadata:
   119    name: enforce-foo
   120  spec:
   121    parameters:
   122      naming_rules:
   123      - kind: Bar
   124        patterns:
   125        # comment 1
   126        - ^(dev|prod|staging|qa|shared)$
   127  `,
   128  		},
   129  
   130  		{
   131  			name: "keep_comments",
   132  			from: `# A
   133  #
   134  # B
   135  
   136  # C
   137  apiVersion: apps/v1
   138  kind: Deployment
   139  spec: # comment 1
   140    # comment 2
   141    replicas: 3 # comment 3
   142    # comment 4
   143  `,
   144  			to: `apiVersion: apps/v1
   145  kind: Deployment
   146  spec:
   147    replicas: 4 # comment 5
   148  `,
   149  			expected: `# A
   150  #
   151  # B
   152  
   153  # C
   154  apiVersion: apps/v1
   155  kind: Deployment
   156  spec: # comment 1
   157    # comment 2
   158    replicas: 4 # comment 5
   159    # comment 4
   160  `,
   161  		},
   162  
   163  		{
   164  			name: "copy_item_comments",
   165  			from: `
   166  apiVersion: apps/v1
   167  kind: Deployment
   168  items:
   169  - a # comment
   170  `,
   171  			to: `
   172  apiVersion: apps/v1
   173  kind: Deployment
   174  items:
   175  - a
   176  `,
   177  			expected: `
   178  apiVersion: apps/v1
   179  kind: Deployment
   180  items:
   181  - a # comment
   182  `,
   183  		},
   184  
   185  		{
   186  			name: "copy_item_comments_2",
   187  			from: `
   188  apiVersion: apps/v1
   189  kind: Deployment
   190  items:
   191  # comment
   192  - a
   193  `,
   194  			to: `
   195  apiVersion: apps/v1
   196  kind: Deployment
   197  items:
   198  - a
   199  `,
   200  			expected: `
   201  apiVersion: apps/v1
   202  kind: Deployment
   203  items:
   204  # comment
   205  - a
   206  `,
   207  		},
   208  
   209  		{
   210  			name: "copy_item_comments_middle",
   211  			from: `
   212  apiVersion: apps/v1
   213  kind: Deployment
   214  items:
   215  - a
   216  - b # comment
   217  - c
   218  `,
   219  			to: `
   220  apiVersion: apps/v1
   221  kind: Deployment
   222  items:
   223  - d
   224  - b
   225  - e
   226  `,
   227  			expected: `
   228  apiVersion: apps/v1
   229  kind: Deployment
   230  items:
   231  - d
   232  - b # comment
   233  - e
   234  `,
   235  		},
   236  
   237  		{
   238  			name: "copy_item_comments_moved",
   239  			from: `
   240  apiVersion: apps/v1
   241  kind: Deployment
   242  items:
   243  - a
   244  - b # comment
   245  - c
   246  `,
   247  			to: `
   248  apiVersion: apps/v1
   249  kind: Deployment
   250  items:
   251  - a
   252  - c
   253  - b
   254  `,
   255  			expected: `
   256  apiVersion: apps/v1
   257  kind: Deployment
   258  items:
   259  - a
   260  - c
   261  - b
   262  `,
   263  		},
   264  
   265  		{
   266  			name: "copy_item_comments_no_match",
   267  			from: `
   268  apiVersion: apps/v1
   269  kind: Deployment
   270  items:
   271  - a # comment
   272  `,
   273  			to: `
   274  apiVersion: apps/v1
   275  kind: Deployment
   276  items:
   277  - b
   278  `,
   279  			expected: `
   280  apiVersion: apps/v1
   281  kind: Deployment
   282  items:
   283  - b
   284  `,
   285  		},
   286  
   287  		{
   288  			name: "copy_item_comments_add",
   289  			from: `
   290  apiVersion: apps/v1
   291  kind: Deployment
   292  items:
   293  - a # comment
   294  `,
   295  			to: `
   296  apiVersion: apps/v1
   297  kind: Deployment
   298  items:
   299  - a
   300  - b
   301  `,
   302  			expected: `
   303  apiVersion: apps/v1
   304  kind: Deployment
   305  items:
   306  - a # comment
   307  - b
   308  `,
   309  		},
   310  
   311  		{
   312  			name: "copy_item_comments_remove",
   313  			from: `
   314  apiVersion: apps/v1
   315  kind: Deployment
   316  items:
   317  - a # comment
   318  - b
   319  `,
   320  			to: `
   321  apiVersion: apps/v1
   322  kind: Deployment
   323  items:
   324  - a
   325  `,
   326  			expected: `
   327  apiVersion: apps/v1
   328  kind: Deployment
   329  items:
   330  - a # comment
   331  `,
   332  		},
   333  
   334  		{
   335  			name: "copy_comments_folded_style",
   336  			from: `
   337  apiVersion: v1
   338  kind: ConfigMap
   339  data:
   340    somekey: "012345678901234567890123456789012345678901234567890123456789012345678901234" # x
   341  `,
   342  			to: `
   343  apiVersion: v1
   344  kind: ConfigMap
   345  data:
   346    somekey: >-
   347      012345678901234567890123456789012345678901234567890123456789012345678901234
   348  `,
   349  			expected: `
   350  apiVersion: v1
   351  kind: ConfigMap
   352  data:
   353    somekey: "012345678901234567890123456789012345678901234567890123456789012345678901234" # x
   354  `,
   355  		},
   356  		{
   357  			name: "sort fields with null value",
   358  			from: `apiVersion: v1
   359  kind: ConfigMap
   360  metadata:
   361    name: workspaces.app.terraform.io
   362    creationTimestamp: null # this field is null
   363    namespace: staging
   364  `,
   365  			to: `apiVersion: v1
   366  kind: ConfigMap
   367  metadata:
   368    name: workspaces.app.terraform.io
   369    creationTimestamp: null
   370    namespace: staging
   371  `,
   372  			expected: `apiVersion: v1
   373  kind: ConfigMap
   374  metadata:
   375    name: workspaces.app.terraform.io
   376    creationTimestamp: null # this field is null
   377    namespace: staging
   378  `,
   379  		},
   380  	}
   381  
   382  	for i := range testCases {
   383  		tc := testCases[i]
   384  		t.Run(tc.name, func(t *testing.T) {
   385  			from, err := yaml.Parse(tc.from)
   386  			if !assert.NoError(t, err) {
   387  				t.FailNow()
   388  			}
   389  
   390  			to, err := yaml.Parse(tc.to)
   391  			if !assert.NoError(t, err) {
   392  				t.FailNow()
   393  			}
   394  
   395  			err = CopyComments(from, to)
   396  			if !assert.NoError(t, err) {
   397  				t.FailNow()
   398  			}
   399  
   400  			actual, err := to.String()
   401  			if !assert.NoError(t, err) {
   402  				t.FailNow()
   403  			}
   404  
   405  			actualFrom, err := from.String()
   406  			if !assert.NoError(t, err) {
   407  				t.FailNow()
   408  			}
   409  
   410  			if !assert.Equal(t, strings.TrimSpace(tc.expected), strings.TrimSpace(actual)) {
   411  				t.FailNow()
   412  			}
   413  
   414  			if !assert.Equal(t, strings.TrimSpace(tc.from), strings.TrimSpace(actualFrom)) {
   415  				t.FailNow()
   416  			}
   417  		})
   418  	}
   419  }
   420  

View as plain text