Skip to content

Commit

Permalink
Add remaining tags
Browse files Browse the repository at this point in the history
Signed-off-by: Kim Christensen <[email protected]>
  • Loading branch information
kichristensen committed Oct 6, 2024
1 parent 1bf0c9e commit f535765
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 55 deletions.
2 changes: 1 addition & 1 deletion docs/content/docs/references/cli/copy.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ porter copy [flags]
-h, --help help for copy
--insecure-registry Don't require TLS for registries
--sign-bundle Sign the bundle using the configured signing plugin
--source string The fully qualified source bundle, including tag or digest.
--source string The fully qualified source bundle, including tag or digest.
```

### Options inherited from parent commands
Expand Down
4 changes: 2 additions & 2 deletions pkg/porter/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (p *Porter) publishFromFile(ctx context.Context, opts PublishOptions) error
BundleDefinitionOptions: opts.BundleDefinitionOptions,
InsecureRegistry: opts.InsecureRegistry,
}
_, err := p.ensureLocalBundleIsUpToDate(ctx, buildOpts)
bundleRef, err := p.ensureLocalBundleIsUpToDate(ctx, buildOpts)
if err != nil {
return err
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func (p *Porter) publishFromFile(ctx context.Context, opts PublishOptions) error
return log.Errorf("porter.yaml is missing registry or reference values needed for publishing")
}

var bundleRef cnab.BundleReference
// var bundleRef cnab.BundleReference
bundleRef.Reference, err = cnab.ParseOCIReference(m.Reference)
if err != nil {
return log.Errorf("invalid reference %s: %w", m.Reference, err)
Expand Down
95 changes: 49 additions & 46 deletions tests/integration/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"get.porter.sh/porter/pkg/cnab"
"get.porter.sh/porter/tests/testdata"
"get.porter.sh/porter/tests/tester"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -59,50 +60,52 @@ func TestCopy_UsesRelocationMap(t *testing.T) {
require.Contains(t, invocationImage["originalImage"].(string), fmt.Sprintf("%s/orig-mydb", reg1))
}

func TestCopy_RetagsEmbeddedImages(t *testing.T) {
test, err := tester.NewTest(t)
defer test.Close()
require.NoError(t, err, "test setup failed")

// Start a temporary registry, that uses plain http (no TLS)
reg1 := test.StartTestRegistry(tester.TestRegistryOptions{UseTLS: false})
defer reg1.Close()

// Publish the bundle to the insecure registry
origRef := fmt.Sprintf("%s/orig-mydb:v0.1.1", reg1)
test.MakeTestBundle(testdata.EmbeddedImg, origRef, tester.WithPreserveTags())

ociRef, err := cnab.ParseOCIReference(origRef)
require.NoError(t, err)
out, _ := test.RequirePorter("install", "--force", "-r", origRef)
require.Contains(t, out, ociRef.Repository())

// Start a temporary (insecure) registry on a random port, with a self-signed certificate
reg2 := test.StartTestRegistry(tester.TestRegistryOptions{UseTLS: true})
defer reg2.Close()

// Copy the bundle to the integration test registry, using --insecure-registry
// because the destination uses a self-signed certificate
copiedRef := fmt.Sprintf("%s/copy-mydb:v0.1.1", reg2)
test.RequirePorter("copy", "--source", origRef, "--destination", copiedRef, "--insecure-registry")

reg1.Close()

// Copy the copied bundle to a new location. This will fail if we aren't using the relocation map.
finalRef := fmt.Sprintf("%s/copy-copy-mydb:v0.1.1", reg2)
test.RequirePorter("copy", "--source", copiedRef, "--destination", finalRef, "--insecure-registry")

finalOCIRef, err := cnab.ParseOCIReference(finalRef)
require.NoError(t, err)
finalOut, _ := test.RequirePorter("install", "--force", "-r", finalRef, "--insecure-registry")
require.Contains(t, finalOut, finalOCIRef.Repository())

// Get the original image from the relocation map
inspectOutput, _, err := test.RunPorter("inspect", "-r", finalRef, "--output=json", "--insecure-registry")
require.NoError(t, err, "porter inspect failed")
var inspectRaw map[string]interface{}
require.NoError(t, json.Unmarshal([]byte(inspectOutput), &inspectRaw))
images := inspectRaw["images"].([]interface{})
alpine := images[0].(map[string]interface{})
require.Contains(t, alpine["originalImage"].(string), fmt.Sprintf("%s/orig-mydb", reg1))
func TestCopy_PreserveTags(t *testing.T) {
testcases := []struct {
name string
preserveTags bool
}{
{"preserve tags", true},
{"do not preserve tags", false},
}

for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
test, err := tester.NewTest(t)
defer test.Close()
require.NoError(t, err, "test setup failed")

// Start a temporary registry, that uses plain http (no TLS)
reg1 := test.StartTestRegistry(tester.TestRegistryOptions{UseTLS: false})
defer reg1.Close()

// Publish the bundle to the insecure registry
origRef := fmt.Sprintf("%s/orig-mydb:v0.1.1", reg1)
opts := []func(*tester.TestBundleOptions){}
if tc.preserveTags {
opts = append(opts, tester.PreserveTags)
}
test.MakeTestBundle(testdata.EmbeddedImg, origRef, opts...)

// Start a temporary (insecure) registry on a random port, with a self-signed certificate
reg2 := test.StartTestRegistry(tester.TestRegistryOptions{UseTLS: true})
defer reg2.Close()

// Copy the bundle to the integration test registry, using --insecure-registry
// because the destination uses a self-signed certificate
copiedRef := fmt.Sprintf("%s/copy-mydb:v0.1.1", reg2)
test.RequirePorter("copy", "--source", origRef, "--destination", copiedRef, "--insecure-registry")

reg1.Close()

// Get the original image from the relocation map
_, err = crane.Digest(fmt.Sprintf("%s/alpine:3.20.3", reg2), crane.Insecure)
if tc.preserveTags {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}
50 changes: 50 additions & 0 deletions tests/integration/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
package integration

import (
"encoding/json"
"fmt"
"path"
"testing"

"get.porter.sh/porter/pkg/yaml"
"get.porter.sh/porter/tests"
"get.porter.sh/porter/tests/testdata"
"get.porter.sh/porter/tests/tester"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -47,3 +50,50 @@ func TestPublish(t *testing.T) {
_, output = test.RequirePorter("publish", "--registry", reg.String(), "--insecure-registry")
tests.RequireOutputContains(t, output, fmt.Sprintf("Bundle %s/porter-hello:v0.0.0 pushed successfully", reg))
}

func TestPublish_PreserveTags(t *testing.T) {
test, err := tester.NewTest(t)
defer test.Close()
require.NoError(t, err, "test setup failed")

// Start a temporary registry, that uses plain http (no TLS)
reg := test.StartTestRegistry(tester.TestRegistryOptions{UseTLS: false})
defer reg.Close()

ref := fmt.Sprintf("%s/embeddedimg:v0.1.1", reg)
test.MakeTestBundle(testdata.EmbeddedImg, ref, tester.PreserveTags)

taggedDigest, err := crane.Digest(fmt.Sprintf("%s/alpine:3.20.3", reg), crane.Insecure)
require.NoError(t, err)

// Confirm that the digest is the same
output, _ := test.RequirePorter("inspect", ref, "-o", "json", "--verbosity", "info")
var images struct {
Images []struct {
Digest string `json:"contentDigest"`
} `json:"images"`
}
require.NoError(t, json.Unmarshal([]byte(output), &images))
require.Equal(t, 1, len(images.Images))
require.Equal(t, taggedDigest, images.Images[0].Digest)
}

func TestPublish_PreserveTagsChanged(t *testing.T) {
test, err := tester.NewTest(t)
defer test.Close()
require.NoError(t, err, "test setup failed")

// Start a temporary registry, that uses plain http (no TLS)
reg := test.StartTestRegistry(tester.TestRegistryOptions{UseTLS: false})
defer reg.Close()

ref := fmt.Sprintf("%s/embeddedimg:v0.1.1", reg)
test.MakeTestBundle(testdata.EmbeddedImg, ref)
_, err = crane.Digest(fmt.Sprintf("%s/alpine:3.20.3", reg), crane.Insecure)
require.Error(t, err)

ref = fmt.Sprintf("%s/embeddedimg:v0.1.2", reg)
test.MakeTestBundle(testdata.EmbeddedImg, ref, tester.PreserveTags)
_, err = crane.Digest(fmt.Sprintf("%s/alpine:3.20.3", reg), crane.Insecure)
require.NoError(t, err)
}
25 changes: 25 additions & 0 deletions tests/integration/testdata/bundles/bundle-with-image/helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -euo pipefail

install() {
echo Hello World
}

upgrade() {
echo World 2.0
}

uninstall() {
echo Goodbye World
}

zombies() {
echo oh noes my brains
}

dump-myfile() {
cat /cnab/app/myfile
}

# Call the requested function and pass the arguments as-is
"$@"
34 changes: 34 additions & 0 deletions tests/integration/testdata/bundles/bundle-with-image/porter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
schemaVersion: 1.0.0-alpha.1
name: mybun
version: 0.1.0
description: "An example Porter configuration"
registry: "localhost:5000"

images:
mybun:
image: "docker.io/getporter/porter-hello"
tag: "v0.1.1"

mixins:
- exec

install:
- exec:
description: "Install Hello World"
command: ./helpers.sh
arguments:
- install

upgrade:
- exec:
description: "Upgrade Hello World"
command: ./helpers.sh
arguments:
- upgrade

uninstall:
- exec:
description: "Uninstall Hello World"
command: ./helpers.sh
arguments:
- uninstall
2 changes: 1 addition & 1 deletion tests/testdata/embeddedimg/porter.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This is a test bundle that can be used as a dependency

schemaVersion: 1.0.0
schemaVersion: 1.0.1
name: embeddedimg
version: 0.1.0
description: "A test bundle dependency"
Expand Down
8 changes: 3 additions & 5 deletions tests/tester/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ type TestBundleOptions struct {
PreserveTags bool
}

func WithPreserveTags() func(*TestBundleOptions) {
return func(opts *TestBundleOptions) {
opts.PreserveTags = true
}
func PreserveTags(opts *TestBundleOptions) {
opts.PreserveTags = true
}

// PrepareTestBundle ensures that the mybuns test bundle is ready to use.
Expand Down Expand Up @@ -64,7 +62,7 @@ func (t Tester) MakeTestBundle(name string, ref string, options ...func(*TestBun
}
t.RequirePorter(cmd...)
}
cmd := []string{"publish", "--reference", ref}
cmd := []string{"publish", "--reference", ref, "--verbosity", "debug"}
if opts.PreserveTags {
cmd = append(cmd, "--preserve-tags")
}
Expand Down

0 comments on commit f535765

Please sign in to comment.