github.com/jaylevin/jenkins-library@v1.230.4/integration/integration_cnb_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  // can be executed with go test -tags=integration ./integration/...
     5  
     6  package main
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/testcontainers/testcontainers-go"
    15  )
    16  
    17  const (
    18  	registryURL = "localhost:5000"
    19  	baseBuilder = "paketobuildpacks/builder:0.2.17-base"
    20  )
    21  
    22  func setupDockerRegistry(t *testing.T, ctx context.Context) testcontainers.Container {
    23  	reqRegistry := testcontainers.ContainerRequest{
    24  		Image:      "registry:2",
    25  		SkipReaper: true,
    26  	}
    27  
    28  	regContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
    29  		ContainerRequest: reqRegistry,
    30  		Started:          true,
    31  	})
    32  	assert.NoError(t, err)
    33  
    34  	return regContainer
    35  }
    36  
    37  func TestNpmProject(t *testing.T) {
    38  	t.Parallel()
    39  	ctx := context.Background()
    40  	registryContainer := setupDockerRegistry(t, ctx)
    41  	defer registryContainer.Terminate(ctx)
    42  
    43  	container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
    44  		Image:   baseBuilder,
    45  		User:    "cnb",
    46  		TestDir: []string{"testdata"},
    47  		Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
    48  	})
    49  
    50  	container2 := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
    51  		Image:   baseBuilder,
    52  		User:    "cnb",
    53  		TestDir: []string{"testdata"},
    54  		Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
    55  	})
    56  
    57  	err := container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--path", "TestCnbIntegration/project", "--customConfig", "TestCnbIntegration/config.yml", "--containerImageName", "node", "--containerImageTag", "0.0.1", "--containerRegistryUrl", registryURL)
    58  	assert.NoError(t, err)
    59  	container.assertHasOutput(t, "running command: /cnb/lifecycle/creator")
    60  	container.assertHasOutput(t, "Selected Node Engine version (using BP_NODE_VERSION): 16")
    61  	container.assertHasOutput(t, "Paketo NPM Start Buildpack")
    62  	container.assertHasOutput(t, fmt.Sprintf("Saving %s/node:0.0.1", registryURL))
    63  	container.assertHasOutput(t, "*** Images (sha256:")
    64  	container.assertHasOutput(t, "SUCCESS")
    65  	container.terminate(t)
    66  
    67  	err = container2.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--path", "TestCnbIntegration/project", "--customConfig", "TestCnbIntegration/config.yml", "--containerImageName", "node", "--containerImageTag", "0.0.1", "--containerRegistryUrl", registryURL)
    68  	assert.NoError(t, err)
    69  	container2.assertHasOutput(t, "running command: /cnb/lifecycle/creator")
    70  	container2.assertHasOutput(t, "Selected Node Engine version (using BP_NODE_VERSION): 16")
    71  	container2.assertHasOutput(t, "Paketo NPM Start Buildpack")
    72  	container2.assertHasOutput(t, fmt.Sprintf("Saving %s/node:0.0.1", registryURL))
    73  	container2.assertHasOutput(t, "*** Images (sha256:")
    74  	container2.assertHasOutput(t, "SUCCESS")
    75  	container2.terminate(t)
    76  }
    77  
    78  func TestProjectDescriptor(t *testing.T) {
    79  	t.Parallel()
    80  	ctx := context.Background()
    81  	registryContainer := setupDockerRegistry(t, ctx)
    82  	defer registryContainer.Terminate(ctx)
    83  
    84  	container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
    85  		Image:   baseBuilder,
    86  		User:    "cnb",
    87  		TestDir: []string{"testdata", "TestCnbIntegration", "project"},
    88  		Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
    89  	})
    90  
    91  	container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--containerImageName", "not-found", "--containerImageTag", "0.0.1", "--containerRegistryUrl", registryURL)
    92  
    93  	container.assertHasOutput(t, "running command: /cnb/lifecycle/creator")
    94  	container.assertHasOutput(t, "Dockerfile doesn't match include pattern, ignoring")
    95  	container.assertHasOutput(t, "srv/hello.js matches include pattern")
    96  	container.assertHasOutput(t, "srv/hello.js matches include pattern")
    97  	container.assertHasOutput(t, "package.json matches include pattern")
    98  	container.assertHasOutput(t, "Downloading buildpack")
    99  	container.assertHasOutput(t, "Setting custom environment variables: 'map[BP_NODE_VERSION:16]'")
   100  	container.assertHasOutput(t, "Selected Node Engine version (using BP_NODE_VERSION): 16")
   101  	container.assertHasOutput(t, "Paketo NPM Start Buildpack")
   102  	container.assertHasOutput(t, fmt.Sprintf("Saving %s/not-found:0.0.1", registryURL))
   103  	container.assertHasOutput(t, "*** Images (sha256:")
   104  	container.assertHasOutput(t, "SUCCESS")
   105  	container.terminate(t)
   106  }
   107  
   108  func TestZipPath(t *testing.T) {
   109  	t.Parallel()
   110  	ctx := context.Background()
   111  	registryContainer := setupDockerRegistry(t, ctx)
   112  	defer registryContainer.Terminate(ctx)
   113  
   114  	container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
   115  		Image:   baseBuilder,
   116  		User:    "cnb",
   117  		TestDir: []string{"testdata", "TestCnbIntegration", "zip"},
   118  		Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
   119  	})
   120  
   121  	container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--containerImageName", "not-found", "--containerImageTag", "0.0.1", "--containerRegistryUrl", registryURL, "--path", "go.zip")
   122  
   123  	container.assertHasOutput(t, "running command: /cnb/lifecycle/creator")
   124  	container.assertHasOutput(t, "Installing Go")
   125  	container.assertHasOutput(t, "Paketo Go Build Buildpack")
   126  	container.assertHasOutput(t, fmt.Sprintf("Saving %s/not-found:0.0.1", registryURL))
   127  	container.assertHasOutput(t, "*** Images (sha256:")
   128  	container.assertHasOutput(t, "SUCCESS")
   129  	container.terminate(t)
   130  }
   131  
   132  func TestNonZipPath(t *testing.T) {
   133  	t.Parallel()
   134  	ctx := context.Background()
   135  	registryContainer := setupDockerRegistry(t, ctx)
   136  	defer registryContainer.Terminate(ctx)
   137  
   138  	container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
   139  		Image:   baseBuilder,
   140  		User:    "cnb",
   141  		TestDir: []string{"testdata", "TestMtaIntegration", "npm"},
   142  		Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
   143  	})
   144  
   145  	container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--containerImageName", "not-found", "--containerImageTag", "0.0.1", "--containerRegistryUrl", registryURL, "--path", "mta.yaml")
   146  
   147  	container.assertHasOutput(t, "Copying  '/project/mta.yaml' into '/workspace' failed: application path must be a directory or zip")
   148  	container.terminate(t)
   149  }
   150  
   151  func TestNpmCustomBuildpacksFullProject(t *testing.T) {
   152  	t.Parallel()
   153  	ctx := context.Background()
   154  	registryContainer := setupDockerRegistry(t, ctx)
   155  	defer registryContainer.Terminate(ctx)
   156  
   157  	container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
   158  		Image:   baseBuilder,
   159  		User:    "cnb",
   160  		TestDir: []string{"testdata", "TestMtaIntegration", "npm"},
   161  		Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
   162  	})
   163  
   164  	container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--buildpacks", "gcr.io/paketo-buildpacks/nodejs:0.14.0", "--containerImageName", "not-found", "--containerImageTag", "0.0.1", "--containerRegistryUrl", registryURL)
   165  
   166  	container.assertHasOutput(t, "Setting custom buildpacks: '[gcr.io/paketo-buildpacks/nodejs:0.14.0]'")
   167  	container.assertHasOutput(t, "Downloading buildpack 'gcr.io/paketo-buildpacks/nodejs:0.14.0' to /tmp/buildpacks_cache/sha256:")
   168  	container.assertHasOutput(t, "running command: /cnb/lifecycle/creator")
   169  	container.assertHasOutput(t, "Paketo NPM Start Buildpack")
   170  	container.assertHasOutput(t, fmt.Sprintf("Saving %s/not-found:0.0.1", registryURL))
   171  	container.assertHasOutput(t, "*** Images (sha256:")
   172  	container.assertHasOutput(t, "SUCCESS")
   173  	container.terminate(t)
   174  }
   175  
   176  func TestNpmCustomBuildpacksBuildpacklessProject(t *testing.T) {
   177  	t.Parallel()
   178  	ctx := context.Background()
   179  	registryContainer := setupDockerRegistry(t, ctx)
   180  	defer registryContainer.Terminate(ctx)
   181  
   182  	container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
   183  		Image:   "paketobuildpacks/builder:buildpackless-full",
   184  		User:    "cnb",
   185  		TestDir: []string{"testdata", "TestMtaIntegration", "npm"},
   186  		Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
   187  	})
   188  
   189  	container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--buildpacks", "gcr.io/paketo-buildpacks/nodejs:0.14.0", "--containerImageName", "not-found", "--containerImageTag", "0.0.1", "--containerRegistryUrl", registryURL)
   190  
   191  	container.assertHasOutput(t, "Setting custom buildpacks: '[gcr.io/paketo-buildpacks/nodejs:0.14.0]'")
   192  	container.assertHasOutput(t, "Downloading buildpack 'gcr.io/paketo-buildpacks/nodejs:0.14.0' to /tmp/buildpacks_cache/sha256:")
   193  	container.assertHasOutput(t, "running command: /cnb/lifecycle/creator")
   194  	container.assertHasOutput(t, "Paketo NPM Start Buildpack")
   195  	container.assertHasOutput(t, fmt.Sprintf("Saving %s/not-found:0.0.1", registryURL))
   196  	container.assertHasOutput(t, "*** Images (sha256:")
   197  	container.assertHasOutput(t, "SUCCESS")
   198  	container.terminate(t)
   199  }
   200  
   201  func TestWrongBuilderProject(t *testing.T) {
   202  	t.Parallel()
   203  	container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
   204  		Image:   "nginx:latest",
   205  		TestDir: []string{"testdata", "TestMtaIntegration", "npm"},
   206  	})
   207  
   208  	container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--containerImageName", "not-found", "--containerImageTag", "0.0.1", "--containerRegistryUrl", "test")
   209  
   210  	container.assertHasOutput(t, "the provided dockerImage is not a valid builder")
   211  	container.terminate(t)
   212  }
   213  
   214  func TestBindings(t *testing.T) {
   215  	t.Parallel()
   216  	ctx := context.Background()
   217  	registryContainer := setupDockerRegistry(t, ctx)
   218  	defer registryContainer.Terminate(ctx)
   219  
   220  	container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
   221  		Image:   baseBuilder,
   222  		User:    "cnb",
   223  		TestDir: []string{"testdata"},
   224  		Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
   225  	})
   226  
   227  	container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--customConfig", "TestCnbIntegration/config.yml", "--containerImageName", "not-found", "--containerImageTag", "0.0.1", "--containerRegistryUrl", registryURL, "--path", "TestMtaIntegration/maven")
   228  
   229  	container.assertHasOutput(t, "bindings/maven-settings/settings.xml: only whitespace content allowed before start tag")
   230  	container.assertHasFile(t, "/tmp/platform/bindings/dummy-binding/type")
   231  	container.assertHasFile(t, "/tmp/platform/bindings/dummy-binding/dummy.yml")
   232  	container.terminate(t)
   233  }
   234  
   235  func TestMultiImage(t *testing.T) {
   236  	t.Parallel()
   237  	ctx := context.Background()
   238  	registryContainer := setupDockerRegistry(t, ctx)
   239  	defer registryContainer.Terminate(ctx)
   240  
   241  	container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
   242  		Image:   baseBuilder,
   243  		User:    "cnb",
   244  		TestDir: []string{"testdata", "TestCnbIntegration"},
   245  		Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
   246  	})
   247  
   248  	container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--customConfig", "config_multi_image.yml")
   249  
   250  	container.assertHasOutput(t, "Previous image with name \"localhost:5000/io-buildpacks-my-app:latest\" not found")
   251  	container.assertHasOutput(t, "Saving localhost:5000/io-buildpacks-my-app:latest...")
   252  	container.assertHasOutput(t, "Previous image with name \"localhost:5000/go-app:v1.0.0\" not found")
   253  	container.assertHasOutput(t, "Saving localhost:5000/go-app:v1.0.0...")
   254  	container.assertHasOutput(t, "Using cached buildpack")
   255  	container.assertHasOutput(t, "Saving localhost:5000/my-app2:latest...")
   256  	container.terminate(t)
   257  }
   258  
   259  func TestPreserveFiles(t *testing.T) {
   260  	t.Parallel()
   261  	ctx := context.Background()
   262  	registryContainer := setupDockerRegistry(t, ctx)
   263  	defer registryContainer.Terminate(ctx)
   264  
   265  	container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
   266  		Image:   baseBuilder,
   267  		User:    "cnb",
   268  		TestDir: []string{"testdata", "TestCnbIntegration"},
   269  		Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
   270  	})
   271  
   272  	container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--customConfig", "config_preserve_files.yml")
   273  	container.assertHasFile(t, "/project/project/node_modules/base/README.md")
   274  	container.assertHasFile(t, "/project/project/package-lock.json")
   275  	container.terminate(t)
   276  }
   277  
   278  func TestPreserveFilesIgnored(t *testing.T) {
   279  	t.Parallel()
   280  	ctx := context.Background()
   281  	registryContainer := setupDockerRegistry(t, ctx)
   282  	defer registryContainer.Terminate(ctx)
   283  
   284  	container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
   285  		Image:   baseBuilder,
   286  		User:    "cnb",
   287  		TestDir: []string{"testdata", "TestCnbIntegration"},
   288  		Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
   289  	})
   290  
   291  	container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--customConfig", "config_preserve_files.yml", "--path", "zip/go.zip", "--containerImageName", "go-zip")
   292  	container.assertHasOutput(t, "skipping preserving files because the source")
   293  	container.terminate(t)
   294  }