From d8d7a5bc59797d0b1874a12991834f2470e1186d Mon Sep 17 00:00:00 2001 From: zongz Date: Tue, 9 Jan 2024 17:10:40 +0800 Subject: [PATCH 1/2] feat: 'kpm add' supports adding the dependency tagged by git commit Signed-off-by: zongz --- pkg/api/kpm_run_test.go | 31 ++- .../dep_git_commit/expected | 27 +++ .../dep_git_commit/kcl.mod | 7 + .../dep_git_commit/main.k | 45 +++++ .../dep_git_tag/expected | 28 +++ .../test_run_no_sum_check/dep_git_tag/kcl.mod | 7 + .../test_run_no_sum_check/dep_git_tag/main.k | 45 +++++ .../test_run_no_sum_check/dep_oci/expected | 1 + .../{ => dep_oci}/kcl.mod | 0 .../{ => dep_oci}/main.k | 0 pkg/client/client.go | 27 ++- pkg/client/client_test.go | 63 ++++++ .../add_with_git_commit/test_pkg/kcl.mod.bak | 6 + .../test_pkg/kcl.mod.expect | 7 + .../test_pkg/kcl.mod.lock.bak | 0 .../test_pkg/kcl.mod.lock.expect | 8 + .../add_with_git_commit/test_pkg/main.k | 1 + pkg/cmd/cmd_add.go | 29 ++- pkg/git/git.go | 183 +++++++++++++++++- pkg/git/git_test.go | 54 +++++- pkg/package/modfile.go | 67 ++++++- pkg/package/modfile_test.go | 45 ++++- pkg/package/toml.go | 15 +- test/e2e/kpm_test.go | 29 +++ .../test_kpm_add_git_commit/test_suite.env | 2 + .../test_kpm_add_git_commit/test_suite.input | 1 + .../test_kpm_add_git_commit/test_suite.stderr | 0 .../test_kpm_add_git_commit/test_suite.stdout | 2 + .../test_suite.env | 2 + .../test_suite.input | 1 + .../test_suite.stderr | 0 .../test_suite.stdout | 1 + .../test_suite.env | 2 + .../test_suite.input | 1 + .../test_suite.stderr | 0 .../test_suite.stdout | 27 +++ .../test_suite.env | 2 + .../test_suite.input | 1 + .../test_suite.stderr | 0 .../test_suite.stdout | 28 +++ .../test_data/test_kpm_add_git_commit/kcl.mod | 0 .../test_kpm_add_git_commit/kcl.mod.lock | 0 .../test_data/test_kpm_add_git_commit/main.k | 0 .../test_kpm_metadata_with_commit_dep/kcl.mod | 7 + .../kcl.mod.lock | 8 + .../test_kpm_metadata_with_commit_dep/main.k | 1 + .../test_kpm_run_with_git_commit_dep/kcl.mod | 7 + .../kcl.mod.lock | 8 + .../test_kpm_run_with_git_commit_dep/main.k | 45 +++++ .../kcl.mod | 7 + .../kcl.mod.lock | 8 + .../test_kpm_run_with_git_commit_dep_1/main.k | 3 + .../sub/kcl.mod | 7 + .../sub/kcl.mod.lock | 8 + .../sub/main.k | 45 +++++ test/e2e/utils.go | 2 +- 56 files changed, 906 insertions(+), 45 deletions(-) create mode 100644 pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/expected create mode 100644 pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/kcl.mod create mode 100644 pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/main.k create mode 100644 pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/expected create mode 100644 pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/kcl.mod create mode 100644 pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/main.k create mode 100644 pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_oci/expected rename pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/{ => dep_oci}/kcl.mod (100%) rename pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/{ => dep_oci}/main.k (100%) create mode 100644 pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.bak create mode 100644 pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect create mode 100644 pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.lock.bak create mode 100644 pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.lock.expect create mode 100644 pkg/client/test_data/add_with_git_commit/test_pkg/main.k create mode 100644 test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.env create mode 100644 test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.input create mode 100644 test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.stderr create mode 100644 test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.stdout create mode 100644 test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.env create mode 100644 test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.input create mode 100644 test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.stderr create mode 100644 test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.stdout create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.env create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.input create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.stderr create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.stdout create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.env create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.input create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.stderr create mode 100644 test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.stdout create mode 100644 test/e2e/test_suites/test_data/test_kpm_add_git_commit/kcl.mod create mode 100644 test/e2e/test_suites/test_data/test_kpm_add_git_commit/kcl.mod.lock create mode 100644 test/e2e/test_suites/test_data/test_kpm_add_git_commit/main.k create mode 100644 test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/kcl.mod create mode 100644 test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/kcl.mod.lock create mode 100644 test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/main.k create mode 100644 test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/kcl.mod create mode 100644 test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/kcl.mod.lock create mode 100644 test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/main.k create mode 100644 test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/kcl.mod create mode 100644 test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/kcl.mod.lock create mode 100644 test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/main.k create mode 100644 test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/kcl.mod create mode 100644 test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/kcl.mod.lock create mode 100644 test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/main.k diff --git a/pkg/api/kpm_run_test.go b/pkg/api/kpm_run_test.go index afe70c6a..abf95598 100644 --- a/pkg/api/kpm_run_test.go +++ b/pkg/api/kpm_run_test.go @@ -192,14 +192,25 @@ func TestRunPkgWithOpts(t *testing.T) { } func TestRunWithOptsAndNoSumCheck(t *testing.T) { - pkgPath := getTestDir("test_run_pkg_in_path") - - res, err := RunWithOpts( - opt.WithNoSumCheck(true), - opt.WithEntries([]string{filepath.Join(pkgPath, "test_run_no_sum_check", "main.k")}), - opt.WithKclOption(kcl.WithWorkDir(filepath.Join(pkgPath, "test_run_no_sum_check"))), - ) - assert.Equal(t, err, nil) - assert.Equal(t, res.GetRawYamlResult(), "a: Hello World!") - assert.Equal(t, err, nil) + pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_no_sum_check") + testCases := []string{"dep_git_commit", "dep_git_tag", "dep_oci"} + + for _, testCase := range testCases { + + pathMainK := filepath.Join(pkgPath, testCase, "main.k") + workDir := filepath.Join(pkgPath, testCase) + modLock := filepath.Join(workDir, "kcl.mod.lock") + expected, err := os.ReadFile(filepath.Join(pkgPath, testCase, "expected")) + assert.Equal(t, err, nil) + + res, err := RunWithOpts( + opt.WithNoSumCheck(true), + opt.WithEntries([]string{pathMainK}), + opt.WithKclOption(kcl.WithWorkDir(workDir)), + ) + assert.Equal(t, err, nil) + assert.Equal(t, utils.DirExists(modLock), false) + assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), utils.RmNewline(string(expected))) + assert.Equal(t, err, nil) + } } diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/expected b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/expected new file mode 100644 index 00000000..92bb78bf --- /dev/null +++ b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/expected @@ -0,0 +1,27 @@ +workload: + containers: + nginx: + image: nginx:v1 + command: + - /bin/sh + - -c + - echo hi + args: + - /bin/sh + - -c + - echo hi + env: + env1: VALUE + env2: secret://sec-name/key + workingDir: /tmp + readinessProbe: + probeHandler: + url: http://localhost:80 + initialDelaySeconds: 10 + replicas: 2 +monitoring: + interval: 30s + timeout: 15s + path: /metrics + port: web + scheme: http \ No newline at end of file diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/kcl.mod b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/kcl.mod new file mode 100644 index 00000000..8f27a88e --- /dev/null +++ b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "dep_git_commit" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db" } diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/main.k b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/main.k new file mode 100644 index 00000000..320a5761 --- /dev/null +++ b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_commit/main.k @@ -0,0 +1,45 @@ +import catalog.models.schema.v1 as ac +import catalog.models.schema.v1.workload as wl +import catalog.models.schema.v1.workload.container as c +import catalog.models.schema.v1.workload.container.probe as p +import catalog.models.schema.v1.monitoring as m + +ac.AppConfiguration { + workload: wl.Service { + containers: { + "nginx": c.Container { + image: "nginx:v1" + # Run the following command as defined + command: ["/bin/sh", "-c", "echo hi"] + # Extra arguments append to command defined above + args: ["/bin/sh", "-c", "echo hi"] + env: { + # An environment variable of name "env1" and value "VALUE" will be set + "env1": "VALUE" + # An environment variable of name "env2" and value of the key "key" in the + # secret named "sec-name" will be set. + "env2": "secret://sec-name/key" + } + # Run the command "/bin/sh -c echo hi", as defined above, in the directory "/tmp" + workingDir: "/tmp" + # Configure a HTTP readiness probe + readinessProbe: p.Probe { + probeHandler: p.Http { + url: "http://localhost:80" + } + initialDelaySeconds: 10 + } + } + } + # Set the replicas + replicas: 2 + } + # Add the monitoring configuration backed by Prometheus + monitoring: m.Prometheus{ + interval: "30s" + timeout: "15s" + path: "/metrics" + port: "web" + scheme: "http" + } +} \ No newline at end of file diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/expected b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/expected new file mode 100644 index 00000000..5a0e6855 --- /dev/null +++ b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/expected @@ -0,0 +1,28 @@ +workload: + containers: + nginx: + image: nginx:v1 + command: + - /bin/sh + - -c + - echo hi + args: + - /bin/sh + - -c + - echo hi + env: + env1: VALUE + env2: secret://sec-name/key + workingDir: /tmp + readinessProbe: + probeHandler: + url: http://localhost:80 + initialDelaySeconds: 10 + replicas: 2 + type: Deployment +monitoring: + interval: 30s + timeout: 15s + path: /metrics + port: web + scheme: http \ No newline at end of file diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/kcl.mod b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/kcl.mod new file mode 100644 index 00000000..8365decd --- /dev/null +++ b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "dep_git_tag" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "0.1.1" } diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/main.k b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/main.k new file mode 100644 index 00000000..320a5761 --- /dev/null +++ b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/main.k @@ -0,0 +1,45 @@ +import catalog.models.schema.v1 as ac +import catalog.models.schema.v1.workload as wl +import catalog.models.schema.v1.workload.container as c +import catalog.models.schema.v1.workload.container.probe as p +import catalog.models.schema.v1.monitoring as m + +ac.AppConfiguration { + workload: wl.Service { + containers: { + "nginx": c.Container { + image: "nginx:v1" + # Run the following command as defined + command: ["/bin/sh", "-c", "echo hi"] + # Extra arguments append to command defined above + args: ["/bin/sh", "-c", "echo hi"] + env: { + # An environment variable of name "env1" and value "VALUE" will be set + "env1": "VALUE" + # An environment variable of name "env2" and value of the key "key" in the + # secret named "sec-name" will be set. + "env2": "secret://sec-name/key" + } + # Run the command "/bin/sh -c echo hi", as defined above, in the directory "/tmp" + workingDir: "/tmp" + # Configure a HTTP readiness probe + readinessProbe: p.Probe { + probeHandler: p.Http { + url: "http://localhost:80" + } + initialDelaySeconds: 10 + } + } + } + # Set the replicas + replicas: 2 + } + # Add the monitoring configuration backed by Prometheus + monitoring: m.Prometheus{ + interval: "30s" + timeout: "15s" + path: "/metrics" + port: "web" + scheme: "http" + } +} \ No newline at end of file diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_oci/expected b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_oci/expected new file mode 100644 index 00000000..adf15e56 --- /dev/null +++ b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_oci/expected @@ -0,0 +1 @@ +a: Hello World! \ No newline at end of file diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/kcl.mod b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_oci/kcl.mod similarity index 100% rename from pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/kcl.mod rename to pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_oci/kcl.mod diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/main.k b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_oci/main.k similarity index 100% rename from pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/main.k rename to pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_oci/main.k diff --git a/pkg/client/client.go b/pkg/client/client.go index 5fb8f2f5..7cc86f14 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -736,7 +736,7 @@ func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Depend if err != nil { return nil, err } - dep.Version = dep.Source.Git.Tag + dep.LocalFullPath = localPath // Creating symbolic links in a global cache is not an optimal solution. // This allows kclvm to locate the package by default. @@ -746,6 +746,12 @@ func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Depend return nil, err } dep.FullName = dep.GenDepFullName() + // If the dependency is from git commit, the version is the commit id. + // If the dependency is from git tag, the version is the tag. + dep.Version, err = dep.Source.Git.GetValidGitReference() + if err != nil { + return nil, err + } } if dep.Source.Oci != nil { @@ -784,12 +790,27 @@ func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Depend // DownloadFromGit will download the dependency from the git repository. func (c *KpmClient) DownloadFromGit(dep *pkg.Git, localPath string) (string, error) { + var msg string + if len(dep.Tag) != 0 { + msg = fmt.Sprintf("with tag '%s'", dep.Tag) + } + + if len(dep.Commit) != 0 { + msg = fmt.Sprintf("with commit '%s'", dep.Commit) + } + reporter.ReportMsgTo( - fmt.Sprintf("downloading '%s' with tag '%s'", dep.Url, dep.Tag), + fmt.Sprintf("cloning '%s' %s", dep.Url, msg), c.logWriter, ) - _, err := git.Clone(dep.Url, dep.Tag, localPath, c.logWriter) + _, err := git.CloneWithOpts( + git.WithCommit(dep.Commit), + git.WithTag(dep.Tag), + git.WithRepoURL(dep.Url), + git.WithLocalPath(localPath), + git.WithWriter(c.logWriter), + ) if err != nil { return localPath, reporter.NewErrorEvent( diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 61062d11..c741e185 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -998,3 +998,66 @@ func TestAddWithDiffVersionWithSumCheck(t *testing.T) { _ = os.Remove(pkgWithSumCheckPathModLock) }() } + +func TestAddWithGitCommit(t *testing.T) { + pkgPath := getTestDir("add_with_git_commit") + + testPkgPath := filepath.Join(pkgPath, "test_pkg") + testPkgPathModBak := filepath.Join(testPkgPath, "kcl.mod.bak") + testPkgPathMod := filepath.Join(testPkgPath, "kcl.mod") + testPkgPathModExpect := filepath.Join(testPkgPath, "kcl.mod.expect") + testPkgPathModLock := filepath.Join(testPkgPath, "kcl.mod.lock") + testPkgPathModLockBak := filepath.Join(testPkgPath, "kcl.mod.lock.bak") + testPkgPathModLockExpect := filepath.Join(testPkgPath, "kcl.mod.lock.expect") + + err := copy.Copy(testPkgPathModBak, testPkgPathMod) + assert.Equal(t, err, nil) + err = copy.Copy(testPkgPathModLockBak, testPkgPathModLock) + assert.Equal(t, err, nil) + + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + kclPkg, err := kpmcli.LoadPkgFromPath(testPkgPath) + assert.Equal(t, err, nil) + + opts := opt.AddOptions{ + LocalPath: pkgPath, + RegistryOpts: opt.RegistryOptions{ + Git: &opt.GitOptions{ + Url: "https://github.com/KusionStack/catalog.git", + Commit: "a29e3db", + }, + }, + } + kpmcli.SetLogWriter(nil) + _, err = kpmcli.AddDepWithOpts(kclPkg, &opts) + + assert.Equal(t, err, nil) + + modContent, err := os.ReadFile(testPkgPathMod) + modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "") + modContentStr = strings.ReplaceAll(modContentStr, "\n", "") + assert.Equal(t, err, nil) + + modExpectContent, err := os.ReadFile(testPkgPathModExpect) + modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "") + modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "") + + assert.Equal(t, err, nil) + assert.Equal(t, modContentStr, modExpectContentStr) + + modLockContent, err := os.ReadFile(testPkgPathModLock) + modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "") + modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "") + assert.Equal(t, err, nil) + modLockExpectContent, err := os.ReadFile(testPkgPathModLockExpect) + modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "") + modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "") + assert.Equal(t, err, nil) + assert.Equal(t, modLockContentStr, modLockExpectContentStr) + + defer func() { + _ = os.Remove(testPkgPathMod) + _ = os.Remove(testPkgPathModLock) + }() +} diff --git a/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.bak b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.bak new file mode 100644 index 00000000..446bc2a2 --- /dev/null +++ b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.bak @@ -0,0 +1,6 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] diff --git a/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect new file mode 100644 index 00000000..475d36f8 --- /dev/null +++ b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db" } diff --git a/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.lock.bak b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.lock.bak new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.lock.expect b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.lock.expect new file mode 100644 index 00000000..ccc62bba --- /dev/null +++ b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.lock.expect @@ -0,0 +1,8 @@ +[dependencies] + [dependencies.catalog] + name = "catalog" + full_name = "catalog_a29e3db" + version = "a29e3db" + sum = "kFmlrYJbJUFFTEXjC9cquc80WB+UpZ/6oMPKrfgyeks=" + url = "https://github.com/KusionStack/catalog.git" + commit = "a29e3db" diff --git a/pkg/client/test_data/add_with_git_commit/test_pkg/main.k b/pkg/client/test_data/add_with_git_commit/test_pkg/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_git_commit/test_pkg/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/cmd/cmd_add.go b/pkg/cmd/cmd_add.go index fdf55ff3..59b7fc5f 100644 --- a/pkg/cmd/cmd_add.go +++ b/pkg/cmd/cmd_add.go @@ -32,6 +32,10 @@ func NewAddCmd(kpmcli *client.KpmClient) *cli.Command { Name: "tag", Usage: "Git repository tag", }, + &cli.StringSliceFlag{ + Name: "commit", + Usage: "Git repository commit", + }, &cli.BoolFlag{ Name: FLAG_NO_SUM_CHECK, Usage: "do not check the checksum of the package and update kcl.mod.lock", @@ -111,14 +115,14 @@ func KpmAdd(c *cli.Context, kpmcli *client.KpmClient) error { } // onlyOnceOption is used to check that the value of some parameters can only appear once. -func onlyOnceOption(c *cli.Context, name string) (*string, *reporter.KpmEvent) { +func onlyOnceOption(c *cli.Context, name string) (string, *reporter.KpmEvent) { inputOpt := c.StringSlice(name) if len(inputOpt) > 1 { - return nil, reporter.NewErrorEvent(reporter.InvalidCmd, fmt.Errorf("the argument '%s' cannot be used multiple times", name)) + return "", reporter.NewErrorEvent(reporter.InvalidCmd, fmt.Errorf("the argument '%s' cannot be used multiple times", name)) } else if len(inputOpt) == 1 { - return &inputOpt[0], nil + return inputOpt[0], nil } else { - return nil, nil + return "", nil } } @@ -176,18 +180,25 @@ func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, *reporter.Kp return nil, err } - if gitUrl == nil { + gitCommit, err := onlyOnceOption(c, "commit") + + if err != (*reporter.KpmEvent)(nil) { + return nil, err + } + + if gitUrl == "" { return nil, reporter.NewErrorEvent(reporter.InvalidGitUrl, fmt.Errorf("the argument 'git' is required")) } - if gitTag == nil { - return nil, reporter.NewErrorEvent(reporter.WithoutGitTag, fmt.Errorf("the argument 'tag' is required")) + if (gitTag == "" && gitCommit == "") || (gitTag != "" && gitCommit != "") { + return nil, reporter.NewErrorEvent(reporter.WithoutGitTag, fmt.Errorf("invalid arguments, one of commit or tag should be passed")) } return &opt.RegistryOptions{ Git: &opt.GitOptions{ - Url: *gitUrl, - Tag: *gitTag, + Url: gitUrl, + Tag: gitTag, + Commit: gitCommit, }, }, nil } diff --git a/pkg/git/git.go b/pkg/git/git.go index 385a65ce..d7483583 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -1,25 +1,192 @@ package git import ( + "errors" "fmt" "io" + "strings" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" ) -// Clone will clone from `repoURL` to `localPath` via git. +// CloneOptions is a struct for specifying options for cloning a git repository +type CloneOptions struct { + RepoURL string + Commit string + Tag string + Branch string + LocalPath string + Writer io.Writer +} + +// CloneOption is a function that modifies CloneOptions +type CloneOption func(*CloneOptions) + +// WithRepoURL sets the repo URL for CloneOptions +func WithRepoURL(repoURL string) CloneOption { + return func(o *CloneOptions) { + o.RepoURL = repoURL + } +} + +// WithBranch sets the branch for CloneOptions +func WithBranch(branch string) CloneOption { + return func(o *CloneOptions) { + o.Branch = branch + } +} + +// WithCommit sets the commit for CloneOptions +func WithCommit(commit string) CloneOption { + return func(o *CloneOptions) { + o.Commit = commit + } +} + +// WithTag sets the tag for CloneOptions +func WithTag(tag string) CloneOption { + return func(o *CloneOptions) { + o.Tag = tag + } +} + +// WithLocalPath sets the local path for CloneOptions +func WithLocalPath(localPath string) CloneOption { + return func(o *CloneOptions) { + o.LocalPath = localPath + } +} + +// WithWriter sets the writer for CloneOptions +func WithWriter(writer io.Writer) CloneOption { + return func(o *CloneOptions) { + o.Writer = writer + } +} + +// Validate checks if the CloneOptions are valid +func (cloneOpts *CloneOptions) Validate() error { + onlyOneAllowed := 0 + if cloneOpts.Branch != "" { + onlyOneAllowed++ + } + if cloneOpts.Tag != "" { + onlyOneAllowed++ + } + if cloneOpts.Commit != "" { + onlyOneAllowed++ + } + + if onlyOneAllowed > 1 { + return errors.New("only one of branch, tag or commit is allowed") + } + + return nil +} + +// Clone clones a git repository +func (cloneOpts *CloneOptions) Clone() (*git.Repository, error) { + err := cloneOpts.Validate() + if err != nil { + return nil, err + } + + gitCloneOpts := &git.CloneOptions{ + URL: cloneOpts.RepoURL, + Progress: nil, + } + + if cloneOpts.Tag != "" { + gitCloneOpts.ReferenceName = plumbing.ReferenceName(plumbing.NewTagReferenceName(cloneOpts.Tag)) + } + + if cloneOpts.Branch != "" { + gitCloneOpts.ReferenceName = plumbing.ReferenceName(plumbing.NewBranchReferenceName(cloneOpts.Branch)) + } + + repo, err := git.PlainClone(cloneOpts.LocalPath, false, gitCloneOpts) + if err != nil { + return nil, err + } + // if the commit is specified, checkout the commit + if cloneOpts.Commit != "" { + // Checkout the specific commit + w, err := repo.Worktree() + if err != nil { + return nil, err + } + + // checkout the commit + err = w.Checkout(&git.CheckoutOptions{ + Hash: plumbing.NewHash(cloneOpts.Commit), + }) + + // if the commit is not found, try to find the full commit hash + if err != nil { + // get all the references + commits, err := repo.CommitObjects() + if err != nil { + return nil, err + } + + checkoutFinished := errors.New("checkout by commit finished") + // iterate over the references, find the full commit hash and checkout + err = commits.ForEach(func(commit *object.Commit) error { + // if the commit hash starts with the commit, checkout it + if strings.HasPrefix(commit.Hash.String(), cloneOpts.Commit) { + err = w.Checkout(&git.CheckoutOptions{ + Hash: commit.Hash, + }) + if err != nil { + return err + } + return checkoutFinished + } + return nil + }) + + if err != nil && err == checkoutFinished { + return repo, nil + } + + if err != checkoutFinished { + if err != nil { + return nil, err + } else { + return nil, fmt.Errorf("commit '%s' not found", cloneOpts.Commit) + } + } + return repo, nil + } + } + + return repo, nil +} + +// CloneWithOpts will clone from `repoURL` to `localPath` via git by using CloneOptions +func CloneWithOpts(opts ...CloneOption) (*git.Repository, error) { + cloneOpts := &CloneOptions{} + for _, opt := range opts { + opt(cloneOpts) + } + + err := cloneOpts.Validate() + if err != nil { + return nil, err + } + + return cloneOpts.Clone() +} + +// Clone will clone from `repoURL` to `localPath` via git by tag name. +// Deprecated: This function will be removed in a future version. Use CloneWithOpts instead. func Clone(repoURL string, tagName string, localPath string, writer io.Writer) (*git.Repository, error) { repo, err := git.PlainClone(localPath, false, &git.CloneOptions{ URL: repoURL, Progress: writer, - ReferenceName: plumbing.ReferenceName(CreateTagRef(tagName)), + ReferenceName: plumbing.ReferenceName(plumbing.NewTagReferenceName(tagName)), }) return repo, err } - -const TAG_PREFIX = "refs/tags/%s" - -func CreateTagRef(tagName string) string { - return fmt.Sprintf(TAG_PREFIX, tagName) -} diff --git a/pkg/git/git_test.go b/pkg/git/git_test.go index 2bb59212..a82c04c4 100644 --- a/pkg/git/git_test.go +++ b/pkg/git/git_test.go @@ -1,11 +1,59 @@ package git import ( + "bytes" + "os" "testing" - "github.com/stretchr/testify/assert" + "gotest.tools/v3/assert" ) -func TestCreateTagRef(t *testing.T) { - assert.Equal(t, CreateTagRef("test"), "refs/tags/test") +func TestWithGitOptions(t *testing.T) { + cloneOpts := &CloneOptions{} + WithRepoURL("test_url")(cloneOpts) + assert.Equal(t, cloneOpts.RepoURL, "test_url") + WithBranch("test_branch")(cloneOpts) + assert.Equal(t, cloneOpts.Branch, "test_branch") + WithCommit("test_commit")(cloneOpts) + assert.Equal(t, cloneOpts.Commit, "test_commit") + WithTag("test_tag")(cloneOpts) + assert.Equal(t, cloneOpts.Tag, "test_tag") + WithLocalPath("test_local_path")(cloneOpts) + assert.Equal(t, cloneOpts.LocalPath, "test_local_path") + WithWriter(nil)(cloneOpts) + assert.Equal(t, cloneOpts.Writer, nil) +} + +func TestValidateGitOptions(t *testing.T) { + cloneOpts := &CloneOptions{} + WithBranch("test_branch")(cloneOpts) + err := cloneOpts.Validate() + assert.Equal(t, err, nil) + WithCommit("test_commit")(cloneOpts) + err = cloneOpts.Validate() + assert.Equal(t, err.Error(), "only one of branch, tag or commit is allowed") +} + +func TestCloneWithOptions(t *testing.T) { + var buf bytes.Buffer + + tmpdir, err := os.MkdirTemp("", "git") + assert.Equal(t, err, nil) + defer func() { + rErr := os.RemoveAll(tmpdir) + assert.Equal(t, rErr, nil) + }() + + repo, err := CloneWithOpts( + WithRepoURL("https://github.com/KusionStack/catalog.git"), + WithCommit("4e59d5852cd7"), + WithWriter(&buf), + WithLocalPath(tmpdir), + ) + assert.Equal(t, err, nil) + + head, err := repo.Head() + assert.Equal(t, err, nil) + assert.Equal(t, head.Hash().String(), "4e59d5852cd76542f9f0ec65e5773ca9f4e02462") + assert.Equal(t, err, nil) } diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index 49b67b66..c7a0006e 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -2,6 +2,7 @@ package pkg import ( + "errors" "fmt" "os" "path/filepath" @@ -169,8 +170,18 @@ func (d *Dependency) GetAliasName() string { // WithTheSameVersion will check whether two dependencies have the same version. func (d Dependency) WithTheSameVersion(other Dependency) bool { - return d.Name == other.Name && - d.Version == other.Version + + sameNameAndVersion := d.Name == other.Name && d.Version == other.Version + + sameGitSrc := true + if d.Source.Git != nil && other.Source.Git != nil { + sameGitSrc = d.Source.Git.Url == other.Source.Git.Url && + (d.Source.Git.Branch == other.Source.Git.Branch || + d.Source.Git.Commit == other.Source.Git.Commit || + d.Source.Git.Tag == other.Source.Git.Tag) + } + + return sameNameAndVersion && sameGitSrc } // GetLocalFullPath will get the local path of a dependency. @@ -234,6 +245,32 @@ type Git struct { Tag string `toml:"git_tag,omitempty"` } +// GetValidGitReference will get the valid git reference from git source. +// Only one of branch, tag or commit is allowed. +func (git *Git) GetValidGitReference() (string, error) { + nonEmptyFields := 0 + var nonEmptyRef string + + if git.Tag != "" { + nonEmptyFields++ + nonEmptyRef = git.Tag + } + if git.Commit != "" { + nonEmptyFields++ + nonEmptyRef = git.Commit + } + if git.Branch != "" { + nonEmptyFields++ + nonEmptyRef = git.Branch + } + + if nonEmptyFields != 1 { + return "", errors.New("only one of branch, tag or commit is allowed") + } + + return nonEmptyRef, nil +} + // ModFileExists returns whether a 'kcl.mod' file exists in the path. func ModFileExists(path string) (bool, error) { return utils.Exists(filepath.Join(path, MOD_FILE)) @@ -362,13 +399,23 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) { Tag: opt.Git.Tag, } + gitRef, err := gitSource.GetValidGitReference() + if err != nil { + return nil, err + } + + fullName, err := ParseRepoFullNameFromGitSource(gitSource) + if err != nil { + return nil, err + } + return &Dependency{ Name: ParseRepoNameFromGitSource(gitSource), - FullName: ParseRepoFullNameFromGitSource(gitSource), + FullName: fullName, Source: Source{ Git: &gitSource, }, - Version: gitSource.Tag, + Version: gitRef, }, nil } if opt.Oci != nil { @@ -413,11 +460,15 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) { const PKG_NAME_PATTERN = "%s_%s" // ParseRepoFullNameFromGitSource will extract the kcl package name from the git url. -func ParseRepoFullNameFromGitSource(gitSrc Git) string { - if len(gitSrc.Tag) != 0 { - return fmt.Sprintf(PKG_NAME_PATTERN, utils.ParseRepoNameFromGitUrl(gitSrc.Url), gitSrc.Tag) +func ParseRepoFullNameFromGitSource(gitSrc Git) (string, error) { + ref, err := gitSrc.GetValidGitReference() + if err != nil { + return "", err } - return utils.ParseRepoNameFromGitUrl(gitSrc.Url) + if len(ref) != 0 { + return fmt.Sprintf(PKG_NAME_PATTERN, utils.ParseRepoNameFromGitUrl(gitSrc.Url), ref), nil + } + return utils.ParseRepoNameFromGitUrl(gitSrc.Url), nil } // ParseRepoNameFromGitSource will extract the kcl package name from the git url. diff --git a/pkg/package/modfile_test.go b/pkg/package/modfile_test.go index 97e7cb4b..4269a4e5 100644 --- a/pkg/package/modfile_test.go +++ b/pkg/package/modfile_test.go @@ -87,12 +87,53 @@ func TestModFileExists(t *testing.T) { } func TestParseOpt(t *testing.T) { + _, err := ParseOpt(&opt.RegistryOptions{ + Git: &opt.GitOptions{ + Url: "test.git", + Branch: "test_branch", + Commit: "test_commit", + Tag: "test_tag", + }, + }) + assert.Equal(t, err.Error(), "only one of branch, tag or commit is allowed") dep, err := ParseOpt(&opt.RegistryOptions{ Git: &opt.GitOptions{ Url: "test.git", Branch: "test_branch", + Commit: "", + Tag: "", + }, + }) + assert.Equal(t, err, nil) + assert.Equal(t, dep.Name, "test") + assert.Equal(t, dep.FullName, "test_test_branch") + assert.Equal(t, dep.Url, "test.git") + assert.Equal(t, dep.Branch, "test_branch") + assert.Equal(t, dep.Commit, "") + assert.Equal(t, dep.Git.Tag, "") + + dep, err = ParseOpt(&opt.RegistryOptions{ + Git: &opt.GitOptions{ + Url: "test.git", + Branch: "", Commit: "test_commit", + Tag: "", + }, + }) + assert.Equal(t, err, nil) + assert.Equal(t, dep.Name, "test") + assert.Equal(t, dep.FullName, "test_test_commit") + assert.Equal(t, dep.Url, "test.git") + assert.Equal(t, dep.Branch, "") + assert.Equal(t, dep.Commit, "test_commit") + assert.Equal(t, dep.Git.Tag, "") + + dep, err = ParseOpt(&opt.RegistryOptions{ + Git: &opt.GitOptions{ + Url: "test.git", + Branch: "", + Commit: "", Tag: "test_tag", }, }) @@ -100,8 +141,8 @@ func TestParseOpt(t *testing.T) { assert.Equal(t, dep.Name, "test") assert.Equal(t, dep.FullName, "test_test_tag") assert.Equal(t, dep.Url, "test.git") - assert.Equal(t, dep.Branch, "test_branch") - assert.Equal(t, dep.Commit, "test_commit") + assert.Equal(t, dep.Branch, "") + assert.Equal(t, dep.Commit, "") assert.Equal(t, dep.Git.Tag, "test_tag") } diff --git a/pkg/package/toml.go b/pkg/package/toml.go index 520b22c7..be64cf63 100644 --- a/pkg/package/toml.go +++ b/pkg/package/toml.go @@ -111,6 +111,7 @@ func (source *Source) MarshalTOML() string { const GTI_URL_PATTERN = "git = \"%s\"" const GTI_TAG_PATTERN = "tag = \"%s\"" +const GTI_COMMIT_PATTERN = "commit = \"%s\"" const SEPARATOR = ", " func (git *Git) MarshalTOML() string { @@ -122,6 +123,10 @@ func (git *Git) MarshalTOML() string { sb.WriteString(SEPARATOR) sb.WriteString(fmt.Sprintf(GTI_TAG_PATTERN, git.Tag)) } + if len(git.Commit) != 0 { + sb.WriteString(SEPARATOR) + sb.WriteString(fmt.Sprintf(GTI_COMMIT_PATTERN, git.Commit)) + } return sb.String() } @@ -266,7 +271,10 @@ func (dep *Dependency) UnmarshalModTOML(data interface{}) error { dep.Source = source var version string if source.Git != nil { - version = source.Git.Tag + version, err = source.Git.GetValidGitReference() + if err != nil { + return err + } } if source.Oci != nil { version = source.Oci.Tag @@ -312,6 +320,7 @@ func (source *Source) UnmarshalModTOML(data interface{}) error { const GTI_URL_FLAG = "git" const GTI_TAG_FLAG = "tag" +const GTI_COMMIT_FLAG = "commit" func (git *Git) UnmarshalModTOML(data interface{}) error { meta, ok := data.(map[string]interface{}) @@ -327,6 +336,10 @@ func (git *Git) UnmarshalModTOML(data interface{}) error { git.Tag = v } + if v, ok := meta[GTI_COMMIT_FLAG].(string); ok { + git.Commit = v + } + return nil } diff --git a/test/e2e/kpm_test.go b/test/e2e/kpm_test.go index 780f3d77..c5558a2f 100644 --- a/test/e2e/kpm_test.go +++ b/test/e2e/kpm_test.go @@ -171,6 +171,29 @@ var _ = ginkgo.Describe("Kpm CLI Testing", func() { } }) + ginkgo.Context("testing 'kpm add '", func() { + testSuitesRoot := filepath.Join(filepath.Join(filepath.Join(GetWorkDir(), TEST_SUITES_DIR), "kpm"), "kpm_add") + testSuites := LoadAllTestSuites(testSuitesRoot) + testDataRoot := filepath.Join(filepath.Join(GetWorkDir(), TEST_SUITES_DIR), "test_data") + for _, ts := range testSuites { + ts := ts + ginkgo.It(ts.GetTestSuiteInfo(), func() { + workspace := GetWorkspace() + CopyDir(filepath.Join(testDataRoot, ts.Name), filepath.Join(workspace, ts.Name)) + + input := ReplaceAllKeyByValue(ts.Input, "", filepath.Join(workspace, ts.Name)) + stdout, stderr, err := ExecKpmWithWorkDir(input, filepath.Join(workspace, ts.Name)) + + expectedStdout := ReplaceAllKeyByValue(ts.ExpectStdout, "", workspace) + expectedStderr := ReplaceAllKeyByValue(ts.ExpectStderr, "", workspace) + + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + gomega.Expect(stdout).To(gomega.ContainSubstring(expectedStdout)) + gomega.Expect(stderr).To(gomega.ContainSubstring(expectedStderr)) + }) + } + }) + ginkgo.Context("testing 'kpm metadata '", func() { testSuitesRoot := filepath.Join(filepath.Join(filepath.Join(GetWorkDir(), TEST_SUITES_DIR), "kpm"), "kpm_metadata") testSuites := LoadAllTestSuites(testSuitesRoot) @@ -184,10 +207,16 @@ var _ = ginkgo.Describe("Kpm CLI Testing", func() { input := ReplaceAllKeyByValue(ts.Input, "", filepath.Join(workspace, ts.Name)) stdout, stderr, err := ExecKpmWithWorkDir(input, filepath.Join(workspace, ts.Name)) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) expectedStdout := ReplaceAllKeyByValue(ts.ExpectStdout, "", workspace) expectedStderr := ReplaceAllKeyByValue(ts.ExpectStderr, "", workspace) + home, err := os.UserHomeDir() + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + expectedStdout = ReplaceAllKeyByValue(expectedStdout, "", home) + expectedStderr = ReplaceAllKeyByValue(expectedStderr, "", home) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) gomega.Expect(stdout).To(gomega.ContainSubstring(expectedStdout)) gomega.Expect(stderr).To(gomega.ContainSubstring(expectedStderr)) diff --git a/test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.env b/test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.env new file mode 100644 index 00000000..4c789529 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/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_add/test_kpm_add_git_commit/test_suite.input b/test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.input new file mode 100644 index 00000000..9ba6e569 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.input @@ -0,0 +1 @@ +kpm add -git https://github.com/KusionStack/catalog.git -commit a29e3db \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.stderr b/test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.stderr new file mode 100644 index 00000000..e69de29b diff --git a/test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.stdout b/test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.stdout new file mode 100644 index 00000000..3c5d3d69 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_add/test_kpm_add_git_commit/test_suite.stdout @@ -0,0 +1,2 @@ +adding dependency 'catalog' +cloning 'https://github.com/KusionStack/catalog.git' with commit 'a29e3db' \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.env b/test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.env new file mode 100644 index 00000000..4c789529 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/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_metadata/test_kpm_metadata_with_commit_dep/test_suite.input b/test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.input new file mode 100644 index 00000000..032346d8 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.input @@ -0,0 +1 @@ +kpm metadata \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.stderr b/test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.stderr new file mode 100644 index 00000000..e69de29b diff --git a/test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.stdout b/test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.stdout new file mode 100644 index 00000000..8979959c --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_metadata/test_kpm_metadata_with_commit_dep/test_suite.stdout @@ -0,0 +1 @@ +{"packages":{"catalog":{"name":"catalog","manifest_path":"/.kcl/kpm/catalog_c5435e733"}}} \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.env b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.env new file mode 100644 index 00000000..4c789529 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/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_git_commit_dep/test_suite.input b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.input new file mode 100644 index 00000000..b69acba6 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.input @@ -0,0 +1 @@ +kpm --quiet run \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.stderr b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.stderr new file mode 100644 index 00000000..e69de29b diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.stdout b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.stdout new file mode 100644 index 00000000..df865cb7 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep/test_suite.stdout @@ -0,0 +1,27 @@ +workload: + containers: + nginx: + image: nginx:v1 + command: + - /bin/sh + - -c + - echo hi + args: + - /bin/sh + - -c + - echo hi + env: + env1: VALUE + env2: secret://sec-name/key + workingDir: /tmp + readinessProbe: + probeHandler: + url: http://localhost:80 + initialDelaySeconds: 10 + replicas: 2 +monitoring: + interval: 30s + timeout: 15s + path: /metrics + port: web + scheme: http diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.env b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.env new file mode 100644 index 00000000..4c789529 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/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_git_commit_dep_1/test_suite.input b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.input new file mode 100644 index 00000000..b69acba6 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.input @@ -0,0 +1 @@ +kpm --quiet run \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.stderr b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.stderr new file mode 100644 index 00000000..e69de29b diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.stdout b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.stdout new file mode 100644 index 00000000..5efb7246 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_git_commit_dep_1/test_suite.stdout @@ -0,0 +1,28 @@ +a: + workload: + containers: + nginx: + image: nginx:v1 + command: + - /bin/sh + - -c + - echo hi + args: + - /bin/sh + - -c + - echo hi + env: + env1: VALUE + env2: secret://sec-name/key + workingDir: /tmp + readinessProbe: + probeHandler: + url: http://localhost:80 + initialDelaySeconds: 10 + replicas: 2 + monitoring: + interval: 30s + timeout: 15s + path: /metrics + port: web + scheme: http diff --git a/test/e2e/test_suites/test_data/test_kpm_add_git_commit/kcl.mod b/test/e2e/test_suites/test_data/test_kpm_add_git_commit/kcl.mod new file mode 100644 index 00000000..e69de29b diff --git a/test/e2e/test_suites/test_data/test_kpm_add_git_commit/kcl.mod.lock b/test/e2e/test_suites/test_data/test_kpm_add_git_commit/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/test/e2e/test_suites/test_data/test_kpm_add_git_commit/main.k b/test/e2e/test_suites/test_data/test_kpm_add_git_commit/main.k new file mode 100644 index 00000000..e69de29b diff --git a/test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/kcl.mod b/test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/kcl.mod new file mode 100644 index 00000000..0d4b9abb --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "test_web_service" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "c5435e733" } diff --git a/test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/kcl.mod.lock b/test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/kcl.mod.lock new file mode 100644 index 00000000..f6276949 --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/kcl.mod.lock @@ -0,0 +1,8 @@ +[dependencies] + [dependencies.catalog] + name = "catalog" + full_name = "catalog_c5435e733" + version = "c5435e733" + sum = "kFmlrYJbJUFFTEXjC9cquc80WB+UpZ/6oMPKrfgyeks=" + url = "https://github.com/KusionStack/catalog.git" + commit = "c5435e733" diff --git a/test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/main.k b/test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_metadata_with_commit_dep/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/kcl.mod b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/kcl.mod new file mode 100644 index 00000000..88baa964 --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "test_kpm_run_with_git_commit_dep" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db" } diff --git a/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/kcl.mod.lock b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/kcl.mod.lock new file mode 100644 index 00000000..ccc62bba --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/kcl.mod.lock @@ -0,0 +1,8 @@ +[dependencies] + [dependencies.catalog] + name = "catalog" + full_name = "catalog_a29e3db" + version = "a29e3db" + sum = "kFmlrYJbJUFFTEXjC9cquc80WB+UpZ/6oMPKrfgyeks=" + url = "https://github.com/KusionStack/catalog.git" + commit = "a29e3db" diff --git a/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/main.k b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/main.k new file mode 100644 index 00000000..320a5761 --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep/main.k @@ -0,0 +1,45 @@ +import catalog.models.schema.v1 as ac +import catalog.models.schema.v1.workload as wl +import catalog.models.schema.v1.workload.container as c +import catalog.models.schema.v1.workload.container.probe as p +import catalog.models.schema.v1.monitoring as m + +ac.AppConfiguration { + workload: wl.Service { + containers: { + "nginx": c.Container { + image: "nginx:v1" + # Run the following command as defined + command: ["/bin/sh", "-c", "echo hi"] + # Extra arguments append to command defined above + args: ["/bin/sh", "-c", "echo hi"] + env: { + # An environment variable of name "env1" and value "VALUE" will be set + "env1": "VALUE" + # An environment variable of name "env2" and value of the key "key" in the + # secret named "sec-name" will be set. + "env2": "secret://sec-name/key" + } + # Run the command "/bin/sh -c echo hi", as defined above, in the directory "/tmp" + workingDir: "/tmp" + # Configure a HTTP readiness probe + readinessProbe: p.Probe { + probeHandler: p.Http { + url: "http://localhost:80" + } + initialDelaySeconds: 10 + } + } + } + # Set the replicas + replicas: 2 + } + # Add the monitoring configuration backed by Prometheus + monitoring: m.Prometheus{ + interval: "30s" + timeout: "15s" + path: "/metrics" + port: "web" + scheme: "http" + } +} \ No newline at end of file diff --git a/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/kcl.mod b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/kcl.mod new file mode 100644 index 00000000..88baa964 --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "test_kpm_run_with_git_commit_dep" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db" } diff --git a/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/kcl.mod.lock b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/kcl.mod.lock new file mode 100644 index 00000000..ccc62bba --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/kcl.mod.lock @@ -0,0 +1,8 @@ +[dependencies] + [dependencies.catalog] + name = "catalog" + full_name = "catalog_a29e3db" + version = "a29e3db" + sum = "kFmlrYJbJUFFTEXjC9cquc80WB+UpZ/6oMPKrfgyeks=" + url = "https://github.com/KusionStack/catalog.git" + commit = "a29e3db" diff --git a/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/main.k b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/main.k new file mode 100644 index 00000000..b2afb886 --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/main.k @@ -0,0 +1,3 @@ +import sub as s + +a = s.sub_app \ No newline at end of file diff --git a/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/kcl.mod b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/kcl.mod new file mode 100644 index 00000000..bde5dc72 --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "sub" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db" } diff --git a/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/kcl.mod.lock b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/kcl.mod.lock new file mode 100644 index 00000000..ccc62bba --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/kcl.mod.lock @@ -0,0 +1,8 @@ +[dependencies] + [dependencies.catalog] + name = "catalog" + full_name = "catalog_a29e3db" + version = "a29e3db" + sum = "kFmlrYJbJUFFTEXjC9cquc80WB+UpZ/6oMPKrfgyeks=" + url = "https://github.com/KusionStack/catalog.git" + commit = "a29e3db" diff --git a/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/main.k b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/main.k new file mode 100644 index 00000000..e16249e3 --- /dev/null +++ b/test/e2e/test_suites/test_data/test_kpm_run_with_git_commit_dep_1/sub/main.k @@ -0,0 +1,45 @@ +import catalog.models.schema.v1 as ac +import catalog.models.schema.v1.workload as wl +import catalog.models.schema.v1.workload.container as c +import catalog.models.schema.v1.workload.container.probe as p +import catalog.models.schema.v1.monitoring as m + +sub_app = ac.AppConfiguration { + workload: wl.Service { + containers: { + "nginx": c.Container { + image: "nginx:v1" + # Run the following command as defined + command: ["/bin/sh", "-c", "echo hi"] + # Extra arguments append to command defined above + args: ["/bin/sh", "-c", "echo hi"] + env: { + # An environment variable of name "env1" and value "VALUE" will be set + "env1": "VALUE" + # An environment variable of name "env2" and value of the key "key" in the + # secret named "sec-name" will be set. + "env2": "secret://sec-name/key" + } + # Run the command "/bin/sh -c echo hi", as defined above, in the directory "/tmp" + workingDir: "/tmp" + # Configure a HTTP readiness probe + readinessProbe: p.Probe { + probeHandler: p.Http { + url: "http://localhost:80" + } + initialDelaySeconds: 10 + } + } + } + # Set the replicas + replicas: 2 + } + # Add the monitoring configuration backed by Prometheus + monitoring: m.Prometheus{ + interval: "30s" + timeout: "15s" + path: "/metrics" + port: "web" + scheme: "http" + } +} \ No newline at end of file diff --git a/test/e2e/utils.go b/test/e2e/utils.go index 671b2fb9..4f819bd9 100644 --- a/test/e2e/utils.go +++ b/test/e2e/utils.go @@ -61,7 +61,7 @@ func CopyDir(srcDir, dstDir string) { } } -var KEYS = []string{"", "", ""} +var KEYS = []string{"", "", "", ""} // IsIgnore will reture whether the expected result in 'expectedStr' should be ignored. func IsIgnore(expectedStr string) bool { From 8a23512e7c78f65aa137e0d9db969e63d76d3134 Mon Sep 17 00:00:00 2001 From: zongz Date: Wed, 10 Jan 2024 14:27:07 +0800 Subject: [PATCH 2/2] fix: fix failed test case Signed-off-by: zongz --- .../test_run_no_sum_check/dep_git_tag/kcl.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/kcl.mod b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/kcl.mod index 8365decd..10fc2edf 100644 --- a/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/kcl.mod +++ b/pkg/api/test_data/test_run_pkg_in_path/test_run_no_sum_check/dep_git_tag/kcl.mod @@ -4,4 +4,4 @@ edition = "0.0.1" version = "0.0.1" [dependencies] -catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "0.1.1" } +catalog = { git = "https://github.com/KusionStack/catalog.git", tag = "0.1.1" }