Skip to content

Commit

Permalink
chore: [APPAI-1770] Setup Acceptance Tests for CLI (#46)
Browse files Browse the repository at this point in the history
* added tests

* added tests

* check if binary exists

check if binary exists

check if binary exists

failure 1

trying

trying

trying

trying

trying

trying

setup done windows

path issue

path issue

path issue

path issue

path issue

issues fixed

issues fixed

final test preps

exclude windows from abs path

small fix

added coverage

final fix

linter errors

fix nodot

fix

linter fixes

final commit

fix go mod

ignore acctests in unit tests

ignore acctests in unit tests

ignore acctests in unit tests

fix

acceptance tests command

added comments

global cleanup function

* added tests

* run on PR

* run on PR

final tests fix

final tests fix

final tests fix

removed comments
  • Loading branch information
Meer Sawood authored Apr 26, 2021
1 parent caeee8f commit ba93c2f
Show file tree
Hide file tree
Showing 29 changed files with 2,052 additions and 16 deletions.
92 changes: 92 additions & 0 deletions .github/workflows/ci-acctests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

name: CI-Acceptance-Tests

on:
pull_request:
branches: [ main ]
push:
branches: [ main ]
repository_dispatch:
types: [acceptance-command]

jobs:
Test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15

- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- uses: actions/setup-python@v2
with:
python-version: '3.7'

- uses: actions/setup-node@v2
with:
node-version: '14'
- name: Create comment
if: github.event_name == 'repository_dispatch' && github.event.action == 'acceptance-command'
uses: peter-evans/create-or-update-comment@v1
with:
edit-mode: replace
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
body: |
**Edit:** :test_tube: [CI has Started acceptance Test]( https://github.com/${{github.repository}}/actions/runs/${{github.run_id}})
reactions: eyes
- name: build binary
if: runner.os == 'Linux' || runner.os == 'macOS'
run: go build -o acceptance-tests/crda

- name: build binary [Windows]
if: runner.os == 'Windows'
run: go build -o acceptance-tests/crda.exe

- name: Install dependencies
run: go mod vendor

- name: Run Tests
working-directory: ./acceptance-tests
env:
THREE_SCALE_KEY: ${{ secrets.THREE_SCALE_KEY }}
CRDA_KEY: ${{ secrets.CRDA_KEY }}
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
run: ./test.sh
shell: bash

- name: Upload log as artifact
if: always()
uses: actions/upload-artifact@v2
with:
name: Test-log-${{ runner.os }}
path: ./acceptance-tests/logs.txt
- name: Create Success comment
if: github.event_name == 'repository_dispatch' && github.event.action == 'acceptance-command'
uses: peter-evans/create-or-update-comment@v1
with:
edit-mode: replace
issue-number: ${{ github.event.client_payload.github.payload.issue.number }}
body: |
:v: [E2E Run Successfull](https://github.com/${{github.repository}}/actions/runs/${{github.run_id}})
reactions: hooray, heart
- name: Create fail comment
if: failure() && github.event_name == 'repository_dispatch' && github.event.action == 'acceptance-command'
uses: peter-evans/create-or-update-comment@v1
with:
edit-mode: replace
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
body: |
**Edit:** :facepalm: [Acceptance Tests Failed]( https://github.com/${{github.repository}}/actions/runs/${{github.run_id}})
reactions: confused


2 changes: 1 addition & 1 deletion .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
go mod tidy
git diff --exit-code go.mod
- name: Unit Test
run: go test -gcflags=-l -v -coverprofile=coverage.txt -covermode=atomic ./...
run: go test `go list ./... | grep -v acceptance-tests` -gcflags=-l -v -coverprofile=coverage.txt -covermode=atomic
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/slash-command.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Slash Command Dispatch
on:
issue_comment:
types: [created]
jobs:
slashCommandDispatch:
runs-on: ubuntu-latest
steps:
- name: Slash Command Dispatch
uses: peter-evans/slash-command-dispatch@v2
with:
token: ${{ secrets.CR_PAT }}
commands: |
acceptance
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
.DS_Store
.devcontainer

.env
data/
*.coverprofile
logs.txt
coverage/
.ginkgo.report


# Python excludes because bilingual repository
__pycache__/
Expand Down
19 changes: 19 additions & 0 deletions acceptance-tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM registry.access.redhat.com/ubi8/go-toolset

USER root
WORKDIR /tests
COPY . /tests
RUN go get github.com/onsi/ginkgo/ginkgo
RUN go get github.com/onsi/gomega/...
RUN yum -y update
RUN yum install -y maven
RUN yum install -y java-1.8.0-openjdk
RUN mvn --version
RUN yum install -y python36; yum clean all && ln -s /usr/bin/python3 /usr/bin/python
RUN yum install -y python3-pip
RUN python3 -V
RUN python3 -m pip -V
RUN pip3 -V
RUN rpm -i https://github.com/fabric8-analytics/cli-tools/releases/download/v0.1.0/crda_0.1.0_Linux-64bit.rpm

ENTRYPOINT [ "/tests/test.sh" ]
25 changes: 25 additions & 0 deletions acceptance-tests/acceptance_tests_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package acceptance_tests_test

import (
"testing"
"time"

"github.com/fabric8-analytics/cli-tools/acceptance-tests/helper"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestAcceptanceTests(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "AcceptanceTests Suite")
}

var _ = BeforeSuite(func() {
SetDefaultEventuallyTimeout(1 * time.Minute)
helper.CreateDataDir()
helper.CheckforSynkToken()
})

var _ = AfterSuite(func() {
helper.CleanupSuite()
})
14 changes: 14 additions & 0 deletions acceptance-tests/acceptance_tests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package acceptance_tests_test

import (
"github.com/fabric8-analytics/cli-tools/acceptance-tests/tests"
. "github.com/onsi/ginkgo"
)

var _ = Describe("AcceptanceTests", func() {

Describe("PR ACCEPTANCE TESTS", tests.PrCheckSuite)

Describe("NIGHTLY SUITE", tests.NightlySuite)

})
166 changes: 166 additions & 0 deletions acceptance-tests/helper/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package helper

import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)

// runningCmd is the function which logs the running command
func runningCmd(cmd *exec.Cmd) string {
prog := filepath.Base(cmd.Path)
return fmt.Sprintf("Running %s with args %v", prog, cmd.Args)
}

// PrintWithGinkgo is the function used for logging
func PrintWithGinkgo(data string){
prefix := fmt.Sprintf("[%s] ", "Test Log")
prefixWriter := gexec.NewPrefixedWriter(prefix, GinkgoWriter)
fmt.Fprintln(prefixWriter, data)
}

// CmdRunner is the function used to run a command
func CmdRunner(program string, args ...string) *gexec.Session {
//prefix ginkgo verbose output with program name
prefix := fmt.Sprintf("[%s] ", filepath.Base(program))
prefixWriter := gexec.NewPrefixedWriter(prefix, GinkgoWriter)
command := exec.Command(program, args...)
fmt.Fprintln(GinkgoWriter, runningCmd(command))
session, err := gexec.Start(command, prefixWriter, prefixWriter)
Expect(err).NotTo(HaveOccurred())
return session
}

// CmdShouldPassWithExit2 asserts the command passed with exit 2
func CmdShouldPassWithExit2(program string, args ...string) string {
session := CmdRunner(program, args...)
Eventually(session).Should(gexec.Exit(2), runningCmd(session.Command))
return string(session.Out.Contents())
}

// CmdShouldPassWithExit1 asserts the command passed with exit 1
func CmdShouldPassWithExit1(program string, args ...string) string {
session := CmdRunner(program, args...)
Eventually(session).Should(gexec.Exit(1), runningCmd(session.Command))
return string(session.Out.Contents())
}

// CmdShouldFailWithExit1 asserts the command passed with exit 1
func CmdShouldFailWithExit1(program string, args ...string) string {
session := CmdRunner(program, args...)
Eventually(session).Should(gexec.Exit(1), runningCmd(session.Command))
return string(session.Err.Contents())
}

// CmdShouldPassWithoutError asserts if the command is passed without error
func CmdShouldPassWithoutError(program string, args ...string) string {
session := CmdRunner(program, args...)
Eventually(session).ShouldNot(gexec.Exit(1),runningCmd(session.Command))
Eventually(session).ShouldNot(gexec.Exit(2),runningCmd(session.Command))
Eventually(session).Should(gexec.Exit(), runningCmd(session.Command))
return string(session.Out.Contents())
}

// Getabspath Gets the Absolute Path
func Getabspath(path string) (string, error) {
if path == "" {
return "", errors.New("empty file path supplied")
}
cmd := exec.Command("pwd")
stdout, err := cmd.Output()
if err != nil {
return "", errors.New("cannot run PWD")
}
pwd := string(stdout)
pwd = strings.TrimSpace(pwd)
result := pwd + path
return result, nil

}

// CommonBeforeEach is a common before each function
func CommonBeforeEach(file string, target string) (string, string) {

if target == "npm" {
return file, "/package.json"
}else if target == "pypi"{
return file, "/requirements.txt"
}else if target == "go"{
return file, "/go.mod"
}else if target == "maven"{
return file, "/pom.xml"
}else {
return "", ""
}
}

// CheckforSynkToken Checks for Snyk Token var in env
func CheckforSynkToken() {
_, present := os.LookupEnv("snyk_token")
if !present {
fmt.Printf("snyk token env variable not present")
os.Exit(1)
} else {
fmt.Println("Snyk Token var present.. Starting Test Suite")
}
}

// CreateDataDir creates data directory
func CreateDataDir() error {
err := os.Mkdir("data", 0755)
if err != nil {
return err
}
return nil
}

// CleanupSuite deletes files used by tests
func CleanupSuite() error {
err := os.RemoveAll("data")
if err != nil {
return err
}
return nil
}

// Cleanup global cleanup function
func Cleanup(path string) error {
contents, err := filepath.Glob(path)
if err != nil {
return err
}
for _, item := range contents {
err = os.RemoveAll(item)
if err != nil {
return err
}
}
return nil
}

// CopyContentstoTarget Copies the files to target
func CopyContentstoTarget(filename string, target string) error {
from, err := os.Open(filename)
if err != nil {
return err
}
defer from.Close()

to, err := os.OpenFile(target, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
defer to.Close()
_, err = io.Copy(to, from)
if err != nil {
return err
}
return nil
}
13 changes: 13 additions & 0 deletions acceptance-tests/manifests/go.mod.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/fabric8-analytics/acceptance_tests/data

go 1.14

require (
code.cloudfoundry.org/archiver v0.0.0-20170223024658-7291196139d7
github.com/googleapis/gax-go v1.0.3
github.com/googleapis/gax-go/v2 v2.0.5
github.com/onsi/ginkgo v1.14.2 // indirect
github.com/onsi/gomega v1.10.3 // indirect
github.com/slackhq/nebula v1.1.0
github.com/stretchr/testify v1.6.1 // indirect
)
12 changes: 12 additions & 0 deletions acceptance-tests/manifests/go2.mod.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/fabric8-analytics/acceptance_tests/data

go 1.14

require (
code.cloudfoundry.org/archiver v0.0.0-20170223024658-7291196139d7
github.com/googleapis/gax-go v1.0.3
github.com/googleapis/gax-go/v2 v2.0.5
github.com/onsi/ginkgo v1.14.2 // indirect
github.com/onsi/gomega v1.10.3 // indirect
github.com/stretchr/testify v1.6.1 // indirect
)
Loading

0 comments on commit ba93c2f

Please sign in to comment.