Skip to content

Commit

Permalink
test(bundles): test remote to remote bundle copy
Browse files Browse the repository at this point in the history
Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Sep 12, 2022
1 parent bdc1721 commit eaa6d58
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 11 deletions.
14 changes: 4 additions & 10 deletions cmd/werf/bundle/publish/publish.go
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"
"time"

"github.com/Masterminds/semver"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
helm_v3 "helm.sh/helm/v3/cmd/helm"
Expand All @@ -26,7 +25,6 @@ import (
"github.com/werf/werf/pkg/git_repo"
"github.com/werf/werf/pkg/git_repo/gitdata"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/slug"
"github.com/werf/werf/pkg/ssh_agent"
"github.com/werf/werf/pkg/storage/lrumeta"
"github.com/werf/werf/pkg/storage/manager"
Expand Down Expand Up @@ -371,15 +369,11 @@ func runPublish(ctx context.Context) error {
FileValues: *commonCmdData.SetFile,
}

chartVersion := cmdData.Tag
if _, err := semver.NewVersion(chartVersion); err != nil {
chartVersion = fmt.Sprintf("0.0.0-%d-%s", time.Now().Unix(), slug.Slug(chartVersion))
if _, err := semver.NewVersion(chartVersion); err != nil {
fallbackChartVersion := fmt.Sprintf("0.0.0-%d", time.Now().Unix())
logboek.Context(ctx).Warn().LogF("Unable to use %q as chart version, will fallback on chart version %q\n", chartVersion, fallbackChartVersion)
chartVersion = fallbackChartVersion
}
sv, err := bundles.BundleTagToChartVersion(ctx, cmdData.Tag, time.Now())
if err != nil {
return fmt.Errorf("unable to set chart version from bundle tag %q: %w", cmdData.Tag, err)
}
chartVersion := sv.String()

bundleTmpDir := filepath.Join(werf.GetServiceDir(), "tmp", "bundles", uuid.NewV4().String())
defer os.RemoveAll(bundleTmpDir)
Expand Down
37 changes: 37 additions & 0 deletions pkg/deploy/bundles/bundle_tag.go
@@ -0,0 +1,37 @@
package bundles

import (
"context"
"fmt"
"time"

"github.com/Masterminds/semver"

"github.com/werf/logboek"
"github.com/werf/werf/pkg/slug"
)

func BundleTagToChartVersion(ctx context.Context, tag string, now time.Time) (*semver.Version, error) {
sv, err := semver.NewVersion(tag)
if err == nil {
return sv, nil
}

// TODO: come up with a better idea for generating reproducible, consistent and monotonously increasing semver

tsWithTagVersion := fmt.Sprintf("0.0.0-%d-%s", now.Unix(), slug.Slug(tag))
sv, err = semver.NewVersion(tsWithTagVersion)
if err == nil {
return sv, nil
}

fallbackVersion := fmt.Sprintf("0.0.0-%d", now.Unix())
logboek.Context(ctx).Warn().LogF("Unable to use %q as chart version, will fallback on chart version %q\n", tsWithTagVersion, fallbackVersion)

sv, err = semver.NewVersion(fallbackVersion)
if err != nil {
return nil, fmt.Errorf("unable to use fallback chart version %q: %w", fallbackVersion, err)
}

return sv, nil
}
41 changes: 41 additions & 0 deletions pkg/deploy/bundles/bundle_tag_test.go
@@ -0,0 +1,41 @@
package bundles

import (
"context"
"fmt"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("BundleTagToSemver", func() {
It("converts arbitrary tag to semver if tag not already semver", func() {
ctx := context.Background()
now := time.Now()

{
sv, err := BundleTagToChartVersion(ctx, "latest", now)
Expect(err).To(BeNil())
Expect(sv.String()).To(Equal(fmt.Sprintf("0.0.0-%d-latest", now.Unix())))
}

{
sv, err := BundleTagToChartVersion(ctx, "my-branch/ABC", now)
Expect(err).To(BeNil())
Expect(sv.String()).To(Equal(fmt.Sprintf("0.0.0-%d-my-branch-abc", now.Unix())))
}

{
sv, err := BundleTagToChartVersion(ctx, "1.24.425-prerelease11", now)
Expect(err).To(BeNil())
Expect(sv.String()).To(Equal("1.24.425-prerelease11"))
}

{
sv, err := BundleTagToChartVersion(ctx, "0.2.10", now)
Expect(err).To(BeNil())
Expect(sv.String()).To(Equal("0.2.10"))
}
})
})
87 changes: 87 additions & 0 deletions pkg/deploy/bundles/copy_test.go
Expand Up @@ -236,6 +236,93 @@ werf:
Expect(origImages[imageName]).To(Equal(v))
}
Expect(newRepo).To(Equal(origRepo))

// TODO: check copied images?
})

