Skip to content

Commit

Permalink
Moved TestAppKindChangeWithMetadataOutput to a separate file (#880)
Browse files Browse the repository at this point in the history
Signed-off-by: jignyasamishra <iamjignyasa@gmail.com>
  • Loading branch information
jignyasamishra authored Jan 15, 2024
1 parent 2f0080a commit 101c28b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 85 deletions.
85 changes: 0 additions & 85 deletions test/e2e/app_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ package e2e

import (
"fmt"
"os"
"strings"
"testing"
"time"

uitest "github.com/cppforlife/go-cli-ui/ui/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vmware-tanzu/carvel-kapp/pkg/kapp/app"
"gopkg.in/yaml.v2"
)

func TestAppChange(t *testing.T) {
Expand Down Expand Up @@ -141,88 +138,6 @@ spec:
})
}

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

yaml1 := `
---
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-config
annotations:
kapp.k14s.io/versioned: ""
data:
redis-config: |
maxmemory 3mb
maxmemory-policy allkeys-lru
`

yaml2 := `
---
apiVersion: v1
kind: Secret
metadata:
name: kapp-secret-1
namespace: kapp-namespace-2
`

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

cleanUp()
defer cleanUp()

firstDeploy, err := os.CreateTemp(os.TempDir(), "output1")
assert.NoError(t, err)
secondDeploy, err := os.CreateTemp(os.TempDir(), "output2")
assert.NoError(t, err)
thirdDeploy, err := os.CreateTemp(os.TempDir(), "output3")
assert.NoError(t, err)

defer func() {
os.Remove(firstDeploy.Name())
os.Remove(secondDeploy.Name())
}()

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

logger.Section("deploy with changes", func() {
kapp.RunWithOpts([]string{"deploy", "--app-metadata-file-output", secondDeploy.Name(), "-f", "-", "-a", name}, RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml2)})
})

logger.Section("deploy with no changes", func() {
kapp.RunWithOpts([]string{"deploy", "--app-metadata-file-output", thirdDeploy.Name(), "-f", "-", "-a", name}, RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml2)})
})

configMapFirstDeploy, err := os.ReadFile(firstDeploy.Name())
assert.NoError(t, err)

firstConfigMap := yamlSubset{}
require.NoError(t, yaml.Unmarshal(configMapFirstDeploy, &firstConfigMap))
require.Equal(t, yamlSubset{LastChange: lastChange{Namespaces: []string{env.Namespace}}, UsedGKs: []usedGK{{Group: "", Kind: "ConfigMap"}}}, firstConfigMap)

configMapSecondDeploy, err := os.ReadFile(secondDeploy.Name())
assert.NoError(t, err)

secondConfigMap := yamlSubset{}
require.NoError(t, yaml.Unmarshal(configMapSecondDeploy, &secondConfigMap))
require.Equal(t, yamlSubset{LastChange: lastChange{Namespaces: []string{env.Namespace}}, UsedGKs: []usedGK{{Group: "", Kind: "Secret"}}}, secondConfigMap)

configMapThirdDeploy, err := os.ReadFile(thirdDeploy.Name())
assert.NoError(t, err)

thirdConfigMap := yamlSubset{}
require.NoError(t, yaml.Unmarshal(configMapThirdDeploy, &thirdConfigMap))
require.Equal(t, yamlSubset{LastChange: lastChange{Namespaces: []string{env.Namespace}}, UsedGKs: []usedGK{{Group: "", Kind: "Secret"}}}, thirdConfigMap)
}

func TestAppChangesMaxToKeep(t *testing.T) {
env := BuildEnv(t)
logger := Logger{}
Expand Down
96 changes: 96 additions & 0 deletions test/e2e/app_metadata_output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

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

yaml1 := `
---
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-config
annotations:
kapp.k14s.io/versioned: ""
data:
redis-config: |
maxmemory 3mb
maxmemory-policy allkeys-lru
`

yaml2 := `
---
apiVersion: v1
kind: Secret
metadata:
name: kapp-secret-1
namespace: kapp-namespace-2
`

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

cleanUp()
defer cleanUp()

firstDeploy, err := os.CreateTemp(os.TempDir(), "output1")
assert.NoError(t, err)
secondDeploy, err := os.CreateTemp(os.TempDir(), "output2")
assert.NoError(t, err)
thirdDeploy, err := os.CreateTemp(os.TempDir(), "output3")
assert.NoError(t, err)

defer func() {
os.Remove(firstDeploy.Name())
os.Remove(secondDeploy.Name())
}()

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

logger.Section("deploy with changes", func() {
kapp.RunWithOpts([]string{"deploy", "--app-metadata-file-output", secondDeploy.Name(), "-f", "-", "-a", name}, RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml2)})
})

logger.Section("deploy with no changes", func() {
kapp.RunWithOpts([]string{"deploy", "--app-metadata-file-output", thirdDeploy.Name(), "-f", "-", "-a", name}, RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml2)})
})

configMapFirstDeploy, err := os.ReadFile(firstDeploy.Name())
assert.NoError(t, err)

firstConfigMap := yamlSubset{}
require.NoError(t, yaml.Unmarshal(configMapFirstDeploy, &firstConfigMap))
require.Equal(t, yamlSubset{LastChange: lastChange{Namespaces: []string{env.Namespace}}, UsedGKs: []usedGK{{Group: "", Kind: "ConfigMap"}}}, firstConfigMap)

configMapSecondDeploy, err := os.ReadFile(secondDeploy.Name())
assert.NoError(t, err)

secondConfigMap := yamlSubset{}
require.NoError(t, yaml.Unmarshal(configMapSecondDeploy, &secondConfigMap))
require.Equal(t, yamlSubset{LastChange: lastChange{Namespaces: []string{env.Namespace}}, UsedGKs: []usedGK{{Group: "", Kind: "Secret"}}}, secondConfigMap)

configMapThirdDeploy, err := os.ReadFile(thirdDeploy.Name())
assert.NoError(t, err)

thirdConfigMap := yamlSubset{}
require.NoError(t, yaml.Unmarshal(configMapThirdDeploy, &thirdConfigMap))
require.Equal(t, yamlSubset{LastChange: lastChange{Namespaces: []string{env.Namespace}}, UsedGKs: []usedGK{{Group: "", Kind: "Secret"}}}, thirdConfigMap)
}

0 comments on commit 101c28b

Please sign in to comment.