Skip to content

Commit

Permalink
Compute Upstream Lock Ref Correctly (#3073)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmaly authored May 2, 2022
1 parent 7865b26 commit 3705750
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 8 deletions.
4 changes: 2 additions & 2 deletions e2e/testdata/porch/rpkg-clone/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ commands:
upstream:
git:
directory: empty
ref: v1
ref: empty/v1
repo: https://github.com/platkrm/test-blueprints.git
type: git
upstreamLock:
git:
commit: 3de8635354eda8e7de756494a4e0eb5c12af01ab
directory: empty
ref: v1
ref: empty/v1
repo: https://github.com/platkrm/test-blueprints.git
type: git
kind: ResourceList
Expand Down
8 changes: 4 additions & 4 deletions porch/apiserver/pkg/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (t *PorchSuite) TestCloneFromUpstream(ctx context.Context) {
Git: &kptfilev1.GitLock{
Repo: testBlueprintsRepo,
Directory: "basens",
Ref: "v1",
Ref: "basens/v1",
},
}
if !cmp.Equal(want, got) {
Expand All @@ -230,7 +230,7 @@ func (t *PorchSuite) TestCloneFromUpstream(ctx context.Context) {
Git: &kptfilev1.Git{
Repo: testBlueprintsRepo,
Directory: "basens",
Ref: "v1",
Ref: "basens/v1",
},
}); !cmp.Equal(want, got) {
t.Errorf("unexpected upstream returned (-want, +got) %s", cmp.Diff(want, got))
Expand Down Expand Up @@ -423,7 +423,7 @@ func (t *PorchSuite) TestCloneIntoDeploymentRepository(ctx context.Context) {
Git: &kptfilev1.GitLock{
Repo: testBlueprintsRepo,
Directory: "basens",
Ref: "v1",
Ref: "basens/v1",
},
}
if !cmp.Equal(want, got) {
Expand All @@ -436,7 +436,7 @@ func (t *PorchSuite) TestCloneIntoDeploymentRepository(ctx context.Context) {
Git: &kptfilev1.Git{
Repo: testBlueprintsRepo,
Directory: "basens",
Ref: "v1",
Ref: "basens/v1",
},
}); !cmp.Equal(want, got) {
t.Errorf("unexpected upstream returned (-want, +got) %s", cmp.Diff(want, got))
Expand Down
13 changes: 11 additions & 2 deletions porch/repository/pkg/git/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,28 @@ func (p *gitPackageRevision) GetUpstreamLock() (kptfile.Upstream, kptfile.Upstre
return kptfile.Upstream{}, kptfile.UpstreamLock{}, fmt.Errorf("cannot determine package lock: %w", err)
}

if p.ref == nil {
return kptfile.Upstream{}, kptfile.UpstreamLock{}, fmt.Errorf("cannot determine package lock; package has no ref")
}

ref, err := refInRemoteFromRefInLocal(p.ref.Name())
if err != nil {
return kptfile.Upstream{}, kptfile.UpstreamLock{}, fmt.Errorf("cannot determine package lock for %q: %v", p.ref, err)
}

return kptfile.Upstream{
Type: kptfile.GitOrigin,
Git: &kptfile.Git{
Repo: repo,
Directory: p.path,
Ref: p.revision,
Ref: ref.Short(),
},
}, kptfile.UpstreamLock{
Type: kptfile.GitOrigin,
Git: &kptfile.GitLock{
Repo: repo,
Directory: p.path,
Ref: p.revision,
Ref: ref.Short(),
Commit: p.commit.String(),
},
}, nil
Expand Down
126 changes: 126 additions & 0 deletions porch/repository/pkg/git/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package git

import (
"context"
"path/filepath"
"testing"

v1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
configapi "github.com/GoogleContainerTools/kpt/porch/api/porchconfig/v1alpha1"
"github.com/GoogleContainerTools/kpt/porch/repository/pkg/repository"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/go-cmp/cmp"
)

func (g GitSuite) TestUpstreamLock(t *testing.T) {
tempdir := t.TempDir()
tarfile := filepath.Join("testdata", "drafts-repository.tar")
repo, address := ServeGitRepositoryWithBranch(t, tarfile, tempdir, g.branch)

ctx := context.Background()
const (
repositoryName = "lock"
namespace = "default"
)

git, err := OpenRepository(ctx, repositoryName, namespace, &configapi.GitRepository{
Repo: address,
Branch: g.branch,
Directory: "/",
}, tempdir, GitRepositoryOptions{})
if err != nil {
t.Fatalf("Failed to open Git repository loaded from %q: %v", tarfile, err)
}

revisions, err := git.ListPackageRevisions(ctx, repository.ListPackageRevisionFilter{})
if err != nil {
t.Fatalf("Failed to list packages from %q: %v", tarfile, err)
}

wantRefs := map[repository.PackageRevisionKey]string{
{Repository: repositoryName, Package: "empty", Revision: "v1"}: "empty/v1",
{Repository: repositoryName, Package: "basens", Revision: "v1"}: "basens/v1",
{Repository: repositoryName, Package: "basens", Revision: "v2"}: "basens/v2",
{Repository: repositoryName, Package: "istions", Revision: "v1"}: "istions/v1",
{Repository: repositoryName, Package: "istions", Revision: "v2"}: "istions/v2",

{Repository: repositoryName, Package: "basens", Revision: g.branch}: g.branch,
{Repository: repositoryName, Package: "empty", Revision: g.branch}: g.branch,
{Repository: repositoryName, Package: "istions", Revision: g.branch}: g.branch,
}

for _, rev := range revisions {
if rev.Lifecycle() != v1alpha1.PackageRevisionLifecyclePublished {
continue
}

upstream, lock, err := rev.GetUpstreamLock()
if err != nil {
t.Errorf("GetUpstreamLock(%q) failed: %v", rev.Key(), err)
}
if got, want := upstream.Type, v1.GitOrigin; got != want {
t.Errorf("upstream.Type: got %s, want %s", got, want)
}
if got, want := lock.Type, v1.GitOrigin; got != want {
t.Errorf("lock.Type: got %s, want %s", got, want)
}

key := rev.Key()
wantRef, ok := wantRefs[key]
if !ok {
t.Errorf("Unexpected package found; %q", rev.Key())
}

type gitAddress struct {
Repo, Directory, Ref string
}

// Check upstream values
if got, want := (gitAddress{
Repo: upstream.Git.Repo,
Directory: upstream.Git.Directory,
Ref: upstream.Git.Ref,
}), (gitAddress{
Repo: address,
Directory: key.Package,
Ref: wantRef,
}); !cmp.Equal(want, got) {
t.Errorf("Package upstream differs (-want,+got): %s", cmp.Diff(want, got))
}

// Check upstream lock values
if got, want := (gitAddress{
Repo: lock.Git.Repo,
Directory: lock.Git.Directory,
Ref: lock.Git.Ref,
}), (gitAddress{
Repo: address,
Directory: key.Package,
Ref: wantRef,
}); !cmp.Equal(want, got) {
t.Errorf("Package upstream lock differs (-want,+got): %s", cmp.Diff(want, got))
}

// Check the commit
if commit, err := repo.ResolveRevision(plumbing.Revision(wantRef)); err != nil {
t.Errorf("ResolveRevision(%q) failed: %v", wantRef, err)
} else if got, want := lock.Git.Commit, commit.String(); got != want {
t.Errorf("Commit: got %s, want %s", got, want)
}
}
}

0 comments on commit 3705750

Please sign in to comment.