Skip to content

Commit

Permalink
Merge pull request #7 from reactiveops/rs/initial-tests
Browse files Browse the repository at this point in the history
Adding initial tests + CI
  • Loading branch information
robscott authored Dec 26, 2018
2 parents 67d7e93 + 5715c25 commit ff7c459
Show file tree
Hide file tree
Showing 15 changed files with 4,172 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
version: 2

references:
docker_build_and_push: &docker_build_and_push
run:
name: Docker login, build, and push
command: |
docker login quay.io -u="reactiveops+circleci" -p="${quay_token}"
docker build -f Dockerfile -t quay.io/reactiveops/fairwinds:$DOCKER_BASE_TAG .
docker push quay.io/reactiveops/fairwinds:$DOCKER_BASE_TAG
jobs:
test:
working_directory: /go/src/github.com/reactiveops/fairwinds/
docker:
- image: circleci/golang:1.11

steps:
- checkout
- run: dep ensure
- run: go test ./pkg/... -coverprofile cover.out

build:
docker:
- image: circleci/buildpack-deps:stretch
steps:
- checkout
- setup_remote_docker
- run: echo 'export DOCKER_BASE_TAG=dev-$CIRCLE_SHA1' >> $BASH_ENV
- *docker_build_and_push

release:
docker:
- image: circleci/buildpack-deps:stretch
steps:
- checkout
- setup_remote_docker
- run: echo 'export DOCKER_BASE_TAG=$CIRCLE_TAG' >> $BASH_ENV
- *docker_build_and_push


workflows:
version: 2
build:
jobs:
- test
- build:
context: org-global
# Allow using testing tags for testing circle test + build steps
filters:
tags:
only: /^testing-.*/
release:
jobs:
- release:
context: org-global
filters:
branches:
ignore: /.*/
# Testing tags are reserved for testing circle test + build steps
tags:
ignore: /^testing-.*/
17 changes: 17 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

215 changes: 215 additions & 0 deletions pkg/validator/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
// Copyright 2018 ReactiveOps
//
// 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 validator

import (
"k8s.io/apimachinery/pkg/api/resource"
"testing"

conf "github.com/reactiveops/fairwinds/pkg/config"
types "github.com/reactiveops/fairwinds/pkg/types"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
)

func TestValidateResourcesEmptyConfig(t *testing.T) {
container := corev1.Container{
Name: "Empty",
}

cv := ContainerValidation{
Container: container,
}

expected := conf.RequestsAndLimits{}

cv.validateResources(expected)
assert.Len(t, cv.Failures, 0)
}

func TestValidateResourcesEmptyContainer(t *testing.T) {
container := corev1.Container{
Name: "Empty",
}

resourceConf := conf.RequestsAndLimits{
Requests: conf.ResourceList{
"cpu": conf.ResourceMinMax{
Min: "100m",
Max: "1",
},
"memory": conf.ResourceMinMax{
Min: "100Mi",
Max: "3Gi",
},
},
Limits: conf.ResourceList{
"cpu": conf.ResourceMinMax{
Min: "150m",
Max: "2",
},
"memory": conf.ResourceMinMax{
Min: "150Mi",
Max: "4Gi",
},
},
}

expectedFailures := []types.Failure{
{
Name: "requests.cpu",
Expected: "100m",
Actual: "0",
},
{
Name: "requests.memory",
Expected: "100Mi",
Actual: "0",
},
{
Name: "limits.cpu",
Expected: "150m",
Actual: "0",
},
{
Name: "limits.memory",
Expected: "150Mi",
Actual: "0",
},
}

testValidateResources(t, &container, &resourceConf, &expectedFailures)
}

func TestValidateResourcesPartiallyValid(t *testing.T) {
cpuRequest := resource.Quantity{}
cpuRequest.SetMilli(100)

cpuLimit := resource.Quantity{}
cpuLimit.SetMilli(200)

container := corev1.Container{
Name: "Empty",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"cpu": cpuRequest,
},
Limits: corev1.ResourceList{
"cpu": cpuLimit,
},
},
}

resourceConf := conf.RequestsAndLimits{
Requests: conf.ResourceList{
"cpu": conf.ResourceMinMax{
Min: "100m",
Max: "1",
},
"memory": conf.ResourceMinMax{
Min: "100Mi",
Max: "3Gi",
},
},
Limits: conf.ResourceList{
"cpu": conf.ResourceMinMax{
Min: "150m",
Max: "2",
},
"memory": conf.ResourceMinMax{
Min: "150Mi",
Max: "4Gi",
},
},
}

expectedFailures := []types.Failure{
{
Name: "requests.memory",
Expected: "100Mi",
Actual: "0",
},
{
Name: "limits.memory",
Expected: "150Mi",
Actual: "0",
},
}

testValidateResources(t, &container, &resourceConf, &expectedFailures)
}

func TestValidateResourcesFullyValid(t *testing.T) {
cpuRequest := resource.Quantity{}
cpuRequest.SetMilli(100)

cpuLimit := resource.Quantity{}
cpuLimit.SetMilli(200)

memoryRequest := resource.Quantity{}
memoryRequest.SetScaled(105, resource.Mega)

memoryLimit := resource.Quantity{}
memoryLimit.SetScaled(200, resource.Mega)

container := corev1.Container{
Name: "Empty",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"cpu": cpuRequest,
"memory": memoryRequest,
},
Limits: corev1.ResourceList{
"cpu": cpuLimit,
"memory": memoryLimit,
},
},
}

resourceConf := conf.RequestsAndLimits{
Requests: conf.ResourceList{
"cpu": conf.ResourceMinMax{
Min: "100m",
Max: "1",
},
"memory": conf.ResourceMinMax{
Min: "100Mi",
Max: "3Gi",
},
},
Limits: conf.ResourceList{
"cpu": conf.ResourceMinMax{
Min: "150m",
Max: "2",
},
"memory": conf.ResourceMinMax{
Min: "150Mi",
Max: "4Gi",
},
},
}

testValidateResources(t, &container, &resourceConf, &[]types.Failure{})
}

func testValidateResources(t *testing.T, container *corev1.Container, conf *conf.RequestsAndLimits, expectedFailures *[]types.Failure) {
cv := ContainerValidation{
Container: *container,
}

cv.validateResources(*conf)
assert.Len(t, cv.Failures, len(*expectedFailures))
assert.ElementsMatch(t, cv.Failures, *expectedFailures)
}
27 changes: 27 additions & 0 deletions vendor/github.com/pmezard/go-difflib/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ff7c459

Please sign in to comment.