github.com/databricks/cli@v0.203.0/bundle/config/mutator/override_compute_test.go (about)

     1  package mutator_test
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/databricks/cli/bundle"
     9  	"github.com/databricks/cli/bundle/config"
    10  	"github.com/databricks/cli/bundle/config/mutator"
    11  	"github.com/databricks/cli/bundle/config/resources"
    12  	"github.com/databricks/databricks-sdk-go/service/compute"
    13  	"github.com/databricks/databricks-sdk-go/service/jobs"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestOverrideDevelopment(t *testing.T) {
    19  	os.Setenv("DATABRICKS_CLUSTER_ID", "")
    20  	bundle := &bundle.Bundle{
    21  		Config: config.Root{
    22  			Bundle: config.Bundle{
    23  				Mode:      config.Development,
    24  				ComputeID: "newClusterID",
    25  			},
    26  			Resources: config.Resources{
    27  				Jobs: map[string]*resources.Job{
    28  					"job1": {JobSettings: &jobs.JobSettings{
    29  						Name: "job1",
    30  						Tasks: []jobs.Task{
    31  							{
    32  								NewCluster: &compute.ClusterSpec{},
    33  							},
    34  							{
    35  								ExistingClusterId: "cluster2",
    36  							},
    37  						},
    38  					}},
    39  				},
    40  			},
    41  		},
    42  	}
    43  
    44  	m := mutator.OverrideCompute()
    45  	err := m.Apply(context.Background(), bundle)
    46  	require.NoError(t, err)
    47  	assert.Nil(t, bundle.Config.Resources.Jobs["job1"].Tasks[0].NewCluster)
    48  	assert.Equal(t, "newClusterID", bundle.Config.Resources.Jobs["job1"].Tasks[0].ExistingClusterId)
    49  	assert.Equal(t, "newClusterID", bundle.Config.Resources.Jobs["job1"].Tasks[1].ExistingClusterId)
    50  }
    51  
    52  func TestOverrideDevelopmentEnv(t *testing.T) {
    53  	os.Setenv("DATABRICKS_CLUSTER_ID", "newClusterId")
    54  	bundle := &bundle.Bundle{
    55  		Config: config.Root{
    56  			Resources: config.Resources{
    57  				Jobs: map[string]*resources.Job{
    58  					"job1": {JobSettings: &jobs.JobSettings{
    59  						Name: "job1",
    60  						Tasks: []jobs.Task{
    61  							{
    62  								NewCluster: &compute.ClusterSpec{},
    63  							},
    64  							{
    65  								ExistingClusterId: "cluster2",
    66  							},
    67  						},
    68  					}},
    69  				},
    70  			},
    71  		},
    72  	}
    73  
    74  	m := mutator.OverrideCompute()
    75  	err := m.Apply(context.Background(), bundle)
    76  	require.NoError(t, err)
    77  	assert.Equal(t, "cluster2", bundle.Config.Resources.Jobs["job1"].Tasks[1].ExistingClusterId)
    78  }
    79  
    80  func TestOverrideProduction(t *testing.T) {
    81  	bundle := &bundle.Bundle{
    82  		Config: config.Root{
    83  			Bundle: config.Bundle{
    84  				ComputeID: "newClusterID",
    85  			},
    86  			Resources: config.Resources{
    87  				Jobs: map[string]*resources.Job{
    88  					"job1": {JobSettings: &jobs.JobSettings{
    89  						Name: "job1",
    90  						Tasks: []jobs.Task{
    91  							{
    92  								NewCluster: &compute.ClusterSpec{},
    93  							},
    94  							{
    95  								ExistingClusterId: "cluster2",
    96  							},
    97  						},
    98  					}},
    99  				},
   100  			},
   101  		},
   102  	}
   103  
   104  	m := mutator.OverrideCompute()
   105  	err := m.Apply(context.Background(), bundle)
   106  	require.Error(t, err)
   107  }
   108  
   109  func TestOverrideProductionEnv(t *testing.T) {
   110  	os.Setenv("DATABRICKS_CLUSTER_ID", "newClusterId")
   111  	bundle := &bundle.Bundle{
   112  		Config: config.Root{
   113  			Resources: config.Resources{
   114  				Jobs: map[string]*resources.Job{
   115  					"job1": {JobSettings: &jobs.JobSettings{
   116  						Name: "job1",
   117  						Tasks: []jobs.Task{
   118  							{
   119  								NewCluster: &compute.ClusterSpec{},
   120  							},
   121  							{
   122  								ExistingClusterId: "cluster2",
   123  							},
   124  						},
   125  					}},
   126  				},
   127  			},
   128  		},
   129  	}
   130  
   131  	m := mutator.OverrideCompute()
   132  	err := m.Apply(context.Background(), bundle)
   133  	require.NoError(t, err)
   134  }