From 0594982604cb1a553bb12545cfa0d5a285617e5a Mon Sep 17 00:00:00 2001 From: Madhankumar Chellamuthu Date: Wed, 22 Nov 2023 16:53:33 +0530 Subject: [PATCH] Adding os environment keys to cmd environment (#1391) * Adding os environment keys to cmd environment Signed-off-by: rcmadhankumar * kctrl dev test added for fetch from git source Signed-off-by: rcmadhankumar --------- Signed-off-by: rcmadhankumar --- cli/pkg/kctrl/local/detailed_cmd_runner.go | 4 + cli/test/e2e/dev_test.go | 123 +++++++++++++++++++++ 2 files changed, 127 insertions(+) diff --git a/cli/pkg/kctrl/local/detailed_cmd_runner.go b/cli/pkg/kctrl/local/detailed_cmd_runner.go index fe08953a5..b2c4df28e 100644 --- a/cli/pkg/kctrl/local/detailed_cmd_runner.go +++ b/cli/pkg/kctrl/local/detailed_cmd_runner.go @@ -6,6 +6,7 @@ package local import ( "fmt" "io" + "os" goexec "os/exec" "github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec" @@ -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) diff --git a/cli/test/e2e/dev_test.go b/cli/test/e2e/dev_test.go index 55f5a9274..e2a3edbde 100644 --- a/cli/test/e2e/dev_test.go +++ b/cli/test/e2e/dev_test.go @@ -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() @@ -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() @@ -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": "", + "kind": "Deployment", + "name": "simple-app", + "namespace": "kctrl-test", + "owner": "kapp", + "reconcile_info": "", + "reconcile_state": "ok", + }, + { + "age": "", + "kind": "Endpoints", + "name": "simple-app", + "namespace": "kctrl-test", + "owner": "cluster", + "reconcile_info": "", + "reconcile_state": "ok", + }, + { + "age": "", + "kind": "Service", + "name": "simple-app", + "namespace": "kctrl-test", + "owner": "kapp", + "reconcile_info": "", + "reconcile_state": "ok", + }, + { + "age": "", + "kind": "ReplicaSet", + "name": "simple-app", + "namespace": "kctrl-test", + "owner": "cluster", + "reconcile_info": "", + "reconcile_state": "ok", + }, + { + "age": "", + "kind": "Pod", + "name": "simple-app", + "namespace": "kctrl-test", + "owner": "cluster", + "reconcile_info": "", + "reconcile_state": "ok", + }, + { + "age": "", + "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 }