get.porter.sh/porter@v1.3.0/pkg/cnab/provider/action_test.go (about)

     1  package cnabprovider
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"get.porter.sh/porter/pkg/cnab"
     7  	"get.porter.sh/porter/pkg/config"
     8  	"get.porter.sh/porter/pkg/storage"
     9  	"get.porter.sh/porter/pkg/test"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/cnabio/cnab-go/bundle"
    14  	"github.com/cnabio/cnab-go/driver"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestAddRelocation(t *testing.T) {
    20  	t.Parallel()
    21  
    22  	data, err := os.ReadFile("testdata/relocation-mapping.json")
    23  	require.NoError(t, err)
    24  
    25  	d := NewTestRuntime(t)
    26  	defer d.Close()
    27  
    28  	var args ActionArguments
    29  	require.NoError(t, json.Unmarshal(data, &args.BundleReference.RelocationMap))
    30  
    31  	opConf := d.AddRelocation(args)
    32  
    33  	invoImage := bundle.InvocationImage{}
    34  	invoImage.Image = "gabrtv/microservice@sha256:cca460afa270d4c527981ef9ca4989346c56cf9b20217dcea37df1ece8120687"
    35  
    36  	op := &driver.Operation{
    37  		Files: make(map[string]string),
    38  		Image: invoImage,
    39  	}
    40  	err = opConf(op)
    41  	assert.NoError(t, err)
    42  
    43  	mapping, ok := op.Files["/cnab/app/relocation-mapping.json"]
    44  	assert.True(t, ok)
    45  	assert.Equal(t, string(data), mapping)
    46  	assert.Equal(t, "my.registry/microservice@sha256:cca460afa270d4c527981ef9ca4989346c56cf9b20217dcea37df1ece8120687", op.Image.Image)
    47  
    48  }
    49  
    50  func TestAddFiles(t *testing.T) {
    51  	t.Parallel()
    52  
    53  	ctx := context.Background()
    54  	d := NewTestRuntime(t)
    55  	defer d.Close()
    56  
    57  	// Make a test claim
    58  	instName := "mybuns"
    59  	run1 := storage.NewRun("", instName)
    60  	run1.NewResult(cnab.StatusPending)
    61  	i := d.TestInstallations.CreateInstallation(storage.NewInstallation("", instName), d.TestInstallations.SetMutableInstallationValues)
    62  	d.TestInstallations.CreateRun(run1, d.TestInstallations.SetMutableRunValues)
    63  
    64  	// Prep the files in the bundle
    65  	args := ActionArguments{
    66  		Installation: i,
    67  	}
    68  	op := &driver.Operation{}
    69  	err := d.AddFiles(ctx, args)(op)
    70  	require.NoError(t, err, "AddFiles failed")
    71  
    72  	// Check that we injected a CNAB claim and not our Run representation, they aren't exactly 1:1 the same format
    73  	require.Contains(t, op.Files, config.ClaimFilepath, "The claim should have been injected into the bundle")
    74  	test.CompareGoldenFile(t, "testdata/want-claim.json", op.Files[config.ClaimFilepath])
    75  }