get.porter.sh/porter@v1.3.0/tests/integration/copy_test.go (about)

     1  //go:build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"testing"
     9  
    10  	"get.porter.sh/porter/pkg/cnab"
    11  	"get.porter.sh/porter/tests/testdata"
    12  	"get.porter.sh/porter/tests/tester"
    13  	"github.com/google/go-containerregistry/pkg/crane"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestCopy_UsesRelocationMap(t *testing.T) {
    18  	test, err := tester.NewTest(t)
    19  	defer test.Close()
    20  	require.NoError(t, err, "test setup failed")
    21  
    22  	// Start a temporary registry, that uses plain http (no TLS)
    23  	reg1 := test.StartTestRegistry(tester.TestRegistryOptions{UseTLS: false})
    24  
    25  	// Publish the bundle to the insecure registry
    26  	origRef := fmt.Sprintf("%s/orig-mydb:v0.1.1", reg1)
    27  	test.MakeTestBundle(testdata.MyDb, origRef)
    28  
    29  	ociRef, err := cnab.ParseOCIReference(origRef)
    30  	require.NoError(t, err)
    31  	out, _ := test.RequirePorter("install", "--force", "-r", origRef)
    32  	require.Contains(t, out, ociRef.Repository())
    33  
    34  	// Start a temporary (insecure) registry on a random port, with a self-signed certificate
    35  	reg2 := test.StartTestRegistry(tester.TestRegistryOptions{UseTLS: true})
    36  
    37  	// Copy the bundle to the integration test registry, using --insecure-registry
    38  	// because the destination uses a self-signed certificate
    39  	copiedRef := fmt.Sprintf("%s/copy-mydb:v0.1.1", reg2)
    40  	test.RequirePorter("copy", "--source", origRef, "--destination", copiedRef, "--insecure-registry")
    41  
    42  	reg1.Close()
    43  
    44  	// Copy the copied bundle to a new location. This will fail if we aren't using the relocation map.
    45  	finalRef := fmt.Sprintf("%s/copy-copy-mydb:v0.1.1", reg2)
    46  	test.RequirePorter("copy", "--source", copiedRef, "--destination", finalRef, "--insecure-registry")
    47  
    48  	finalOCIRef, err := cnab.ParseOCIReference(finalRef)
    49  	require.NoError(t, err)
    50  	finalOut, _ := test.RequirePorter("install", "--force", "-r", finalRef, "--insecure-registry")
    51  	require.Contains(t, finalOut, finalOCIRef.Repository())
    52  
    53  	// Get the original image from the relocation map
    54  	inspectOutput, _, err := test.RunPorter("inspect", "-r", finalRef, "--output=json", "--insecure-registry")
    55  	require.NoError(t, err, "porter inspect failed")
    56  	var inspectRaw map[string]interface{}
    57  	require.NoError(t, json.Unmarshal([]byte(inspectOutput), &inspectRaw))
    58  	images := inspectRaw["invocationImages"].([]interface{})
    59  	invocationImage := images[0].(map[string]interface{})
    60  	require.Contains(t, invocationImage["originalImage"].(string), fmt.Sprintf("%s/orig-mydb", reg1))
    61  }
    62  
    63  func TestCopy_PreserveTags(t *testing.T) {
    64  	testcases := []struct {
    65  		name         string
    66  		preserveTags bool
    67  	}{
    68  		{"preserve tags", true},
    69  		{"do not preserve tags", false},
    70  	}
    71  
    72  	for _, tc := range testcases {
    73  		tc := tc
    74  		t.Run(tc.name, func(t *testing.T) {
    75  			test, err := tester.NewTest(t)
    76  			defer test.Close()
    77  			require.NoError(t, err, "test setup failed")
    78  
    79  			// Start a temporary registry, that uses plain http (no TLS)
    80  			reg1 := test.StartTestRegistry(tester.TestRegistryOptions{UseTLS: false})
    81  			defer reg1.Close()
    82  
    83  			// Publish the bundle to the insecure registry
    84  			origRef := fmt.Sprintf("%s/orig-mydb:v0.1.1", reg1)
    85  			opts := []func(*tester.TestBundleOptions){}
    86  			if tc.preserveTags {
    87  				opts = append(opts, tester.PreserveTags)
    88  			}
    89  			test.MakeTestBundle(testdata.EmbeddedImg, origRef, opts...)
    90  
    91  			// Start a temporary (insecure) registry on a random port, with a self-signed certificate
    92  			reg2 := test.StartTestRegistry(tester.TestRegistryOptions{UseTLS: true})
    93  			defer reg2.Close()
    94  
    95  			// Copy the bundle to the integration test registry, using --insecure-registry
    96  			// because the destination uses a self-signed certificate
    97  			copiedRef := fmt.Sprintf("%s/copy-mydb:v0.1.1", reg2)
    98  			test.RequirePorter("copy", "--source", origRef, "--destination", copiedRef, "--insecure-registry")
    99  
   100  			reg1.Close()
   101  
   102  			// Get the original image from the relocation map
   103  			_, err = crane.Digest(fmt.Sprintf("%s/alpine:3.20.3", reg2), crane.Insecure)
   104  			if tc.preserveTags {
   105  				require.NoError(t, err)
   106  			} else {
   107  				require.Error(t, err)
   108  			}
   109  		})
   110  	}
   111  }