Skip to content

Commit 15d80cb

Browse files
committed
Add tests for new action
1 parent 225c718 commit 15d80cb

File tree

1 file changed

+270
-0
lines changed

1 file changed

+270
-0
lines changed
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package transcribe_test
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"slices"
10+
"testing"
11+
12+
"github.com/aws/aws-sdk-go-v2/service/transcribe"
13+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
14+
"github.com/hashicorp/terraform-plugin-testing/terraform"
15+
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
16+
"github.com/hashicorp/terraform-provider-aws/internal/conns"
17+
"github.com/hashicorp/terraform-provider-aws/names"
18+
)
19+
20+
func TestAccTranscribeStartTranscriptionJobAction_basic(t *testing.T) {
21+
ctx := acctest.Context(t)
22+
rName := acctest.RandomWithPrefix(t, acctest.ResourcePrefix)
23+
bucketName := acctest.RandomWithPrefix(t, acctest.ResourcePrefix)
24+
25+
resource.ParallelTest(t, resource.TestCase{
26+
PreCheck: func() { acctest.PreCheck(ctx, t) },
27+
ErrorCheck: acctest.ErrorCheck(t, names.TranscribeServiceID),
28+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
29+
Steps: []resource.TestStep{
30+
{
31+
Config: testAccStartTranscriptionJobActionConfig_basic(rName, bucketName),
32+
Check: resource.ComposeTestCheckFunc(
33+
testAccCheckTranscriptionJobExists(ctx, rName),
34+
testAccCheckTranscriptionJobStatus(ctx, rName, "IN_PROGRESS", "COMPLETED"),
35+
),
36+
},
37+
},
38+
})
39+
}
40+
41+
func TestAccTranscribeStartTranscriptionJobAction_identifyLanguage(t *testing.T) {
42+
ctx := acctest.Context(t)
43+
rName := acctest.RandomWithPrefix(t, acctest.ResourcePrefix)
44+
bucketName := acctest.RandomWithPrefix(t, acctest.ResourcePrefix)
45+
46+
resource.ParallelTest(t, resource.TestCase{
47+
PreCheck: func() { acctest.PreCheck(ctx, t) },
48+
ErrorCheck: acctest.ErrorCheck(t, names.TranscribeServiceID),
49+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
50+
Steps: []resource.TestStep{
51+
{
52+
Config: testAccStartTranscriptionJobActionConfig_identifyLanguage(rName, bucketName),
53+
Check: resource.ComposeTestCheckFunc(
54+
testAccCheckTranscriptionJobExists(ctx, rName),
55+
testAccCheckTranscriptionJobStatus(ctx, rName, "IN_PROGRESS", "COMPLETED"),
56+
testAccCheckTranscriptionJobIdentifyLanguage(ctx, rName, true),
57+
),
58+
},
59+
},
60+
})
61+
}
62+
63+
func TestAccTranscribeStartTranscriptionJobAction_withOutputLocation(t *testing.T) {
64+
ctx := acctest.Context(t)
65+
rName := acctest.RandomWithPrefix(t, acctest.ResourcePrefix)
66+
bucketName := acctest.RandomWithPrefix(t, acctest.ResourcePrefix)
67+
68+
resource.ParallelTest(t, resource.TestCase{
69+
PreCheck: func() { acctest.PreCheck(ctx, t) },
70+
ErrorCheck: acctest.ErrorCheck(t, names.TranscribeServiceID),
71+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
72+
Steps: []resource.TestStep{
73+
{
74+
Config: testAccStartTranscriptionJobActionConfig_withOutputLocation(rName, bucketName),
75+
Check: resource.ComposeTestCheckFunc(
76+
testAccCheckTranscriptionJobExists(ctx, rName),
77+
testAccCheckTranscriptionJobStatus(ctx, rName, "IN_PROGRESS", "COMPLETED"),
78+
),
79+
},
80+
},
81+
})
82+
}
83+
84+
func testAccCheckTranscriptionJobExists(ctx context.Context, jobName string) resource.TestCheckFunc {
85+
return func(s *terraform.State) error {
86+
conn := acctest.Provider.Meta().(*conns.AWSClient).TranscribeClient(ctx)
87+
88+
input := &transcribe.GetTranscriptionJobInput{
89+
TranscriptionJobName: &jobName,
90+
}
91+
92+
_, err := conn.GetTranscriptionJob(ctx, input)
93+
if err != nil {
94+
return fmt.Errorf("transcription job %s not found: %w", jobName, err)
95+
}
96+
97+
return nil
98+
}
99+
}
100+
101+
func testAccCheckTranscriptionJobStatus(ctx context.Context, jobName string, expectedStatuses ...string) resource.TestCheckFunc {
102+
return func(s *terraform.State) error {
103+
conn := acctest.Provider.Meta().(*conns.AWSClient).TranscribeClient(ctx)
104+
105+
input := &transcribe.GetTranscriptionJobInput{
106+
TranscriptionJobName: &jobName,
107+
}
108+
109+
output, err := conn.GetTranscriptionJob(ctx, input)
110+
if err != nil {
111+
return fmt.Errorf("error getting transcription job %s: %w", jobName, err)
112+
}
113+
114+
if output.TranscriptionJob == nil {
115+
return fmt.Errorf("transcription job %s not found", jobName)
116+
}
117+
118+
actualStatus := string(output.TranscriptionJob.TranscriptionJobStatus)
119+
if slices.Contains(expectedStatuses, actualStatus) {
120+
return nil
121+
}
122+
123+
return fmt.Errorf("expected transcription job %s status to be one of %v, got %s", jobName, expectedStatuses, actualStatus)
124+
}
125+
}
126+
127+
func testAccCheckTranscriptionJobIdentifyLanguage(ctx context.Context, jobName string, expected bool) resource.TestCheckFunc {
128+
return func(s *terraform.State) error {
129+
conn := acctest.Provider.Meta().(*conns.AWSClient).TranscribeClient(ctx)
130+
131+
input := &transcribe.GetTranscriptionJobInput{
132+
TranscriptionJobName: &jobName,
133+
}
134+
135+
output, err := conn.GetTranscriptionJob(ctx, input)
136+
if err != nil {
137+
return fmt.Errorf("error getting transcription job %s: %w", jobName, err)
138+
}
139+
140+
if output.TranscriptionJob == nil {
141+
return fmt.Errorf("transcription job %s not found", jobName)
142+
}
143+
144+
actual := output.TranscriptionJob.IdentifyLanguage != nil && *output.TranscriptionJob.IdentifyLanguage
145+
if actual != expected {
146+
return fmt.Errorf("expected transcription job %s identify_language to be %t, got %t", jobName, expected, actual)
147+
}
148+
149+
return nil
150+
}
151+
}
152+
153+
func testAccStartTranscriptionJobActionConfig_basic(rName, bucketName string) string {
154+
return fmt.Sprintf(`
155+
resource "aws_s3_bucket" "test" {
156+
bucket = %[2]q
157+
force_destroy = true
158+
}
159+
160+
resource "aws_s3_object" "test" {
161+
bucket = aws_s3_bucket.test.bucket
162+
key = "test-audio.wav"
163+
source = "test-fixtures/test-audio.wav"
164+
}
165+
166+
action "aws_transcribe_start_transcription_job" "test" {
167+
config {
168+
transcription_job_name = %[1]q
169+
media_file_uri = "s3://${aws_s3_bucket.test.bucket}/${aws_s3_object.test.key}"
170+
language_code = "en-US"
171+
timeout = 600
172+
}
173+
}
174+
175+
resource "terraform_data" "test" {
176+
triggers_replace = [
177+
aws_s3_object.test.etag
178+
]
179+
180+
input = "completed"
181+
182+
lifecycle {
183+
action_trigger {
184+
events = [before_create, before_update]
185+
actions = [action.aws_transcribe_start_transcription_job.test]
186+
}
187+
}
188+
}
189+
`, rName, bucketName)
190+
}
191+
192+
func testAccStartTranscriptionJobActionConfig_identifyLanguage(rName, bucketName string) string {
193+
return fmt.Sprintf(`
194+
resource "aws_s3_bucket" "test" {
195+
bucket = %[2]q
196+
force_destroy = true
197+
}
198+
199+
resource "aws_s3_object" "test" {
200+
bucket = aws_s3_bucket.test.bucket
201+
key = "test-audio.wav"
202+
source = "test-fixtures/test-audio.wav"
203+
}
204+
205+
action "aws_transcribe_start_transcription_job" "test" {
206+
config {
207+
transcription_job_name = %[1]q
208+
media_file_uri = "s3://${aws_s3_bucket.test.bucket}/${aws_s3_object.test.key}"
209+
identify_language = true
210+
timeout = 600
211+
}
212+
}
213+
214+
resource "terraform_data" "test" {
215+
triggers_replace = [
216+
aws_s3_object.test.etag
217+
]
218+
219+
input = "completed"
220+
221+
lifecycle {
222+
action_trigger {
223+
events = [before_create, before_update]
224+
actions = [action.aws_transcribe_start_transcription_job.test]
225+
}
226+
}
227+
}
228+
`, rName, bucketName)
229+
}
230+
231+
func testAccStartTranscriptionJobActionConfig_withOutputLocation(rName, bucketName string) string {
232+
return fmt.Sprintf(`
233+
resource "aws_s3_bucket" "test" {
234+
bucket = %[2]q
235+
force_destroy = true
236+
}
237+
238+
resource "aws_s3_object" "test" {
239+
bucket = aws_s3_bucket.test.bucket
240+
key = "test-audio.wav"
241+
source = "test-fixtures/test-audio.wav"
242+
}
243+
244+
action "aws_transcribe_start_transcription_job" "test" {
245+
config {
246+
transcription_job_name = %[1]q
247+
media_file_uri = "s3://${aws_s3_bucket.test.bucket}/${aws_s3_object.test.key}"
248+
language_code = "en-US"
249+
output_bucket_name = aws_s3_bucket.test.bucket
250+
output_key = "transcripts/%[1]s.json"
251+
timeout = 600
252+
}
253+
}
254+
255+
resource "terraform_data" "test" {
256+
triggers_replace = [
257+
aws_s3_object.test.etag
258+
]
259+
260+
input = "completed"
261+
262+
lifecycle {
263+
action_trigger {
264+
events = [before_create, before_update]
265+
actions = [action.aws_transcribe_start_transcription_job.test]
266+
}
267+
}
268+
}
269+
`, rName, bucketName)
270+
}

0 commit comments

Comments
 (0)