Skip to content

Commit

Permalink
test(integration): Add scaffold for running integration tests (#73)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>
  • Loading branch information
danielpacak authored Jul 27, 2020
1 parent a0ad1ad commit bce75c3
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 5 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,23 @@ jobs:
- name: Verify generated code
run: GOPATH="$(go env GOPATH)" ./hack/verify-codegen.sh
- name: Run unit tests
run: make test
run: make unit-tests
- name: Upload code coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.txt
- name: Setup Kubernetes cluster (KIND)
uses: engineerd/setup-kind@v0.4.0
- name: Test connection to Kubernetes cluster
run: |
kubectl cluster-info
- name: Run integration tests
run: |
make integration-tests
kubectl get crd
env:
KUBECONFIG: /home/runner/.kube/config
- name: Release snapshot
uses: goreleaser/goreleaser-action@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
with:
fetch-depth: 0
- name: Run unit tests
run: make test
run: make unit-tests
- name: Release
uses: goreleaser/goreleaser-action@v2
with:
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ build: starboard
$(BINARY): $(SOURCES)
CGO_ENABLED=0 go build -o ./bin/$(BINARY) ./cmd/starboard/main.go

test: $(SOURCES)
unit-tests: $(SOURCES)
go test -v -short -race -timeout 30s -coverprofile=coverage.txt -covermode=atomic ./...

integration-tests: build
go test -v ./integration-tests
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.14
require (
github.com/google/go-containerregistry v0.1.1
github.com/google/uuid v1.1.1
github.com/onsi/ginkgo v1.12.0
github.com/onsi/gomega v1.9.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.5.1
Expand Down
5 changes: 4 additions & 1 deletion hack/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
// This package imports things required by build scripts, to force `go mod` to see them as dependencies.
package tools

import _ "k8s.io/code-generator"
import (
_ "k8s.io/code-generator"
_ "github.com/onsi/ginkgo/ginkgo"
)
50 changes: 50 additions & 0 deletions integration-tests/starboard_integration_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package integration_tests

import (
"os"
"testing"

"github.com/onsi/gomega/gexec"
"k8s.io/client-go/tools/clientcmd"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
"k8s.io/client-go/kubernetes"
)

var (
kubernetesClientset kubernetes.Interface
apiextensionsClientset apiextensions.ApiextensionsV1beta1Interface
)

var (
pathToStarboardCLI string
)

var _ = BeforeSuite(func() {
var err error
pathToStarboardCLI, err = gexec.Build("github.com/aquasecurity/starboard/cmd/starboard")
Expect(err).ToNot(HaveOccurred())

config, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG"))
Expect(err).ToNot(HaveOccurred())

kubernetesClientset, err = kubernetes.NewForConfig(config)
Expect(err).ToNot(HaveOccurred())

apiextensionsClientset, err = apiextensions.NewForConfig(config)
Expect(err).ToNot(HaveOccurred())
})

func TestStarboardCLI(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test")
}
RegisterFailHandler(Fail)
RunSpecs(t, "Starboard CLI")
}

var _ = AfterSuite(func() {
gexec.CleanupBuildArtifacts()
})
66 changes: 66 additions & 0 deletions integration-tests/starboard_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package integration_tests

import (
"context"
"os/exec"

apiextentions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"

meta "k8s.io/apimachinery/pkg/apis/meta/v1"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Starboard CLI", func() {

BeforeEach(func() {
// currently do nothing
})

Describe("Running version command", func() {
It("should print the current version of the executable binary", func() {
cmd := exec.Command(pathToStarboardCLI, []string{"version"}...)
output, err := cmd.Output()
Expect(err).ToNot(HaveOccurred())
Expect(string(output)).To(Equal("Starboard Version: {Version:dev Commit:none Date:unknown}\n"))
})

})

Describe("Running init command", func() {
It("should initialize Starboard", func() {
cmd := exec.Command(pathToStarboardCLI, []string{"init", "-v", "3"}...)
err := cmd.Run()
Expect(err).ToNot(HaveOccurred())

crdsList, err := apiextensionsClientset.CustomResourceDefinitions().List(context.TODO(), meta.ListOptions{})
Expect(err).ToNot(HaveOccurred())

GetNames := func(crds []apiextentions.CustomResourceDefinition) []string {
names := make([]string, len(crds))
for i, crd := range crds {
names[i] = crd.Name
}
return names
}

Expect(crdsList.Items).To(WithTransform(GetNames, ContainElements(
"ciskubebenchreports.aquasecurity.github.io",
"configauditreports.aquasecurity.github.io",
"kubehunterreports.aquasecurity.github.io",
"vulnerabilities.aquasecurity.github.io",
)))

_, err = kubernetesClientset.CoreV1().Namespaces().Get(context.TODO(), "starboard", meta.GetOptions{})
Expect(err).ToNot(HaveOccurred())

// TODO Assert other Kubernetes resources that we create in the init command
})
})

AfterEach(func() {
// currently do nothing
})

})
2 changes: 1 addition & 1 deletion pkg/polaris/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

const (
polarisContainerName = "polaris"
polarisContainerName = "polaris"
polarisContainerImage = "quay.io/fairwinds/polaris:1.1.0"
polarisConfigVolume = "config-volume"
polarisConfigMap = "polaris"
Expand Down

0 comments on commit bce75c3

Please sign in to comment.