-
Notifications
You must be signed in to change notification settings - Fork 13
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
24ed3e8
commit 92e9185
Showing
9 changed files
with
203 additions
and
81 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
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,20 +1,96 @@ | ||
package cd | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/harness-io/harness-go-sdk/harness/helpers" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func getClient() *ApiClient { | ||
return NewClient(&Configuration{ | ||
c, _ := NewClient(&Config{ | ||
AccountId: helpers.EnvVars.AccountId.Get(), | ||
APIKey: helpers.EnvVars.ApiKey.Get(), | ||
DebugLogging: false, | ||
}) | ||
|
||
return c | ||
} | ||
|
||
func GetUnauthorizedClient() *ApiClient { | ||
return NewClient(&Configuration{ | ||
c, _ := NewClient(&Config{ | ||
AccountId: helpers.EnvVars.AccountId.Get(), | ||
APIKey: "BAD KEY", | ||
}) | ||
|
||
return c | ||
} | ||
|
||
func TestClientRequireApiKey_Config(t *testing.T) { | ||
os.Clearenv() | ||
|
||
cfg := &Config{ | ||
AccountId: "ACCOUNT_ID", | ||
} | ||
_, err := NewClient(cfg) | ||
|
||
require.Error(t, err, InvalidConfigError{}) | ||
require.Equal(t, "ApiKey", err.(InvalidConfigError).Field) | ||
|
||
cfg = &Config{ | ||
AccountId: "ACCOUNT_ID", | ||
APIKey: "APIKEY", | ||
} | ||
c, err := NewClient(cfg) | ||
|
||
require.Equal(t, c.Configuration.APIKey, cfg.APIKey) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestClientRequireApiKey_Envvar(t *testing.T) { | ||
os.Clearenv() | ||
os.Setenv(helpers.EnvVars.ApiKey.String(), "APIKEY") | ||
|
||
cfg := &Config{ | ||
AccountId: "ACCOUNT_ID", | ||
} | ||
c, err := NewClient(cfg) | ||
|
||
require.NoError(t, err, InvalidConfigError{}) | ||
require.Equal(t, "APIKEY", c.Configuration.APIKey) | ||
} | ||
|
||
func TestClientRequireAccountId_Config(t *testing.T) { | ||
os.Clearenv() | ||
|
||
cfg := &Config{ | ||
APIKey: "APIKEY", | ||
} | ||
_, err := NewClient(cfg) | ||
|
||
require.Error(t, err, InvalidConfigError{}) | ||
require.Equal(t, "AccountId", err.(InvalidConfigError).Field) | ||
|
||
cfg = &Config{ | ||
AccountId: "ACCOUNT_ID", | ||
APIKey: "APIKEY", | ||
} | ||
c, err := NewClient(cfg) | ||
|
||
require.Equal(t, c.Configuration.AccountId, cfg.AccountId) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestClientRequireAccountId_Envvar(t *testing.T) { | ||
os.Clearenv() | ||
os.Setenv(helpers.EnvVars.AccountId.String(), "ACCOUNT_ID") | ||
|
||
cfg := &Config{ | ||
APIKey: "APIKEY", | ||
} | ||
c, err := NewClient(cfg) | ||
|
||
require.NoError(t, err, InvalidConfigError{}) | ||
require.Equal(t, "ACCOUNT_ID", c.Configuration.AccountId) | ||
} |
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,25 @@ | ||
package cd | ||
|
||
import "fmt" | ||
|
||
type InvalidConfigError struct { | ||
Config *Config | ||
Field string | ||
Err error | ||
} | ||
|
||
func (c *Config) NewInvalidConfigError(field string, err error) InvalidConfigError { | ||
return InvalidConfigError{ | ||
Config: c, | ||
Field: field, | ||
Err: err, | ||
} | ||
} | ||
|
||
func (e InvalidConfigError) Error() string { | ||
return fmt.Sprintf("invalid config: %s must be set %s", e.Field, e.Err) | ||
} | ||
|
||
func (e InvalidConfigError) Unwrap() error { | ||
return e.Err | ||
} |
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 |
---|---|---|
@@ -1,56 +1,49 @@ | ||
package logging | ||
|
||
import ( | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
easy "github.com/t-tomalak/logrus-easy-formatter" | ||
) | ||
|
||
// func IsDebugOrHigher() bool { | ||
// lvl := log.GetLevel() | ||
// return lvl == log.DebugLevel || lvl == log.TraceLevel | ||
// } | ||
|
||
func IsDebugOrHigher(logger *log.Logger) bool { | ||
lvl := logger.GetLevel() | ||
return lvl == log.DebugLevel || lvl == log.TraceLevel | ||
} | ||
|
||
func GetDefaultLogger(debug bool) *log.Logger { | ||
level := log.InfoLevel | ||
if debug { | ||
level = log.DebugLevel | ||
} | ||
|
||
logger := &log.Logger{ | ||
Out: os.Stderr, | ||
Level: level, | ||
Formatter: &easy.Formatter{ | ||
TimestampFormat: "2006-01-02 15:04:05", | ||
LogFormat: "%time% [%lvl%] harness-go-sdk %msg%\n", | ||
}, | ||
// NewLogger creates a new logger with default settings. | ||
func NewLogger() *log.Logger { | ||
logger := log.New() | ||
logger.Formatter = &easy.Formatter{ | ||
TimestampFormat: "2006-01-02 15:04:05", | ||
LogFormat: "%time% [%lvl%] harness-go-sdk %msg%\n", | ||
} | ||
|
||
return logger | ||
} | ||
|
||
type LeveledLogger struct { | ||
type LeveledLogger interface { | ||
Error(msg string, keysAndValues ...interface{}) | ||
Warn(msg string, keysAndValues ...interface{}) | ||
Info(msg string, keysAndValues ...interface{}) | ||
Debug(msg string, keysAndValues ...interface{}) | ||
IsDebugOrHigher() bool | ||
} | ||
|
||
type Logger struct { | ||
Logger *log.Logger | ||
} | ||
|
||
func (l *LeveledLogger) Error(msg string, keysAndValues ...interface{}) { | ||
func (l *Logger) Error(msg string, keysAndValues ...interface{}) { | ||
l.Logger.Errorf(msg, keysAndValues...) | ||
} | ||
|
||
func (l *LeveledLogger) Info(msg string, keysAndValues ...interface{}) { | ||
func (l *Logger) Info(msg string, keysAndValues ...interface{}) { | ||
l.Logger.Infof(msg, keysAndValues...) | ||
} | ||
|
||
func (l *LeveledLogger) Debug(msg string, keysAndValues ...interface{}) { | ||
func (l *Logger) Debug(msg string, keysAndValues ...interface{}) { | ||
l.Logger.Debugf(msg, keysAndValues...) | ||
} | ||
|
||
func (l *LeveledLogger) Warn(msg string, keysAndValues ...interface{}) { | ||
func (l *Logger) Warn(msg string, keysAndValues ...interface{}) { | ||
l.Logger.Warnf(msg, keysAndValues...) | ||
} |
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.