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

     1  package cnab
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/cnabio/cnab-go/bundle"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestReadDependencyV1Properties(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	data, err := os.ReadFile("testdata/bundle.json")
    16  	require.NoError(t, err, "cannot read bundle file")
    17  
    18  	b, err := bundle.Unmarshal(data)
    19  	require.NoError(t, err, "could not unmarshal the bundle")
    20  
    21  	bun := ExtendedBundle{*b}
    22  	assert.True(t, bun.HasDependenciesV1())
    23  
    24  	deps, err := bun.ReadDependenciesV1()
    25  	require.NoError(t, err, "ReadDependenciesV1 failed")
    26  	assert.NotNil(t, deps, "Dependencies was not populated")
    27  	assert.Len(t, deps.Requires, 2, "Dependencies.Requires is the wrong length")
    28  
    29  	dep := deps.Requires["storage"]
    30  	assert.NotNil(t, dep, "expected Dependencies.Requires to have an entry for 'storage'")
    31  	assert.Equal(t, "somecloud/blob-storage", dep.Bundle, "Dependency.Bundle is incorrect")
    32  	assert.Nil(t, dep.Version, "Dependency.Version should be nil")
    33  
    34  	dep = deps.Requires["mysql"]
    35  	assert.NotNil(t, dep, "expected Dependencies.Requires to have an entry for 'mysql'")
    36  	assert.Equal(t, "somecloud/mysql", dep.Bundle, "Dependency.Bundle is incorrect")
    37  	assert.True(t, dep.Version.AllowPrereleases, "Dependency.Bundle.Version.AllowPrereleases should be true")
    38  	assert.Equal(t, []string{"5.7.x"}, dep.Version.Ranges, "Dependency.Bundle.Version.Ranges is incorrect")
    39  
    40  }
    41  
    42  func TestSupportsDependenciesV1(t *testing.T) {
    43  	t.Parallel()
    44  
    45  	t.Run("supported", func(t *testing.T) {
    46  		b := ExtendedBundle{bundle.Bundle{
    47  			RequiredExtensions: []string{DependenciesV1ExtensionKey}}}
    48  
    49  		assert.True(t, b.SupportsDependenciesV1())
    50  	})
    51  	t.Run("unsupported", func(t *testing.T) {
    52  		b := ExtendedBundle{}
    53  
    54  		assert.False(t, b.SupportsDependenciesV1())
    55  	})
    56  }
    57  
    58  func TestHasDependenciesV1(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	t.Run("has dependencies", func(t *testing.T) {
    62  		b := ExtendedBundle{bundle.Bundle{
    63  			RequiredExtensions: []string{DependenciesV1ExtensionKey},
    64  			Custom: map[string]interface{}{
    65  				DependenciesV1ExtensionKey: struct{}{},
    66  			}}}
    67  
    68  		assert.True(t, b.HasDependenciesV1())
    69  	})
    70  	t.Run("no dependencies", func(t *testing.T) {
    71  		b := ExtendedBundle{bundle.Bundle{
    72  			RequiredExtensions: []string{DependenciesV1ExtensionKey}}}
    73  
    74  		assert.False(t, b.HasDependenciesV1())
    75  	})
    76  }