github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/acceptance/assertions/output.go (about)

     1  //go:build acceptance
     2  // +build acceptance
     3  
     4  package assertions
     5  
     6  import (
     7  	"fmt"
     8  	"regexp"
     9  	"strings"
    10  	"testing"
    11  
    12  	h "github.com/buildpacks/pack/testhelpers"
    13  )
    14  
    15  type OutputAssertionManager struct {
    16  	testObject *testing.T
    17  	assert     h.AssertionManager
    18  	output     string
    19  }
    20  
    21  func NewOutputAssertionManager(t *testing.T, output string) OutputAssertionManager {
    22  	return OutputAssertionManager{
    23  		testObject: t,
    24  		assert:     h.NewAssertionManager(t),
    25  		output:     output,
    26  	}
    27  }
    28  
    29  func (o OutputAssertionManager) ReportsSuccessfulImageBuild(name string) {
    30  	o.testObject.Helper()
    31  
    32  	o.assert.ContainsF(o.output, "Successfully built image '%s'", name)
    33  }
    34  
    35  func (o OutputAssertionManager) ReportsSuccessfulIndexLocallyCreated(name string) {
    36  	o.testObject.Helper()
    37  
    38  	o.assert.ContainsF(o.output, "Successfully created manifest list '%s'", name)
    39  }
    40  
    41  func (o OutputAssertionManager) ReportsSuccessfulIndexPushed(name string) {
    42  	o.testObject.Helper()
    43  
    44  	o.assert.ContainsF(o.output, "Successfully pushed manifest list '%s' to registry", name)
    45  }
    46  
    47  func (o OutputAssertionManager) ReportsSuccessfulManifestAddedToIndex(name string) {
    48  	o.testObject.Helper()
    49  
    50  	o.assert.ContainsF(o.output, "Successfully added image '%s' to index", name)
    51  }
    52  
    53  func (o OutputAssertionManager) ReportsSuccessfulIndexDeleted() {
    54  	o.testObject.Helper()
    55  
    56  	o.assert.Contains(o.output, "Successfully deleted manifest list(s) from local storage")
    57  }
    58  
    59  func (o OutputAssertionManager) ReportsSuccessfulIndexAnnotated(name, manifest string) {
    60  	o.testObject.Helper()
    61  
    62  	o.assert.ContainsF(o.output, "Successfully annotated image '%s' in index '%s'", name, manifest)
    63  }
    64  
    65  func (o OutputAssertionManager) ReportsSuccessfulRemoveManifestFromIndex(name string) {
    66  	o.testObject.Helper()
    67  
    68  	o.assert.ContainsF(o.output, "Successfully removed image(s) from index: '%s'", name)
    69  }
    70  
    71  func (o OutputAssertionManager) ReportSuccessfulQuietBuild(name string) {
    72  	o.testObject.Helper()
    73  	o.testObject.Log("quiet mode")
    74  
    75  	o.assert.Matches(strings.TrimSpace(o.output), regexp.MustCompile(name+`@sha256:[\w]{12,64}`))
    76  }
    77  
    78  func (o OutputAssertionManager) ReportsSuccessfulRebase(name string) {
    79  	o.testObject.Helper()
    80  
    81  	o.assert.ContainsF(o.output, "Successfully rebased image '%s'", name)
    82  }
    83  
    84  func (o OutputAssertionManager) ReportsUsingBuildCacheVolume() {
    85  	o.testObject.Helper()
    86  
    87  	o.testObject.Log("uses a build cache volume")
    88  	o.assert.Contains(o.output, "Using build cache volume")
    89  }
    90  
    91  func (o OutputAssertionManager) ReportsSelectingRunImageMirror(mirror string) {
    92  	o.testObject.Helper()
    93  
    94  	o.testObject.Log("selects expected run image mirror")
    95  	o.assert.ContainsF(o.output, "Selected run image mirror '%s'", mirror)
    96  }
    97  
    98  func (o OutputAssertionManager) ReportsSelectingRunImageMirrorFromLocalConfig(mirror string) {
    99  	o.testObject.Helper()
   100  
   101  	o.testObject.Log("local run-image mirror is selected")
   102  	o.assert.ContainsF(o.output, "Selected run image mirror '%s' from local config", mirror)
   103  }
   104  
   105  func (o OutputAssertionManager) ReportsSkippingRestore() {
   106  	o.testObject.Helper()
   107  	o.testObject.Log("skips restore")
   108  
   109  	o.assert.Contains(o.output, "Skipping 'restore' due to clearing cache")
   110  }
   111  
   112  func (o OutputAssertionManager) ReportsRunImageStackNotMatchingBuilder(runImageStack, builderStack string) {
   113  	o.testObject.Helper()
   114  
   115  	o.assert.Contains(
   116  		o.output,
   117  		fmt.Sprintf("run-image stack id '%s' does not match builder stack '%s'", runImageStack, builderStack),
   118  	)
   119  }
   120  
   121  func (o OutputAssertionManager) WithoutColors() {
   122  	o.testObject.Helper()
   123  	o.testObject.Log("has no color")
   124  
   125  	o.assert.NoMatches(o.output, regexp.MustCompile(`\x1b\[[0-9;]*m`))
   126  }
   127  
   128  func (o OutputAssertionManager) ReportsAddingBuildpack(name, version string) {
   129  	o.testObject.Helper()
   130  
   131  	o.assert.ContainsF(o.output, "Adding buildpack '%s' version '%s' to builder", name, version)
   132  }
   133  
   134  func (o OutputAssertionManager) ReportsPullingImage(image string) {
   135  	o.testObject.Helper()
   136  
   137  	o.assert.ContainsF(o.output, "Pulling image '%s'", image)
   138  }
   139  
   140  func (o OutputAssertionManager) ReportsImageNotExistingOnDaemon(image string) {
   141  	o.testObject.Helper()
   142  
   143  	o.assert.ContainsF(o.output, "image '%s' does not exist on the daemon", image)
   144  }
   145  
   146  func (o OutputAssertionManager) ReportsPackageCreation(name string) {
   147  	o.testObject.Helper()
   148  
   149  	o.assert.ContainsF(o.output, "Successfully created package '%s'", name)
   150  }
   151  
   152  func (o OutputAssertionManager) ReportsInvalidExtension(extension string) {
   153  	o.testObject.Helper()
   154  
   155  	o.assert.ContainsF(o.output, "'%s' is not a valid extension for a packaged buildpack. Packaged buildpacks must have a '.cnb' extension", extension)
   156  }
   157  
   158  func (o OutputAssertionManager) ReportsPackagePublished(name string) {
   159  	o.testObject.Helper()
   160  
   161  	o.assert.ContainsF(o.output, "Successfully published package '%s'", name)
   162  }
   163  
   164  func (o OutputAssertionManager) ReportsCommandUnknown(command string) {
   165  	o.testObject.Helper()
   166  
   167  	o.assert.ContainsF(o.output, `unknown command "%s" for "pack"`, command)
   168  }
   169  
   170  func (o OutputAssertionManager) IncludesUsagePrompt() {
   171  	o.testObject.Helper()
   172  
   173  	o.assert.Contains(o.output, "Run 'pack --help' for usage.")
   174  }
   175  
   176  func (o OutputAssertionManager) ReportsSettingDefaultBuilder(name string) {
   177  	o.testObject.Helper()
   178  
   179  	o.assert.ContainsF(o.output, "Builder '%s' is now the default builder", name)
   180  }
   181  
   182  func (o OutputAssertionManager) IncludesSuggestedBuildersHeading() {
   183  	o.testObject.Helper()
   184  
   185  	o.assert.Contains(o.output, "Suggested builders:")
   186  }
   187  
   188  func (o OutputAssertionManager) IncludesMessageToSetDefaultBuilder() {
   189  	o.testObject.Helper()
   190  
   191  	o.assert.Contains(o.output, "Please select a default builder with:")
   192  }
   193  
   194  func (o OutputAssertionManager) IncludesSuggestedStacksHeading() {
   195  	o.testObject.Helper()
   196  
   197  	o.assert.Contains(o.output, "Stacks maintained by the community:")
   198  }
   199  
   200  func (o OutputAssertionManager) IncludesTrustedBuildersHeading() {
   201  	o.testObject.Helper()
   202  
   203  	o.assert.Contains(o.output, "Trusted Builders:")
   204  }
   205  
   206  const googleBuilder = "gcr.io/buildpacks/builder:v1"
   207  
   208  func (o OutputAssertionManager) IncludesGoogleBuilder() {
   209  	o.testObject.Helper()
   210  
   211  	o.assert.Contains(o.output, googleBuilder)
   212  }
   213  
   214  func (o OutputAssertionManager) IncludesPrefixedGoogleBuilder() {
   215  	o.testObject.Helper()
   216  
   217  	o.assert.Matches(o.output, regexp.MustCompile(fmt.Sprintf(`Google:\s+'%s'`, googleBuilder)))
   218  }
   219  
   220  var herokuBuilders = []string{
   221  	"heroku/builder:22",
   222  }
   223  
   224  func (o OutputAssertionManager) IncludesHerokuBuilders() {
   225  	o.testObject.Helper()
   226  
   227  	o.assert.ContainsAll(o.output, herokuBuilders...)
   228  }
   229  
   230  func (o OutputAssertionManager) IncludesPrefixedHerokuBuilders() {
   231  	o.testObject.Helper()
   232  
   233  	for _, builder := range herokuBuilders {
   234  		o.assert.Matches(o.output, regexp.MustCompile(fmt.Sprintf(`Heroku:\s+'%s'`, builder)))
   235  	}
   236  }
   237  
   238  var paketoBuilders = []string{
   239  	"paketobuildpacks/builder-jammy-base",
   240  	"paketobuildpacks/builder-jammy-full",
   241  	"paketobuildpacks/builder-jammy-tiny",
   242  }
   243  
   244  func (o OutputAssertionManager) IncludesPaketoBuilders() {
   245  	o.testObject.Helper()
   246  
   247  	o.assert.ContainsAll(o.output, paketoBuilders...)
   248  }
   249  
   250  func (o OutputAssertionManager) IncludesPrefixedPaketoBuilders() {
   251  	o.testObject.Helper()
   252  
   253  	for _, builder := range paketoBuilders {
   254  		o.assert.Matches(o.output, regexp.MustCompile(fmt.Sprintf(`Paketo Buildpacks:\s+'%s'`, builder)))
   255  	}
   256  }
   257  
   258  func (o OutputAssertionManager) IncludesDeprecationWarning() {
   259  	o.testObject.Helper()
   260  
   261  	o.assert.Matches(o.output, regexp.MustCompile(fmt.Sprintf(`Warning: Command 'pack [\w-]+' has been deprecated, please use 'pack [\w-\s]+' instead`)))
   262  }
   263  
   264  func (o OutputAssertionManager) ReportsSuccesfulRunImageMirrorsAdd(image, mirror string) {
   265  	o.testObject.Helper()
   266  
   267  	o.assert.ContainsF(o.output, "Run Image '%s' configured with mirror '%s'\n", image, mirror)
   268  }
   269  
   270  func (o OutputAssertionManager) ReportsReadingConfig() {
   271  	o.testObject.Helper()
   272  
   273  	o.assert.Contains(o.output, "reading config")
   274  }
   275  
   276  func (o OutputAssertionManager) ReportsInvalidBuilderToml() {
   277  	o.testObject.Helper()
   278  
   279  	o.assert.Contains(o.output, "invalid builder toml")
   280  }