get.porter.sh/porter@v1.3.0/pkg/storage/migrations/legacy_plugin_adapter_test.go (about)

     1  package migrations
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"get.porter.sh/porter/pkg/config"
    11  	"get.porter.sh/porter/pkg/storage/migrations/crudstore"
    12  	testmigrations "get.porter.sh/porter/pkg/storage/migrations/testhelpers"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  // Verify that we can retrieve data from the old plugins
    18  func TestLegacyPluginAdapter(t *testing.T) {
    19  	c := testmigrations.CreateLegacyPorterHome(t)
    20  	defer c.Close()
    21  
    22  	home, err := c.GetHomeDir()
    23  	require.NoError(t, err, "could not get the home directory")
    24  
    25  	adapter := NewLegacyPluginAdapter(c.Config, home, "src")
    26  	defer adapter.Close()
    27  
    28  	ctx := context.Background()
    29  
    30  	// List installation names
    31  	installationNames, err := adapter.List(ctx, "installations", "")
    32  	require.NoError(t, err, "failed to list the installation names")
    33  	wantInstallationNames := []string{"creds-tutorial", "hello-llama", "hello1", "sensitive-data"}
    34  	require.Equal(t, wantInstallationNames, installationNames, "expected 4 installations")
    35  
    36  	// Retrieve a claim document
    37  	result, err := adapter.Read(ctx, "claims", "01G1VJQJV0RN5AW5BSZHNTVYTV")
    38  	require.NoError(t, err, "failed to read the claim document")
    39  
    40  	// Check that we did read the claim correctly through the plugin
    41  	var gotData map[string]interface{}
    42  	err = json.Unmarshal(result, &gotData)
    43  	require.NoError(t, err, "failed to unmarshal the claim document")
    44  
    45  	var wantData map[string]interface{}
    46  	contents, err := os.ReadFile(filepath.Join(home, "claims/hello1/01G1VJQJV0RN5AW5BSZHNTVYTV.json"))
    47  	require.NoError(t, err, "error reading the test claim to compare results")
    48  	require.NoError(t, json.Unmarshal(contents, &wantData), "failed to unmarshal the test claim")
    49  	assert.Equal(t, wantData, gotData, "The claim data read through the plugin doesn't match the original test claim")
    50  }
    51  
    52  func TestLegacyPluginAdapter_makePluginConfig(t *testing.T) {
    53  	c := testmigrations.CreateLegacyPorterHome(t)
    54  	defer c.Close()
    55  
    56  	home, err := c.GetHomeDir()
    57  	require.NoError(t, err, "could not get the home directory")
    58  
    59  	adapter := NewLegacyPluginAdapter(c.Config, home, "src")
    60  	defer adapter.Close()
    61  
    62  	cfg := adapter.makePluginConfig()
    63  
    64  	// Test that we are using the legacy plugin interface
    65  	assert.Equal(t, "storage", cfg.Interface, "incorrect plugin interface")
    66  	assert.Equal(t, &crudstore.Plugin{}, cfg.Plugin, "incorrect plugin wrapper")
    67  	assert.Equal(t, uint(1), cfg.ProtocolVersion, "incorrect plugin protocol version")
    68  
    69  	// Test that we are using the correct default plugin based on porter v0
    70  	// Use an empty config since we are testing defaults
    71  	defaultPlugin := cfg.GetDefaultPlugin(&config.Config{})
    72  	assert.Equal(t, "filesystem", defaultPlugin, "incorrect default storage plugin")
    73  
    74  	// Test that we are using the storage account specified by the user
    75  	// Use an empty config since we are testing defaults
    76  	defaultStorageName := cfg.GetDefaultPluggable(&config.Config{})
    77  	assert.Equal(t, "src", defaultStorageName, "incorrect default storage account used")
    78  
    79  	// Test that we can retrieve a named legacy storage account
    80  	// Use the real config from our temp PORTER_HOME, which has the azure storage account defined
    81  	plugin, err := cfg.GetPluggable(c.Config, "azure")
    82  	require.NoError(t, err, "GetPluggable failed")
    83  	assert.Equal(t, "azure", plugin.GetName(), "incorrect plugin retrieved")
    84  }