Skip to content

Commit

Permalink
Add tests (#220)
Browse files Browse the repository at this point in the history
* fix(): add url to webhook

* fix(): fixing aws session config

* test(): adding tests

* test(): adding common webhook

* test(): adding tests to common webhook

* test(): adding tests to common webhook

* test(): adding aws common functionality and tests

* test(): adding tests for community gateways

* test(): adding tests for community gateways

* test(): adding tests for storage grid gateway

* test(): adding tests for core gateways

* test(): adding tests to core gateways

* test(): adding tests to gateway

* test(): adding tests to gateway

* test(): adding tests for sensor

* docs(): updating protocol docs

* test(): adding tests

* chore(): update readme

* chore(): fix comments

* chore(): fix version

* chore(): update .lock
  • Loading branch information
VaibhavPage authored and magaldima committed Mar 14, 2019
1 parent 9efd6a1 commit af1f855
Show file tree
Hide file tree
Showing 63 changed files with 3,191 additions and 525 deletions.
459 changes: 81 additions & 378 deletions Gopkg.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![slack](https://img.shields.io/badge/slack-argoproj-brightgreen.svg?logo=slack)](https://argoproj.github.io/community/join-slack)
[![Build Status](https://travis-ci.org/argoproj/argo-events.svg?branch=master)](https://travis-ci.org/argoproj/argo-events)
[![Coverage Status](https://coveralls.io/repos/github/argoproj/argo-events/badge.svg)](https://coveralls.io/github/argoproj/argo-events)
[![GoDoc](https://godoc.org/github.com/argoproj/argo-events?status.svg)](https://godoc.org/github.com/argoproj/argo-events/pkg/apis)
[![GoDoc](https://godoc.org/github.com/argoproj/argo-events?status.svg)](https://godoc.org/github.com/argoproj/argo-events/pkg/apis)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)

<p align="center">
Expand Down
10 changes: 8 additions & 2 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func DefaultServiceName(serviceName string) string {
return fmt.Sprintf("%s-svc", serviceName)
}

// DefaultGatewayConfigurationName returns a formulated name for a gateway configuration
func DefaultGatewayConfigurationName(gatewayName string, configurationName string) string {
// DefaultEventSourceName returns a formulated name for a gateway configuration
func DefaultEventSourceName(gatewayName string, configurationName string) string {
return fmt.Sprintf("%s:%s", gatewayName, configurationName)
}

Expand Down Expand Up @@ -88,6 +88,12 @@ func SendErrorResponse(writer http.ResponseWriter, response string) {
writer.Write([]byte(response))
}

// SendInternalErrorResponse sends http internal error response
func SendInternalErrorResponse(writer http.ResponseWriter, response string) {
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte(response))
}

// LoggerConf returns standard logging configuration
func LoggerConf() zerolog.ConsoleWriter {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
Expand Down
53 changes: 51 additions & 2 deletions common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,35 @@ limitations under the License.
package common

import (
"testing"

"fmt"
"github.com/smartystreets/goconvey/convey"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/fake"
)

type fakeHttpWriter struct {
header int
payload []byte
}

func (f *fakeHttpWriter) Header() http.Header {
return http.Header{}
}

func (f *fakeHttpWriter) Write(body []byte) (int, error) {
f.payload = body
return len(body), nil
}

func (f *fakeHttpWriter) WriteHeader(status int) {
f.header = status
}

func TestGetObjectHash(t *testing.T) {
convey.Convey("Given a value, hash it", t, func() {
hash, err := GetObjectHash(&corev1.Pod{})
Expand All @@ -47,6 +66,36 @@ func TestDefaultConfigMapName(t *testing.T) {
assert.Equal(t, "sensor-controller-configmap", res)
}

func TestDefaultServiceName(t *testing.T) {
convey.Convey("Given a service, get the default name", t, func() {
convey.So(DefaultServiceName("default"), convey.ShouldEqual, fmt.Sprintf("%s-svc", "default"))
})
}

func TestDefaultNatsQueueName(t *testing.T) {
convey.Convey("Given a nats queue, get the default name", t, func() {
convey.So(DefaultNatsQueueName("default"), convey.ShouldEqual, "default-queue")
})
}

func TestHTTPMethods(t *testing.T) {
convey.Convey("Given a http write", t, func() {
convey.Convey("Write a success response", func() {
f := &fakeHttpWriter{}
SendSuccessResponse(f, "hello")
convey.So(string(f.payload), convey.ShouldEqual, "hello")
convey.So(f.header, convey.ShouldEqual, http.StatusOK)
})

convey.Convey("Write a failure response", func() {
f := &fakeHttpWriter{}
SendErrorResponse(f, "failure")
convey.So(string(f.payload), convey.ShouldEqual, "failure")
convey.So(f.header, convey.ShouldEqual, http.StatusBadRequest)
})
})
}

func TestServerResourceForGroupVersionKind(t *testing.T) {
convey.Convey("Given a k8s client", t, func() {
fakeClient := fake.NewSimpleClientset()
Expand Down
44 changes: 44 additions & 0 deletions controllers/gateway/state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package gateway

import (
"testing"

"github.com/argoproj/argo-events/common"
"github.com/argoproj/argo-events/pkg/client/gateway/clientset/versioned/fake"
"github.com/smartystreets/goconvey/convey"
)

func TestPersistUpdates(t *testing.T) {
convey.Convey("Given a gateway resource", t, func() {
namespace := "argo-events"
client := fake.NewSimpleClientset()
logger := common.GetLoggerContext(common.LoggerConf()).Logger()
gw, err := getGateway()
convey.So(err, convey.ShouldBeNil)

convey.Convey("Create the gateway", func() {
gw, err = client.ArgoprojV1alpha1().Gateways(namespace).Create(gw)
convey.So(err, convey.ShouldBeNil)
convey.So(gw, convey.ShouldNotBeNil)

gw.ObjectMeta.Labels = map[string]string{
"default": "default",
}

convey.Convey("Update the gateway", func() {
updatedGw, err := PersistUpdates(client, gw, &logger)
convey.So(err, convey.ShouldBeNil)
convey.So(updatedGw, convey.ShouldNotEqual, gw)
convey.So(updatedGw.Labels, convey.ShouldResemble, gw.Labels)

updatedGw.Labels["new"] = "new"

convey.Convey("Reapply the gateway", func() {
err := ReapplyUpdates(client, updatedGw)
convey.So(err, convey.ShouldBeNil)
convey.So(len(updatedGw.Labels), convey.ShouldEqual, 2)
})
})
})
})
}
6 changes: 3 additions & 3 deletions docs/gateway-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

## Table of Contents

- [github.com/argoproj/argo-events/pkg/apis/gateway/v1alpha1/generated.proto](#github.com/argoproj/argo-events/pkg/apis/gateway/v1alpha1/generated.proto)
- [pkg/apis/gateway/v1alpha1/generated.proto](#pkg/apis/gateway/v1alpha1/generated.proto)
- [Gateway](#gh.neting.cc.argoproj.argo_events.pkg.apis.gateway.v1alpha1.Gateway)
- [GatewayList](#gh.neting.cc.argoproj.argo_events.pkg.apis.gateway.v1alpha1.GatewayList)
- [GatewayNotificationWatcher](#gh.neting.cc.argoproj.argo_events.pkg.apis.gateway.v1alpha1.GatewayNotificationWatcher)
Expand All @@ -22,10 +22,10 @@



<a name="github.com/argoproj/argo-events/pkg/apis/gateway/v1alpha1/generated.proto"></a>
<a name="pkg/apis/gateway/v1alpha1/generated.proto"></a>
<p align="right"><a href="#top">Top</a></p>

## github.com/argoproj/argo-events/pkg/apis/gateway/v1alpha1/generated.proto
## pkg/apis/gateway/v1alpha1/generated.proto



Expand Down
65 changes: 62 additions & 3 deletions docs/sensor-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@

## Table of Contents

- [github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1/generated.proto](#github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1/generated.proto)
- [pkg/apis/sensor/v1alpha1/generated.proto](#pkg/apis/sensor/v1alpha1/generated.proto)
- [ArtifactLocation](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.ArtifactLocation)
- [ConfigmapArtifact](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.ConfigmapArtifact)
- [DataFilter](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.DataFilter)
- [DependencyGroup](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.DependencyGroup)
- [EventDependency](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.EventDependency)
- [EventDependencyFilter](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.EventDependencyFilter)
- [FileArtifact](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.FileArtifact)
- [GitArtifact](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.GitArtifact)
- [GitCreds](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.GitCreds)
- [GitRemoteConfig](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.GitRemoteConfig)
- [GroupVersionKind](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.GroupVersionKind)
- [NodeStatus](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.NodeStatus)
- [ResourceObject](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.ResourceObject)
Expand All @@ -36,10 +39,10 @@



<a name="github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1/generated.proto"></a>
<a name="pkg/apis/sensor/v1alpha1/generated.proto"></a>
<p align="right"><a href="#top">Top</a></p>

## github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1/generated.proto
## pkg/apis/sensor/v1alpha1/generated.proto



Expand All @@ -56,6 +59,7 @@ ArtifactLocation describes the source location for an external artifact
| file | [FileArtifact](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.FileArtifact) | optional | |
| url | [URLArtifact](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.URLArtifact) | optional | |
| configmap | [ConfigmapArtifact](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.ConfigmapArtifact) | optional | |
| git | [GitArtifact](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.GitArtifact) | optional | |



Expand Down Expand Up @@ -165,6 +169,61 @@ FileArtifact contains information about an artifact in a filesystem



<a name="gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.GitArtifact"></a>

### GitArtifact
GitArtifact contains information about an artifact stored in git


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| url | [string](#string) | optional | Git URL |
| cloneDirectory | [string](#string) | optional | Directory to clone the repository. We clone complete directory because GitArtifact is not limited to any specific Git service providers. Hence we don&#39;t use any specific git provider client. |
| creds | [GitCreds](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.GitCreds) | optional | Creds contain reference to git username and password &#43;optional |
| namespace | [string](#string) | optional | Namespace where creds are stored. &#43;optional |
| sshKeyPath | [string](#string) | optional | SSHKeyPath is path to your ssh key path. Use this if you don&#39;t want to provide username and password. ssh key path must be mounted in sensor pod. &#43;optional |
| filePath | [string](#string) | optional | Path to file that contains trigger resource definition |
| branch | [string](#string) | optional | Branch to use to pull trigger resource &#43;optional |
| tag | [string](#string) | optional | Tag to use to pull trigger resource &#43;optional |
| remote | [GitRemoteConfig](#gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.GitRemoteConfig) | optional | Git remote to manage set of tracked repositories. Defaults to &#34;origin&#34;. Refer https://git-scm.com/docs/git-remote &#43;optional |






<a name="gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.GitCreds"></a>

### GitCreds
GitCreds contain reference to git username and password


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| username | [k8s.io.api.core.v1.SecretKeySelector](#k8s.io.api.core.v1.SecretKeySelector) | optional | |
| password | [k8s.io.api.core.v1.SecretKeySelector](#k8s.io.api.core.v1.SecretKeySelector) | optional | |






<a name="gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.GitRemoteConfig"></a>

### GitRemoteConfig



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| name | [string](#string) | optional | Name of the remote to fetch from. |
| urls | [string](#string) | repeated | URLs the URLs of a remote repository. It must be non-empty. Fetch will always use the first URL, while push will use all of them. |






<a name="gh.neting.cc.argoproj.argo_events.pkg.apis.sensor.v1alpha1.GroupVersionKind"></a>

### GroupVersionKind
Expand Down
50 changes: 50 additions & 0 deletions gateways/common/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2018 BlackRock, Inc.
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 common

import (
"github.com/argoproj/argo-events/store"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
)

// GetAWSCreds reads credential stored in Kubernetes secrets and return it.
func GetAWSCreds(client kubernetes.Interface, namespace string, access *corev1.SecretKeySelector, secret *corev1.SecretKeySelector) (*credentials.Credentials, error) {
accessKey, err := store.GetSecrets(client, namespace, access.Name, access.Key)
if err != nil {
return nil, err
}
secretKey, err := store.GetSecrets(client, namespace, secret.Name, secret.Key)
if err != nil {
return nil, err
}

return credentials.NewStaticCredentialsFromCreds(credentials.Value{
AccessKeyID: accessKey,
SecretAccessKey: secretKey,
}), nil
}

func GetAWSSession(creds *credentials.Credentials, region string) (*session.Session, error) {
return session.NewSession(&aws.Config{
Region: &region,
Credentials: creds,
})
}
Loading

0 comments on commit af1f855

Please sign in to comment.