From e2e2abd1a0c5ea35e68fe7afa70c21e4d834c372 Mon Sep 17 00:00:00 2001 From: zongz Date: Tue, 10 Oct 2023 14:22:20 +0800 Subject: [PATCH] fix: 'kpm run' will trigger the automatic download of the dependencies Signed-off-by: zongz --- pkg/client/client.go | 12 ++++++++++++ pkg/client/client_test.go | 19 +++++++++++++++++++ .../test_data/resolve_dep_with_kclmod/kcl.mod | 7 +++++++ .../test_data/resolve_dep_with_kclmod/main.k | 10 ++++++++++ .../test_suite.env | 2 ++ .../test_suite.input | 1 + .../test_suite.stderr | 0 .../test_suite.stdout | 11 +++++++++++ .../test_kpm_run_with_only_kcl_mod/kcl.mod | 7 +++++++ .../test_kpm_run_with_only_kcl_mod/main.k | 10 ++++++++++ 10 files changed, 79 insertions(+) create mode 100644 pkg/client/test_data/resolve_dep_with_kclmod/kcl.mod create mode 100644 pkg/client/test_data/resolve_dep_with_kclmod/main.k create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.env create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.input create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stderr create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stdout create mode 100644 test/e2e/test_suites/test_data/test_kpm_run_with_only_kcl_mod/kcl.mod create mode 100644 test/e2e/test_suites/test_data/test_kpm_run_with_only_kcl_mod/main.k diff --git a/pkg/client/client.go b/pkg/client/client.go index 72b0d1dc..3b9eb801 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -114,6 +114,13 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro searchPath = c.homePath } + // alian the dependencies between kcl.mod and kcl.mod.lock + for name, d := range kclPkg.ModFile.Dependencies.Deps { + if _, ok := kclPkg.Dependencies.Deps[name]; !ok { + kclPkg.Dependencies.Deps[name] = d + } + } + for name, d := range kclPkg.Dependencies.Deps { searchFullPath := filepath.Join(searchPath, d.FullName) if !update { @@ -161,6 +168,11 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro } } + // update the kcl.mod and kcl.mod.lock. + err := kclPkg.UpdateModAndLockFile() + if err != nil { + return err + } return nil } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 1a485811..6f04d29b 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -296,6 +296,25 @@ func TestVendorDeps(t *testing.T) { os.RemoveAll(filepath.Join(testDir, "my_kcl")) } +func TestResolveDepsWithOnlyKclMod(t *testing.T) { + testDir := getTestDir("resolve_dep_with_kclmod") + assert.Equal(t, utils.DirExists(filepath.Join(testDir, "kcl.mod.lock")), false) + kclPkg, err := pkg.LoadKclPkg(testDir) + assert.Equal(t, err, nil) + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + depsMap, err := kpmcli.ResolveDepsIntoMap(kclPkg) + assert.Equal(t, err, nil) + assert.Equal(t, len(depsMap), 1) + assert.Equal(t, utils.DirExists(filepath.Join(testDir, "kcl.mod.lock")), true) + assert.Equal(t, depsMap["k8s"], filepath.Join(kpmcli.homePath, "k8s_1.17")) + assert.Equal(t, utils.DirExists(filepath.Join(kpmcli.homePath, "k8s_1.17")), true) + defer func() { + err := os.Remove(filepath.Join(testDir, "kcl.mod.lock")) + assert.Equal(t, err, nil) + }() +} + func TestResolveDepsVendorMode(t *testing.T) { testDir := getTestDir("resolve_deps") kpm_home := filepath.Join(testDir, "kpm_home") diff --git a/pkg/client/test_data/resolve_dep_with_kclmod/kcl.mod b/pkg/client/test_data/resolve_dep_with_kclmod/kcl.mod new file mode 100644 index 00000000..1724ca41 --- /dev/null +++ b/pkg/client/test_data/resolve_dep_with_kclmod/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "resolve_dep_with_kclmod" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +k8s = "1.17" diff --git a/pkg/client/test_data/resolve_dep_with_kclmod/main.k b/pkg/client/test_data/resolve_dep_with_kclmod/main.k new file mode 100644 index 00000000..7f748048 --- /dev/null +++ b/pkg/client/test_data/resolve_dep_with_kclmod/main.k @@ -0,0 +1,10 @@ +import k8s.api.core.v1 as k8core + +k8core.Pod { + metadata.name = "web-app" + spec.containers = [{ + name = "main-container" + image = "nginx" + ports = [{containerPort = 80}] + }] +} \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.env b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.env new file mode 100644 index 00000000..4c789529 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.env @@ -0,0 +1,2 @@ +KPM_HOME="" +KCLVM_VENDOR_HOME="" \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.input b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.input new file mode 100644 index 00000000..b02e55a1 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.input @@ -0,0 +1 @@ +kpm run \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stderr b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stderr new file mode 100644 index 00000000..e69de29b diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stdout b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stdout new file mode 100644 index 00000000..52b79fa3 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stdout @@ -0,0 +1,11 @@ +kpm: downloading 'test/k8s:1.27' from 'localhost:5001/test/k8s:1.27'. +apiVersion: v1 +kind: Pod +metadata: + name: web-app +spec: + containers: + - image: nginx + name: main-container + ports: + - containerPort: 80 diff --git a/test/e2e/test_suites/test_data/test_kpm_run_with_only_kcl_mod/kcl.mod b/test/e2e/test_suites/test_data/test_kpm_run_with_only_kcl_mod/kcl.mod new file mode 100644 index 00000000..9c529adf --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_run_with_only_kcl_mod/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "test_kpm_run_with_only_kcl_mod" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +k8s = "1.27" diff --git a/test/e2e/test_suites/test_data/test_kpm_run_with_only_kcl_mod/main.k b/test/e2e/test_suites/test_data/test_kpm_run_with_only_kcl_mod/main.k new file mode 100644 index 00000000..7f748048 --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_run_with_only_kcl_mod/main.k @@ -0,0 +1,10 @@ +import k8s.api.core.v1 as k8core + +k8core.Pod { + metadata.name = "web-app" + spec.containers = [{ + name = "main-container" + image = "nginx" + ports = [{containerPort = 80}] + }] +} \ No newline at end of file