generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from SAP/addTests
test: add new tests
- Loading branch information
Showing
6 changed files
with
426 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestSetupConfigDir(t *testing.T) { | ||
|
||
t.Setenv("BTP_USERNAME", "username") | ||
t.Setenv("BTP_PASSWORD", "password") | ||
t.Setenv("BTP_GLOBALACCOUNT", "ga") | ||
|
||
curWd, _ := os.Getwd() | ||
|
||
setupConfigDir("configFolder") | ||
if _, err := os.Stat(filepath.Join(curWd, "configFolder")); os.IsNotExist(err) { | ||
t.Errorf("Directory should have been created") | ||
} | ||
|
||
if _, err := os.Stat(filepath.Join(curWd, "configFolder", "provider.tf")); os.IsNotExist(err) { | ||
t.Errorf("File should have been copied to existing directory") | ||
} | ||
|
||
os.RemoveAll(filepath.Join(curWd, "configFolder")) | ||
cleanup() | ||
} | ||
|
||
func setupTestEnvironment() (string, func()) { | ||
tempDir, err := os.MkdirTemp("", "providerTest") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return tempDir, func() { | ||
os.RemoveAll(tempDir) | ||
} | ||
} | ||
|
||
func TestConfigureProvider(t *testing.T) { | ||
tempDir, cleanup := setupTestEnvironment() | ||
defer cleanup() | ||
|
||
TmpFolder = tempDir | ||
|
||
t.Setenv("BTP_USERNAME", "testuser") | ||
t.Setenv("BTP_PASSWORD", "testpass") | ||
t.Setenv("BTP_GLOBALACCOUNT", "testaccount") | ||
t.Setenv("BTP_CLI_SERVER_URL", "https://test.com") | ||
|
||
configureProvider() | ||
expectedFilePath := filepath.Join(TmpFolder, "provider.tf") | ||
if _, err := os.Stat(expectedFilePath); os.IsNotExist(err) { | ||
t.Errorf("Expected file %s does not exist", expectedFilePath) | ||
} | ||
|
||
expectedContent := `terraform { | ||
required_providers { | ||
btp = { | ||
source = "SAP/btp" | ||
version = "1.6.0" | ||
} | ||
} | ||
} | ||
provider "btp" { | ||
globalaccount = "testaccount" | ||
cli_server_url="https://test.com" | ||
}` | ||
|
||
content, err := os.ReadFile(expectedFilePath) | ||
if err != nil { | ||
t.Fatalf("Failed to read file %s: %v", expectedFilePath, err) | ||
} | ||
|
||
if string(content) != expectedContent { | ||
t.Errorf("Content of the file %s does not match expected content.\nGot:\n%s\nExpected:\n%s", expectedFilePath, string(content), expectedContent) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package tfutils | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateFileWithContent(t *testing.T) { | ||
fileName := "testfile.txt" | ||
content := "This is a test." | ||
|
||
defer os.Remove(fileName) | ||
|
||
err := CreateFileWithContent(fileName, content) | ||
|
||
assert.NoError(t, err, "expected no error when creating file with content") | ||
|
||
fileData, err := os.ReadFile(fileName) | ||
assert.NoError(t, err, "expected no error when reading the file") | ||
assert.Equal(t, content, string(fileData), "file content should match the input content") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"Description": "Gets details about a subaccount.\n\n__Tip:__\nYou must be assigned to the admin or viewer role of the global account, directory, or subaccount.\n\n## Example Usage\n\n```terraform\n# Read a subaccount by ID\ndata \"btp_subaccount\" \"my_account_byid\" {\n id = \"6aa64c2f-38c1-49a9-b2e8-cf9fea769b7f\"\n}\n\n# Read a subaccount by region and subdomain\ndata \"btp_subaccount\" \"my_account_bysubdomain\" {\n region = \"eu10\"\n subdomain = \"my-subaccount-subdomain\"\n}\n```", | ||
"Arguments": {}, | ||
"Attributes": {}, | ||
"Import": "data \"btp_subaccount\" \"all\"{\n id = \"The ID of the subaccount\" \n}\n" | ||
} |
Oops, something went wrong.