-
-
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.
test: Redo tests for the CheckHealth function
- Loading branch information
Showing
2 changed files
with
82 additions
and
15 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 |
---|---|---|
@@ -1,32 +1,83 @@ | ||
package check_health | ||
|
||
import ( | ||
"os" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/Dobefu/csb/cmd/cs_sdk" | ||
"github.com/Dobefu/csb/cmd/database" | ||
"github.com/Dobefu/csb/cmd/init_env" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var err error | ||
|
||
func TestMain(t *testing.T) { | ||
var err error | ||
databaseConnect = func() error { return nil } | ||
csSdkRequest = func(path string, method string, body map[string]interface{}, useManagementToken bool) (map[string]interface{}, error) { | ||
return map[string]interface{}{ | ||
"label": map[string]interface{}{ | ||
"uid": "dummy label"}, | ||
}, nil | ||
} | ||
|
||
err = Main() | ||
assert.NotEqual(t, nil, err) | ||
defer func() { databaseConnect = database.Connect }() | ||
defer func() { csSdkRequest = cs_sdk.Request }() | ||
|
||
init_env.Main("../../.env.test") | ||
err = database.Connect() | ||
err = Main() | ||
assert.Equal(t, nil, err) | ||
} | ||
|
||
func TestMainErrNoDatabase(t *testing.T) { | ||
databaseConnect = func() error { return errors.New("no database") } | ||
csSdkRequest = func(path string, method string, body map[string]interface{}, useManagementToken bool) (map[string]interface{}, error) { | ||
return map[string]interface{}{}, nil | ||
} | ||
|
||
defer func() { databaseConnect = database.Connect }() | ||
defer func() { csSdkRequest = cs_sdk.Request }() | ||
|
||
err = Main() | ||
assert.Equal(t, nil, err) | ||
assert.NotEqual(t, nil, err) | ||
} | ||
|
||
func TestMainErrNoLabel(t *testing.T) { | ||
databaseConnect = func() error { return nil } | ||
csSdkRequest = func(path string, method string, body map[string]interface{}, useManagementToken bool) (map[string]interface{}, error) { | ||
return map[string]interface{}{}, nil | ||
} | ||
|
||
oldApiKey := os.Getenv("CS_API_KEY") | ||
os.Setenv("CS_API_KEY", "bogus") | ||
defer func() { databaseConnect = database.Connect }() | ||
defer func() { csSdkRequest = cs_sdk.Request }() | ||
|
||
err = Main() | ||
assert.NotEqual(t, nil, err) | ||
} | ||
|
||
func TestCheckCsSdkErrLabelCreate(t *testing.T) { | ||
csSdkRequest = func(path string, method string, body map[string]interface{}, useManagementToken bool) (map[string]interface{}, error) { | ||
return nil, errors.New("cannot connect") | ||
} | ||
|
||
defer func() { csSdkRequest = cs_sdk.Request }() | ||
|
||
err = checkCsSdk() | ||
assert.NotEqual(t, nil, err) | ||
} | ||
|
||
func TestCheckCsSdkErrLabelDelete(t *testing.T) { | ||
csSdkRequest = func(path string, method string, body map[string]interface{}, useManagementToken bool) (map[string]interface{}, error) { | ||
if method == "DELETE" { | ||
return nil, errors.New("cannot connect") | ||
} | ||
|
||
return map[string]interface{}{ | ||
"label": map[string]interface{}{ | ||
"uid": "dummy label"}, | ||
}, nil | ||
} | ||
|
||
os.Setenv("CS_API_KEY", oldApiKey) | ||
defer func() { csSdkRequest = cs_sdk.Request }() | ||
|
||
err = checkCsSdk() | ||
assert.NotEqual(t, nil, err) | ||
} |