Skip to content

Commit

Permalink
Unit tests for provider AWS (#2)
Browse files Browse the repository at this point in the history
* Setup the required mocking framework for testing

* Code refactoring done to accommodate tests

* Revendored test dependencies

* Makefile now supports testing of driver

* Updated CI scripts and Makefiles

Co-Authored-By: Amshuman K R <amshuman.rao.karaya@sap.com>
  • Loading branch information
prashanth26 and Amshuman K R committed Jul 4, 2019
1 parent 3082e9c commit 6608928
Show file tree
Hide file tree
Showing 613 changed files with 268,862 additions and 17,572 deletions.
2 changes: 1 addition & 1 deletion .ci/build
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved.
# Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
12 changes: 8 additions & 4 deletions .ci/check
Expand Up @@ -34,7 +34,7 @@ fi
# The `go <cmd>` commands requires to see the target repository to be part of a
# Go workspace. Thus, if we are not yet in a Go workspace, let's create one
# temporarily by using symbolic links.
if [[ "${SOURCE_PATH}" != *"src/github.com/gardener/gardener" ]]; then
if [[ "${SOURCE_PATH}" != *"src/github.com/gardener/machine-controller-manager-provider-aws" ]]; then
SOURCE_SYMLINK_PATH="${SOURCE_PATH}/tmp/src/github.com/gardener/machine-controller-manager-provider-aws"
if [[ -d "${SOURCE_PATH}/tmp" ]]; then
rm -rf "${SOURCE_PATH}/tmp"
Expand All @@ -49,10 +49,13 @@ if [[ "${SOURCE_PATH}" != *"src/github.com/gardener/gardener" ]]; then
fi

# Install Golint (linting tool).
go get -u github.com/golang/lint/golint
if ! which golint 1>/dev/null; then
echo -n "Installing golint... "
GO111MODULE=off go get -u golang.org/x/lint/golint
echo "done."
fi

###############################################################################

PACKAGES="$(go list -e ./... | grep -vE '/tmp/|/vendor/')"
LINT_FOLDERS="$(echo ${PACKAGES} | sed "s|github.com/gardener/machine-controller-manager-provider-aws|.|g")"

Expand All @@ -64,5 +67,6 @@ go fmt ${PACKAGES}

# Execute lint checks.
for package in ${LINT_FOLDERS}; do
golint -set_exit_status $(find $package -maxdepth 1 -name "*.go" | grep -vE 'zz_generated|_test.go')
golint -set_exit_status $(find $package -maxdepth 1 -name "*.go" | grep -vE 'zz_generated|_test.go')
done
echo "Check script has passed successfully"
82 changes: 82 additions & 0 deletions .ci/test
@@ -0,0 +1,82 @@
#!/usr/bin/env bash

# Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved.
#
# 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.

set -e

# For the test step concourse will set the following environment variables:
# SOURCE_PATH - path to component repository root directory.

if [[ $(uname) == 'Darwin' ]]; then
READLINK_BIN="greadlink"
else
READLINK_BIN="readlink"
fi

if [[ -z "${SOURCE_PATH}" ]]; then
export SOURCE_PATH="$(${READLINK_BIN} -f $(dirname ${0})/..)"
else
export SOURCE_PATH="$(${READLINK_BIN} -f "${SOURCE_PATH}")"
fi

# The `go <cmd>` commands requires to see the target repository to be part of a
# Go workspace. Thus, if we are not yet in a Go workspace, let's create one
# temporarily by using symbolic links.
if [[ "${SOURCE_PATH}" != *"src/github.com/gardener/machine-controller-manager-provider-aws" ]]; then
SOURCE_SYMLINK_PATH="${SOURCE_PATH}/tmp/src/github.com/gardener/machine-controller-manager-provider-aws"
if [[ -d "${SOURCE_PATH}/tmp" ]]; then
rm -rf "${SOURCE_PATH}/tmp"
fi
mkdir -p "${SOURCE_PATH}/tmp/src/github.com/gardener"
ln -s "${SOURCE_PATH}" "${SOURCE_SYMLINK_PATH}"
cd "${SOURCE_SYMLINK_PATH}"

export GOPATH="${SOURCE_PATH}/tmp"
export GOBIN="${SOURCE_PATH}/tmp/bin"
export PATH="${GOBIN}:${PATH}"
fi

# Install Ginkgo (test framework) to be able to execute the tests.
echo "Fetching Ginkgo frawework"
go get -u github.com/onsi/ginkgo/ginkgo
echo "Successfully fetched Ginkgo frawework"

##############################################################################

function test_with_coverage() {
local output_dir=test/output
local coverprofile_file=coverprofile.out
mkdir -p test/output
ginkgo $GINKGO_COMMON_FLAGS --coverprofile ${coverprofile_file} -covermode=set -outputdir ${output_dir} ${TEST_PACKAGES}

sed -i -e '/mode: set/d' ${output_dir}/${coverprofile_file}
{( echo "mode: set"; cat ${output_dir}/${coverprofile_file} )} > ${output_dir}/${coverprofile_file}.temp
mv ${output_dir}/${coverprofile_file}.temp ${output_dir}/${coverprofile_file}
go tool cover -func ${output_dir}/${coverprofile_file}
}

###############################################################################

if [[ "${SKIP_UNIT_TESTS}" != "" ]]; then
echo ">>>>>Skipping unit tests"
else
echo ">>>>> Invoking unit tests"
TEST_PACKAGES="pkg"
GINKGO_COMMON_FLAGS="-r -timeout=1h0m0s --randomizeAllSpecs --randomizeSuites --failOnPending --progress"
test_with_coverage
echo ">>>>> Finished executing unit tests"
fi

echo "CI tests have passed successfully"
4 changes: 3 additions & 1 deletion .gitignore
@@ -1,3 +1,5 @@
.note
main
seqdiagram.diag
seqdiagram.diag
cmi-server
*coverprofile.out*
4 changes: 2 additions & 2 deletions CODEOWNERS
Validating CODEOWNERS rules …
@@ -1,2 +1,2 @@
# machine-controller-manager maintainers
* @gardener/mcm-maintainers
# machine-controller-manager-provider-aws maintainers
* @gardener/mcm-maintainers
6 changes: 6 additions & 0 deletions Dockerfile
@@ -0,0 +1,6 @@
FROM alpine:3.6

RUN apk add --update bash curl
COPY bin/rel/cmi-server /cmi-server
WORKDIR /
ENTRYPOINT ["/cmi-server"]

0 comments on commit 6608928

Please sign in to comment.