-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aad0681
commit cdf4353
Showing
37 changed files
with
12,605 additions
and
3 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,27 @@ | ||
name: Go Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
go-tests: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup Golang | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version-file: go.mod | ||
|
||
- run: go version | ||
|
||
- name: Run GARM Go Tests | ||
run: make go-test |
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,129 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConfig_Validate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
config *Config | ||
errString error | ||
}{ | ||
{ | ||
name: "ValidConfig", | ||
config: &Config{ | ||
Zone: "europe-west1-d", | ||
ProjectId: "my-project", | ||
NetworkID: "my-network", | ||
SubnetworkID: "my-subnetwork", | ||
CredentialsFile: "path/to/credentials.json", | ||
ExternalIPAccess: true, | ||
}, | ||
errString: nil, | ||
}, | ||
{ | ||
name: "MissingRegion", | ||
config: &Config{ | ||
ProjectId: "my-project", | ||
NetworkID: "my-network", | ||
SubnetworkID: "my-subnetwork", | ||
CredentialsFile: "path/to/credentials.json", | ||
ExternalIPAccess: true, | ||
}, | ||
errString: fmt.Errorf("missing region"), | ||
}, | ||
{ | ||
name: "MissingProjectID", | ||
config: &Config{ | ||
Zone: "europe-west1-d", | ||
NetworkID: "my-network", | ||
SubnetworkID: "my-subnetwork", | ||
CredentialsFile: "path/to/credentials.json", | ||
}, | ||
errString: fmt.Errorf("missing project_id"), | ||
}, | ||
{ | ||
name: "MissingNetworkID", | ||
config: &Config{ | ||
Zone: "europe-west1-d", | ||
ProjectId: "my-project", | ||
SubnetworkID: "my-subnetwork", | ||
CredentialsFile: "path/to/credentials.json", | ||
ExternalIPAccess: true, | ||
}, | ||
errString: fmt.Errorf("missing network_id"), | ||
}, | ||
{ | ||
name: "MissingSubnetworkID", | ||
config: &Config{ | ||
Zone: "europe-west1-d", | ||
ProjectId: "my-project", | ||
NetworkID: "my-network", | ||
CredentialsFile: "path/to/credentials.json", | ||
ExternalIPAccess: true, | ||
}, | ||
errString: fmt.Errorf("missing subnetwork_id"), | ||
}, | ||
{ | ||
name: "MissingCredentialsFile", | ||
config: &Config{ | ||
Zone: "europe-west1-d", | ||
ProjectId: "my-project", | ||
NetworkID: "my-network", | ||
SubnetworkID: "my-subnetwork", | ||
ExternalIPAccess: true, | ||
}, | ||
errString: fmt.Errorf("missing credentials_file"), | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := tc.config.Validate() | ||
if tc.errString == nil { | ||
require.Nil(t, err) | ||
} else { | ||
require.Equal(t, tc.errString, err) | ||
} | ||
|
||
}) | ||
} | ||
} | ||
|
||
func TestNewConfig(t *testing.T) { | ||
mockData := ` | ||
project_id = "garm-testing" | ||
zone = "europe-west1-d" | ||
network_id = "projects/garm-testing/global/networks/garm" | ||
subnetwork_id = "projects/garm-testing/regions/europe-west1/subnetworks/garm" | ||
credentials_file = "/home/ubuntu/service-account-key.json" | ||
external_ip_access = true | ||
` | ||
// Create a temporary file | ||
tmpFile, err := os.CreateTemp("", "config-*.toml") | ||
require.NoError(t, err, "Failed to create temporary file") | ||
defer os.Remove(tmpFile.Name()) // Clean up after the test | ||
|
||
// Write the mock TOML data to the temporary file | ||
_, err = tmpFile.WriteString(mockData) | ||
require.NoError(t, err, "Failed to write to temporary file") | ||
err = tmpFile.Close() | ||
require.NoError(t, err, "Failed to close temporary file") | ||
|
||
// Use the temporary file path as the argument to NewConfig | ||
cfg, err := NewConfig(tmpFile.Name()) | ||
require.NoError(t, err, "NewConfig returned an error") | ||
|
||
// Validate the content of the Config object | ||
require.Equal(t, "garm-testing", cfg.ProjectId, "ProjectId value did not match expected") | ||
require.Equal(t, "europe-west1-d", cfg.Zone, "Zone value did not match expected") | ||
require.Equal(t, "projects/garm-testing/global/networks/garm", cfg.NetworkID, "NetworkId value did not match expected") | ||
require.Equal(t, "projects/garm-testing/regions/europe-west1/subnetworks/garm", cfg.SubnetworkID, "SubnetworkId value did not match expected") | ||
require.Equal(t, "/home/ubuntu/service-account-key.json", cfg.CredentialsFile, "CredentialsFile value did not match expected") | ||
require.Equal(t, true, cfg.ExternalIPAccess, "ExternalIpAccess value did not match expected") | ||
} |
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
Oops, something went wrong.