-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cli test mode and profile tests (#313)
CLI supports building in test mode to enable mocking the profile See README for details. --------- Co-authored-by: jakedoublev <jake.vanvorhis@virtru.com>
- Loading branch information
1 parent
ed21f81
commit e0bc183
Showing
10 changed files
with
304 additions
and
45 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
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
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,74 @@ | ||
package profiles | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/opentdf/otdfctl/pkg/config" | ||
"github.com/zalando/go-keyring" | ||
) | ||
|
||
const testModeMsg = ` | ||
******************** | ||
RUNNING IN TEST MODE | ||
test config: %s | ||
******************** | ||
` | ||
|
||
var ( | ||
testProfile *Profile | ||
testCfg = os.Getenv("OTDFCTL_TEST_PROFILE") | ||
) | ||
|
||
type testConfig struct { | ||
// global config is used to get the store in a bad state | ||
GlobalConfig config.Config `json:"globalConfig,omitempty"` | ||
|
||
// set the default profile | ||
DefaultProfile string `json:"defaultProfile,omitempty"` | ||
|
||
// profiles to add | ||
Profiles []ProfileConfig `json:"profiles,omitempty"` | ||
} | ||
|
||
func init() { | ||
// If running in test mode, use the mock keyring | ||
if config.TestMode == "true" { | ||
fmt.Printf(testModeMsg, testCfg) | ||
|
||
keyring.MockInit() | ||
|
||
// configure the keyring based on the test config | ||
// unmarsal the test config | ||
if testCfg != "" { | ||
var err error | ||
var cfg testConfig | ||
if err := json.NewDecoder(bytes.NewReader([]byte(testCfg))).Decode(&cfg); err != nil { | ||
panic(err) | ||
} | ||
|
||
testProfile, err = New() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, p := range cfg.Profiles { | ||
err := testProfile.AddProfile(p.Name, p.Endpoint, p.TlsNoVerify, cfg.DefaultProfile == p.Name) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// set default | ||
if cfg.DefaultProfile != "" { | ||
if err := testProfile.SetDefaultProfile(cfg.DefaultProfile); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.