From 202e2b6cba803d4c7ec11ba30663f4a635beb175 Mon Sep 17 00:00:00 2001 From: zongz Date: Wed, 4 Dec 2024 17:26:21 +0800 Subject: [PATCH] fix: fix test ci Signed-off-by: zongz --- pkg/client/deperated.go | 9 ++- pkg/client/issues_test.go | 12 +-- pkg/client/push.go | 76 ++++++++++--------- pkg/client/push_test.go | 13 ++-- pkg/cmd/cmd_push.go | 17 +++-- pkg/opt/opt.go | 14 +++- pkg/utils/utils.go | 19 ++++- .../kpm_pull_with_oci_url/test_suite.stdout | 2 +- .../test_suite.stdout | 2 +- .../pull_private_pkg/test_suite.stdout | 2 +- .../test_suite.stderr | 2 +- .../test_suite.stderr | 2 +- 12 files changed, 101 insertions(+), 69 deletions(-) diff --git a/pkg/client/deperated.go b/pkg/client/deperated.go index 9cc72974..8a6d6385 100644 --- a/pkg/client/deperated.go +++ b/pkg/client/deperated.go @@ -198,7 +198,7 @@ func (c *KpmClient) CompileOciPkg(ociSource, version string, opts *opt.CompileOp source := downloader.Source{ Oci: &downloader.Oci{ Reg: ociOpts.Reg, - Repo: ociOpts.Repo, + Repo: utils.JoinPath(ociOpts.Repo, ociOpts.Ref), Tag: ociOpts.Tag, }, } @@ -1338,7 +1338,7 @@ func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) er return reporter.NewErrorEvent(reporter.Bug, err) } - repoPath := utils.JoinPath(ociOpts.Reg, ociOpts.Repo) + repoPath := utils.JoinPath(ociOpts.Reg, ociOpts.Repo, ociOpts.Ref) cred, err := c.GetCredentials(ociOpts.Reg) if err != nil { return err @@ -1373,9 +1373,10 @@ func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) er ociOpts.Tag = tagSelected - full_repo := utils.JoinPath(ociOpts.Reg, ociOpts.Repo) + full_repo := utils.JoinPath(ociOpts.Reg, ociOpts.Repo, ociOpts.Ref) + full_ref := utils.JoinPath(ociOpts.Repo, ociOpts.Ref) reporter.ReportMsgTo( - fmt.Sprintf("pulling '%s:%s' from '%s'", ociOpts.Repo, tagSelected, full_repo), + fmt.Sprintf("pulling '%s:%s' from '%s'", full_ref, tagSelected, full_repo), c.logWriter, ) diff --git a/pkg/client/issues_test.go b/pkg/client/issues_test.go index bde94956..a6babb5e 100644 --- a/pkg/client/issues_test.go +++ b/pkg/client/issues_test.go @@ -14,7 +14,6 @@ import ( "kcl-lang.io/kpm/pkg/downloader" "kcl-lang.io/kpm/pkg/features" "kcl-lang.io/kpm/pkg/mock" - "kcl-lang.io/kpm/pkg/opt" pkg "kcl-lang.io/kpm/pkg/package" "kcl-lang.io/kpm/pkg/reporter" "kcl-lang.io/kpm/pkg/utils" @@ -486,11 +485,12 @@ func TestKclIssue1768(t *testing.T) { err = kpmcli.Push( WithPushModPath(pushedModPath), - WithPushOciOptions(&opt.OciOptions{ - Reg: "localhost:5001", - Repo: "test", - Ref: "oci_pushed_mod", - Tag: "v9.9.9", + WithPushSource(downloader.Source{ + Oci: &downloader.Oci{ + Reg: "localhost:5001", + Repo: "test/oci_pushed_mod", + Tag: "v9.9.9", + }, }), ) diff --git a/pkg/client/push.go b/pkg/client/push.go index 08559a4a..a48da46e 100644 --- a/pkg/client/push.go +++ b/pkg/client/push.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "kcl-lang.io/kpm/pkg/downloader" "kcl-lang.io/kpm/pkg/errors" "kcl-lang.io/kpm/pkg/oci" "kcl-lang.io/kpm/pkg/opt" @@ -19,28 +20,25 @@ type PushOptions struct { // repository is set to the default repository `kcl-lang` in the settings // reference is set to the package name in `kcl.mod` // tag is set to the package version in `kcl.mod` - OciOpts *opt.OciOptions + Source downloader.Source ModPath string VendorMode bool } type PushOption func(*PushOptions) error -// WithPushVendorMode sets the vendor mode for the Push method. -func WithPushVendorMode(vendorMode bool) PushOption { +// WithPushSource sets the source for the Push method. +func WithPushSource(source downloader.Source) PushOption { return func(opts *PushOptions) error { - opts.VendorMode = vendorMode + opts.Source = source return nil } } -// WithPushOciOptions sets the oci options for the Push method. -func WithPushOciOptions(ociOpts *opt.OciOptions) PushOption { +// WithPushVendorMode sets the vendor mode for the Push method. +func WithPushVendorMode(vendorMode bool) PushOption { return func(opts *PushOptions) error { - if ociOpts == nil { - return fmt.Errorf("ociOpts cannot be nil") - } - opts.OciOpts = ociOpts + opts.VendorMode = vendorMode return nil } } @@ -57,30 +55,17 @@ func WithPushModPath(modPath string) PushOption { } // fillDefaultPushOptions will fill the default values for the PushOptions. -func (c *KpmClient) fillDefaultPushOptions(opts *PushOptions, kMod *pkg.KclPkg) { - if opts.OciOpts == nil { - opts.OciOpts = &opt.OciOptions{ - Reg: c.GetSettings().DefaultOciRegistry(), - Repo: c.GetSettings().DefaultOciRepo(), - Ref: kMod.ModFile.Pkg.Name, - Tag: kMod.ModFile.Pkg.Version, - } - } else { - if opts.OciOpts.Reg == "" { - opts.OciOpts.Reg = c.GetSettings().DefaultOciRegistry() - } - - if opts.OciOpts.Repo == "" { - opts.OciOpts.Repo = c.GetSettings().DefaultOciRepo() - } +func (c *KpmClient) fillDefaultPushOptions(ociOpt *opt.OciOptions, kMod *pkg.KclPkg) { + if ociOpt.Reg == "" { + ociOpt.Reg = c.GetSettings().DefaultOciRegistry() + } - if opts.OciOpts.Ref == "" { - opts.OciOpts.Ref = kMod.ModFile.Pkg.Name - } + if ociOpt.Repo == "" { + ociOpt.Repo = c.GetSettings().DefaultOciRepo() + } - if opts.OciOpts.Tag == "" { - opts.OciOpts.Tag = kMod.ModFile.Pkg.Version - } + if ociOpt.Tag == "" { + ociOpt.Tag = kMod.ModFile.Pkg.Version } } @@ -102,8 +87,28 @@ func (c *KpmClient) Push(opts ...PushOption) error { return err } - c.fillDefaultPushOptions(pushOpts, kMod) - ociOpts := pushOpts.OciOpts + source := pushOpts.Source + ociUrl, err := source.ToString() + if err != nil { + return err + } + if source.Oci == nil { + return fmt.Errorf("'%s' is not an oci source, only support oci source", ociUrl) + } + + var tag string + if source.Oci.Tag == "" { + tag = kMod.ModFile.Pkg.Version + } else { + tag = source.Oci.Tag + } + + ociOpts, err := c.ParseOciOptionFromString(ociUrl, tag) + if err != nil { + return err + } + c.fillDefaultPushOptions(ociOpts, kMod) + ociOpts.Annotations, err = kMod.GenOciManifestFromPkg() if err != nil { return err @@ -130,8 +135,7 @@ func (c *KpmClient) Push(opts ...PushOption) error { // PushToOci will push a kcl package to oci registry. func (c *KpmClient) pushToOci(localPath string, ociOpts *opt.OciOptions) error { - repoPath := utils.JoinPath(ociOpts.Reg, ociOpts.Repo) - repoPath = utils.JoinPath(repoPath, ociOpts.Ref) + repoPath := utils.JoinPath(ociOpts.Reg, ociOpts.Repo, ociOpts.Ref) cred, err := c.GetCredentials(ociOpts.Reg) if err != nil { return err diff --git a/pkg/client/push_test.go b/pkg/client/push_test.go index d360ffec..ff5f6525 100644 --- a/pkg/client/push_test.go +++ b/pkg/client/push_test.go @@ -8,7 +8,6 @@ import ( "github.com/stretchr/testify/assert" "kcl-lang.io/kpm/pkg/downloader" "kcl-lang.io/kpm/pkg/mock" - "kcl-lang.io/kpm/pkg/opt" pkg "kcl-lang.io/kpm/pkg/package" "kcl-lang.io/kpm/pkg/reporter" ) @@ -41,10 +40,14 @@ func TestPush(t *testing.T) { err = kpmcli.Push( WithPushModPath(pushedModPath), - WithPushOciOptions(&opt.OciOptions{ - Reg: "localhost:5001", - Repo: "test", - }), + WithPushSource( + downloader.Source{ + Oci: &downloader.Oci{ + Reg: "localhost:5001", + Repo: "test/push_0", + }, + }, + ), ) if err != (*reporter.KpmEvent)(nil) { diff --git a/pkg/cmd/cmd_push.go b/pkg/cmd/cmd_push.go index fff6661d..51aa7b2e 100644 --- a/pkg/cmd/cmd_push.go +++ b/pkg/cmd/cmd_push.go @@ -11,6 +11,7 @@ import ( "github.com/urfave/cli/v2" "kcl-lang.io/kpm/pkg/client" + "kcl-lang.io/kpm/pkg/downloader" "kcl-lang.io/kpm/pkg/errors" "kcl-lang.io/kpm/pkg/oci" "kcl-lang.io/kpm/pkg/opt" @@ -165,16 +166,18 @@ func pushPackage(ociUrl string, kclPkg *pkg.KclPkg, vendorMode bool, kpmcli *cli ) } - ociOpts.Annotations, err = kclPkg.GenOciManifestFromPkg() - if err != nil { - return err - } - - reporter.ReportMsgTo(fmt.Sprintf("package '%s' will be pushed", kclPkg.GetPkgName()), kpmcli.GetLogWriter()) // Push it. err = kpmcli.Push( client.WithPushModPath(kclPkg.HomePath), - client.WithPushOciOptions(ociOpts), + client.WithPushSource( + downloader.Source{ + Oci: &downloader.Oci{ + Reg: ociOpts.Reg, + Repo: utils.JoinPath(ociOpts.Repo, ociOpts.Ref), + Tag: ociOpts.Tag, + }, + }, + ), client.WithPushVendorMode(vendorMode), ) if err != (*reporter.KpmEvent)(nil) { diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index 78e34e9f..31d184c6 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -7,6 +7,7 @@ import ( "io" "net/url" "os" + gpath "path" "path/filepath" "strings" @@ -467,9 +468,18 @@ func ParseOciUrl(ociUrl string) (*OciOptions, *reporter.KpmEvent) { return nil, reporter.NewEvent(reporter.UrlSchemeNotOci) } + repo, ref := gpath.Split(u.Path) + repo = strings.TrimPrefix(strings.TrimSuffix(repo, "/"), "/") + + if repo == "" { + repo = strings.TrimPrefix(strings.TrimSuffix(u.Path, "/"), "/") + ref = "" + } + return &OciOptions{ Reg: u.Host, - Repo: u.Path, + Repo: repo, + Ref: ref, }, nil } @@ -491,7 +501,7 @@ func (oci *OciOptions) AddStoragePathSuffix(pathPrefix string) string { // SanitizePathSuffix will take 'Registry/Repo/Tag' as a path suffix and sanitize it. func (oci *OciOptions) SanitizePathWithSuffix(pathPrefix string) string { - return path.SanitizePath(filepath.Join(filepath.Join(filepath.Join(pathPrefix, oci.Reg), oci.Repo), oci.Tag)) + return path.SanitizePath(filepath.Join(pathPrefix, oci.Reg, oci.Repo, oci.Ref, oci.Tag)) } type OciManifestOptions struct { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 61508ba9..baa5b1c0 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -552,10 +552,21 @@ func RmNewline(s string) string { } // JoinPath will join the 'elem' to the 'base' with '/'. -func JoinPath(base, elem string) string { - base = strings.TrimSuffix(base, "/") - elem = strings.TrimPrefix(elem, "/") - return base + "/" + elem +func JoinPath(elems ...string) string { + var result string + for i, elem := range elems { + if elem == "" { + continue + } + elem = strings.TrimPrefix(elem, "/") + elem = strings.TrimSuffix(elem, "/") + if i == 0 { + result = elem + } else { + result = result + "/" + elem + } + } + return result } // IsUrl will check whether the string 'str' is a url. diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/kpm_pull_with_oci_url/test_suite.stdout b/test/e2e/test_suites/kpm/exec_outside_pkg/kpm_pull_with_oci_url/test_suite.stdout index b6cf454b..2cc0b57c 100644 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/kpm_pull_with_oci_url/test_suite.stdout +++ b/test/e2e/test_suites/kpm/exec_outside_pkg/kpm_pull_with_oci_url/test_suite.stdout @@ -1,4 +1,4 @@ start to pull 'oci://localhost:5001/test/k8s' the latest version '1.31.2' will be pulled -pulling '/test/k8s:1.31.2' from 'localhost:5001/test/k8s' +pulling 'test/k8s:1.31.2' from 'localhost:5001/test/k8s' pulled 'oci://localhost:5001/test/k8s' in '/localhost:5001/test/k8s/1.31.2' successfully \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/kpm_pull_with_oci_url_tag/test_suite.stdout b/test/e2e/test_suites/kpm/exec_outside_pkg/kpm_pull_with_oci_url_tag/test_suite.stdout index 3656dc86..369fe05d 100644 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/kpm_pull_with_oci_url_tag/test_suite.stdout +++ b/test/e2e/test_suites/kpm/exec_outside_pkg/kpm_pull_with_oci_url_tag/test_suite.stdout @@ -1,3 +1,3 @@ start to pull 'oci://localhost:5001/test/k8s' with tag '1.14' -pulling '/test/k8s:1.14' from 'localhost:5001/test/k8s' +pulling 'test/k8s:1.14' from 'localhost:5001/test/k8s' pulled 'oci://localhost:5001/test/k8s' in '/localhost:5001/test/k8s/1.14' successfully \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/pull_private_pkg/test_suite.stdout b/test/e2e/test_suites/kpm/exec_outside_pkg/pull_private_pkg/test_suite.stdout index 2d980a93..46493f6e 100644 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/pull_private_pkg/test_suite.stdout +++ b/test/e2e/test_suites/kpm/exec_outside_pkg/pull_private_pkg/test_suite.stdout @@ -1,4 +1,4 @@ start to pull 'oci://localhost:5001/test/helloworld' the latest version '0.1.2' will be pulled -pulling '/test/helloworld:0.1.2' from 'localhost:5001/test/helloworld' +pulling 'test/helloworld:0.1.2' from 'localhost:5001/test/helloworld' pulled 'oci://localhost:5001/test/helloworld' in '/localhost:5001/test/helloworld/0.1.2' successfully \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_url/test_suite.stderr b/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_url/test_suite.stderr index 7dfb5555..545b0fd2 100644 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_url/test_suite.stderr +++ b/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_url/test_suite.stderr @@ -1 +1 @@ -repository 'invalid_url/' not found \ No newline at end of file +repository 'invalid_url' not found \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_url_without_tag/test_suite.stderr b/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_url_without_tag/test_suite.stderr index affa8704..de1ddacb 100644 --- a/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_url_without_tag/test_suite.stderr +++ b/test/e2e/test_suites/kpm/exec_outside_pkg/run_oci_with_invalid_url_without_tag/test_suite.stderr @@ -1 +1 @@ -repository 'invalid_url/' not found +repository 'invalid_url' not found