Files
kustomize/api/resource/resource_test.go

126 lines
2.8 KiB
Go

// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package resource_test
import (
"reflect"
"testing"
"sigs.k8s.io/kustomize/api/provider"
"sigs.k8s.io/kustomize/api/resid"
. "sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/api/types"
)
var factory = provider.NewDefaultDepProvider().GetResourceFactory()
var testConfigMap = factory.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "winnie",
"namespace": "hundred-acre-wood",
},
})
const genArgOptions = "{nsfx:false,beh:unspecified}"
//nolint:gosec
const configMapAsString = `{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"winnie","namespace":"hundred-acre-wood"}}`
var testDeployment = factory.FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "pooh",
},
})
const deploymentAsString = `{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"name":"pooh"}}`
func TestAsYAML(t *testing.T) {
expected := `apiVersion: apps/v1
kind: Deployment
metadata:
name: pooh
`
yaml, err := testDeployment.AsYAML()
if err != nil {
t.Fatal(err)
}
if string(yaml) != expected {
t.Fatalf("--- expected\n%s\n--- got\n%s\n", expected, string(yaml))
}
}
func TestResourceString(t *testing.T) {
tests := []struct {
in *Resource
s string
}{
{
in: testConfigMap,
s: configMapAsString + genArgOptions,
},
{
in: testDeployment,
s: deploymentAsString + genArgOptions,
},
}
for _, test := range tests {
if test.in.String() != test.s {
t.Fatalf("Expected %s == %s", test.in.String(), test.s)
}
}
}
func TestResourceId(t *testing.T) {
tests := []struct {
in *Resource
id resid.ResId
}{
{
in: testConfigMap,
id: resid.NewResIdWithNamespace(
resid.Gvk{Version: "v1", Kind: "ConfigMap"}, "winnie", "hundred-acre-wood"),
},
{
in: testDeployment,
id: resid.NewResId(resid.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}, "pooh"),
},
}
for _, test := range tests {
if test.in.OrgId() != test.id {
t.Fatalf("Expected %v, but got %v\n", test.id, test.in.OrgId())
}
}
}
func TestDeepCopy(t *testing.T) {
r := factory.FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "pooh",
},
})
r.AppendRefBy(resid.NewResId(resid.Gvk{Group: "somegroup", Kind: "MyKind"}, "random"))
var1 := types.Var{
Name: "SERVICE_ONE",
ObjRef: types.Target{
Gvk: resid.Gvk{Version: "v1", Kind: "Service"},
Name: "backendOne"},
}
r.AppendRefVarName(var1)
cr := r.DeepCopy()
if !reflect.DeepEqual(r, cr) {
t.Errorf("expected %v\nbut got%v", r, cr)
}
}