Skip to content

Commit

Permalink
add sync_type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattJsonar committed Sep 5, 2024
1 parent 7296d60 commit 98ada49
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
22 changes: 20 additions & 2 deletions dsfhub/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,26 @@ type Config struct {

var missingAPITokenMessage = "DSF HUB API Token must be provided"
var missingDSFHostMessage = "DSF HUB host/API endpoint must be provided"
var validSyncTypes = []string { "SYNC_GW_BLOCKING", "SYNC_GW_NON_BLOCKING", "DO_NOT_SYNC_GW"}
var invalidSyncTypeMessage = "Invalid sync_type. Available values: " + strings.Join(validSyncTypes, ", ")

// Client configures and returns a fully initialized DSF Client
func (c *Config) Client() (interface{}, error) {
// Check DSFToken env var
// Check DSFToken
if strings.TrimSpace(c.DSFHUBToken) == "" {
return nil, errors.New(missingAPITokenMessage)
}
// Check DSFHost env var
// Check DSFHost
if strings.TrimSpace(c.DSFHUBHost) == "" {
return nil, errors.New(missingDSFHostMessage)
}
// Check sync_type param
syncType, exists := c.Params["syncType"]
if exists {
if ! IsValidSyncType(syncType) {
return nil, errors.New(invalidSyncTypeMessage)
}
}

// Create client
client := NewClient(c)
Expand All @@ -46,3 +55,12 @@ func (c *Config) Client() (interface{}, error) {

return client, nil
}

func IsValidSyncType (sync_type string) bool {
for _, valid_sync_type := range validSyncTypes {
if sync_type == valid_sync_type {
return true
}
}
return false
}
27 changes: 27 additions & 0 deletions dsfhub/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,30 @@ func TestValidDSFHUBToken(t *testing.T) {
t.Error("Client should not be nil")
}
}

func TestInvalidSyncType(t *testing.T) {
log.Printf("======================== BEGIN TEST ========================")
log.Printf("[INFO] Running test TestInvalidSyncType \n")

server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.URL.String() != "/dsf/api/v2/gateways" {
t.Errorf("Should have have hit /gateways endpoint. Got: %s", req.URL.String())
}
rw.Write([]byte(`{ "data": [ { "applianceId": 1, "applianceType": "DSF_HUB", "id": "a1b2c3-4d5e-6f7g-8h9i-9adf5a7d8a72-172.16.1.123", "name": "ba-dsf-4.12-hub", "hostname": "172.16.1.123", "serverType": "IMPERVA WAREHOUSE", "sonar": { "jsonarUid": "a1b2c3-4d5e-6f7g-8h9i-678910" } } ] }`))
}))
defer server.Close()


invalidSyncType := "BAD_SYNC_TYPE"
log.Printf("[INFO] Configuring client with sync_type: '%v'\n", invalidSyncType)
log.Printf("[DEBUG] Test server URL %v \n", server.URL)

config := Config{DSFHUBToken: "good", DSFHUBHost: server.URL, Params: map[string]string{"syncType": invalidSyncType}}
client, err := config.Client()
if err == nil {
t.Errorf("Should have received an error, got a client: %q", client)
}
if err.Error() != invalidSyncTypeMessage {
t.Errorf("Should have invalid sync_type message, got: %s", err)
}
}
2 changes: 1 addition & 1 deletion dsfhub/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {
"Example: 'true/false'. Can be set via TF_VAR_insecure_ssl environment variable.",

"sync_type": "Determines whether to sync asset creation/update operations with the Agentless gateways. Available values:\n" +
"SYNC_GW_BLOCKING: The operation is synchronous and blocks until all gateways have been updated.\n" +
"SYNC_GW_BLOCKING: The operation is synchronous and blocks until all gateways have been updated. This means that, if syncing the assets to Agentless Gateways fails, the provider will throw an error and not continue. This may result in a difference between the state of which Terraform is aware and the assets that were actually imported.\n" +
"SYNC_GW_NON_BLOCKING: The operation is asynchronous and returns immediately.\n" +
"DO_NOT_SYNC_GW: The operation is synchronous and does not update the gateways.\n" +
"Default: SYNC_GW_BLOCKING",
Expand Down

0 comments on commit 98ada49

Please sign in to comment.