Skip to content

Commit 440022f

Browse files
committed
Adding tests to confirm no-op when migrating from SDKv2 to the framework (#112)
1 parent 3000b90 commit 440022f

File tree

1 file changed

+255
-9
lines changed

1 file changed

+255
-9
lines changed

internal/provider/data_source_archive_file_test.go

Lines changed: 255 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ func TestAccArchiveFile_Basic(t *testing.T) {
1717
f := filepath.Join(td, "zip_file_acc_test.zip")
1818

1919
var fileSize string
20-
r.Test(t, r.TestCase{
20+
r.ParallelTest(t, r.TestCase{
2121
ProtoV5ProviderFactories: protoV5ProviderFactories(),
2222
Steps: []r.TestStep{
2323
{
2424
Config: testAccArchiveFileContentConfig(f),
2525
Check: r.ComposeTestCheckFunc(
26-
testAccArchiveFileExists(f, &fileSize),
26+
testAccArchiveFileSize(f, &fileSize),
2727
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
2828
r.TestCheckResourceAttr(
2929
"data.archive_file.foo", "output_base64sha256", "P7VckxoEiUO411WN3nwuS/yOBL4zsbVWkQU9E1I5H6c=",
@@ -39,7 +39,7 @@ func TestAccArchiveFile_Basic(t *testing.T) {
3939
{
4040
Config: testAccArchiveFileFileConfig(f),
4141
Check: r.ComposeTestCheckFunc(
42-
testAccArchiveFileExists(f, &fileSize),
42+
testAccArchiveFileSize(f, &fileSize),
4343
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
4444
r.TestCheckResourceAttr(
4545
"data.archive_file.foo", "output_base64sha256", "UTE4f5cWfaR6p0HfOrLILxgvF8UUwiJTjTRwjQTgdWs=",
@@ -55,7 +55,7 @@ func TestAccArchiveFile_Basic(t *testing.T) {
5555
{
5656
Config: testAccArchiveFileDirConfig(f),
5757
Check: r.ComposeTestCheckFunc(
58-
testAccArchiveFileExists(f, &fileSize),
58+
testAccArchiveFileSize(f, &fileSize),
5959
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
6060
r.TestCheckResourceAttr(
6161
"data.archive_file.foo", "output_base64sha256", "ydB8wtq8nK9vQ77VH6YTwoHmyljK46jW+uIJSwCzNpo=",
@@ -71,22 +71,250 @@ func TestAccArchiveFile_Basic(t *testing.T) {
7171
{
7272
Config: testAccArchiveFileDirExcludesConfig(f),
7373
Check: r.ComposeTestCheckFunc(
74-
testAccArchiveFileExists(f, &fileSize),
74+
testAccArchiveFileSize(f, &fileSize),
7575
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
7676
),
7777
},
7878
{
79-
Config: testAccArchiveFileMultiConfig(f),
79+
Config: testAccArchiveFileMultiSourceConfig(f),
8080
Check: r.ComposeTestCheckFunc(
81-
testAccArchiveFileExists(f, &fileSize),
81+
testAccArchiveFileSize(f, &fileSize),
8282
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
8383
),
8484
},
8585
},
8686
})
8787
}
8888

89-
func testAccArchiveFileExists(filename string, fileSize *string) r.TestCheckFunc {
89+
func TestDataSource_UpgradeFromVersion2_2_0_ContentConfig(t *testing.T) {
90+
td := testTempDir(t)
91+
defer os.RemoveAll(td)
92+
93+
f := filepath.Join(td, "zip_file_acc_test_upgrade_content_config.zip")
94+
95+
var fileSize string
96+
97+
r.ParallelTest(t, r.TestCase{
98+
Steps: []r.TestStep{
99+
{
100+
ExternalProviders: map[string]r.ExternalProvider{
101+
"archive": {
102+
VersionConstraint: "2.2.0",
103+
Source: "hashicorp/archive",
104+
},
105+
},
106+
Config: testAccArchiveFileContentConfig(f),
107+
Check: r.ComposeTestCheckFunc(
108+
testAccArchiveFileSize(f, &fileSize),
109+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
110+
r.TestCheckResourceAttr(
111+
"data.archive_file.foo", "output_base64sha256", "P7VckxoEiUO411WN3nwuS/yOBL4zsbVWkQU9E1I5H6c=",
112+
),
113+
r.TestCheckResourceAttr(
114+
"data.archive_file.foo", "output_md5", "ea35f0444ea9a3d5641d8760bc2815cc",
115+
),
116+
r.TestCheckResourceAttr(
117+
"data.archive_file.foo", "output_sha", "019c79c4dc14dbe1edb3e467b2de6a6aad148717",
118+
),
119+
),
120+
},
121+
{
122+
ProtoV5ProviderFactories: protoV5ProviderFactories(),
123+
Config: testAccArchiveFileContentConfig(f),
124+
Check: r.ComposeTestCheckFunc(
125+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
126+
r.TestCheckResourceAttr(
127+
"data.archive_file.foo", "output_base64sha256", "P7VckxoEiUO411WN3nwuS/yOBL4zsbVWkQU9E1I5H6c=",
128+
),
129+
r.TestCheckResourceAttr(
130+
"data.archive_file.foo", "output_md5", "ea35f0444ea9a3d5641d8760bc2815cc",
131+
),
132+
r.TestCheckResourceAttr(
133+
"data.archive_file.foo", "output_sha", "019c79c4dc14dbe1edb3e467b2de6a6aad148717",
134+
),
135+
),
136+
},
137+
},
138+
})
139+
}
140+
141+
func TestDataSource_UpgradeFromVersion2_2_0_FileConfig(t *testing.T) {
142+
td := testTempDir(t)
143+
defer os.RemoveAll(td)
144+
145+
f := filepath.Join(td, "zip_file_acc_test_upgrade_file_config.zip")
146+
147+
var fileSize string
148+
149+
r.ParallelTest(t, r.TestCase{
150+
Steps: []r.TestStep{
151+
{
152+
ExternalProviders: map[string]r.ExternalProvider{
153+
"archive": {
154+
VersionConstraint: "2.2.0",
155+
Source: "hashicorp/archive",
156+
},
157+
},
158+
Config: testAccArchiveFileFileConfig(f),
159+
Check: r.ComposeTestCheckFunc(
160+
testAccArchiveFileSize(f, &fileSize),
161+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
162+
r.TestCheckResourceAttr(
163+
"data.archive_file.foo", "output_base64sha256", "UTE4f5cWfaR6p0HfOrLILxgvF8UUwiJTjTRwjQTgdWs=",
164+
),
165+
r.TestCheckResourceAttr(
166+
"data.archive_file.foo", "output_md5", "59fbc9e62af3cbc2f588f97498240dae",
167+
),
168+
r.TestCheckResourceAttr(
169+
"data.archive_file.foo", "output_sha", "ce4ee1450ab93ac86e11446649e44cea907b6568",
170+
),
171+
),
172+
},
173+
{
174+
ProtoV5ProviderFactories: protoV5ProviderFactories(),
175+
Config: testAccArchiveFileFileConfig(f),
176+
Check: r.ComposeTestCheckFunc(
177+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
178+
r.TestCheckResourceAttr(
179+
"data.archive_file.foo", "output_base64sha256", "UTE4f5cWfaR6p0HfOrLILxgvF8UUwiJTjTRwjQTgdWs=",
180+
),
181+
r.TestCheckResourceAttr(
182+
"data.archive_file.foo", "output_md5", "59fbc9e62af3cbc2f588f97498240dae",
183+
),
184+
r.TestCheckResourceAttr(
185+
"data.archive_file.foo", "output_sha", "ce4ee1450ab93ac86e11446649e44cea907b6568",
186+
),
187+
),
188+
},
189+
},
190+
})
191+
}
192+
193+
func TestDataSource_UpgradeFromVersion2_2_0_DirConfig(t *testing.T) {
194+
td := testTempDir(t)
195+
defer os.RemoveAll(td)
196+
197+
f := filepath.Join(td, "zip_file_acc_test_upgrade_dir_config.zip")
198+
199+
var fileSize string
200+
201+
r.ParallelTest(t, r.TestCase{
202+
Steps: []r.TestStep{
203+
{
204+
ExternalProviders: map[string]r.ExternalProvider{
205+
"archive": {
206+
VersionConstraint: "2.2.0",
207+
Source: "hashicorp/archive",
208+
},
209+
},
210+
Config: testAccArchiveFileDirConfig(f),
211+
Check: r.ComposeTestCheckFunc(
212+
testAccArchiveFileSize(f, &fileSize),
213+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
214+
r.TestCheckResourceAttr(
215+
"data.archive_file.foo", "output_base64sha256", "ydB8wtq8nK9vQ77VH6YTwoHmyljK46jW+uIJSwCzNpo=",
216+
),
217+
r.TestCheckResourceAttr(
218+
"data.archive_file.foo", "output_md5", "b73f64a383716070aa4a29563b8b14d4",
219+
),
220+
r.TestCheckResourceAttr(
221+
"data.archive_file.foo", "output_sha", "76d20a402eefd1cfbdc47886abd4e0909616c191",
222+
),
223+
),
224+
},
225+
{
226+
ProtoV5ProviderFactories: protoV5ProviderFactories(),
227+
Config: testAccArchiveFileDirConfig(f),
228+
Check: r.ComposeTestCheckFunc(
229+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
230+
r.TestCheckResourceAttr(
231+
"data.archive_file.foo", "output_base64sha256", "ydB8wtq8nK9vQ77VH6YTwoHmyljK46jW+uIJSwCzNpo=",
232+
),
233+
r.TestCheckResourceAttr(
234+
"data.archive_file.foo", "output_md5", "b73f64a383716070aa4a29563b8b14d4",
235+
),
236+
r.TestCheckResourceAttr(
237+
"data.archive_file.foo", "output_sha", "76d20a402eefd1cfbdc47886abd4e0909616c191",
238+
),
239+
),
240+
},
241+
},
242+
})
243+
}
244+
245+
func TestDataSource_UpgradeFromVersion2_2_0_DirExcludesConfig(t *testing.T) {
246+
td := testTempDir(t)
247+
defer os.RemoveAll(td)
248+
249+
f := filepath.Join(td, "zip_file_acc_test_upgrade_dir_excludes.zip")
250+
251+
var fileSize, outputSha string
252+
253+
r.ParallelTest(t, r.TestCase{
254+
Steps: []r.TestStep{
255+
{
256+
ExternalProviders: map[string]r.ExternalProvider{
257+
"archive": {
258+
VersionConstraint: "2.2.0",
259+
Source: "hashicorp/archive",
260+
},
261+
},
262+
Config: testAccArchiveFileDirExcludesConfig(f),
263+
Check: r.ComposeTestCheckFunc(
264+
testAccArchiveFileSize(f, &fileSize),
265+
testExtractResourceAttr("data.archive_file.foo", "output_sha", &outputSha),
266+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
267+
),
268+
},
269+
{
270+
ProtoV5ProviderFactories: protoV5ProviderFactories(),
271+
Config: testAccArchiveFileDirExcludesConfig(f),
272+
Check: r.ComposeTestCheckFunc(
273+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
274+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_sha", &outputSha),
275+
),
276+
},
277+
},
278+
})
279+
}
280+
281+
func TestDataSource_UpgradeFromVersion2_2_0_SourceConfig(t *testing.T) {
282+
td := testTempDir(t)
283+
defer os.RemoveAll(td)
284+
285+
f := filepath.Join(td, "zip_file_acc_test_upgrade_source.zip")
286+
287+
var fileSize, outputSha string
288+
289+
r.ParallelTest(t, r.TestCase{
290+
Steps: []r.TestStep{
291+
{
292+
ExternalProviders: map[string]r.ExternalProvider{
293+
"archive": {
294+
VersionConstraint: "2.2.0",
295+
Source: "hashicorp/archive",
296+
},
297+
},
298+
Config: testAccArchiveFileMultiSourceConfig(f),
299+
Check: r.ComposeTestCheckFunc(
300+
testAccArchiveFileSize(f, &fileSize),
301+
testExtractResourceAttr("data.archive_file.foo", "output_sha", &outputSha),
302+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
303+
),
304+
},
305+
{
306+
ProtoV5ProviderFactories: protoV5ProviderFactories(),
307+
Config: testAccArchiveFileMultiSourceConfig(f),
308+
Check: r.ComposeTestCheckFunc(
309+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
310+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_sha", &outputSha),
311+
),
312+
},
313+
},
314+
})
315+
}
316+
317+
func testAccArchiveFileSize(filename string, fileSize *string) r.TestCheckFunc {
90318
return func(s *terraform.State) error {
91319
*fileSize = ""
92320
fi, err := os.Stat(filename)
@@ -142,7 +370,7 @@ data "archive_file" "foo" {
142370
`, filepath.ToSlash(outputPath))
143371
}
144372

145-
func testAccArchiveFileMultiConfig(outputPath string) string {
373+
func testAccArchiveFileMultiSourceConfig(outputPath string) string {
146374
return fmt.Sprintf(`
147375
data "archive_file" "foo" {
148376
type = "zip"
@@ -166,3 +394,21 @@ func testTempDir(t *testing.T) string {
166394
}
167395
return tmp
168396
}
397+
398+
func testExtractResourceAttr(resourceName string, attributeName string, attributeValue *string) r.TestCheckFunc {
399+
return func(s *terraform.State) error {
400+
rs, ok := s.RootModule().Resources[resourceName]
401+
if !ok {
402+
return fmt.Errorf("resource name %s not found in state", resourceName)
403+
}
404+
405+
attrValue, ok := rs.Primary.Attributes[attributeName]
406+
if !ok {
407+
return fmt.Errorf("attribute %s not found in resource %s state", attributeName, resourceName)
408+
}
409+
410+
*attributeValue = attrValue
411+
412+
return nil
413+
}
414+
}

0 commit comments

Comments
 (0)