...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/snippet/snippetgeneration/snippetgeneration_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/snippet/snippetgeneration

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package snippetgeneration
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/fileutil"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/repo"
    23  )
    24  
    25  func TestSnippifyResourceConfig(t *testing.T) {
    26  	tests := []struct {
    27  		testName string
    28  		config   string
    29  		expected string
    30  	}{
    31  		{
    32  			testName: "basicConfig",
    33  			config: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
    34  kind: MyResourceKind
    35  metadata:
    36    name: myresourcekind-sample
    37  spec:
    38    field: value`,
    39  			expected: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
    40  kind: MyResourceKind
    41  metadata:
    42    name: \${1:myresourcekind-name}
    43  spec:
    44    field: \${2:value}`,
    45  		},
    46  		{
    47  			testName: "basicConfigWithLabel",
    48  			config: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
    49  kind: MyResourceKind
    50  metadata:
    51    name: myresourcekind-sample
    52    labels:
    53      labelA: valueA
    54  spec:
    55    field: value`,
    56  			expected: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
    57  kind: MyResourceKind
    58  metadata:
    59    labels:
    60      \${1:labelA}: \${2:valueA}
    61    name: \${3:myresourcekind-name}
    62  spec:
    63    field: \${4:value}`,
    64  		},
    65  
    66  		{
    67  			testName: "basicConfigWithLabels",
    68  			config: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
    69  kind: MyResourceKind
    70  metadata:
    71    name: myresourcekind-sample
    72    labels:
    73      labelA: valueA
    74      labelB: valueB
    75  spec:
    76    field: value`,
    77  			expected: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
    78  kind: MyResourceKind
    79  metadata:
    80    labels:
    81      \${1:labelA}: \${2:valueA}
    82      \${3:labelB}: \${4:valueB}
    83    name: \${5:myresourcekind-name}
    84  spec:
    85    field: \${6:value}`,
    86  		},
    87  		{
    88  			testName: "basicConfigWithComments",
    89  			config: `# Top-level comment
    90  apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
    91  kind: MyResourceKind #In-line comment
    92  metadata:
    93    name: myresourcekind-sample
    94  spec:
    95    # Comment
    96    field: value`,
    97  			expected: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
    98  kind: MyResourceKind
    99  metadata:
   100    name: \${1:myresourcekind-name}
   101  spec:
   102    field: \${2:value}`,
   103  		},
   104  		{
   105  			testName: "basicConfigWithBraces",
   106  			config: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
   107  kind: MyResourceKind
   108  metadata:
   109    name: myresourcekind-sample
   110  spec:
   111    field: ${uniqueId}`,
   112  			expected: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
   113  kind: MyResourceKind
   114  metadata:
   115    name: \${1:myresourcekind-name}
   116  spec:
   117    field: \${2:[uniqueId]}`,
   118  		},
   119  		{
   120  			testName: "configWithVariousFieldTypes",
   121  			config: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
   122  kind: MyResourceKind
   123  metadata:
   124    name: myresourcekind-sample
   125  spec:
   126    numVal: 9000
   127    numWithScientificNotation: 1e+12
   128    floatVal: 3.14
   129    boolVal: true
   130    stringValWithQuotes: "Hello world"
   131    stringVal: Hello world`,
   132  			expected: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
   133  kind: MyResourceKind
   134  metadata:
   135    name: \${1:myresourcekind-name}
   136  spec:
   137    numVal: \${2:9000}
   138    numWithScientificNotation: \${3:1e+12}
   139    floatVal: \${4:3.14}
   140    boolVal: \${5:true}
   141    stringValWithQuotes: \${6:Hello world}
   142    stringVal: \${7:Hello world}`,
   143  		},
   144  		{
   145  			testName: "configWithNestedMaps",
   146  			config: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
   147  kind: MyResourceKind
   148  metadata:
   149    name: myresourcekind-sample
   150  spec:
   151    fieldA:
   152      field: value
   153    fieldB:
   154      field:
   155        field: value`,
   156  			expected: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
   157  kind: MyResourceKind
   158  metadata:
   159    name: \${1:myresourcekind-name}
   160  spec:
   161    fieldA:
   162      field: \${2:value}
   163    fieldB:
   164      field:
   165        field: \${3:value}`,
   166  		},
   167  		{
   168  			testName: "configWithSequences",
   169  			config: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
   170  kind: MyResourceKind
   171  metadata:
   172    name: myresourcekind-sample
   173  spec:
   174    field:
   175    - valueOne
   176    - valueTwo
   177    - valueThree`,
   178  			expected: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
   179  kind: MyResourceKind
   180  metadata:
   181    name: \${1:myresourcekind-name}
   182  spec:
   183    field:
   184    - \${2:valueOne}
   185    - \${3:valueTwo}
   186    - \${4:valueThree}`,
   187  		},
   188  		{
   189  			testName: "configWithNestedMapsAndSequences",
   190  			config: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
   191  kind: MyResourceKind
   192  metadata:
   193    name: myresourcekind-sample
   194  spec:
   195    field:
   196      fieldA:
   197      - fieldA: value
   198        fieldB:
   199          fieldA:
   200          - valueOne
   201          - valueTwo
   202          - valueThree
   203      - fieldA: value
   204        fieldB:
   205          fieldA:
   206          - value
   207      fieldB: value`,
   208  			expected: `apiVersion: myservice.cnrm.cloud.google.com/v1alpha2
   209  kind: MyResourceKind
   210  metadata:
   211    name: \${1:myresourcekind-name}
   212  spec:
   213    field:
   214      fieldA:
   215      - fieldA: \${2:value}
   216        fieldB:
   217          fieldA:
   218          - \${3:valueOne}
   219          - \${4:valueTwo}
   220          - \${5:valueThree}
   221      - fieldA: \${6:value}
   222        fieldB:
   223          fieldA:
   224          - \${7:value}
   225      fieldB: \${8:value}`,
   226  		},
   227  	}
   228  	for _, tc := range tests {
   229  		t.Run(tc.testName, func(t *testing.T) {
   230  			output, err := SnippifyResourceConfig([]byte(tc.config))
   231  			if err != nil {
   232  				t.Fatal(err)
   233  			}
   234  			if tc.expected != strings.TrimSuffix(output.InsertText, "\n") {
   235  				t.Errorf("\n# Expected:\n%v\n---\n# Actual:\n%v\n", tc.expected, output.InsertText)
   236  			}
   237  		})
   238  	}
   239  }
   240  
   241  func TestAllResourcesHaveSampleFileUsedForSnippets(t *testing.T) {
   242  	samplesPath := repo.GetResourcesSamplesPath()
   243  	resources, err := fileutil.SubdirsIn(samplesPath)
   244  	if err != nil {
   245  		t.Fatal(err)
   246  	}
   247  	for _, resource := range resources {
   248  		_, err := PathToSampleFileUsedForSnippets(resource)
   249  		if err != nil {
   250  			t.Fatalf("Failed to get sample file to use for snippet generation "+
   251  				"for resource samples directory '%v': %v\n\n"+
   252  
   253  				"Did you forget to update the 'preferredSampleForResource' map "+
   254  				"in snippetgeneration.go ?", resource, err)
   255  		}
   256  	}
   257  }
   258  

View as plain text