Skip to content

Commit

Permalink
Merge pull request #393 from zong-zhe/fix-push-failure
Browse files Browse the repository at this point in the history
fix: fix push error
  • Loading branch information
Peefy authored Jul 23, 2024
2 parents 4d2ae6e + 6bb0136 commit 1b13f62
Show file tree
Hide file tree
Showing 30 changed files with 150 additions and 25 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ jobs:
with:
go-version: 1.22

- name: Prepare the test oci registry
run: |
./scripts/reg.sh
./scripts/e2e_prepare.sh
- name: Login to ghcr.io
run: |
./bin/kpm login -u ${{ secrets.DEPLOY_ACCESS_NAME }} -p ${{ secrets.DEPLOY_ACCESS_TOKEN }} ghcr.io
- name: Login to docker.io
run: |
./bin/kpm login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} docker.io
- name: run e2e
run: |
make e2e
1 change: 0 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ jobs:

- run: go build ./...
- run: go vet ./...

- name: Running go tests with coverage
env:
GO111MODULE: on
Expand Down
10 changes: 5 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ func (c *KpmClient) AcquireTheLatestOciVersion(ociSource downloader.Oci) (string
ociClient, err := oci.NewOciClientWithOpts(
oci.WithCredential(cred),
oci.WithRepoPath(repoPath),
oci.WithPlainHttp(c.GetSettings().DefaultOciPlainHttp()),
oci.WithSettings(c.GetSettings()),
)

if err != nil {
Expand Down Expand Up @@ -1359,7 +1359,7 @@ func (c *KpmClient) DownloadPkgFromOci(dep *downloader.Oci, localPath string) (*
ociClient, err := oci.NewOciClientWithOpts(
oci.WithCredential(cred),
oci.WithRepoPath(repoPath),
oci.WithPlainHttp(c.GetSettings().DefaultOciPlainHttp()),
oci.WithSettings(c.GetSettings()),
)

if err != nil {
Expand Down Expand Up @@ -1573,7 +1573,7 @@ func (c *KpmClient) PushToOci(localPath string, ociOpts *opt.OciOptions) error {
ociCli, err := oci.NewOciClientWithOpts(
oci.WithCredential(cred),
oci.WithRepoPath(repoPath),
oci.WithPlainHttp(c.GetSettings().DefaultOciPlainHttp()),
oci.WithSettings(c.GetSettings()),
)

if err != nil {
Expand Down Expand Up @@ -1893,7 +1893,7 @@ func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) er
ociCli, err := oci.NewOciClientWithOpts(
oci.WithCredential(cred),
oci.WithRepoPath(repoPath),
oci.WithPlainHttp(c.GetSettings().DefaultOciPlainHttp()),
oci.WithSettings(c.GetSettings()),
)

if err != nil {
Expand Down Expand Up @@ -1942,7 +1942,7 @@ func (c *KpmClient) FetchOciManifestIntoJsonStr(opts opt.OciFetchOptions) (strin
ociCli, err := oci.NewOciClientWithOpts(
oci.WithCredential(cred),
oci.WithRepoPath(repoPath),
oci.WithPlainHttp(c.GetSettings().DefaultOciPlainHttp()),
oci.WithSettings(c.GetSettings()),
)

if err != nil {
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (d *OciDownloader) Download(opts DownloadOptions) error {
ociCli, err := oci.NewOciClientWithOpts(
oci.WithCredential(cred),
oci.WithRepoPath(repoPath),
oci.WithPlainHttp(opts.Settings.DefaultOciPlainHttp()),
oci.WithSettings(&opts.Settings),
)

if err != nil {
Expand Down
55 changes: 41 additions & 14 deletions pkg/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"log"
"net"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -96,13 +97,23 @@ type OciClient struct {
repo *remote.Repository
ctx *context.Context
logWriter io.Writer
settings *settings.Settings
isPlainHttp *bool
cred *remoteauth.Credential
PullOciOptions *PullOciOptions
}

// OciClientOption configures how we set up the OciClient
type OciClientOption func(*OciClient) error

// WithSettings sets the kpm settings of the OciClient
func WithSettings(settings *settings.Settings) OciClientOption {
return func(c *OciClient) error {
c.settings = settings
return nil
}
}

// WithRepoPath sets the repo path of the OciClient
func WithRepoPath(repoPath string) OciClientOption {
return func(c *OciClient) error {
Expand All @@ -126,10 +137,7 @@ func WithCredential(credential *remoteauth.Credential) OciClientOption {
// WithPlainHttp sets the plain http of the OciClient
func WithPlainHttp(plainHttp bool) OciClientOption {
return func(c *OciClient) error {
if c.repo == nil {
return fmt.Errorf("repo is nil")
}
c.repo.PlainHTTP = plainHttp
c.isPlainHttp = &plainHttp
return nil
}
}
Expand Down Expand Up @@ -164,17 +172,36 @@ func NewOciClientWithOpts(opts ...OciClientOption) (*OciClient, error) {
Credential: remoteauth.StaticCredential(client.repo.Reference.Host(), *client.cred),
}

return &OciClient{
repo: client.repo,
ctx: &ctx,
PullOciOptions: &PullOciOptions{
CopyOpts: &oras.CopyOptions{
CopyGraphOptions: oras.CopyGraphOptions{
MaxMetadataBytes: DEFAULT_LIMIT_STORE_SIZE, // default is 64 MiB
},
// If the plain http is not specified
if client.isPlainHttp == nil {
// Set the default value of the plain http
registry := client.repo.Reference.String()
host, _, _ := net.SplitHostPort(registry)
if host == "localhost" || registry == "localhost" {
// not specified, defaults to plain http for localhost
client.repo.PlainHTTP = true
}

// If the plain http is specified in the settings file
// Override the default value of the plain http
if client.settings != nil {
isPlainHttp, force := client.settings.ForceOciPlainHttp()
if force {
client.repo.PlainHTTP = isPlainHttp
}
}
}

client.ctx = &ctx
client.PullOciOptions = &PullOciOptions{
CopyOpts: &oras.CopyOptions{
CopyGraphOptions: oras.CopyGraphOptions{
MaxMetadataBytes: DEFAULT_LIMIT_STORE_SIZE, // default is 64 MiB
},
},
}, nil
}

return client, nil
}

// NewOciClient will new an OciClient.
Expand All @@ -195,7 +222,7 @@ func NewOciClient(regName, repoName string, settings *settings.Settings) (*OciCl
return NewOciClientWithOpts(
WithRepoPath(utils.JoinPath(regName, repoName)),
WithCredential(credential),
WithPlainHttp(settings.DefaultOciPlainHttp()),
WithSettings(settings),
)
}

Expand Down
4 changes: 0 additions & 4 deletions scripts/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ set -o pipefail
# Install ginkgo
GO111MODULE=on go install github.com/onsi/ginkgo/v2/[email protected]

# Build kpm binary
LDFLAGS="-X kcl-lang.io/kpm/pkg/version.version=test_version"
go build -ldflags "$LDFLAGS" -o ./bin/kpm

# Prepare e2e test env
./scripts/e2e_prepare.sh

Expand Down
4 changes: 4 additions & 0 deletions scripts/e2e_prepare.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

# Build kpm binary
LDFLAGS="-X kcl-lang.io/kpm/pkg/version.version=test_version"
go build -ldflags "$LDFLAGS" -o ./bin/kpm

# Check kpm version
version=$(./bin/kpm --version)
if ! echo "$version" | grep -q "kpm version test_version"; then
Expand Down
50 changes: 50 additions & 0 deletions test/e2e/kpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package e2e
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
Expand Down Expand Up @@ -316,4 +318,52 @@ var _ = ginkgo.Describe("Kpm CLI Testing", func() {
})
}
})

ginkgo.Context("testing 'test push '", func() {
testSuitesRoot := filepath.Join(filepath.Join(filepath.Join(GetWorkDir(), TEST_SUITES_DIR), "kpm"), "kpm_push")
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))

tag := fmt.Sprintf("test-%d", time.Now().Unix())
kpmcli, err := client.NewKpmClient()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
fmt.Printf("ts.Name: %v\n", ts.Name)
kpkg, err := kpmcli.LoadPkgFromPath(filepath.Join(workspace, ts.Name))
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

kpkg.ModFile.Pkg.Version = tag
kpkg.ModFile.Pkg.Name = "helloworld"

kpkg.HomePath = filepath.Join(workspace, ts.Name, "helloworld")
kpkg.ModFile.HomePath = kpkg.HomePath
err = os.MkdirAll(kpkg.HomePath, os.ModePerm)
fmt.Printf("kpkg.HomePath: %v\n", kpkg.HomePath)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = kpmcli.InitEmptyPkg(kpkg)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

// Init a package with tag create by time
input := ReplaceAllKeyByValue(ts.Input, "<workspace>", filepath.Join(workspace, ts.Name))
stdout, stderr, err := ExecKpmWithWorkDir(input, kpkg.HomePath)
expectedStdout := ReplaceAllKeyByValue(ts.ExpectStdout, "<workspace>", workspace)
expectedStderr := ReplaceAllKeyByValue(ts.ExpectStderr, "<workspace>", workspace)

gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
if !IsIgnore(expectedStdout) {
gomega.Expect(stdout).To(gomega.ContainSubstring(expectedStdout))
}
if !IsIgnore(expectedStderr) {
gomega.Expect(stderr).To(gomega.ContainSubstring(expectedStderr))
}

gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
}
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm push oci://docker.io/kcllang/helloworld
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package 'helloworld' will be pushed
pushed [registry] docker.io/kcllang/helloworld
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm push oci://ghcr.io/kcl-lang/helloworld
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package 'helloworld' will be pushed
pushed [registry] ghcr.io/kcl-lang/helloworld
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm push oci://localhost:5001/test/helloworld
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package 'helloworld' will be pushed
pushed [registry] localhost:5001/test/helloworld
6 changes: 6 additions & 0 deletions test/e2e/test_suites/test_data/test_push_docker/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "helloworld"
edition = "v0.9.0"
version = "0.0.1"


Empty file.
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_data/test_push_docker/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
6 changes: 6 additions & 0 deletions test/e2e/test_suites/test_data/test_push_ghcr/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "helloworld"
edition = "v0.9.0"
version = "0.0.1"


Empty file.
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_data/test_push_ghcr/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
6 changes: 6 additions & 0 deletions test/e2e/test_suites/test_data/test_push_localhost/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "helloworld"
edition = "v0.9.0"
version = "0.0.1"


Empty file.
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_data/test_push_localhost/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'

0 comments on commit 1b13f62

Please sign in to comment.