get.porter.sh/porter@v1.3.0/pkg/pkgmgmt/feed/feed_test.go (about)

     1  package feed
     2  
     3  import (
     4  	"context"
     5  	"net/url"
     6  	"testing"
     7  
     8  	"get.porter.sh/porter/pkg/portercontext"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestMixinFeed_Search_Latest(t *testing.T) {
    14  	tc := portercontext.NewTestContext(t)
    15  	f := NewMixinFeed(tc.Context)
    16  
    17  	f.Index["helm"] = make(map[string]*MixinFileset)
    18  	f.Index["helm"]["canary"] = &MixinFileset{
    19  		Mixin:   "helm",
    20  		Version: "canary",
    21  	}
    22  	f.Index["helm"]["v1.2.3"] = &MixinFileset{
    23  		Mixin:   "helm",
    24  		Version: "v1.2.3",
    25  	}
    26  	f.Index["helm"]["v1.2.4"] = &MixinFileset{
    27  		Mixin:   "helm",
    28  		Version: "v1.2.4",
    29  	}
    30  	f.Index["helm"]["v2-latest"] = &MixinFileset{
    31  		Mixin:   "helm",
    32  		Version: "v2-latest",
    33  	}
    34  	f.Index["helm"]["v2.0.0-alpha.1"] = &MixinFileset{
    35  		Mixin:   "helm",
    36  		Version: "v2.0.0-alpha.1",
    37  	}
    38  
    39  	result := f.Search("helm", "latest")
    40  	require.NotNil(t, result)
    41  	assert.Equal(t, "v1.2.4", result.Version)
    42  
    43  	// Now try to get v2-latest specifically
    44  	result = f.Search("helm", "v2-latest")
    45  	require.NotNil(t, result)
    46  	assert.Equal(t, "v2-latest", result.Version)
    47  }
    48  
    49  func TestMixinFeed_Search_Canary(t *testing.T) {
    50  	tc := portercontext.NewTestContext(t)
    51  	f := NewMixinFeed(tc.Context)
    52  
    53  	f.Index["helm"] = make(map[string]*MixinFileset)
    54  	f.Index["helm"]["canary"] = &MixinFileset{
    55  		Mixin:   "helm",
    56  		Version: "canary",
    57  	}
    58  	f.Index["helm"]["v1.2.4"] = &MixinFileset{
    59  		Mixin:   "helm",
    60  		Version: "v1.2.4",
    61  	}
    62  	f.Index["helm"]["v2-canary"] = &MixinFileset{
    63  		Mixin:   "helm",
    64  		Version: "v2-canary",
    65  	}
    66  
    67  	result := f.Search("helm", "canary")
    68  	require.NotNil(t, result)
    69  	assert.Equal(t, "canary", result.Version)
    70  
    71  	// Now try to get v2-canary specifically
    72  	result = f.Search("helm", "v2-canary")
    73  	require.NotNil(t, result)
    74  	assert.Equal(t, "v2-canary", result.Version)
    75  }
    76  
    77  func TestMixinFileset_FindDownloadURL(t *testing.T) {
    78  	t.Run("darwin/arm64 fallback to amd64", func(t *testing.T) {
    79  		link, _ := url.Parse("https://example.com/mymixin-darwin-amd64")
    80  
    81  		fs := MixinFileset{
    82  			Mixin: "mymixin",
    83  			Files: []*MixinFile{
    84  				{URL: link},
    85  			},
    86  		}
    87  
    88  		result := fs.FindDownloadURL(context.Background(), "darwin", "arm64")
    89  		assert.Contains(t, result.String(), "amd64", "When an arm64 binary is not available for mac, fallback to using an amd64")
    90  	})
    91  
    92  	t.Run("darwin/arm64 binary exists", func(t *testing.T) {
    93  		link, _ := url.Parse("https://example.com/mymixin-darwin-arm64")
    94  
    95  		fs := MixinFileset{
    96  			Mixin: "mymixin",
    97  			Files: []*MixinFile{
    98  				{URL: link},
    99  			},
   100  		}
   101  
   102  		result := fs.FindDownloadURL(context.Background(), "darwin", "arm64")
   103  		assert.Contains(t, result.String(), "arm64", "When an arm64 binary is available, use it")
   104  	})
   105  
   106  	t.Run("non-darwin arm64 no special handling", func(t *testing.T) {
   107  		link, _ := url.Parse("https://example.com/mymixin-myos-amd64")
   108  
   109  		fs := MixinFileset{
   110  			Mixin: "mymixin",
   111  			Files: []*MixinFile{
   112  				{URL: link},
   113  			},
   114  		}
   115  
   116  		result := fs.FindDownloadURL(context.Background(), "myos", "arm64")
   117  		assert.Nil(t, result)
   118  	})
   119  }