get.porter.sh/porter@v1.3.0/tests/tester/helpers.go (about)

     1  package tester
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"get.porter.sh/porter/pkg/porter"
    10  	"get.porter.sh/porter/pkg/storage"
    11  	"get.porter.sh/porter/pkg/yaml"
    12  	"get.porter.sh/porter/tests"
    13  	"get.porter.sh/porter/tests/testdata"
    14  	"github.com/stretchr/testify/require"
    15  	"github.com/uwu-tools/magex/shx"
    16  )
    17  
    18  type TestBundleOptions struct {
    19  	PreserveTags bool
    20  }
    21  
    22  func PreserveTags(opts *TestBundleOptions) {
    23  	opts.PreserveTags = true
    24  }
    25  
    26  // PrepareTestBundle ensures that the mybuns test bundle is ready to use.
    27  func (t Tester) PrepareTestBundle() {
    28  	// Build and publish an interesting test bundle and its dependency
    29  	t.MakeTestBundle(testdata.MyDb, testdata.MyDbRef)
    30  	t.MakeTestBundle(testdata.MyBuns, testdata.MyBunsRef)
    31  
    32  	t.ApplyTestBundlePrerequisites()
    33  }
    34  
    35  // ApplyTestBundlePrerequisites ensures that anything required by the test bundle, mybuns, is ready to use.
    36  func (t Tester) ApplyTestBundlePrerequisites() {
    37  	// These are environment variables referenced by the mybuns credential set
    38  	os.Setenv("USER", "porterci")
    39  	os.Setenv("ALT_USER", "porterci2")
    40  
    41  	t.RequirePorter("parameters", "apply", filepath.Join(t.RepoRoot, "tests/testdata/params/mybuns.yaml"), "--namespace=")
    42  	t.RequirePorter("credentials", "apply", filepath.Join(t.RepoRoot, "tests/testdata/creds/mybuns.yaml"), "--namespace=")
    43  }
    44  
    45  func (t Tester) MakeTestBundle(name string, ref string, options ...func(*TestBundleOptions)) {
    46  	opts := TestBundleOptions{}
    47  	for _, option := range options {
    48  		option(&opts)
    49  	}
    50  
    51  	if _, _, err := t.RunPorter("explain", ref); err == nil {
    52  		return
    53  	}
    54  	pwd, _ := os.Getwd()
    55  	defer t.Chdir(pwd)
    56  	t.Chdir(filepath.Join(t.RepoRoot, "tests/testdata/", name))
    57  	output, err := shx.OutputS("docker", "inspect", strings.Replace(ref, name, name+"-installer", 1))
    58  	if output == "[]" || err != nil {
    59  		cmd := []string{"build"}
    60  		if opts.PreserveTags {
    61  			cmd = append(cmd, "--preserve-tags")
    62  		}
    63  		t.RequirePorter(cmd...)
    64  	}
    65  	cmd := []string{"publish", "--reference", ref, "--verbosity", "debug"}
    66  	if opts.PreserveTags {
    67  		cmd = append(cmd, "--preserve-tags")
    68  	}
    69  	t.RequirePorter(cmd...)
    70  }
    71  
    72  func (t Tester) ShowInstallation(namespace string, name string) (porter.DisplayInstallation, error) {
    73  	stdout, _, err := t.RunPorter("show", name, "--namespace", namespace, "--output=json", "--verbosity=info")
    74  	if err != nil {
    75  		return porter.DisplayInstallation{}, err
    76  	}
    77  
    78  	var di porter.DisplayInstallation
    79  
    80  	require.NoError(t.T, json.Unmarshal([]byte(stdout), &di))
    81  
    82  	return di, nil
    83  }
    84  
    85  func (t Tester) RequireInstallationExists(namespace string, name string) porter.DisplayInstallation {
    86  	di, err := t.ShowInstallation(namespace, name)
    87  	require.NoError(t.T, err)
    88  	require.Equal(t.T, name, di.Name, "incorrect installation name")
    89  	require.Equal(t.T, namespace, di.Namespace, "incorrect installation namespace")
    90  
    91  	return di
    92  }
    93  
    94  func (t Tester) RequireInstallationNotFound(namespace string, name string) {
    95  	_, err := t.ShowInstallation(namespace, name)
    96  	t.RequireNotFoundReturned(err)
    97  }
    98  
    99  func (t Tester) RequireNotFoundReturned(err error) {
   100  	require.Error(t.T, err)
   101  	require.Contains(t.T, err.Error(), "not found")
   102  }
   103  
   104  func (t Tester) ListInstallations(allNamespaces bool, namespace string, name string, labels []string) ([]porter.DisplayInstallation, error) {
   105  	args := []string{
   106  		"list",
   107  		"--output=json",
   108  		"--name", name,
   109  		"--verbosity=info",
   110  	}
   111  	if allNamespaces {
   112  		args = append(args, "--all-namespaces")
   113  	} else {
   114  		args = append(args, "--namespace", namespace)
   115  	}
   116  	for _, l := range labels {
   117  		args = append(args, "--label", l)
   118  	}
   119  
   120  	stdout, _, err := t.RunPorter(args...)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	var installations []porter.DisplayInstallation
   126  	require.NoError(t.T, json.Unmarshal([]byte(stdout), &installations))
   127  	return installations, nil
   128  }
   129  
   130  func (t Tester) RequireInstallationInList(namespace, name string, list []storage.Installation) storage.Installation {
   131  	for _, i := range list {
   132  		if i.Namespace == namespace && i.Name == name {
   133  			return i
   134  		}
   135  	}
   136  
   137  	t.T.Fatalf("expected %s/%s to be in the list of installations", namespace, name)
   138  	return storage.Installation{}
   139  }
   140  
   141  // EditYaml applies a set of yq transformations to a file.
   142  func (t Tester) EditYaml(path string, transformations ...func(yq *yaml.Editor) error) {
   143  	t.TestContext.EditYaml(path, transformations...)
   144  }
   145  
   146  // RequireFileMode checks that all files in the specified path match the specified
   147  // file mode. Uses a glob pattern to match.
   148  func (t *Tester) RequireFileMode(path string, mode os.FileMode) {
   149  	if !tests.AssertDirectoryPermissionsEqual(t.T, path, mode) {
   150  		t.T.FailNow()
   151  	}
   152  }