get.porter.sh/porter@v1.3.0/pkg/cnab/dependencies_v2_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 TestReadDependencyV2Properties(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	data, err := os.ReadFile("testdata/bundle-depsv2.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.HasDependenciesV2())
    23  
    24  	deps, err := bun.ReadDependenciesV2()
    25  	require.NoError(t, err)
    26  
    27  	assert.NotNil(t, deps, "DependenciesV2 was not populated")
    28  	assert.Len(t, deps.Requires, 2, "DependenciesV2.Requires is the wrong length")
    29  
    30  	dep := deps.Requires["storage"]
    31  	assert.NotNil(t, dep, "expected DependenciesV2.Requires to have an entry for 'storage'")
    32  	assert.Equal(t, "somecloud/blob-storage", dep.Bundle, "DependencyV2.Bundle is incorrect")
    33  	assert.Empty(t, dep.Version, "DependencyV2.Version should be nil")
    34  
    35  	dep = deps.Requires["mysql"]
    36  	assert.NotNil(t, dep, "expected DependenciesV2.Requires to have an entry for 'mysql'")
    37  	assert.Equal(t, "somecloud/mysql", dep.Bundle, "DependencyV2.Bundle is incorrect")
    38  	assert.Equal(t, "5.7.x", dep.Version, "DependencyV2.Bundle.Version is incorrect")
    39  
    40  }
    41  
    42  func TestSupportsDependenciesV2(t *testing.T) {
    43  	t.Parallel()
    44  
    45  	t.Run("supported", func(t *testing.T) {
    46  		b := ExtendedBundle{bundle.Bundle{
    47  			RequiredExtensions: []string{DependenciesV2ExtensionKey}}}
    48  
    49  		assert.True(t, b.SupportsDependenciesV2())
    50  	})
    51  	t.Run("unsupported", func(t *testing.T) {
    52  		b := ExtendedBundle{}
    53  
    54  		assert.False(t, b.SupportsDependenciesV2())
    55  	})
    56  }
    57  
    58  func TestHasDependenciesV2(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	t.Run("has dependencies", func(t *testing.T) {
    62  		b := ExtendedBundle{bundle.Bundle{
    63  			RequiredExtensions: []string{DependenciesV2ExtensionKey},
    64  			Custom: map[string]interface{}{
    65  				DependenciesV2ExtensionKey: struct{}{}}}}
    66  
    67  		assert.True(t, b.HasDependenciesV2())
    68  	})
    69  	t.Run("no dependencies", func(t *testing.T) {
    70  		b := ExtendedBundle{bundle.Bundle{
    71  			RequiredExtensions: []string{DependenciesV2ExtensionKey}}}
    72  
    73  		assert.False(t, b.HasDependenciesV2())
    74  	})
    75  }