Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep overrides to repository targets even with same host #66

Merged
merged 2 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions internal/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ func (m Manifest) Update(images []string) Manifest {
//
// To preserve the current settings, set the manifest host and repository values
// to the ones present in the current manifest.
if foundSource.Target.Host != m.Target.Host {
updatedSource.Target.Host = foundSource.Target.Host
}
if foundSource.Target.Repository != m.Target.Repository {
updatedSource.Target.Repository = foundSource.Target.Repository
if foundSource.Target.Host != m.Target.Host ||
foundSource.Target.Repository != m.Target.Repository {
updatedSource.Target = foundSource.Target
}

updatedSources = append(updatedSources, updatedSource)
Expand Down
160 changes: 160 additions & 0 deletions internal/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manifest
import (
"encoding/base64"
"os"
"reflect"
"testing"
)

Expand Down Expand Up @@ -280,3 +281,162 @@ func TestSource_TargetDoesNotSupportNestedRepositories_MultiplePaths(t *testing.
t.Errorf("unexpected target string. expected %s, actual %s", expectedTarget, source.TargetImage())
}
}

func TestManifest_Update(t *testing.T) {
base := Manifest{
Target: Target{
Host: "mycr.com",
Repository: "",
},
}

testCases := []struct {
desc string
existingManifest Manifest
input []string
expected Manifest
}{
{
desc: "replaces tags with latest version without repository",
input: []string{"mycr.com/foo/bar:1.2.3"},
existingManifest: base,
expected: Manifest{
Target: base.Target,
Sources: []Source{
{
Repository: "foo/bar",
Tag: "1.2.3",
},
},
},
},
{
desc: "replaces tags with latest version with repository",
input: []string{"mycr.com/foo/bar:1.2.3"},
existingManifest: Manifest{
Target: Target{
Host: base.Target.Host,
Repository: "foo",
},
},
expected: Manifest{
Target: Target{
Host: "mycr.com",
Repository: "foo",
},
Sources: []Source{
{
Repository: "bar",
Tag: "1.2.3",
},
},
},
},
{
desc: "preserves source specific host overrides from manifest",
input: []string{"myothercr.com/foo/bar:1.2.3"},
existingManifest: Manifest{
Target: base.Target,
Sources: []Source{
{
Repository: "foo/bar",
Tag: "1.0.0",
Target: Target{
Host: "myothercr.com",
},
},
},
},
expected: Manifest{
Target: base.Target,
Sources: []Source{
{
Repository: "foo/bar",
Tag: "1.2.3",
Target: Target{
Host: "myothercr.com",
},
},
},
},
},
{
desc: "omits target with matching host and repository",
input: []string{"mycr.com/foo/bar:1.2.3"},
existingManifest: Manifest{
Target: Target{
Host: "mycr.com",
Repository: "foo",
},
Sources: []Source{
{
Repository: "bar",
Tag: "1.0.0",
Target: Target{
Host: "mycr.com",
Repository: "foo",
},
},
},
},
expected: Manifest{
Target: Target{
Host: "mycr.com",
Repository: "foo",
},
Sources: []Source{
{
Repository: "bar",
Tag: "1.2.3",
Target: Target{},
},
},
},
},
{
desc: "includes target with matching host but different repository",
input: []string{"mycr.com/foo/bar:1.2.3"},
existingManifest: Manifest{
Target: Target{
Host: "mycr.com",
Repository: "",
},
Sources: []Source{
{
Repository: "bar",
Tag: "1.0.0",
Target: Target{
Host: "mycr.com",
Repository: "foo",
},
},
},
},
expected: Manifest{
Target: Target{
Host: "mycr.com",
Repository: "",
},
Sources: []Source{
{
Repository: "bar",
Tag: "1.2.3",
Target: Target{
Host: "mycr.com",
Repository: "foo",
},
},
},
},
},
}

for _, testCase := range testCases {
t.Run(testCase.desc, func(t *testing.T) {
result := testCase.existingManifest.Update(testCase.input)
if !reflect.DeepEqual(result, testCase.expected) {
t.Errorf("expected '%v' got '%v'", testCase.expected, result)
}
})
}
}