-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor k8s_secrets_storage to eliminate CodeClimate errors
This change includes a bunch of refactoring to eliminate the following CodeClimate issues: - Function NewProviderForType has 7 arguments (exceeds 4 allowed). Consider refactoring. Found in pkg/secrets/provide_conjur_secrets.go - Function NewProvider has 5 arguments (exceeds 4 allowed). Consider refactoring. Found in pkg/secrets/k8s_secrets_storage/provide_conjur_secrets.go - Method k8sProvider.Provide has 6 return statements (exceeds 4 allowed). Found in pkg/secrets/k8s_secrets_storage/provide_conjur_secrets.go Changes include: - Eliminate the following arguments from secrets.NewProviderForType: - k8s.RetrieveK8sSecret - k8s.UpdateK8sSecret Now these dependencies are injected in a non-exported version of this function. - Similar to previous change, eliminated the same arguments in NewProvider in k8s_secrets_storage/provide_conjur_secrets.go. - Converted some functions to receiver methods to eliminate a bunch of arguments for those functions. - Consolidated some functions in k8sProvider.Provide to eliminate some return statements. - Reorganized tests provide_conjur_secrets_test.go to match refactoring changes in the functional code. - Because of the many changes in provide_conjur_secrets_test.go (previous bullet), converted Convey-based tests to table-driven Golang tests. - Fixed the mock AddSecret function. The current implementation only allows one Conjur secret per Kubernetes secret when adding a Kubernetes Secret to the mock database. This function should allow for each Kubernetes Secret to include mappings for multiple Conjur secrets.
- Loading branch information
1 parent
9077b77
commit 73b59f6
Showing
8 changed files
with
758 additions
and
568 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
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,82 @@ | ||
package mocks | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Logger is used to implement logging functions for testing the | ||
// Kubernetes Secrets storage provider. | ||
type Logger struct { | ||
errors []string | ||
warnings []string | ||
infos []string | ||
debugs []string | ||
} | ||
|
||
// NewLogger returns a shiny, new Logger | ||
func NewLogger() *Logger { | ||
return &Logger{} | ||
} | ||
|
||
// RecordedError logs that an error has occurred and returns a new error | ||
// with the given error message. | ||
func (l *Logger) RecordedError(msg string, args ...interface{}) error { | ||
errStr := fmt.Sprintf(msg, args...) | ||
l.errors = append(l.errors, errStr) | ||
return errors.New(errStr) | ||
} | ||
|
||
// Error logs an error. | ||
func (l *Logger) Error(msg string, args ...interface{}) { | ||
l.errors = append(l.errors, fmt.Sprintf(msg, args...)) | ||
} | ||
|
||
// Warn logs a warning. | ||
func (l *Logger) Warn(msg string, args ...interface{}) { | ||
l.warnings = append(l.warnings, fmt.Sprintf(msg, args...)) | ||
} | ||
|
||
// Info logs an info message. | ||
func (l *Logger) Info(msg string, args ...interface{}) { | ||
l.infos = append(l.infos, fmt.Sprintf(msg, args...)) | ||
} | ||
|
||
// Debug logs a debug message. | ||
func (l *Logger) Debug(msg string, args ...interface{}) { | ||
l.debugs = append(l.debugs, fmt.Sprintf(msg, args...)) | ||
} | ||
|
||
func (l *Logger) messageWasLogged(msg string, loggedMsgs []string) bool { | ||
for _, loggedMsg := range loggedMsgs { | ||
if strings.Contains(loggedMsg, msg) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// ErrorWasLogged determines if an error string appears in any | ||
// errors that have been logged. | ||
func (l *Logger) ErrorWasLogged(errStr string) bool { | ||
return l.messageWasLogged(errStr, l.errors) | ||
} | ||
|
||
// WarningWasLogged determines if a warning string appears in any | ||
// warning messages that have been logged. | ||
func (l *Logger) WarningWasLogged(warning string) bool { | ||
return l.messageWasLogged(warning, l.warnings) | ||
} | ||
|
||
// InfoWasLogged determines if a warning string appears in any | ||
// info messages that have been logged. | ||
func (l *Logger) InfoWasLogged(info string) bool { | ||
return l.messageWasLogged(info, l.infos) | ||
} | ||
|
||
// DebugWasLogged determines if a debug string appears in any | ||
// debug messages that have been logged. | ||
func (l *Logger) DebugWasLogged(debug string) bool { | ||
return l.messageWasLogged(debug, l.debugs) | ||
} |
Oops, something went wrong.