github.com/databricks/cli@v0.203.0/internal/repos_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/databricks/databricks-sdk-go"
    10  	"github.com/databricks/databricks-sdk-go/apierr"
    11  	"github.com/databricks/databricks-sdk-go/service/workspace"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func synthesizeTemporaryRepoPath(t *testing.T, w *databricks.WorkspaceClient, ctx context.Context) string {
    17  	me, err := w.CurrentUser.Me(ctx)
    18  	require.NoError(t, err)
    19  	repoPath := fmt.Sprintf("/Repos/%s/%s", me.UserName, RandomName("empty-repo-integration-"))
    20  
    21  	// Cleanup if repo was created at specified path.
    22  	t.Cleanup(func() {
    23  		oi, err := w.Workspace.GetStatusByPath(ctx, repoPath)
    24  		if apierr.IsMissing(err) {
    25  			return
    26  		}
    27  		require.NoError(t, err)
    28  		err = w.Repos.DeleteByRepoId(ctx, oi.ObjectId)
    29  		require.NoError(t, err)
    30  	})
    31  
    32  	return repoPath
    33  }
    34  
    35  func createTemporaryRepo(t *testing.T, w *databricks.WorkspaceClient, ctx context.Context) (int64, string) {
    36  	repoPath := synthesizeTemporaryRepoPath(t, w, ctx)
    37  	repoInfo, err := w.Repos.Create(ctx, workspace.CreateRepo{
    38  		Path:     repoPath,
    39  		Url:      repoUrl,
    40  		Provider: "gitHub",
    41  	})
    42  	require.NoError(t, err)
    43  	return repoInfo.Id, repoPath
    44  }
    45  
    46  func TestReposCreateWithProvider(t *testing.T) {
    47  	t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
    48  
    49  	ctx := context.Background()
    50  	w, err := databricks.NewWorkspaceClient()
    51  	require.NoError(t, err)
    52  	repoPath := synthesizeTemporaryRepoPath(t, w, ctx)
    53  
    54  	_, stderr := RequireSuccessfulRun(t, "repos", "create", repoUrl, "gitHub", "--path", repoPath)
    55  	assert.Equal(t, "", stderr.String())
    56  
    57  	// Confirm the repo was created.
    58  	oi, err := w.Workspace.GetStatusByPath(ctx, repoPath)
    59  	assert.NoError(t, err)
    60  	assert.Equal(t, workspace.ObjectTypeRepo, oi.ObjectType)
    61  }
    62  
    63  func TestReposCreateWithoutProvider(t *testing.T) {
    64  	t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
    65  
    66  	ctx := context.Background()
    67  	w, err := databricks.NewWorkspaceClient()
    68  	require.NoError(t, err)
    69  	repoPath := synthesizeTemporaryRepoPath(t, w, ctx)
    70  
    71  	_, stderr := RequireSuccessfulRun(t, "repos", "create", repoUrl, "--path", repoPath)
    72  	assert.Equal(t, "", stderr.String())
    73  
    74  	// Confirm the repo was created.
    75  	oi, err := w.Workspace.GetStatusByPath(ctx, repoPath)
    76  	assert.NoError(t, err)
    77  	assert.Equal(t, workspace.ObjectTypeRepo, oi.ObjectType)
    78  }
    79  
    80  func TestReposGet(t *testing.T) {
    81  	t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
    82  
    83  	ctx := context.Background()
    84  	w, err := databricks.NewWorkspaceClient()
    85  	require.NoError(t, err)
    86  
    87  	repoId, repoPath := createTemporaryRepo(t, w, ctx)
    88  
    89  	// Get by ID
    90  	byIdOutput, stderr := RequireSuccessfulRun(t, "repos", "get", strconv.FormatInt(repoId, 10), "--output=json")
    91  	assert.Equal(t, "", stderr.String())
    92  
    93  	// Get by path
    94  	byPathOutput, stderr := RequireSuccessfulRun(t, "repos", "get", repoPath, "--output=json")
    95  	assert.Equal(t, "", stderr.String())
    96  
    97  	// Output should be the same
    98  	assert.Equal(t, byIdOutput.String(), byPathOutput.String())
    99  
   100  	// Get by path fails
   101  	_, stderr, err = RequireErrorRun(t, "repos", "get", repoPath+"-doesntexist", "--output=json")
   102  	assert.ErrorContains(t, err, "failed to look up repo")
   103  
   104  	// Get by path resolves to something other than a repo
   105  	_, stderr, err = RequireErrorRun(t, "repos", "get", "/Repos", "--output=json")
   106  	assert.ErrorContains(t, err, "is not a repo")
   107  }
   108  
   109  func TestReposUpdate(t *testing.T) {
   110  	t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
   111  
   112  	ctx := context.Background()
   113  	w, err := databricks.NewWorkspaceClient()
   114  	require.NoError(t, err)
   115  
   116  	repoId, repoPath := createTemporaryRepo(t, w, ctx)
   117  
   118  	// Update by ID
   119  	byIdOutput, stderr := RequireSuccessfulRun(t, "repos", "update", strconv.FormatInt(repoId, 10), "--branch", "ide")
   120  	assert.Equal(t, "", stderr.String())
   121  
   122  	// Update by path
   123  	byPathOutput, stderr := RequireSuccessfulRun(t, "repos", "update", repoPath, "--branch", "ide")
   124  	assert.Equal(t, "", stderr.String())
   125  
   126  	// Output should be the same
   127  	assert.Equal(t, byIdOutput.String(), byPathOutput.String())
   128  }
   129  
   130  func TestReposDeleteByID(t *testing.T) {
   131  	t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
   132  
   133  	ctx := context.Background()
   134  	w, err := databricks.NewWorkspaceClient()
   135  	require.NoError(t, err)
   136  
   137  	repoId, _ := createTemporaryRepo(t, w, ctx)
   138  
   139  	// Delete by ID
   140  	stdout, stderr := RequireSuccessfulRun(t, "repos", "delete", strconv.FormatInt(repoId, 10))
   141  	assert.Equal(t, "", stdout.String())
   142  	assert.Equal(t, "", stderr.String())
   143  
   144  	// Check it was actually deleted
   145  	_, err = w.Repos.GetByRepoId(ctx, repoId)
   146  	assert.True(t, apierr.IsMissing(err), err)
   147  }
   148  
   149  func TestReposDeleteByPath(t *testing.T) {
   150  	t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
   151  
   152  	ctx := context.Background()
   153  	w, err := databricks.NewWorkspaceClient()
   154  	require.NoError(t, err)
   155  
   156  	repoId, repoPath := createTemporaryRepo(t, w, ctx)
   157  
   158  	// Delete by path
   159  	stdout, stderr := RequireSuccessfulRun(t, "repos", "delete", repoPath)
   160  	assert.Equal(t, "", stdout.String())
   161  	assert.Equal(t, "", stderr.String())
   162  
   163  	// Check it was actually deleted
   164  	_, err = w.Repos.GetByRepoId(ctx, repoId)
   165  	assert.True(t, apierr.IsMissing(err), err)
   166  }