11package policies
22
33import (
4- "context"
54 "testing"
65
76 "github.com/databricks/databricks-sdk-go/apierr"
8- "github.com/databricks/terraform-provider-databricks/common"
97 "github.com/databricks/terraform-provider-databricks/qa"
10- "github.com/stretchr/testify/assert"
11- "github.com/stretchr/testify/require"
128)
139
1410func TestDataSourceClusterPolicy (t * testing.T ) {
15- d , err := qa.ResourceFixture {
11+ qa.ResourceFixture {
1612 Fixtures : []qa.HTTPFixture {
1713 {
1814 Method : "GET" ,
19- Resource : "/api/2.0/policies/clusters/list" ,
15+ Resource : "/api/2.0/policies/clusters/list? " ,
2016 Response : ClusterPolicyList {
2117 Policies : []ClusterPolicy {
2218 {
@@ -32,37 +28,46 @@ func TestDataSourceClusterPolicy(t *testing.T) {
3228 NonWritable : true ,
3329 Resource : DataSourceClusterPolicy (),
3430 ID : "." ,
35- State : map [string ]any {
36- "name" : "policy" ,
31+ HCL : `name = "policy"` ,
32+ }.ApplyAndExpectData (t , map [string ]any {
33+ "id" : "abc" ,
34+ "definition" : `{"abc":"123"}` ,
35+ })
36+ }
37+
38+ func TestDataSourceClusterPolicyError (t * testing.T ) {
39+ qa.ResourceFixture {
40+ Fixtures : []qa.HTTPFixture {
41+ {
42+ Method : "GET" ,
43+ Resource : "/api/2.0/policies/clusters/list?" ,
44+ Status : 404 ,
45+ Response : apierr.APIError {
46+ Message : "searching_error" ,
47+ },
48+ },
3749 },
38- }.Apply (t )
39- require .NoError (t , err )
40- assert .Equal (t , "abc" , d .Id ())
41- assert .Equal (t , `{"abc":"123"}` , d .Get ("definition" ).(string ))
50+ Read : true ,
51+ NonWritable : true ,
52+ Resource : DataSourceClusterPolicy (),
53+ ID : "." ,
54+ HCL : `name = "policy"` ,
55+ }.ExpectError (t , "searching_error" )
4256}
4357
4458func TestDataSourceClusterPolicyNotFound (t * testing.T ) {
45- qa .HTTPFixturesApply (t , []qa.HTTPFixture {
46- {
47- Method : "GET" ,
48- Resource : "/api/2.0/policies/clusters/list" ,
49- Status : 404 ,
50- Response : apierr.APIError {
51- Message : "searching_error" ,
59+ qa.ResourceFixture {
60+ Fixtures : []qa.HTTPFixture {
61+ {
62+ Method : "GET" ,
63+ Resource : "/api/2.0/policies/clusters/list?" ,
64+ Response : ClusterPolicyList {},
5265 },
5366 },
54- {
55- Method : "GET" ,
56- Resource : "/api/2.0/policies/clusters/list" ,
57- Response : ClusterPolicyList {},
58- },
59- }, func (ctx context.Context , client * common.DatabricksClient ) {
60- poolsAPI := NewClusterPoliciesAPI (ctx , client )
61-
62- _ , err := getPolicy (poolsAPI , "searching_error" )
63- assert .EqualError (t , err , "searching_error" )
64-
65- _ , err = getPolicy (poolsAPI , "unknown" )
66- assert .EqualError (t , err , "cluster policy 'unknown' wasn't found" )
67- })
67+ Read : true ,
68+ NonWritable : true ,
69+ Resource : DataSourceClusterPolicy (),
70+ ID : "." ,
71+ HCL : `name = "policy"` ,
72+ }.ExpectError (t , "Policy named 'policy' does not exist" )
6873}
0 commit comments