Skip to content

Commit

Permalink
updated test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
rusenask committed Apr 23, 2019
1 parent cc54e08 commit e5c6c5b
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 33 deletions.
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@ To test Keel while developing:

### Running unit tests

To run unit tests:
Get a test parser (makes output nice):

```bash
go get github.com/mfridman/tparse
```

To run unit tests:

```bash
make test
```

Expand Down
131 changes: 102 additions & 29 deletions secrets/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"fmt"
"testing"

"github.com/keel-hq/keel/provider/helm"
"github.com/keel-hq/keel/types"
"github.com/keel-hq/keel/util/image"
testutil "github.com/keel-hq/keel/util/testing"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
)

var secretDataPayload = `{"https://index.docker.io/v1/":{"username":"user-x","password":"pass-x","email":"[email protected]","auth":"somethinghere"}}`
var secretDataPayload2 = `{"https://index.docker.io/v1/":{"username":"foo-user-x-2","password":"bar-pass-x-2","email":"[email protected]","auth":"somethinghere"}}`

var secretDockerConfigJSONPayload = `{
"auths": {
"quay.io": {
Expand All @@ -31,11 +34,13 @@ func TestGetSecret(t *testing.T) {
imgRef, _ := image.Parse("karolisr/webhook-demo:0.0.11")

impl := &testutil.FakeK8sImplementer{
AvailableSecret: &v1.Secret{
Data: map[string][]byte{
dockerConfigKey: []byte(secretDataPayload),
AvailableSecret: map[string]*v1.Secret{
"myregistrysecret": &v1.Secret{
Data: map[string][]byte{
dockerConfigKey: []byte(secretDataPayload),
},
Type: v1.SecretTypeDockercfg,
},
Type: v1.SecretTypeDockercfg,
},
}

Expand Down Expand Up @@ -65,11 +70,13 @@ func TestGetDockerConfigJSONSecret(t *testing.T) {
imgRef, _ := image.Parse("quay.io/karolisr/webhook-demo:0.0.11")

impl := &testutil.FakeK8sImplementer{
AvailableSecret: &v1.Secret{
Data: map[string][]byte{
dockerConfigJSONKey: []byte(secretDockerConfigJSONPayload),
AvailableSecret: map[string]*v1.Secret{
"myregistrysecret": &v1.Secret{
Data: map[string][]byte{
dockerConfigJSONKey: []byte(secretDockerConfigJSONPayload),
},
Type: v1.SecretTypeDockerConfigJson,
},
Type: v1.SecretTypeDockerConfigJson,
},
}

Expand Down Expand Up @@ -98,11 +105,13 @@ func TestGetDockerConfigJSONSecretUsernmePassword(t *testing.T) {
imgRef, _ := image.Parse("karolisr/webhook-demo:0.0.11")

impl := &testutil.FakeK8sImplementer{
AvailableSecret: &v1.Secret{
Data: map[string][]byte{
dockerConfigJSONKey: []byte(secretDockerConfigJSONPayloadWithUsernamePassword),
AvailableSecret: map[string]*v1.Secret{
"myregistrysecret": &v1.Secret{
Data: map[string][]byte{
dockerConfigJSONKey: []byte(secretDockerConfigJSONPayloadWithUsernamePassword),
},
Type: v1.SecretTypeDockerConfigJson,
},
Type: v1.SecretTypeDockerConfigJson,
},
}

Expand Down Expand Up @@ -132,11 +141,13 @@ func TestGetFromDefaultCredentials(t *testing.T) {
imgRef, _ := image.Parse("karolisr/webhook-demo:0.0.11")

impl := &testutil.FakeK8sImplementer{
AvailableSecret: &v1.Secret{
Data: map[string][]byte{
dockerConfigJSONKey: []byte(secretDockerConfigJSONPayloadWithUsernamePassword),
AvailableSecret: map[string]*v1.Secret{
"myregistrysecret": &v1.Secret{
Data: map[string][]byte{
dockerConfigJSONKey: []byte(secretDockerConfigJSONPayloadWithUsernamePassword),
},
Type: v1.SecretTypeDockerConfigJson,
},
Type: v1.SecretTypeDockerConfigJson,
},
}

Expand Down Expand Up @@ -214,11 +225,13 @@ func TestLookupHelmSecret(t *testing.T) {
},
},
},
AvailableSecret: &v1.Secret{
Data: map[string][]byte{
dockerConfigKey: []byte(fmt.Sprintf(secretDataPayloadEncoded, mustEncode("user-y:pass-y"))),
AvailableSecret: map[string]*v1.Secret{
"myregistrysecret": &v1.Secret{
Data: map[string][]byte{
dockerConfigKey: []byte(fmt.Sprintf(secretDataPayloadEncoded, mustEncode("user-y:pass-y"))),
},
Type: v1.SecretTypeDockercfg,
},
Type: v1.SecretTypeDockercfg,
},
}

Expand Down Expand Up @@ -260,11 +273,13 @@ func TestLookupHelmEncodedSecret(t *testing.T) {
},
},
},
AvailableSecret: &v1.Secret{
Data: map[string][]byte{
dockerConfigKey: []byte(secretDataPayload),
AvailableSecret: map[string]*v1.Secret{
"myregistrysecret": &v1.Secret{
Data: map[string][]byte{
dockerConfigKey: []byte(secretDataPayload),
},
Type: v1.SecretTypeDockercfg,
},
Type: v1.SecretTypeDockercfg,
},
}

Expand All @@ -290,6 +305,62 @@ func TestLookupHelmEncodedSecret(t *testing.T) {
}
}

func TestGetDirectHelmSecret(t *testing.T) {
imgRef, _ := image.Parse("karolisr/webhook-demo:0.0.11")

impl := &testutil.FakeK8sImplementer{
AvailablePods: &v1.PodList{
Items: []v1.Pod{
v1.Pod{
Spec: v1.PodSpec{
ImagePullSecrets: []v1.LocalObjectReference{
v1.LocalObjectReference{
Name: "very-secret-dont-look",
},
},
},
},
},
},
AvailableSecret: map[string]*v1.Secret{
"myregistrysecret": &v1.Secret{
Data: map[string][]byte{
dockerConfigKey: []byte(secretDataPayload2),
},
Type: v1.SecretTypeDockercfg,
},
"very-secret-dont-look": &v1.Secret{
Data: map[string][]byte{
dockerConfigKey: []byte(secretDataPayload),
},
Type: v1.SecretTypeDockercfg,
},
},
}

getter := NewGetter(impl, nil)

trackedImage := &types.TrackedImage{
Image: imgRef,
Namespace: "default",
Secrets: []string{"myregistrysecret"},
Provider: helm.ProviderName,
}

creds, err := getter.Get(trackedImage)
if err != nil {
t.Errorf("failed to get creds: %s", err)
}

if creds.Username != "foo-user-x-2" {
t.Errorf("unexpected username: %s", creds.Username)
}

if creds.Password != "bar-pass-x-2" {
t.Errorf("unexpected pass: %s", creds.Password)
}
}

func TestLookupHelmNoSecretsFound(t *testing.T) {
imgRef, _ := image.Parse("karolisr/webhook-demo:0.0.11")

Expand Down Expand Up @@ -350,11 +421,13 @@ func TestLookupWithPortedRegistry(t *testing.T) {
},
},
},
AvailableSecret: &v1.Secret{
Data: map[string][]byte{
dockerConfigKey: []byte(secretDataPayloadWithPort),
AvailableSecret: map[string]*v1.Secret{
"example.com": &v1.Secret{
Data: map[string][]byte{
dockerConfigKey: []byte(secretDataPayloadWithPort),
},
Type: v1.SecretTypeDockercfg,
},
Type: v1.SecretTypeDockercfg,
},
}

Expand Down
12 changes: 9 additions & 3 deletions util/testing/testing.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package testing

import (
"fmt"

"github.com/keel-hq/keel/internal/k8s"
"github.com/keel-hq/keel/types"
"github.com/keel-hq/keel/util/image"

apps_v1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
core_v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
Expand All @@ -20,7 +22,7 @@ type FakeK8sImplementer struct {
// stores value of an updated deployment
Updated *k8s.GenericResource

AvailableSecret *v1.Secret
AvailableSecret map[string]*v1.Secret

AvailablePods *v1.PodList
DeletedPods []*v1.Pod
Expand Down Expand Up @@ -55,7 +57,11 @@ func (i *FakeK8sImplementer) Secret(namespace, name string) (*v1.Secret, error)
if i.Error != nil {
return nil, i.Error
}
return i.AvailableSecret, nil
s, ok := i.AvailableSecret[name]
if !ok {
return nil, fmt.Errorf("secret %s not found", name)
}
return s, nil
}

// Pods - available pods
Expand Down

0 comments on commit e5c6c5b

Please sign in to comment.