| 1 |
// Copyright 2020 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package utils |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
"sigs.k8s.io/kustomize/api/konfig" |
| 14 |
"sigs.k8s.io/kustomize/api/provider" |
| 15 |
"sigs.k8s.io/kustomize/api/resmap" |
| 16 |
"sigs.k8s.io/kustomize/api/resource" |
| 17 |
"sigs.k8s.io/kustomize/api/types" |
| 18 |
"sigs.k8s.io/kustomize/kyaml/filesys" |
| 19 |
) |
| 20 |
|
| 21 |
func TestDeterminePluginSrcRoot(t *testing.T) { |
| 22 |
actual, err := DeterminePluginSrcRoot(filesys.MakeFsOnDisk()) |
| 23 |
if err != nil { |
| 24 |
t.Error(err) |
| 25 |
} |
| 26 |
if !filepath.IsAbs(actual) { |
| 27 |
t.Errorf("expected absolute path, but got '%s'", actual) |
| 28 |
} |
| 29 |
if !strings.HasSuffix(actual, konfig.RelPluginHome) { |
| 30 |
t.Errorf("expected suffix '%s' in '%s'", konfig.RelPluginHome, actual) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func makeConfigMap(rf *resource.Factory, name, behavior string, hashValue *string) *resource.Resource { |
| 35 |
r, err := rf.FromMap(map[string]interface{}{ |
| 36 |
"apiVersion": "v1", |
| 37 |
"kind": "ConfigMap", |
| 38 |
"metadata": map[string]interface{}{"name": name}, |
| 39 |
}) |
| 40 |
if err != nil { |
| 41 |
panic(err) |
| 42 |
} |
| 43 |
annotations := map[string]string{} |
| 44 |
if behavior != "" { |
| 45 |
annotations[BehaviorAnnotation] = behavior |
| 46 |
} |
| 47 |
if hashValue != nil { |
| 48 |
annotations[HashAnnotation] = *hashValue |
| 49 |
} |
| 50 |
if len(annotations) > 0 { |
| 51 |
if err := r.SetAnnotations(annotations); err != nil { |
| 52 |
panic(err) |
| 53 |
} |
| 54 |
} |
| 55 |
return r |
| 56 |
} |
| 57 |
|
| 58 |
func makeConfigMapOptions(rf *resource.Factory, name, behavior string, disableHash bool) (*resource.Resource, error) { |
| 59 |
return rf.FromMapAndOption(map[string]interface{}{ |
| 60 |
"apiVersion": "v1", |
| 61 |
"kind": "ConfigMap", |
| 62 |
"metadata": map[string]interface{}{"name": name}, |
| 63 |
}, &types.GeneratorArgs{ |
| 64 |
Behavior: behavior, |
| 65 |
Options: &types.GeneratorOptions{DisableNameSuffixHash: disableHash}}) |
| 66 |
} |
| 67 |
|
| 68 |
func strptr(s string) *string { |
| 69 |
return &s |
| 70 |
} |
| 71 |
|
| 72 |
func TestUpdateResourceOptions(t *testing.T) { |
| 73 |
rf := provider.NewDefaultDepProvider().GetResourceFactory() |
| 74 |
in := resmap.New() |
| 75 |
expected := resmap.New() |
| 76 |
cases := []struct { |
| 77 |
behavior string |
| 78 |
needsHash bool |
| 79 |
hashValue *string |
| 80 |
}{ |
| 81 |
{hashValue: strptr("false")}, |
| 82 |
{hashValue: strptr("true"), needsHash: true}, |
| 83 |
{behavior: "replace"}, |
| 84 |
{behavior: "merge"}, |
| 85 |
{behavior: "create"}, |
| 86 |
{behavior: "nonsense"}, |
| 87 |
{behavior: "merge", hashValue: strptr("false")}, |
| 88 |
{behavior: "merge", hashValue: strptr("true"), needsHash: true}, |
| 89 |
} |
| 90 |
for i, c := range cases { |
| 91 |
name := fmt.Sprintf("test%d", i) |
| 92 |
err := in.Append(makeConfigMap(rf, name, c.behavior, c.hashValue)) |
| 93 |
require.NoError(t, err) |
| 94 |
config, err := makeConfigMapOptions(rf, name, c.behavior, !c.needsHash) |
| 95 |
if err != nil { |
| 96 |
t.Errorf("expected new instance with an options but got error: %v", err) |
| 97 |
} |
| 98 |
err = expected.Append(config) |
| 99 |
require.NoError(t, err) |
| 100 |
} |
| 101 |
actual, err := UpdateResourceOptions(in) |
| 102 |
require.NoError(t, err) |
| 103 |
require.NoError(t, expected.ErrorIfNotEqualLists(actual)) |
| 104 |
} |
| 105 |
|
| 106 |
func TestUpdateResourceOptionsWithInvalidHashAnnotationValues(t *testing.T) { |
| 107 |
rf := provider.NewDefaultDepProvider().GetResourceFactory() |
| 108 |
cases := []string{ |
| 109 |
"", |
| 110 |
"FaLsE", |
| 111 |
"TrUe", |
| 112 |
"potato", |
| 113 |
} |
| 114 |
for i := range cases { |
| 115 |
name := fmt.Sprintf("test%d", i) |
| 116 |
in := resmap.New() |
| 117 |
err := in.Append(makeConfigMap(rf, name, "", &cases[i])) |
| 118 |
require.NoError(t, err) |
| 119 |
_, err = UpdateResourceOptions(in) |
| 120 |
require.Error(t, err) |
| 121 |
} |
| 122 |
} |