Skip to content

Commit

Permalink
Adding os environment keys to cmd environment (#1391)
Browse files Browse the repository at this point in the history
* Adding os environment keys to cmd environment

Signed-off-by: rcmadhankumar <[email protected]>

* kctrl dev test added for fetch from git source

Signed-off-by: rcmadhankumar <[email protected]>

---------

Signed-off-by: rcmadhankumar <[email protected]>
  • Loading branch information
rcmadhankumar authored Nov 22, 2023
1 parent 59d4642 commit 0594982
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cli/pkg/kctrl/local/detailed_cmd_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package local
import (
"fmt"
"io"
"os"
goexec "os/exec"

"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
Expand All @@ -28,6 +29,9 @@ func (r DetailedCmdRunner) Run(cmd *goexec.Cmd) error {
cmd.Stderr = io.MultiWriter(r.log, cmd.Stderr)
}

// Adding os environment keys to cmd environment
cmd.Env = append(os.Environ(), cmd.Env...)

fmt.Fprintf(r.log, "==> Executing %s %v\n", cmd.Path, cmd.Args)
defer fmt.Fprintf(r.log, "==> Finished executing %s\n\n", cmd.Path)

Expand Down
123 changes: 123 additions & 0 deletions cli/test/e2e/dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestDev(t *testing.T) {
kapp := Kapp{t, env.Namespace, env.KappBinaryPath, logger}

appName := "dev-test"
gitAppName := "dev-gitapp-test"
saAppName := "dev-test"

sa := ServiceAccounts{env.Namespace}.ForNamespaceYAML()
Expand Down Expand Up @@ -47,8 +48,29 @@ spec:
intoNs: kctrl-test
`, appName)

gitAppYaml := fmt.Sprintf(`---
apiVersion: kappctrl.k14s.io/v1alpha1
kind: App
metadata:
name: %s
namespace: kctrl-test
spec:
serviceAccountName: kappctrl-e2e-ns-sa
fetch:
- git:
url: https://github.com/k14s/k8s-simple-app-example
ref: origin/develop
subPath: config-step-2-template
template:
- ytt: {}
deploy:
- kapp:
intoNs: kctrl-test
`, gitAppName)

cleanUp := func() {
kapp.Run([]string{"delete", "-a", fmt.Sprintf("%s.app", appName)})
kapp.Run([]string{"delete", "-a", fmt.Sprintf("%s.app", gitAppName)})
kapp.Run([]string{"delete", "-a", saAppName})
}
cleanUp()
Expand Down Expand Up @@ -79,4 +101,105 @@ spec:
}
require.Exactly(t, expectedOutputRows, replaceAgeAndSinceDeployed(output.Tables[0].Rows))
})

logger.Section("dev deploy git app", func() {
out, err := kappCtrl.RunWithOpts([]string{"dev", "-f", "-"}, RunOpts{StdinReader: strings.NewReader(gitAppYaml)})
fmt.Printf("\n\n Out: %s \n err: %+v ", out, err)
})

logger.Section("inspect gitApp app resources", func() {
out := kapp.Run([]string{"inspect", "-a", fmt.Sprintf("%s.app", gitAppName), "--json"})
output := uitest.JSONUIFromBytes(t, []byte(out))

expectedOutputRows := []map[string]string{
{
"age": "<replaced>",
"kind": "Deployment",
"name": "simple-app",
"namespace": "kctrl-test",
"owner": "kapp",
"reconcile_info": "",
"reconcile_state": "ok",
},
{
"age": "<replaced>",
"kind": "Endpoints",
"name": "simple-app",
"namespace": "kctrl-test",
"owner": "cluster",
"reconcile_info": "",
"reconcile_state": "ok",
},
{
"age": "<replaced>",
"kind": "Service",
"name": "simple-app",
"namespace": "kctrl-test",
"owner": "kapp",
"reconcile_info": "",
"reconcile_state": "ok",
},
{
"age": "<replaced>",
"kind": "ReplicaSet",
"name": "simple-app",
"namespace": "kctrl-test",
"owner": "cluster",
"reconcile_info": "",
"reconcile_state": "ok",
},
{
"age": "<replaced>",
"kind": "Pod",
"name": "simple-app",
"namespace": "kctrl-test",
"owner": "cluster",
"reconcile_info": "",
"reconcile_state": "ok",
},
{
"age": "<replaced>",
"kind": "EndpointSlice",
"name": "simple-app",
"namespace": "kctrl-test",
"owner": "cluster",
"reconcile_info": "",
"reconcile_state": "ok",
},
}

require.Len(t, output.Tables[0].Rows, 6)

deploymentItem := filterByKeyValuePair(output.Tables[0].Rows, "kind", "Deployment")
require.Exactly(t, expectedOutputRows[0], replaceAgeAndSinceDeployed(deploymentItem)[0])

endpointItem := filterByKeyValuePair(output.Tables[0].Rows, "kind", "Endpoints")
require.Exactly(t, expectedOutputRows[1], replaceAgeAndSinceDeployed(endpointItem)[0])

serviceItem := filterByKeyValuePair(output.Tables[0].Rows, "kind", "Service")
require.Exactly(t, expectedOutputRows[2], replaceAgeAndSinceDeployed(serviceItem)[0])

replicaSetItem := filterByKeyValuePair(output.Tables[0].Rows, "kind", "ReplicaSet")
replicaSetItem[0]["name"] = "simple-app"
require.Exactly(t, expectedOutputRows[3], replaceAgeAndSinceDeployed(replicaSetItem)[0])

podItem := filterByKeyValuePair(output.Tables[0].Rows, "kind", "Pod")
podItem[0]["name"] = "simple-app"
require.Exactly(t, expectedOutputRows[4], replaceAgeAndSinceDeployed(podItem)[0])

endpointSliceItem := filterByKeyValuePair(output.Tables[0].Rows, "kind", "EndpointSlice")
endpointSliceItem[0]["name"] = "simple-app"
require.Exactly(t, expectedOutputRows[5], replaceAgeAndSinceDeployed(endpointSliceItem)[0])
})
}

func filterByKeyValuePair(slice []map[string]string, key, value string) []map[string]string {
var filteredSlice []map[string]string

for _, item := range slice {
if item[key] == value {
filteredSlice = append(filteredSlice, item)
}
}
return filteredSlice
}

0 comments on commit 0594982

Please sign in to comment.