github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/test/e2e/02_component_actions_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package test provides e2e tests for Jackal.
     5  package test
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestComponentActions(t *testing.T) {
    16  	t.Log("E2E: Testing component actions")
    17  
    18  	// Note these files will be created in the package directory, not CWD.
    19  	createArtifacts := []string{
    20  		"examples/component-actions/test-create-before.txt",
    21  		"examples/component-actions/test-create-after.txt",
    22  	}
    23  	deployArtifacts := []string{
    24  		"test-deploy-before.txt",
    25  		"test-deploy-after.txt",
    26  	}
    27  
    28  	allArtifacts := append(deployArtifacts, createArtifacts...)
    29  	e2e.CleanFiles(allArtifacts...)
    30  	defer e2e.CleanFiles(allArtifacts...)
    31  
    32  	/* Create */
    33  	// Try creating the package to test the onCreate actions.
    34  	stdOut, stdErr, err := e2e.Jackal("package", "create", "examples/component-actions", "--confirm")
    35  	require.NoError(t, err, stdOut, stdErr)
    36  	require.Contains(t, stdErr, "Completed \"Create a test file\"")
    37  	require.Contains(t, stdErr, "Completed \"touch test-create-after.txt\"")
    38  	require.Contains(t, stdErr, "multiline!")
    39  	require.Contains(t, stdErr, "updates!")
    40  	require.Contains(t, stdErr, "realtime!")
    41  
    42  	// Test for package create prepare artifacts.
    43  	for _, artifact := range createArtifacts {
    44  		require.FileExists(t, artifact)
    45  	}
    46  
    47  	// Test to ensure the deploy scripts are not executed.
    48  	for _, artifact := range deployArtifacts {
    49  		require.NoFileExists(t, artifact)
    50  	}
    51  
    52  	path := fmt.Sprintf("build/jackal-package-component-actions-%s.tar.zst", e2e.Arch)
    53  	t.Run("action on-deploy-and-remove", func(t *testing.T) {
    54  		t.Parallel()
    55  
    56  		// Deploy the simple script that should pass.
    57  		stdOut, stdErr, err = e2e.Jackal("package", "deploy", path, "--components=on-deploy-and-remove", "--confirm")
    58  		require.NoError(t, err, stdOut, stdErr)
    59  
    60  		// Check that the deploy artifacts were created.
    61  		for _, artifact := range deployArtifacts {
    62  			require.FileExists(t, artifact)
    63  		}
    64  
    65  		// Remove the simple script that should pass.
    66  		stdOut, stdErr, err = e2e.Jackal("package", "remove", path, "--components=on-deploy-and-remove", "--confirm")
    67  		require.NoError(t, err, stdOut, stdErr)
    68  
    69  		// Check that the deploy artifacts were removed.
    70  		for _, artifact := range deployArtifacts {
    71  			require.NoFileExists(t, artifact)
    72  		}
    73  	})
    74  
    75  	t.Run("action on-deploy-with-timeout", func(t *testing.T) {
    76  		t.Parallel()
    77  		// Deploy the simple action that should fail the timeout.
    78  		stdOut, stdErr, err = e2e.Jackal("package", "deploy", path, "--components=on-deploy-with-timeout", "--confirm")
    79  		require.Error(t, err, stdOut, stdErr)
    80  		require.Contains(t, stdErr, "after 1 second")
    81  		require.Contains(t, stdErr, "😭😭😭 this action failed because it took too long to run 😭😭😭")
    82  	})
    83  
    84  	t.Run("action on-deploy-with-variable", func(t *testing.T) {
    85  		t.Parallel()
    86  
    87  		// Test using a Jackal Variable within the action
    88  		stdOut, stdErr, err = e2e.Jackal("package", "deploy", path, "--components=on-deploy-with-variable", "--confirm")
    89  		require.NoError(t, err, stdOut, stdErr)
    90  		require.Contains(t, stdErr, "the dog says ruff")
    91  
    92  	})
    93  
    94  	t.Run("action on-deploy-with-dynamic-variable", func(t *testing.T) {
    95  		t.Parallel()
    96  		// Test using dynamic and multiple-variables
    97  		stdOut, stdErr, err = e2e.Jackal("package", "deploy", path, "--components=on-deploy-with-dynamic-variable,on-deploy-with-multiple-variables", "--confirm")
    98  		require.NoError(t, err, stdOut, stdErr)
    99  		require.Contains(t, stdErr, "the cat says meow")
   100  		require.Contains(t, stdErr, "the dog says ruff")
   101  		require.Contains(t, stdErr, "the snake says hiss")
   102  		require.Contains(t, stdErr, "with a TF_VAR, the snake also says hiss")
   103  
   104  	})
   105  
   106  	t.Run("action on-deploy-with-env-var", func(t *testing.T) {
   107  		t.Parallel()
   108  		deployWithEnvVarArtifact := "test-filename-from-env.txt"
   109  
   110  		// Test using environment variables
   111  		stdOut, stdErr, err = e2e.Jackal("package", "deploy", path, "--components=on-deploy-with-env-var", "--confirm")
   112  		require.NoError(t, err, stdOut, stdErr)
   113  		require.FileExists(t, deployWithEnvVarArtifact)
   114  
   115  		// Remove the env var file at the end of the test
   116  		e2e.CleanFiles(deployWithEnvVarArtifact)
   117  	})
   118  
   119  	t.Run("action on-deploy-with-template", func(t *testing.T) {
   120  		t.Parallel()
   121  		deployTemplatedArtifact := "test-templated.txt"
   122  
   123  		// Test using a templated file but without dynamic variables
   124  		stdOut, stdErr, err = e2e.Jackal("package", "deploy", path, "--components=on-deploy-with-template-use-of-variable", "--confirm")
   125  		require.NoError(t, err, stdOut, stdErr)
   126  		outTemplated, err := os.ReadFile(deployTemplatedArtifact)
   127  		require.NoError(t, err)
   128  		require.Contains(t, string(outTemplated), "The dog says ruff")
   129  		require.Contains(t, string(outTemplated), "The cat says ###JACKAL_VAR_CAT_SOUND###")
   130  		require.Contains(t, string(outTemplated), "The snake says ###JACKAL_VAR_SNAKE_SOUND###")
   131  
   132  		// Remove the templated file so we can test with dynamic variables
   133  		e2e.CleanFiles(deployTemplatedArtifact)
   134  
   135  		// Test using a templated file with dynamic variables
   136  		stdOut, stdErr, err = e2e.Jackal("package", "deploy", path, "--components=on-deploy-with-template-use-of-variable,on-deploy-with-dynamic-variable,on-deploy-with-multiple-variables", "--confirm")
   137  		require.NoError(t, err, stdOut, stdErr)
   138  		outTemplated, err = os.ReadFile(deployTemplatedArtifact)
   139  		require.NoError(t, err)
   140  		require.Contains(t, string(outTemplated), "The dog says ruff")
   141  		require.Contains(t, string(outTemplated), "The cat says meow")
   142  		require.Contains(t, string(outTemplated), "The snake says hiss")
   143  
   144  		// Remove the templated file at the end of the test
   145  		e2e.CleanFiles(deployTemplatedArtifact)
   146  	})
   147  
   148  	t.Run("action on-deploy-immediate-failure", func(t *testing.T) {
   149  		t.Parallel()
   150  		stdOut, stdErr, err = e2e.Jackal("package", "deploy", path, "--components=on-deploy-immediate-failure", "--confirm")
   151  		require.Error(t, err, stdOut, stdErr)
   152  		require.Contains(t, stdErr, "Failed to deploy package")
   153  		// regression test to ensure that failed commands are not erroneously flagged as a timeout
   154  		require.NotContains(t, stdErr, "timed out")
   155  	})
   156  }