github.com/kubeshop/testkube@v1.17.23/contrib/executor/gradle/pkg/runner/runner_integration_test.go (about)

     1  // TODO create integration environment with `gradle` binary installed on OS level
     2  package runner
     3  
     4  import (
     5  	"context"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	cp "github.com/otiai10/copy"
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    14  	"github.com/kubeshop/testkube/pkg/envs"
    15  	"github.com/kubeshop/testkube/pkg/utils/test"
    16  )
    17  
    18  func TestRunGradle_Integration(t *testing.T) {
    19  	test.IntegrationTest(t)
    20  	t.Parallel()
    21  
    22  	ctx := context.Background()
    23  
    24  	t.Run("run gradle wrapper project with explicit target arg", func(t *testing.T) {
    25  		t.Parallel()
    26  		// setup
    27  		tempDir, err := os.MkdirTemp("", "*")
    28  		assert.NoErrorf(t, err, "failed to create temp dir: %v", err)
    29  		defer os.RemoveAll(tempDir)
    30  		repoDir := filepath.Join(tempDir, "repo")
    31  		assert.NoError(t, os.Mkdir(repoDir, 0755))
    32  		_ = cp.Copy("../../examples/hello-gradlew", repoDir)
    33  
    34  		// given
    35  		params := envs.Params{DataDir: tempDir}
    36  		runner, err := NewRunner(context.Background(), params)
    37  		assert.NoError(t, err)
    38  
    39  		execution := testkube.NewQueuedExecution()
    40  		execution.TestType = "gradle/project"
    41  		execution.Content = &testkube.TestContent{
    42  			Type_: string(testkube.TestContentTypeGitDir),
    43  			Repository: &testkube.Repository{
    44  				Uri:    "https://github.com/lreimer/hands-on-testkube.git",
    45  				Branch: "main",
    46  			},
    47  		}
    48  		execution.Command = []string{"gradle"}
    49  		execution.Args = []string{"test", "--no-daemon", "<taskName>", "-p", "<projectDir>"}
    50  
    51  		// when
    52  		result, err := runner.Run(ctx, *execution)
    53  
    54  		// then
    55  		assert.NoError(t, err)
    56  		assert.Equal(t, result.Status, testkube.ExecutionStatusPassed)
    57  	})
    58  
    59  	t.Run("run gradle project test with envs", func(t *testing.T) {
    60  		t.Parallel()
    61  		// setup
    62  		tempDir, err := os.MkdirTemp("", "*")
    63  		assert.NoErrorf(t, err, "failed to create temp dir: %v", err)
    64  		defer os.RemoveAll(tempDir)
    65  		repoDir := filepath.Join(tempDir, "repo")
    66  		assert.NoError(t, os.Mkdir(repoDir, 0755))
    67  		_ = cp.Copy("../../examples/hello-gradle", repoDir)
    68  
    69  		// given
    70  		params := envs.Params{DataDir: tempDir}
    71  		runner, err := NewRunner(context.Background(), params)
    72  		assert.NoError(t, err)
    73  
    74  		execution := testkube.NewQueuedExecution()
    75  		execution.TestType = "gradle/test"
    76  		execution.Content = &testkube.TestContent{
    77  			Type_: string(testkube.TestContentTypeGitDir),
    78  			Repository: &testkube.Repository{
    79  				Uri:    "https://github.com/lreimer/hands-on-testkube.git",
    80  				Branch: "main",
    81  			},
    82  		}
    83  		execution.Command = []string{"gradle"}
    84  		execution.Args = []string{"--no-daemon", "<taskName>", "-p", "<projectDir>"}
    85  		assert.NoError(t, os.Setenv("TESTKUBE_GRADLE", "true"))
    86  
    87  		// when
    88  		result, err := runner.Run(ctx, *execution)
    89  
    90  		// then
    91  		assert.NoError(t, err)
    92  		assert.Equal(t, result.Status, testkube.ExecutionStatusPassed)
    93  		assert.Len(t, result.Steps, 1)
    94  	})
    95  }
    96  
    97  func TestRunErrors_Integration(t *testing.T) {
    98  	test.IntegrationTest(t)
    99  	t.Parallel()
   100  
   101  	ctx := context.Background()
   102  
   103  	t.Run("no RUNNER_DATADIR", func(t *testing.T) {
   104  		t.Parallel()
   105  		// given
   106  		params := envs.Params{DataDir: "/unknown"}
   107  		runner, err := NewRunner(context.Background(), params)
   108  		assert.NoError(t, err)
   109  
   110  		execution := testkube.NewQueuedExecution()
   111  		execution.Command = []string{"gradle"}
   112  		execution.Args = []string{"--no-daemon", "<taskName>", "-p", "<projectDir>"}
   113  
   114  		// when
   115  		_, err = runner.Run(ctx, *execution)
   116  
   117  		// then
   118  		assert.Error(t, err)
   119  	})
   120  
   121  	t.Run("unsupported file-content", func(t *testing.T) {
   122  		t.Parallel()
   123  
   124  		tempDir, err := os.MkdirTemp("", "*")
   125  		assert.NoErrorf(t, err, "failed to create temp dir: %v", err)
   126  		defer os.RemoveAll(tempDir)
   127  
   128  		// given
   129  		params := envs.Params{DataDir: tempDir}
   130  		runner, err := NewRunner(context.Background(), params)
   131  		assert.NoError(t, err)
   132  
   133  		execution := testkube.NewQueuedExecution()
   134  		execution.TestType = "gradle/project"
   135  		execution.Content = testkube.NewStringTestContent("")
   136  		execution.Command = []string{"gradle"}
   137  		execution.Args = []string{"--no-daemon", "<taskName>", "-p", "<projectDir>"}
   138  
   139  		// when
   140  		_, err = runner.Run(ctx, *execution)
   141  
   142  		// then
   143  		assert.EqualError(t, err, "gradle executor handles only repository based tests, but repository is nil")
   144  	})
   145  
   146  	t.Run("no settings.gradle", func(t *testing.T) {
   147  		t.Parallel()
   148  		// setup
   149  		tempDir, err := os.MkdirTemp("", "*")
   150  		assert.NoErrorf(t, err, "failed to create temp dir: %v", err)
   151  		defer os.RemoveAll(tempDir)
   152  
   153  		repoDir := filepath.Join(tempDir, "repo")
   154  		assert.NoError(t, os.Mkdir(repoDir, 0755))
   155  
   156  		// given
   157  		params := envs.Params{DataDir: tempDir}
   158  		runner, err := NewRunner(context.Background(), params)
   159  		assert.NoError(t, err)
   160  
   161  		execution := testkube.NewQueuedExecution()
   162  		execution.TestType = "gradle/project"
   163  		execution.Content = &testkube.TestContent{
   164  			Type_: string(testkube.TestContentTypeGitDir),
   165  			Repository: &testkube.Repository{
   166  				Uri:    "https://github.com/lreimer/hands-on-testkube.git",
   167  				Branch: "main",
   168  			},
   169  		}
   170  		execution.Command = []string{"gradle"}
   171  		execution.Args = []string{"--no-daemon", "<taskName>", "-p", "<projectDir>"}
   172  		// when
   173  		result, err := runner.Run(ctx, *execution)
   174  
   175  		// then
   176  		assert.NoError(t, err)
   177  		assert.Equal(t, result.Status, testkube.ExecutionStatusFailed)
   178  		assert.Contains(t, result.ErrorMessage, "no")
   179  	})
   180  }