@@ -8,11 +8,142 @@ import (
88 "testing"
99
1010 "github.com/databrickslabs/terraform-provider-databricks/common"
11+ "github.com/databrickslabs/terraform-provider-databricks/qa"
1112 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1213 "github.com/stretchr/testify/assert"
1314)
1415
15- func GenString (times int ) []byte {
16+ func TestCreateFileFails (t * testing.T ) {
17+ qa .HTTPFixturesApply (t , []qa.HTTPFixture {
18+ {
19+ Method : "POST" ,
20+ Resource : "/api/2.0/dbfs/create" ,
21+ ExpectedRequest : createHandle {
22+ Path : "/create-fails" ,
23+ Overwrite : true ,
24+ },
25+ Status : 404 ,
26+ Response : common .NotFound ("fails" ),
27+ },
28+ {
29+ Method : "POST" ,
30+ Resource : "/api/2.0/dbfs/close" ,
31+ },
32+ }, func (ctx context.Context , client * common.DatabricksClient ) {
33+ a := NewDbfsAPI (ctx , client )
34+ err := a .Create ("/create-fails" , []byte ("abc" ), true )
35+ assert .EqualError (t , err , "cannot create handle: fails" )
36+ })
37+ }
38+
39+ func TestCreateFile_AddBlockFails (t * testing.T ) {
40+ qa .HTTPFixturesApply (t , []qa.HTTPFixture {
41+ {
42+ Method : "POST" ,
43+ Resource : "/api/2.0/dbfs/create" ,
44+ ExpectedRequest : createHandle {
45+ Path : "/add-fails" ,
46+ Overwrite : true ,
47+ },
48+ Response : handleResponse {123 },
49+ },
50+ {
51+ Method : "POST" ,
52+ Resource : "/api/2.0/dbfs/add-block" ,
53+ ExpectedRequest : addBlock {
54+ Data : "YWJj" ,
55+ Handle : 123 ,
56+ },
57+ Status : 404 ,
58+ Response : common .NotFound ("fails" ),
59+ },
60+ {
61+ Method : "POST" ,
62+ Resource : "/api/2.0/dbfs/close" ,
63+ },
64+ }, func (ctx context.Context , client * common.DatabricksClient ) {
65+ a := NewDbfsAPI (ctx , client )
66+ err := a .Create ("/add-fails" , []byte ("abc" ), true )
67+ assert .EqualError (t , err , "cannot add block: fails" )
68+ })
69+ }
70+
71+ func TestCreateFile_CloseFails (t * testing.T ) {
72+ qa .HTTPFixturesApply (t , []qa.HTTPFixture {
73+ {
74+ Method : "POST" ,
75+ Resource : "/api/2.0/dbfs/create" ,
76+ },
77+ {
78+ Method : "POST" ,
79+ Resource : "/api/2.0/dbfs/add-block" ,
80+ },
81+ {
82+ Method : "POST" ,
83+ Resource : "/api/2.0/dbfs/close" ,
84+ Status : 404 ,
85+ Response : common .NotFound ("fails" ),
86+ },
87+ }, func (ctx context.Context , client * common.DatabricksClient ) {
88+ a := NewDbfsAPI (ctx , client )
89+ err := a .Create ("/close-fails" , []byte ("abc" ), true )
90+ assert .EqualError (t , err , "cannot close handle: fails" )
91+ })
92+ }
93+
94+ func TestDbfsListRecursiveFails (t * testing.T ) {
95+ qa .HTTPFixturesApply (t , []qa.HTTPFixture {
96+ {
97+ Method : "GET" ,
98+ Resource : "/api/2.0/dbfs/list?path=abc" ,
99+ Status : 404 ,
100+ Response : common .NotFound ("fails" ),
101+ },
102+ {
103+ Method : "GET" ,
104+ Resource : "/api/2.0/dbfs/list?path=sub" ,
105+ Response : FileList {
106+ Files : []FileInfo {
107+ {
108+ Path : "bcd" ,
109+ IsDir : true ,
110+ },
111+ },
112+ },
113+ },
114+ {
115+ Method : "GET" ,
116+ ReuseRequest : true ,
117+ Resource : "/api/2.0/dbfs/list?path=bcd" ,
118+ Status : 404 ,
119+ Response : common .NotFound ("fails" ),
120+ },
121+ }, func (ctx context.Context , client * common.DatabricksClient ) {
122+ a := NewDbfsAPI (ctx , client )
123+ _ , err := a .List ("abc" , true )
124+ assert .EqualError (t , err , "cannot list abc: fails" )
125+ _ , err = a .List ("sub" , true )
126+ assert .EqualError (t , err , "cannot list subfolder: cannot list bcd: fails" )
127+ _ , err = a .List ("bcd" , false )
128+ assert .EqualError (t , err , "cannot list bcd: fails" )
129+ })
130+ }
131+
132+ func TestDbfsReadFails (t * testing.T ) {
133+ qa .HTTPFixturesApply (t , []qa.HTTPFixture {
134+ {
135+ MatchAny : true ,
136+ Status : 404 ,
137+ Response : common .NotFound ("fails" ),
138+ },
139+ }, func (ctx context.Context , client * common.DatabricksClient ) {
140+ a := NewDbfsAPI (ctx , client )
141+ _ , err := a .Read ("abc" )
142+ assert .EqualError (t , err , "cannot read abc: fails" )
143+ })
144+ }
145+
146+ func genString (times int ) []byte {
16147 var buf bytes.Buffer
17148 for i := 0 ; i < times ; i ++ {
18149 buf .WriteString ("Hello world how are you doing?\n " )
@@ -27,22 +158,16 @@ func TestAccCreateFile(t *testing.T) {
27158
28159 randomName := acctest .RandStringFromCharSet (10 , acctest .CharSetAlphaNum )
29160 dir := "/client-test/" + randomName
30- dir2 := dir + "/dir2/"
31161 path := dir + "/randomfile"
32162 path2 := dir + "/dir2/randomfile"
33163 path3 := dir + "/dir2/randomfile2"
34164
35- randomStr := GenString ( 500 )
165+ randomStr := genString ( 10 )
36166 client := common .NewClientFromEnvironment ()
37167
38168 dbfsAPI := NewDbfsAPI (context .Background (), client )
39- err := dbfsAPI .Mkdirs (dir )
40- assert .NoError (t , err , err )
41-
42- err = dbfsAPI .Mkdirs (dir2 )
43- assert .NoError (t , err , err )
44169
45- err = dbfsAPI .Create (path , randomStr , true )
170+ err : = dbfsAPI .Create (path , randomStr , true )
46171 assert .NoError (t , err , err )
47172
48173 err = dbfsAPI .Create (path2 , randomStr , true )
0 commit comments