github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/schema/util/util_test.go (about)

     1  /*
     2  Copyright 2019 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/yaml"
    24  	"github.com/GoogleContainerTools/skaffold/testutil"
    25  )
    26  
    27  const yamlFragment string = `global:
    28    enabled: true
    29    localstack: {}
    30  `
    31  
    32  func TestHelmOverridesMarshalling(t *testing.T) {
    33  	h := &HelmOverrides{}
    34  	err := yaml.Unmarshal([]byte(yamlFragment), h)
    35  	testutil.CheckError(t, false, err)
    36  
    37  	asJSON, err := json.Marshal(h)
    38  	testutil.CheckError(t, false, err)
    39  
    40  	err = json.Unmarshal(asJSON, h)
    41  	testutil.CheckError(t, false, err)
    42  
    43  	actual, err := yaml.Marshal(h)
    44  	testutil.CheckErrorAndDeepEqual(t, false, err, yamlFragment, string(actual))
    45  }
    46  
    47  func TestHelmOverridesWhenEmbedded(t *testing.T) {
    48  	h := HelmOverrides{}
    49  	err := yaml.Unmarshal([]byte(yamlFragment), &h)
    50  	testutil.CheckError(t, false, err)
    51  
    52  	out, err := yaml.Marshal(struct {
    53  		Overrides HelmOverrides `yaml:"overrides,omitempty"`
    54  	}{h})
    55  
    56  	testutil.CheckErrorAndDeepEqual(t, false, err, `overrides:
    57    global:
    58      enabled: true
    59      localstack: {}
    60  `, string(out))
    61  }
    62  
    63  func TestYamlpatchNodeMarshalling(t *testing.T) {
    64  	n := &YamlpatchNode{}
    65  	err := yaml.Unmarshal([]byte(yamlFragment), n)
    66  	testutil.CheckError(t, false, err)
    67  
    68  	asJSON, err := json.Marshal(n)
    69  	testutil.CheckError(t, false, err)
    70  
    71  	err = json.Unmarshal(asJSON, n)
    72  	testutil.CheckError(t, false, err)
    73  
    74  	actual, err := yaml.Marshal(n)
    75  	testutil.CheckErrorAndDeepEqual(t, false, err, yamlFragment, string(actual))
    76  }
    77  
    78  func TestYamlpatchNodeWhenEmbedded(t *testing.T) {
    79  	n := &YamlpatchNode{}
    80  	err := yaml.Unmarshal([]byte(yamlFragment), &n)
    81  	testutil.CheckError(t, false, err)
    82  
    83  	out, err := yaml.Marshal(struct {
    84  		Node *YamlpatchNode `yaml:"value,omitempty"`
    85  	}{n})
    86  
    87  	testutil.CheckErrorAndDeepEqual(t, false, err, `value:
    88    global:
    89      enabled: true
    90      localstack: {}
    91  `, string(out))
    92  }
    93  
    94  func TestFlatMap_UnmarshalYAML(t *testing.T) {
    95  	y1 := `val1: foo1
    96  val2: 
    97    val3: bar1
    98    val4: foo2
    99    val5:
   100      val6: bar2
   101  `
   102  	y2 := `val1: foo1
   103  val2.val3: bar1
   104  val2.val4: foo2
   105  val2.val5.val6: bar2
   106  `
   107  
   108  	f1 := &FlatMap{}
   109  	f2 := &FlatMap{}
   110  
   111  	err := yaml.Unmarshal([]byte(y1), &f1)
   112  	testutil.CheckError(t, false, err)
   113  
   114  	err = yaml.Unmarshal([]byte(y2), &f2)
   115  	testutil.CheckError(t, false, err)
   116  
   117  	testutil.CheckDeepEqual(t, *f1, *f2)
   118  
   119  	out, err := yaml.Marshal(struct {
   120  		M *FlatMap `yaml:"value,omitempty"`
   121  	}{f1})
   122  
   123  	testutil.CheckErrorAndDeepEqual(t, false, err, `value:
   124    val1: foo1
   125    val2.val3: bar1
   126    val2.val4: foo2
   127    val2.val5.val6: bar2
   128  `, string(out))
   129  }
   130  
   131  func TestFlatMap_UnmarshalYAMLNested(t *testing.T) {
   132  	y1 := `
   133  val1: foo1
   134  values:
   135    val2:
   136      - val4: bar1
   137        val5: foo2
   138      - val6: bar2
   139        val7: foo3
   140    val3:
   141      - val8: bar3
   142        val9: foo4
   143  `
   144  	y2 := `val1: foo1
   145  values.val2[0].val4: bar1
   146  values.val2[0].val5: foo2
   147  values.val2[1].val6: bar2
   148  values.val2[1].val7: foo3
   149  values.val3[0].val8: bar3
   150  values.val3[0].val9: foo4
   151  `
   152  	f1 := &FlatMap{}
   153  	f2 := &FlatMap{}
   154  
   155  	err := yaml.Unmarshal([]byte(y1), &f1)
   156  	testutil.CheckError(t, false, err)
   157  
   158  	err = yaml.Unmarshal([]byte(y2), &f2)
   159  	testutil.CheckError(t, false, err)
   160  
   161  	testutil.CheckDeepEqual(t, *f1, *f2)
   162  
   163  	out, err := yaml.Marshal(struct {
   164  		M *FlatMap `yaml:"value,omitempty"`
   165  	}{f1})
   166  
   167  	testutil.CheckErrorAndDeepEqual(t, false, err, `value:
   168    val1: foo1
   169    values.val2[0].val4: bar1
   170    values.val2[0].val5: foo2
   171    values.val2[1].val6: bar2
   172    values.val2[1].val7: foo3
   173    values.val3[0].val8: bar3
   174    values.val3[0].val9: foo4
   175  `, string(out))
   176  }