It("should copy remote to archive", func() {
ctx := context.Background()

ch := &chart.Chart{
Metadata: &chart.Metadata{
APIVersion: "v2",
Name: "testproject",
Version: "1.2.3",
Type: "application",
},
Values: map[string]interface{}{
"werf": map[string]interface{}{
"image": map[string]interface{}{
"image-1": "registry.example.com/group/testproject:tag-1",
"image-2": "registry.example.com/group/testproject:tag-2",
"image-3": "registry.example.com/group/testproject:tag-3",
},
"repo": "registry.example.com/group/testproject",
},
},
Files: []*chart.File{
{
Name: "values.yaml",
Data: []byte(`
werf:
image:
image-1: registry.example.com/group/testproject:tag-1
image-2: registry.example.com/group/testproject:tag-2
image-3: registry.example.com/group/testproject:tag-3
repo: registry.example.com/group/testproject
`),
},
},
}

bundlesRegistryClient := NewBundlesRegistryClientStub()
registryClient := NewDockerRegistryStub()

fromAddr, err := ParseAddr("registry.example.com/group/testproject:1.2.3")
Expect(err).NotTo(HaveOccurred())
from := NewRemoteBundle(fromAddr.RegistryAddress, bundlesRegistryClient, registryClient)
bundlesRegistryClient.StubCharts[fromAddr.RegistryAddress.FullName()] = ch
registryClient.RemoteImages["registry.example.com/group/testproject:tag-1"] = []byte(`image-1-bytes`)
registryClient.RemoteImages["registry.example.com/group/testproject:tag-2"] = []byte(`image-2-bytes`)
registryClient.RemoteImages["registry.example.com/group/testproject:tag-3"] = []byte(`image-3-bytes`)

toAddr, err := ParseAddr("registry2.example.com/group2/testproject2:4.5.6")
Expect(err).NotTo(HaveOccurred())
to := NewRemoteBundle(toAddr.RegistryAddress, bundlesRegistryClient, registryClient)

Expect(from.CopyTo(ctx, to)).To(Succeed())

newCh := bundlesRegistryClient.StubCharts[toAddr.RegistryAddress.FullName()]
Expect(newCh).NotTo(BeNil())

{
Expect(newCh.Metadata.Name).To(Equal("testproject2"))
Expect(newCh.Metadata.Version).To(Equal("4.5.6"))
}

{
origImages := ch.Values["werf"].(map[string]interface{})["image"].(map[string]interface{})
newImages := newCh.Values["werf"].(map[string]interface{})["image"].(map[string]interface{})

for imageName := range origImages {
Expect(newImages[imageName]).NotTo(BeNil())
}
for imageName := range newImages {
Expect(origImages[imageName]).NotTo(BeNil())
}
Expect(newImages["image-1"]).To(Equal("registry2.example.com/group2/testproject2:tag-1"))
Expect(newImages["image-2"]).To(Equal("registry2.example.com/group2/testproject2:tag-2"))
Expect(newImages["image-3"]).To(Equal("registry2.example.com/group2/testproject2:tag-3"))

newRepo := newCh.Values["werf"].(map[string]interface{})["repo"].(string)
Expect(newRepo).To(Equal("registry2.example.com/group2/testproject2"))
}

{
Expect(registryClient.RemoteImages["registry2.example.com/group2/testproject2:tag-1"]).To(Equal([]byte(`image-1-bytes`)))
Expect(registryClient.RemoteImages["registry2.example.com/group2/testproject2:tag-2"]).To(Equal([]byte(`image-2-bytes`)))
Expect(registryClient.RemoteImages["registry2.example.com/group2/testproject2:tag-3"]).To(Equal([]byte(`image-3-bytes`)))
}
})
})

Expand Down
14 changes: 13 additions & 1 deletion pkg/deploy/bundles/remote_bundle.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"

v1 "github.com/google/go-containerregistry/pkg/v1"
"helm.sh/helm/v3/pkg/chart"
Expand Down Expand Up @@ -128,7 +129,12 @@ func (bundle *RemoteBundle) CopyFromArchive(ctx context.Context, fromArchive *Bu
}

ch.Metadata.Name = util.Reverse(strings.SplitN(util.Reverse(bundle.RegistryAddress.Repo), "/", 2)[0])
ch.Metadata.Version = bundle.RegistryAddress.Tag

sv, err := BundleTagToChartVersion(ctx, bundle.RegistryAddress.Tag, time.Now())
if err != nil {
return fmt.Errorf("unable to set chart version from bundle tag %q: %w", bundle.RegistryAddress.Tag, err)
}
ch.Metadata.Version = sv.String()

if err := bundle.WriteChart(ctx, ch); err != nil {
return fmt.Errorf("unable to write chart to remote bundle: %w", err)
Expand Down Expand Up @@ -203,6 +209,12 @@ func (bundle *RemoteBundle) CopyFromRemote(ctx context.Context, fromRemote *Remo

ch.Metadata.Name = util.Reverse(strings.SplitN(util.Reverse(bundle.RegistryAddress.Repo), "/", 2)[0])

sv, err := BundleTagToChartVersion(ctx, bundle.RegistryAddress.Tag, time.Now())
if err != nil {
return fmt.Errorf("unable to set chart version from bundle tag %q: %w", bundle.RegistryAddress.Tag, err)
}
ch.Metadata.Version = sv.String()

if err := bundle.WriteChart(ctx, ch); err != nil {
return fmt.Errorf("unable to write chart to destination remote bundle: %w", err)
}
Expand Down

0 comments on commit eaa6d58

Please sign in to comment.