Skip to content

Commit

Permalink
fix: fix missing tag in command push (#561)
Browse files Browse the repository at this point in the history
* fix: fix missing tag in command push

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: fix test ci

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: fix test ci

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: fix test ci

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: fix test TestModCheckerCheck_WithTrustedSum

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: fix test ci

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: fix unit test TestModCheckerCheck_WithTrustedSum

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: fix unit test TestJoinPath

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: fix unit test TestParseOciOptionFromString

Signed-off-by: zongz <zongzhe1024@163.com>

* fix: fix windows test case

Signed-off-by: zongz <zongzhe1024@163.com>

---------

Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe authored Dec 4, 2024
1 parent 5f0a751 commit 9be5efb
Show file tree
Hide file tree
Showing 31 changed files with 582 additions and 152 deletions.
207 changes: 106 additions & 101 deletions pkg/checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/settings"
"kcl-lang.io/kpm/pkg/test"
)

func TestModCheckerCheck(t *testing.T) {
Expand Down Expand Up @@ -145,126 +146,130 @@ func getTestSettings() (*settings.Settings, error) {
}

func TestModCheckerCheck_WithTrustedSum(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping TestModCheckerCheck_WithTrustedSum test on Windows")
}
testFunc := func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping TestModCheckerCheck_WithTrustedSum test on Windows")
}

// Start the local Docker registry required for testing
err := mock.StartDockerRegistry()
assert.Equal(t, err, nil)
// Start the local Docker registry required for testing
err := mock.StartDockerRegistry()
assert.Equal(t, err, nil)

// Push the test package to the local OCI registry
err = mock.PushTestPkgToRegistry()
assert.Equal(t, err, nil)
// Push the test package to the local OCI registry
err = mock.PushTestPkgToRegistry()
assert.Equal(t, err, nil)

// Initialize settings for use with the ModChecker
settings, err := getTestSettings()
assert.Equal(t, err, nil)
// Initialize settings for use with the ModChecker
settings, err := getTestSettings()
assert.Equal(t, err, nil)

// Initialize the ModChecker with required checkers
ModChecker := NewModChecker(WithCheckers(NewIdentChecker(), NewVersionChecker(), NewSumChecker(WithSettings(*settings))))
// Initialize the ModChecker with required checkers
ModChecker := NewModChecker(WithCheckers(NewIdentChecker(), NewVersionChecker(), NewSumChecker(WithSettings(*settings))))

deps1 := orderedmap.NewOrderedMap[string, pkg.Dependency]()
deps1.Set("kcl1", pkg.Dependency{
Name: "test_data",
FullName: "test_data",
Version: "0.0.1",
Sum: "RpZZIvrXwfn5dpt6LqBR8+FlPE9Y+BEou47L3qaCCqk=",
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "localhost:5001",
Repo: "test",
Tag: "0.0.1",
deps1 := orderedmap.NewOrderedMap[string, pkg.Dependency]()
deps1.Set("kcl1", pkg.Dependency{
Name: "test_data",
FullName: "test_data",
Version: "0.0.1",
Sum: "RpZZIvrXwfn5dpt6LqBR8+FlPE9Y+BEou47L3qaCCqk=",
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "localhost:5001",
Repo: "test/test_data",
Tag: "0.0.1",
},
},
},
})
})

deps2 := orderedmap.NewOrderedMap[string, pkg.Dependency]()
deps2.Set("kcl1", pkg.Dependency{
Name: "test_data",
FullName: "test_data",
Version: "0.0.1",
Sum: "Invalid-sum",
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "localhost:5001",
Repo: "test",
Tag: "0.0.1",
deps2 := orderedmap.NewOrderedMap[string, pkg.Dependency]()
deps2.Set("kcl1", pkg.Dependency{
Name: "test_data",
FullName: "test_data",
Version: "0.0.1",
Sum: "Invalid-sum",
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "localhost:5001",
Repo: "test/test_data",
Tag: "0.0.1",
},
},
},
})
})

tests := []struct {
name string
KclPkg pkg.KclPkg
wantErr bool
}{
{
name: "valid kcl package - with sum check",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
tests := []struct {
name string
KclPkg pkg.KclPkg
wantErr bool
}{
{
name: "valid kcl package - with sum check",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps1,
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps1,
},
NoSumCheck: false,
},
NoSumCheck: false,
wantErr: false,
},
wantErr: false,
},
{
name: "valid kcl package - with no sum check enabled",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
{
name: "valid kcl package - with no sum check enabled",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps2,
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps2,
},
NoSumCheck: true,
},
NoSumCheck: true,
wantErr: false,
},
wantErr: false,
},
{
name: "Invalid kcl package - with no sum check disabled - checksum mismatches",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
{
name: "Invalid kcl package - with no sum check disabled - checksum mismatches",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps2,
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps2,
},
NoSumCheck: false,
},
NoSumCheck: false,
wantErr: true,
},
wantErr: true,
},
}
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotErr := ModChecker.Check(tt.KclPkg)
if (gotErr != nil) != tt.wantErr {
t.Errorf("ModChecker.Check(%v) = %v, want error %v", tt.KclPkg, gotErr, tt.wantErr)
}
})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotErr := ModChecker.Check(tt.KclPkg)
if (gotErr != nil) != tt.wantErr {
t.Errorf("ModChecker.Check(%v) = %v, want error %v", tt.KclPkg, gotErr, tt.wantErr)
}
})
}

// Clean the environment after all tests have been run
err = mock.CleanTestEnv()
assert.Equal(t, err, nil)
}

// Clean the environment after all tests have been run
err = mock.CleanTestEnv()
assert.Equal(t, err, nil)
test.RunTestWithGlobalLock(t, "TestModCheckerCheck_WithTrustedSum", testFunc)
}
6 changes: 3 additions & 3 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ func TestParseOciOptionFromString(t *testing.T) {
assert.Equal(t, err, nil)
assert.Equal(t, ociOption.Ref, "")
assert.Equal(t, ociOption.Reg, "test_reg")
assert.Equal(t, ociOption.Repo, "/test_oci_repo")
assert.Equal(t, ociOption.Repo, "test_oci_repo")
assert.Equal(t, ociOption.Tag, "test_tag")
}

Expand Down Expand Up @@ -2255,12 +2255,12 @@ func testPushWithInsecureSkipTLSverify(t *testing.T) {
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

_ = kpmcli.PushToOci("test", ociOpts)
_ = kpmcli.pushToOci("test", ociOpts)

assert.Equal(t, buf.String(), "")

kpmcli.SetInsecureSkipTLSverify(true)
_ = kpmcli.PushToOci("test", ociOpts)
_ = kpmcli.pushToOci("test", ociOpts)

assert.Equal(t, buf.String(), "Called Success\n")
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/client/deperated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
)

Expand Down
Loading

0 comments on commit 9be5efb

Please sign in to comment.