forked from infobloxopen/bloxone-go-client
-
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.
Update go client to support default tags
- Loading branch information
1 parent
9ccdea9
commit 5e1dc48
Showing
4 changed files
with
244 additions
and
5 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
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,133 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/infobloxopen/bloxone-go-client/internal" | ||
) | ||
|
||
func TestConfiguration_internal(t *testing.T) { | ||
type fields struct { | ||
ClientName string | ||
CSPURL string | ||
APIKey string | ||
HTTPClient *http.Client | ||
DefaultTags map[string]string | ||
} | ||
type args struct { | ||
basePath string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want *internal.Configuration | ||
wantErr bool | ||
}{ | ||
{ | ||
"empty API key", | ||
fields{ | ||
APIKey: "", | ||
}, | ||
args{basePath: ""}, | ||
nil, | ||
true, | ||
}, | ||
{"empty clientName", | ||
fields{ | ||
ClientName: "", | ||
}, | ||
args{basePath: ""}, | ||
nil, | ||
true, | ||
}, | ||
{ | ||
"empty DefaultTags", | ||
fields{ | ||
ClientName: "terraform/v1.1#yug278872h", | ||
APIKey: "12323455", | ||
}, | ||
args{basePath: ""}, | ||
&internal.Configuration{ | ||
DefaultHeader: map[string]string{ | ||
HeaderAuthorization: "Token 12323455", | ||
HeaderClient: "terraform/v1.1#yug278872h", | ||
HeaderSDK: sdkIdentifier, | ||
}, | ||
Debug: false, | ||
UserAgent: fmt.Sprintf("bloxone-%s/%s", sdkIdentifier, version), | ||
Servers: []internal.ServerConfiguration{{URL: "https://csp.infoblox.com"}}, | ||
HTTPClient: http.DefaultClient, | ||
DefaultTags: map[string]string{ | ||
clientIdentifier: "terraform", | ||
}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"DefaultTags provided", | ||
fields{ | ||
CSPURL: "https://stage.csp.infoblox.com", | ||
ClientName: "terraformv1.1#yug278872h", | ||
APIKey: "12323455", | ||
DefaultTags: map[string]string{ | ||
"site": "A", | ||
"env": "test", | ||
}, | ||
}, | ||
args{basePath: ""}, | ||
&internal.Configuration{ | ||
DefaultHeader: map[string]string{ | ||
HeaderAuthorization: "Token 12323455", | ||
HeaderClient: "terraformv1.1#yug278872h", | ||
HeaderSDK: sdkIdentifier, | ||
}, | ||
Debug: false, | ||
UserAgent: fmt.Sprintf("bloxone-%s/%s", sdkIdentifier, version), | ||
Servers: []internal.ServerConfiguration{{URL: "https://stage.csp.infoblox.com"}}, | ||
HTTPClient: http.DefaultClient, | ||
DefaultTags: map[string]string{ | ||
clientIdentifier: "terraformv1.1#yug278872h", | ||
"site": "A", | ||
"env": "test", | ||
}, | ||
}, | ||
false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var curEnvVal string | ||
c := Configuration{ | ||
ClientName: tt.fields.ClientName, | ||
CSPURL: tt.fields.CSPURL, | ||
APIKey: tt.fields.APIKey, | ||
HTTPClient: tt.fields.HTTPClient, | ||
DefaultTags: tt.fields.DefaultTags, | ||
} | ||
if c.CSPURL != "" { | ||
curEnvVal = os.Getenv(ENVBloxOneCSPURL) | ||
t.Setenv(ENVBloxOneCSPURL, c.CSPURL) | ||
} | ||
|
||
got, err := c.internal(tt.args.basePath) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("internal() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("internal() got = %v, want %v", got, tt.want) | ||
} | ||
t.Cleanup(func() { | ||
// Set it to the value prior to executing the test | ||
if c.CSPURL != "" { | ||
t.Setenv(ENVBloxOneCSPURL, curEnvVal) | ||
} | ||
}) | ||
}) | ||
} | ||
} |
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,76 @@ | ||
package internal | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestConfiguration_AddDefaultTags(t *testing.T) { | ||
type fields struct { | ||
DefaultTags map[string]string | ||
} | ||
type args struct { | ||
m map[string]string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
expected map[string]string | ||
}{ | ||
{ | ||
name: "empty fields", | ||
fields: fields{ | ||
DefaultTags: map[string]string{}, | ||
}, | ||
args: args{ | ||
m: map[string]string{}, | ||
}, | ||
expected: map[string]string{}, | ||
}, { | ||
name: "existing fields", | ||
fields: fields{ | ||
DefaultTags: map[string]string{ | ||
"key1": "value1", | ||
}, | ||
}, | ||
args: args{ | ||
m: map[string]string{ | ||
"key2": "value2", | ||
}, | ||
}, | ||
expected: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
}, | ||
}, | ||
{ | ||
name: "overriding fields", | ||
fields: fields{ | ||
DefaultTags: map[string]string{ | ||
"key1": "value1", | ||
}, | ||
}, | ||
args: args{ | ||
m: map[string]string{ | ||
"key1": "value2", | ||
}, | ||
}, | ||
expected: map[string]string{ | ||
"key1": "value2", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := &Configuration{ | ||
DefaultTags: tt.fields.DefaultTags, | ||
} | ||
c.AddDefaultTags(tt.args.m) | ||
if !reflect.DeepEqual(c.GetDefaultTags(), tt.expected) { | ||
t.Errorf("internal() want = %v, got = %v", tt.expected, c.GetDefaultTags()) | ||
return | ||
} | ||
}) | ||
} | ||
} |