Skip to content

Commit

Permalink
NO-JIRA Tech Debt sweep: (#1512)
Browse files Browse the repository at this point in the history
- Replace "path" package with "filepath" package, which should be preferred for several reasons:
  1. filepath automatically uses the correct path separator for the current O/S (Windows, Linux, Mac)
  2. filepath automatically normalizes the paths, handling multiple separators, resolving relative paths, etc
  3. filepath includes some extra helper functions

- Ensures consistency in organizing files in the test directory

- Adds helper functions to retrieve certain directories to make this task more consistent (and removes cruft):
  - Root level dir
  - Test data dir
  - Test temp dir

- Adds some test cases

- Adds the golang platform, which can be used to call the provider during tests. Also added IsDevelopmentPlatform() check

- Improves some test cases
  • Loading branch information
bbbco authored Jan 31, 2025
1 parent 0997e25 commit ddd7a50
Show file tree
Hide file tree
Showing 45 changed files with 838 additions and 175 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ coverage_unit.out
cover_unit.html

consistency-errors.log.json

test/temp/*
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package architect_emergencygroup
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"terraform-provider-genesyscloud/genesyscloud/architect_flow"
"terraform-provider-genesyscloud/genesyscloud/provider"
"terraform-provider-genesyscloud/genesyscloud/util"
"terraform-provider-genesyscloud/genesyscloud/util/testrunner"
"testing"

"github.com/google/uuid"
Expand All @@ -27,7 +29,7 @@ func TestAccResourceArchitectEmergencyGroups(t *testing.T) {

flowResourceLabel = "test_flow"
flowName = "Terraform Emergency Test Flow " + uuid.NewString()
flowFilePath = "../../examples/resources/genesyscloud_flow/inboundcall_flow_example.yaml"
flowFilePath = filepath.Join(testrunner.RootDir, "/examples/resources/genesyscloud_flow/inboundcall_flow_example.yaml")
inboundCallConfig = fmt.Sprintf("inboundCall:\n name: %s\n defaultLanguage: en-us\n startUpRef: ./menus/menu[mainMenu]\n initialGreeting:\n tts: Archy says hi!!!\n menus:\n - menu:\n name: Main Menu\n audio:\n tts: You are at the Main Menu, press 9 to disconnect.\n refId: mainMenu\n choices:\n - menuDisconnect:\n name: Disconnect\n dtmf: digit_9", flowName)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"terraform-provider-genesyscloud/genesyscloud/provider"
"terraform-provider-genesyscloud/genesyscloud/util"
"terraform-provider-genesyscloud/genesyscloud/util/testrunner"
"testing"

"github.com/google/uuid"
Expand All @@ -20,7 +21,7 @@ func TestAccDataSourceFlow(t *testing.T) {
inboundcallConfig = fmt.Sprintf("inboundCall:\n name: %s\n defaultLanguage: en-us\n startUpRef: ./menus/menu[mainMenu]\n initialGreeting:\n tts: Archy says hi!!!\n menus:\n - menu:\n name: Main Menu\n audio:\n tts: You are at the Main Menu, press 9 to disconnect.\n refId: mainMenu\n choices:\n - menuDisconnect:\n name: Disconnect\n dtmf: digit_9", flowName)

flowResourceLabel = "test_flow"
filePath = filepath.Join("..", "..", "examples", "resources", "genesyscloud_flow", "inboundcall_flow_example.yaml")
filePath = filepath.Join(testrunner.RootDir, "examples", "resources", "genesyscloud_flow", "inboundcall_flow_example.yaml")
)

resource.Test(t, resource.TestCase{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"strconv"
"strings"
"terraform-provider-genesyscloud/genesyscloud/util/testrunner"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -21,8 +20,6 @@ func isForceUnlockEnabled(d *schema.ResourceData) bool {
}

func GenerateFlowResource(resourceLabel, srcFile, fileContent string, forceUnlock bool, substitutions ...string) string {
fullyQualifiedPath, _ := testrunner.NormalizePath(srcFile)

if fileContent != "" {
updateFile(srcFile, fileContent)
}
Expand All @@ -33,7 +30,7 @@ func GenerateFlowResource(resourceLabel, srcFile, fileContent string, forceUnloc
force_unlock = %v
%s
}
`, resourceLabel, strconv.Quote(srcFile), strconv.Quote(fullyQualifiedPath), forceUnlock, strings.Join(substitutions, "\n"))
`, resourceLabel, strconv.Quote(srcFile), strconv.Quote(srcFile), forceUnlock, strings.Join(substitutions, "\n"))

return flowResourceStr
}
Expand Down
16 changes: 9 additions & 7 deletions genesyscloud/architect_flow/resource_genesyscloud_flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strings"
"terraform-provider-genesyscloud/genesyscloud/provider"
"terraform-provider-genesyscloud/genesyscloud/util"
"terraform-provider-genesyscloud/genesyscloud/util/testrunner"
"testing"
"time"

Expand Down Expand Up @@ -60,7 +62,7 @@ func TestAccResourceArchFlowForceUnlock(t *testing.T) {
flowResourceLabel = "test_force_unlock_flow1"
flowName = "Terraform Flow Test ForceUnlock-" + uuid.NewString()
flowType = "INBOUNDCALL"
filePath = "../../examples/resources/genesyscloud_flow/inboundcall_flow_example.yaml"
filePath = filepath.Join(testrunner.RootDir, "examples/resources/genesyscloud_flow/inboundcall_flow_example.yaml")

inboundcallConfig1 = fmt.Sprintf("inboundCall:\n name: %s\n defaultLanguage: en-us\n startUpRef: ./menus/menu[mainMenu]\n initialGreeting:\n tts: Archy says hi!!!\n menus:\n - menu:\n name: Main Menu\n audio:\n tts: You are at the Main Menu, press 9 to disconnect.\n refId: mainMenu\n choices:\n - menuDisconnect:\n name: Disconnect\n dtmf: digit_9", flowName)
inboundcallConfig2 = fmt.Sprintf("inboundCall:\n name: %s\n defaultLanguage: en-us\n startUpRef: ./menus/menu[mainMenu]\n initialGreeting:\n tts: Archy says hi again!!!\n menus:\n - menu:\n name: Main Menu\n audio:\n tts: You are at the Main Menu, press 9 to disconnect.\n refId: mainMenu\n choices:\n - menuDisconnect:\n name: Disconnect\n dtmf: digit_9", flowName)
Expand Down Expand Up @@ -122,9 +124,9 @@ func TestAccResourceArchFlowStandard(t *testing.T) {
flowDescription2 = "test description 2"
flowType1 = "INBOUNDCALL"
flowType2 = "INBOUNDEMAIL"
filePath1 = "../../examples/resources/genesyscloud_flow/inboundcall_flow_example.yaml" //Have to use an explicit path because the filesha function gets screwy on relative class names
filePath2 = "../../examples/resources/genesyscloud_flow/inboundcall_flow_example2.yaml"
filePath3 = "../../examples/resources/genesyscloud_flow/inboundcall_flow_example3.yaml"
filePath1 = filepath.Join(testrunner.RootDir, "examples/resources/genesyscloud_flow/inboundcall_flow_example.yaml")
filePath2 = filepath.Join(testrunner.RootDir, "examples/resources/genesyscloud_flow/inboundcall_flow_example2.yaml")
filePath3 = filepath.Join(testrunner.RootDir, "examples/resources/genesyscloud_flow/inboundcall_flow_example3.yaml")

inboundcallConfig1 = fmt.Sprintf("inboundCall:\n name: %s\n description: %s\n defaultLanguage: en-us\n startUpRef: ./menus/menu[mainMenu]\n initialGreeting:\n tts: Archy says hi!!!\n menus:\n - menu:\n name: Main Menu\n audio:\n tts: You are at the Main Menu, press 9 to disconnect.\n refId: mainMenu\n choices:\n - menuDisconnect:\n name: Disconnect\n dtmf: digit_9", flowName, flowDescription1)
inboundcallConfig2 = fmt.Sprintf("inboundCall:\n name: %s\n description: %s\n defaultLanguage: en-us\n startUpRef: ./menus/menu[mainMenu]\n initialGreeting:\n tts: Archy says hi!!!!!\n menus:\n - menu:\n name: Main Menu\n audio:\n tts: You are at the Main Menu, press 9 to disconnect.\n refId: mainMenu\n choices:\n - menuDisconnect:\n name: Disconnect\n dtmf: digit_9", flowName, flowDescription2)
Expand Down Expand Up @@ -221,7 +223,7 @@ func TestAccResourceArchFlowSubstitutions(t *testing.T) {
flowName = "Terraform Flow Test-" + uuid.NewString()
flowDescription1 = "description 1"
flowDescription2 = "description 2"
filePath1 = "../../examples/resources/genesyscloud_flow/inboundcall_flow_example_substitutions.yaml"
filePath1 = filepath.Join(testrunner.RootDir, "/examples/resources/genesyscloud_flow/inboundcall_flow_example_substitutions.yaml")
)

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -326,8 +328,8 @@ func TestAccResourceArchFlowSubstitutionsWithMultipleTouch(t *testing.T) {
flowName = "Terraform Flow Test-" + uuid.NewString()
flowDescription1 = "description 1"
flowDescription2 = "description 2"
srcFile = "../../examples/resources/genesyscloud_flow/inboundcall_flow_example_substitutions.yaml"
destFile = "../../examples/resources/genesyscloud_flow/inboundcall_flow_example_holder.yaml"
srcFile = filepath.Join(testrunner.RootDir, "examples/resources/genesyscloud_flow/inboundcall_flow_example_substitutions.yaml")
destFile = filepath.Join(testrunner.RootDir, "examples/resources/genesyscloud_flow/inboundcall_flow_example_holder.yaml")
)

//Copy the example substitution file over to a temp file that can be manipulated and modified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,28 +154,26 @@ func generateFileVoiceFileDataBlock(
fileName string,
fileType string,
) string {
fullyQualifiedPath, _ := testrunner.NormalizePath(fileName)
return fmt.Sprintf(`
voice_file_data {
file_name = "%s"
file_type = "%s"
file_content_hash = filesha256("%s")
}
`, fileName, fileType, fullyQualifiedPath)
`, fileName, fileType, fileName)
}

func generateFileDtmfFileDataBlock(
fileName string,
fileType string,
) string {
fullyQualifiedPath, _ := testrunner.NormalizePath(fileName)
return fmt.Sprintf(`
dtmf_file_data {
file_name = "%s"
file_type = "%s"
file_content_hash = filesha256("%s")
}
`, fileName, fileType, fullyQualifiedPath)
`, fileName, fileType, fileName)
}

func verifyFileUpload(grammarResourcePath string, language string, fileType FileType, filename string) resource.TestCheckFunc {
Expand Down Expand Up @@ -277,7 +275,5 @@ func testVerifyGrammarLanguageDestroyed(state *terraform.State) error {
}

func generateFilePath(filename string) string {
testFolder := "../../test/data/resource/architect_grammar_language/"

return testFolder + filename
return testrunner.GetTestDataPath("resource", ResourceType, filename)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"terraform-provider-genesyscloud/genesyscloud/provider"
resourceExporter "terraform-provider-genesyscloud/genesyscloud/resource_exporter"
Expand Down Expand Up @@ -115,7 +115,7 @@ func ArchitectGrammarLanguageResolver(languageId, exportDirectory, subDirectory
sdkConfig := meta.(*provider.ProviderMeta).ClientConfig
proxy := getArchitectGrammarLanguageProxy(sdkConfig)

fullPath := path.Join(exportDirectory, subDirectory)
fullPath := filepath.Join(exportDirectory, subDirectory)
if err := os.MkdirAll(fullPath, os.ModePerm); err != nil {
return err
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func (d *grammarLanguageDownloader) setLanguageFileExtension() {
func (d *grammarLanguageDownloader) updatePathsInExportConfigMap() {
var (
fileDataMapKey string
filePath = path.Join(d.subDirectory, d.exportFileName)
filePath = filepath.Join(d.subDirectory, d.exportFileName)
)

switch d.fileType {
Expand All @@ -230,9 +230,9 @@ func (d *grammarLanguageDownloader) updatePathsInExportConfigMap() {
fileDataMap["file_name"] = filePath
fileHashVal := fmt.Sprintf(`${filesha256("%s")}`, filePath)
fileDataMap["file_content_hash"] = fileHashVal
fullPath := path.Join(d.exportDirectory, d.subDirectory)
fullPath := filepath.Join(d.exportDirectory, d.subDirectory)
d.resource.State.Attributes["file_name"] = filePath
hash, er := files.HashFileContent(path.Join(fullPath, d.exportFileName))
hash, er := files.HashFileContent(filepath.Join(fullPath, d.exportFileName))
if er != nil {
log.Printf("Error Calculating Hash '%s' ", er)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package architect_user_prompt

import (
"fmt"
"path"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -124,8 +123,8 @@ func TestAccResourceUserPromptWavFile(t *testing.T) {
userPromptDescription1 := "Test prompt with wav audio file"
userPromptResourceLang1 := "en-us"
userPromptResourceText1 := "This is a test greeting!"
userPromptResourceFileName1 := path.Join("../", testrunner.GetTestDataPath("test-prompt-01.wav"))
userPromptResourceFileName2 := path.Join("../", testrunner.GetTestDataPath("test-prompt-02.wav"))
userPromptResourceFileName1 := testrunner.GetTestDataPath("resource", ResourceType, "test-prompt-01.wav")
userPromptResourceFileName2 := testrunner.GetTestDataPath("resource", ResourceType, "test-prompt-02.wav")

userPromptAsset1 := UserPromptResourceStruct{
Language: userPromptResourceLang1,
Expand Down Expand Up @@ -220,7 +219,7 @@ func TestAccResourceUserPromptWavFileURL(t *testing.T) {

httpServerExitDone := &sync.WaitGroup{}
httpServerExitDone.Add(1)
srv := fileserver.Start(httpServerExitDone, path.Join("../", testrunner.GetTestDataPath()), 8100)
srv := fileserver.Start(httpServerExitDone, testrunner.GetTestDataPath("resource", ResourceType), 8100)

resource.Test(t, resource.TestCase{
PreCheck: func() { util.TestAccPreCheck(t) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"regexp"
"terraform-provider-genesyscloud/genesyscloud/provider"
resourceExporter "terraform-provider-genesyscloud/genesyscloud/resource_exporter"
"terraform-provider-genesyscloud/genesyscloud/util"
files "terraform-provider-genesyscloud/genesyscloud/util/files"
"terraform-provider-genesyscloud/genesyscloud/util/resourcedata"
"terraform-provider-genesyscloud/genesyscloud/util/testrunner"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mypurecloud/platform-client-sdk-go/v150/platformclientv2"
Expand Down Expand Up @@ -103,16 +102,16 @@ func updateFilenamesInExportConfigMap(configMap map[string]interface{}, audioDat
}
}
if fileName != "" {
fileNameVal := path.Join(subDir, fileName)
fileContentVal := fmt.Sprintf(`${filesha256("%s")}`, path.Join(subDir, fileName))
fileNameVal := filepath.Join(subDir, fileName)
fileContentVal := fmt.Sprintf(`${filesha256("%s")}`, filepath.Join(subDir, fileName))
r["filename"] = fileNameVal
r["file_content_hash"] = fileContentVal

if resourceID := findResourceID(res, languageStr); resourceID != "" {
res.State.Attributes[fmt.Sprintf("resources.%s.%s", resourceID, "filename")] = fileNameVal
res.State.Attributes[fmt.Sprintf("resources.%s.%s", resourceID, "file_content_hash")] = fileContentVal
fullPath := path.Join(exportDir, subDir)
hash, er := files.HashFileContent(path.Join(fullPath, fileName))
fullPath := filepath.Join(exportDir, subDir)
hash, er := files.HashFileContent(filepath.Join(fullPath, fileName))
if er != nil {
log.Printf("Error Calculating Hash '%s' ", er)
} else {
Expand Down Expand Up @@ -140,8 +139,7 @@ func GenerateUserPromptResource(userPrompt *UserPromptStruct) string {
for _, p := range userPrompt.Resources {
var fileContentHash string
if p.FileContentHash != util.NullValue {
fullyQualifiedPath, _ := testrunner.NormalizePath(p.FileContentHash)
fileContentHash = fmt.Sprintf(`filesha256("%s")`, fullyQualifiedPath)
fileContentHash = fmt.Sprintf(`filesha256("%s")`, p.FileContentHash)
} else {
fileContentHash = util.NullValue
}
Expand Down Expand Up @@ -174,7 +172,7 @@ func GenerateUserPromptResource(userPrompt *UserPromptStruct) string {
}

func ArchitectPromptAudioResolver(promptId, exportDirectory, subDirectory string, configMap map[string]any, meta any, resource resourceExporter.ResourceInfo) error {
fullPath := path.Join(exportDirectory, subDirectory)
fullPath := filepath.Join(exportDirectory, subDirectory)
if err := os.MkdirAll(fullPath, os.ModePerm); err != nil {
return err
}
Expand All @@ -198,7 +196,7 @@ func ArchitectPromptAudioResolver(promptId, exportDirectory, subDirectory string
log.Printf("Found %v resources with downloadable content for prompt '%s'", len(audioDataList), promptId)

for _, data := range audioDataList {
log.Printf("Downloading file '%s' from mediaUri", path.Join(fullPath, data.FileName))
log.Printf("Downloading file '%s' from mediaUri", filepath.Join(fullPath, data.FileName))
if err := files.DownloadExportFile(fullPath, data.FileName, data.MediaUri); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package flow_loglevel

import (
"fmt"
"path/filepath"
"terraform-provider-genesyscloud/genesyscloud/architect_flow"
"terraform-provider-genesyscloud/genesyscloud/provider"
"terraform-provider-genesyscloud/genesyscloud/util"
"terraform-provider-genesyscloud/genesyscloud/util/testrunner"
"testing"

"github.com/google/uuid"
Expand All @@ -22,7 +24,7 @@ func TestAccResourceFlowLogLevel(t *testing.T) {
flowLoglevelAll = "All"
flowLogLevelDisabled = "Disabled"
flowId = "${genesyscloud_flow." + flowResourceLabel + ".id}"
filePath = "../../examples/resources/genesyscloud_flow/inboundcall_flow_example.yaml"
filePath = filepath.Join(testrunner.RootDir, "examples/resources/genesyscloud_flow/inboundcall_flow_example.yaml")
inboundCallConfig = fmt.Sprintf("inboundCall:\n name: %s\n defaultLanguage: en-us\n startUpRef: ./menus/menu[mainMenu]\n initialGreeting:\n tts: Archy says hi!!!\n menus:\n - menu:\n name: Main Menu\n audio:\n tts: You are at the Main Menu, press 9 to disconnect.\n refId: mainMenu\n choices:\n - menuDisconnect:\n name: Disconnect\n dtmf: digit_9", flowName)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package journey_action_map

import (
"fmt"
"path"
"sync"
"terraform-provider-genesyscloud/genesyscloud/provider"
"terraform-provider-genesyscloud/genesyscloud/util"
Expand Down Expand Up @@ -39,7 +38,7 @@ func TestAccResourceJourneyActionMapScheduleGroups(t *testing.T) {
func runJourneyActionMapTestCaseWithFileServer(t *testing.T, testCaseName string, port int) {
httpServerExitDone := &sync.WaitGroup{}
httpServerExitDone.Add(1)
server := fileserver.Start(httpServerExitDone, path.Join("../", testrunner.GetTestDataPath(testrunner.ResourceTestType, ResourceType)), port)
server := fileserver.Start(httpServerExitDone, testrunner.GetTestDataPath(testrunner.ResourceTestType, ResourceType), port)

runJourneyActionMapTestCase(t, testCaseName)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package outbound_callanalysisresponseset

import (
"fmt"
"path/filepath"
"strconv"
"terraform-provider-genesyscloud/genesyscloud/architect_flow"
authDivision "terraform-provider-genesyscloud/genesyscloud/auth_division"
obContactList "terraform-provider-genesyscloud/genesyscloud/outbound_contact_list"
"terraform-provider-genesyscloud/genesyscloud/provider"
routingWrapupcode "terraform-provider-genesyscloud/genesyscloud/routing_wrapupcode"
"terraform-provider-genesyscloud/genesyscloud/util"
"terraform-provider-genesyscloud/genesyscloud/util/testrunner"
"testing"

"github.com/google/uuid"
Expand All @@ -35,7 +37,7 @@ func TestAccResourceOutboundCallAnalysisResponseSet(t *testing.T) {
contactListName = "Terraform Test Contact List " + uuid.NewString()
wrapupCodeName = "Terraform Test WrapUpCode " + uuid.NewString()
outboundFlowName = "Terraform Test Flow " + uuid.NewString()
outboundFlowFilePath = "../../examples/resources/genesyscloud_flow/outboundcall_flow_example.yaml"
outboundFlowFilePath = filepath.Join(testrunner.RootDir, "examples/resources/genesyscloud_flow/outboundcall_flow_example.yaml")

divResourceLabel = "test-division"
divName = "terraform-" + uuid.NewString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package outbound_campaign

import (
"fmt"
"path/filepath"
"terraform-provider-genesyscloud/genesyscloud/provider"
"terraform-provider-genesyscloud/genesyscloud/util"
"terraform-provider-genesyscloud/genesyscloud/util/testrunner"
"testing"

authDivision "terraform-provider-genesyscloud/genesyscloud/auth_division"
Expand All @@ -18,7 +20,7 @@ func TestAccDataSourceOutboundCampaign(t *testing.T) {
resourceLabel = "campaign"
campaignName = "Test Campaign " + uuid.NewString()
dataSourceLabel = "campaign_data"
outboundFlowFilePath = "../../examples/resources/genesyscloud_flow/outboundcall_flow_example.yaml"
outboundFlowFilePath = filepath.Join(testrunner.RootDir, "examples/resources/genesyscloud_flow/outboundcall_flow_example.yaml")
divResourceLabel = "test-outbound-campaign-division"
divName = "terraform-" + uuid.NewString()
)
Expand Down
Loading

0 comments on commit ddd7a50

Please sign in to comment.