Skip to content

Commit

Permalink
test: set LW_API_TOKEN for integration tests (#1577)
Browse files Browse the repository at this point in the history
* test: set LW_API_TOKEN for integration tests

* test: fetch accee_token in NewLaceworkCLI
  • Loading branch information
PengyuanZhao authored Mar 5, 2024
1 parent 85adeea commit 4d0353f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cli/cmd/cli_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ func (c *cliState) NewClient() error {
api.WithTokenAndExpiration(c.Token, c.tokenCache.ExpiresAt))
} else if c.Token != "" {
apiOpts = append(apiOpts, api.WithToken(c.Token))
} else if os.Getenv("LW_API_TOKEN") != "" {
apiOpts = append(apiOpts, api.WithToken(os.Getenv("LW_API_TOKEN")))
}

apiOpts = append(apiOpts,
Expand Down
23 changes: 19 additions & 4 deletions integration/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var (
tfPath string
tf *tfexec.Terraform
execPath string
token string
)

// Use this function to execute a real lacework CLI command, under the hood the function
Expand Down Expand Up @@ -116,6 +117,24 @@ func NewLaceworkCLI(workingDir string, stdin io.Reader, args ...string) *exec.Cm
if os.Getenv(ciTestingUpdaterEnv) == "" {
env = append(env, fmt.Sprintf("%s=1", lwupdater.DisableEnv))
}
// add unique environment variable to notify the CLI that
// it is being executed to run our integration test suite
env = append(env, "LW_CLI_INTEGRATION_MODE=true")

if token == "" {
fmt.Println("Generating access token")
lacework, err := laceworkIntegrationTestClient()
if err != nil {
log.Fatal(err)
}
response, err := lacework.GenerateToken()
if err != nil {
log.Fatal(err)
}
token = response.Token
env = append(env, fmt.Sprintf("LW_API_TOKEN=%s", response.Token))
}

cmd.Env = env
}
return cmd
Expand Down Expand Up @@ -150,10 +169,6 @@ func runLaceworkCLI(workingDir string, args ...string) (stdout bytes.Buffer, std
// what they expect
cmd.Env = append(cmd.Env, "LW_NONINTERACTIVE=false")

// add unique environment variable to notify the CLI that
// it is being executed to run our integration test suite
cmd.Env = append(cmd.Env, "LW_CLI_INTEGRATION_MODE=true")

exitcode, err := runLaceworkCLIFromCmd(cmd)
if exitcode == 999 {
fmt.Println(stderr)
Expand Down

0 comments on commit 4d0353f

Please sign in to comment.