Skip to content

Commit

Permalink
test(integration): adds end-to-end tests
Browse files Browse the repository at this point in the history
Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed May 12, 2020
1 parent c03a416 commit e2eb449
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 7 deletions.
14 changes: 14 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ $ lacework version
lacework v.dev (sha:ca9f95d17f4f2092f89dba7b64eaed6db7493a5a) (time:20200406091143)
```

### Unit Tests

Running unit tests should be as simple as executing the `make test` directive.

### Integration Tests

The integration tests are end-to-end tests that are run against a real Lacework API
Server, for that reason it requires a set of Lacework API keys, to run these tests
locally you need to setup the following environment variables and use the directive
`make integration`, an example of the command you can use is:
```
$ CI_ACCOUNT="<YOUR_ACCOUNT>" CI_API_KEY="<YOUR_API_KEY>" CI_API_SECRET="<YOUR_API_SECRET>" make integration
```

## License and Copyright
Copyright 2020, Lacework Inc.
```
Expand Down
65 changes: 65 additions & 0 deletions integration/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Author:: Salim Afiune Maya (<[email protected]>)
// Copyright:: Copyright 2020, Lacework Inc.
// License:: Apache License, Version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package integration

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestEventCommandAliases(t *testing.T) {
// lacework event
out, err, exitcode := LaceworkCLI("help", "event")
assert.Contains(t, out.String(), "lacework event [command]")
assert.Empty(t, err.String(), "STDERR should be empty")
assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one")

// lacework events
out, err, exitcode = LaceworkCLI("help", "events")
assert.Contains(t, out.String(), "lacework event [command]")
assert.Empty(t, err.String(), "STDERR should be empty")
assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one")
}

func TestEventCommandList(t *testing.T) {
// @afiune shippable doesn't allow us to have encrypted variables inside our build jobs,
// and because of that, we are disabling a few tests when running inside our "CI" pipeline
if os.Getenv("CI") == "true" {
return
}

out, err, exitcode := LaceworkCLIWithTOMLConfig("event", "list")
assert.Contains(t, out.String(), "EVENT ID",
"STDOUT table headers changed, please check")
assert.Contains(t, out.String(), "TYPE",
"STDOUT table headers changed, please check")
assert.Contains(t, out.String(), "SEVERITY",
"STDOUT table headers changed, please check")
assert.Contains(t, out.String(), "START TIME",
"STDOUT table headers changed, please check")
assert.Contains(t, out.String(), "END TIME",
"STDOUT table headers changed, please check")
assert.Empty(t,
err.String(),
"STDERR should be empty")
assert.Equal(t, 0, exitcode,
"EXITCODE is not the expected one")
}
42 changes: 41 additions & 1 deletion integration/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
)

Expand Down Expand Up @@ -58,7 +60,7 @@ func LaceworkCLI(args ...string) (bytes.Buffer, bytes.Buffer, int) {
}

func LaceworkCLIWithTOMLConfig(args ...string) (bytes.Buffer, bytes.Buffer, int) {
dir := createTOMLConfig()
dir := createTOMLConfigFromCIvars()
defer os.RemoveAll(dir)

return runLaceworkCLI(dir, args...)
Expand Down Expand Up @@ -113,3 +115,41 @@ func findLaceworkCLIBinary() string {

return "lacework"
}

func createTOMLConfigFromCIvars() string {
if os.Getenv("CI_ACCOUNT") == "" ||
os.Getenv("CI_API_KEY") == "" ||
os.Getenv("CI_API_SECRET") == "" {
// @afiune add instructions
log.Fatal(missingCIEnvironmentVariables())
}

dir, err := ioutil.TempDir("", "lacework-toml")
if err != nil {
panic(err)
}

configFile := filepath.Join(dir, ".lacework.toml")
c := []byte(`[default]
account = '` + os.Getenv("CI_ACCOUNT") + `'
api_key = '` + os.Getenv("CI_API_KEY") + `'
api_secret = '` + os.Getenv("CI_API_SECRET") + `'
`)
err = ioutil.WriteFile(configFile, c, 0644)
if err != nil {
panic(err)
}
return dir
}

func missingCIEnvironmentVariables() string {
return `
ERROR
Missing CI environment variables.
To run the integration tests you need to setup a few environment variables, look
at https://github.com/lacework/go-sdk/tree/master/cli#integration-tests for
more information.
`
}
68 changes: 68 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Author:: Salim Afiune Maya (<[email protected]>)
// Copyright:: Copyright 2020, Lacework Inc.
// License:: Apache License, Version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package integration

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestIntegrationCommandAliases(t *testing.T) {
// lacework integration
out, err, exitcode := LaceworkCLI("help", "integration")
assert.Contains(t, out.String(), "lacework integration [command]")
assert.Empty(t, err.String(), "STDERR should be empty")
assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one")

// lacework integrations
out, err, exitcode = LaceworkCLI("help", "integrations")
assert.Contains(t, out.String(), "lacework integration [command]")
assert.Empty(t, err.String(), "STDERR should be empty")
assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one")

// lacework int
out, err, exitcode = LaceworkCLI("help", "int")
assert.Contains(t, out.String(), "lacework integration [command]")
assert.Empty(t, err.String(), "STDERR should be empty")
assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one")
}

func TestIntegrationCommandList(t *testing.T) {
// @afiune shippable doesn't allow us to have encrypted variables inside our build jobs,
// and because of that, we are disabling a few tests when running inside our "CI" pipeline
if os.Getenv("CI") == "true" {
return
}
out, err, exitcode := LaceworkCLIWithTOMLConfig("integration", "list")
assert.Contains(t, out.String(), "INTEGRATION GUID",
"STDOUT table headers changed, please check")
assert.Contains(t, out.String(), "NAME",
"STDOUT table headers changed, please check")
assert.Contains(t, out.String(), "TYPE",
"STDOUT table headers changed, please check")
assert.Contains(t, out.String(), "STATUS",
"STDOUT table headers changed, please check")
assert.Empty(t,
err.String(),
"STDERR should be empty")
assert.Equal(t, 0, exitcode,
"EXITCODE is not the expected one")
}
9 changes: 3 additions & 6 deletions shippable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ branches:
only:
- master

env:
- secure: ltwWrcwLiW6HvrnX0SJGS5yktmJBVd8chypLDN3LQoj/T3KptIbXu7O5nnZ6R+O2qV30IG4NkKe4VcHIsZE2KP5CC+K7WLpmp+lTH5Qz9VwM9e1Zlz14V0oP8qENuA8q6B5FeKLf8ReUDUHqqpAyE2pwzkgQ8ZdFcG+QlvrlL7GoR5b0xTQH5LAAIjGbmsonNVC8C6Ep5tf7BYEUgOrB8qLGWd48fw9CIt9YrCbJlvupo0bE0RdDvdvZFwgrHFl3riq1IaM9+5jmlLJPH099+b03ICh8QYWJhoqUB37QOXEjQYQpck8o4Sih6/XZtikeunjBewg7f3txfiQqod8S1w==

build:
ci:
- make prepare
- make ci
ci:
- make prepare
- make ci

0 comments on commit e2eb449

Please sign in to comment.