Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
E2E tests for versioned-explicit-ref annotation. Refactoring for legi…
Browse files Browse the repository at this point in the history
…bility.
100mik committed Sep 21, 2021
1 parent 69d2f7f commit 15512b2
Showing 3 changed files with 114 additions and 7 deletions.
10 changes: 5 additions & 5 deletions pkg/kapp/diff/explicit_versioned_ref.go
Original file line number Diff line number Diff line change
@@ -32,14 +32,14 @@ func NewExplicitVersionedRef(res VersionedResource, annotation ExplicitVersioned
return &ExplicitVersionedRef{res, annotation}
}

// Returns true if the
// Returns true if the resource is referenced by the annotation
func (e *ExplicitVersionedRef) IsReferenced() (bool, error) {
references, err := e.refStringList()
references, err := e.references()
if err != nil {
return false, err
}

referenceKey := e.resourceRefString()
referenceKey := e.referenceKey()

for _, v := range references {
if v == referenceKey {
@@ -50,7 +50,7 @@ func (e *ExplicitVersionedRef) IsReferenced() (bool, error) {
return false, nil
}

func (e *ExplicitVersionedRef) refStringList() ([]string, error) {
func (e *ExplicitVersionedRef) references() ([]string, error) {
list := []string{}
for _, v := range e.Annotation.References {
v, err := e.validateAndReplaceCoreGroup(v)
@@ -76,7 +76,7 @@ func (e *ExplicitVersionedRef) validateAndReplaceCoreGroup(resourceDescription V
return resourceDescription, nil
}

func (e *ExplicitVersionedRef) resourceRefString() string {
func (e *ExplicitVersionedRef) referenceKey() string {
return e.Resource.UniqVersionedKey().String()
}

4 changes: 2 additions & 2 deletions pkg/kapp/diff/versioned_resource.go
Original file line number Diff line number Diff line change
@@ -97,12 +97,12 @@ func (d VersionedResource) updateAffected(rule ctlconf.TemplateRule, rs []ctlres
return fmt.Errorf("Error unmarshalling explicit references : %s", err)
}

isTarget, err := NewExplicitVersionedRef(d, annotation).IsReferenced()
isReferenced, err := NewExplicitVersionedRef(d, annotation).IsReferenced()
if err != nil {
return err
}

if isTarget {
if isReferenced {
if annotation.VersionedNames == nil {
annotation.VersionedNames = map[string]string{}
}
107 changes: 107 additions & 0 deletions test/e2e/versioned_explicit_reference_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"reflect"
"strings"
"testing"

uitest "github.com/cppforlife/go-cli-ui/ui/test"
)

func TestVersionedExplicitReference(t *testing.T) {
env := BuildEnv(t)
logger := Logger{}
kapp := Kapp{t, env.Namespace, env.KappBinaryPath, logger}

yaml1 := `
---
apiVersion: v1
kind: ConfigMap
metadata:
name: config-1
annotations:
kapp.k14s.io/versioned: ""
data:
foo: bar
---
apiVersion: v1
kind: ConfigMap
metadata:
name: config-2
annotations:
kapp.k14s.io/versioned-explicit-ref: '{ "references": [ { "namespace": "kapp-test", "kind" : "ConfigMap", "name": "config-1"} ] }'
data:
foo: bar
`

yaml2 := `
---
apiVersion: v1
kind: ConfigMap
metadata:
name: config-1
annotations:
kapp.k14s.io/versioned: ""
data:
foo: alpha
---
apiVersion: v1
kind: ConfigMap
metadata:
name: config-2
annotations:
kapp.k14s.io/versioned-explicit-ref: '{ "references": [ { "namespace": "kapp-test", "kind" : "ConfigMap", "name": "config-1"} ] }'
data:
foo: bar
`

name := "test-versioned-explicit-references"
cleanUp := func() {
kapp.Run([]string{"delete", "-a", name})
}

cleanUp()
defer cleanUp()

logger.Section("deploy initial", func() {
kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name},
RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml1)})
})

logger.Section("update versioned resource", func() {
out, _ := kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name, "--json"},
RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml2)})
resp := uitest.JSONUIFromBytes(t, []byte(out))

expected := []map[string]string{{
"age": "",
"conditions": "",
"kind": "ConfigMap",
"name": "config-1-ver-2",
"namespace": "kapp-test",
"op": "create",
"op_strategy": "",
"reconcile_info": "",
"reconcile_state": "",
"wait_to": "reconcile",
}, {
"age": "<replaced>",
"conditions": "",
"kind": "ConfigMap",
"name": "config-2",
"namespace": "kapp-test",
"op": "update",
"op_strategy": "",
"reconcile_info": "",
"reconcile_state": "ok",
"wait_to": "reconcile",
}}

if !reflect.DeepEqual(replaceAge(resp.Tables[0].Rows), expected) {
t.Fatalf("Expected to see correct changes, but did not: '%s'", out)
}
})
}

0 comments on commit 15512b2

Please sign in to comment.