Skip to content

Commit

Permalink
Allow to override the test image resolver, to use env variables. (#168)
Browse files Browse the repository at this point in the history
Also, fixing test of test/pkg/k8s.TestResolveAddress which behaved flaky
on downstream projects.
  • Loading branch information
cardil authored Mar 2, 2022
1 parent b9f7f43 commit 0414b6a
Show file tree
Hide file tree
Showing 13 changed files with 423 additions and 63 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/cloudevents/sdk-go/v2 v2.8.0
github.com/ghodss/yaml v1.0.0
github.com/google/go-containerregistry v0.8.1-0.20220120151853-ac864e57b117
github.com/google/uuid v1.3.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/magefile/mage v1.11.0
Expand Down
2 changes: 1 addition & 1 deletion test/e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ set -Eeuo pipefail

./mage publish

go_test_e2e ./test/...
go_test_e2e -timeout 10m ./test/... || fail_test 'kn-event e2e tests'

success
2 changes: 1 addition & 1 deletion test/e2e/ics_send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func TestInClusterSender(t *testing.T) {
test.MaybeSkip(t)
e2e.RegisterPackages()
e2e.ConfigureImages(t)

t.Parallel()

Expand Down
18 changes: 18 additions & 0 deletions test/e2e/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build e2e
// +build e2e

package e2e

import (
"knative.dev/kn-plugin-event/test/images"
"knative.dev/reconciler-test/pkg/environment"
)

// ConfigureImages will register packages to be built into test images.
func ConfigureImages(t images.TestingT) {
environment.RegisterPackage(watholaForwarderPackage)
images.ResolveImages(t, []string{
"knative.dev/reconciler-test/cmd/eventshub",
watholaForwarderPackage,
})
}
5 changes: 0 additions & 5 deletions test/e2e/kn_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ import (

const watholaForwarderPackage = "knative.dev/eventing/test/test_images/wathola-forwarder"

// RegisterPackages will register packages to be built into test images.
func RegisterPackages() {
environment.RegisterPackage(watholaForwarderPackage)
}

// SendEventToKnService returns a feature.Feature that verifies the kn-event
// can send to Knative service.
func SendEventToKnService() *feature.Feature {
Expand Down
66 changes: 66 additions & 0 deletions test/images/envbased.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package images

import (
"errors"
"fmt"
"os"
"path"
"regexp"
"strings"

"github.com/google/go-containerregistry/pkg/name"
)

var (
// ErrNotFound is returned when there is no environment variable set for
// given KO path.
ErrNotFound = errors.New("expected environment variable not found")
nonAlphaNumeric = regexp.MustCompile("[^A-Z0-9]+")
)

// EnvironmentalBasedResolver will try to resolve the images from prefixed
// environment variables.
type EnvironmentalBasedResolver struct {
Prefix string
}

func (c *EnvironmentalBasedResolver) Applicable() bool {
prefix := c.normalizedPrefix()
for _, environment := range os.Environ() {
const equalitySignParts = 2
parts := strings.SplitN(environment, "=", equalitySignParts)
key := parts[0]
if strings.HasPrefix(key, prefix) {
return true
}
}
return false
}

func (c *EnvironmentalBasedResolver) Resolve(kopath string) (name.Reference, error) {
prefix := c.normalizedPrefix()
shortName := normalize(path.Base(kopath))
key := fmt.Sprintf("%s_%s", prefix, shortName)
val, ok := os.LookupEnv(key)
if !ok {
return nil, fmt.Errorf("%w: '%s' - kopath: %s", ErrNotFound, key, kopath)
}
ref, err := name.ParseReference(val)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrNotFound, err)
}
return ref, nil
}

func (c *EnvironmentalBasedResolver) normalizedPrefix() string {
return normalize(c.Prefix)
}

func normalize(in string) string {
return strings.Trim(
nonAlphaNumeric.ReplaceAllString(strings.ToUpper(in), "_"),
"_",
)
}

var _ Resolver = &EnvironmentalBasedResolver{}
5 changes: 5 additions & 0 deletions test/images/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package images

// Resolvers an array of resolvers to which resolvers could be added for
// downstream projects.
var Resolvers = make([]Resolver, 0, 1) //nolint:gochecknoglobals
56 changes: 56 additions & 0 deletions test/images/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package images

import (
"github.com/google/go-containerregistry/pkg/name"
"k8s.io/apimachinery/pkg/util/json"
"knative.dev/reconciler-test/pkg/environment"
)

// Resolver will resolve given KO package paths into real OCI images references.
// This interface probably should be moved into reconciler-test framework. See:
// https://github.com/knative-sandbox/reconciler-test/issues/303
type Resolver interface {
// Resolve will resolve given KO package path into real OCI image reference.
Resolve(kopath string) (name.Reference, error)
// Applicable will tell that given resolver is applicable to current runtime
// environment, or not.
Applicable() bool
}

// TestingT a subset of testing.T.
type TestingT interface {
Logf(fmt string, args ...interface{})
Fatal(args ...interface{})
Fatalf(fmt string, args ...interface{})
}

// ResolveImages will try to resolve the images, using given resolver(s).
func ResolveImages(t TestingT, packages []string) {
for _, resolver := range Resolvers {
if resolver.Applicable() {
resolveImagesWithResolver(t, packages, resolver)
return
}
}
if len(Resolvers) > 0 {
t.Fatalf("Couldn't resolve images with registered resolvers: %+q", Resolvers)
}
}

func resolveImagesWithResolver(t TestingT, packages []string, resolver Resolver) {
resolved := make(map[string]string)
for _, pack := range packages {
kopath := "ko://" + pack
image, err := resolver.Resolve(kopath)
if err != nil {
t.Fatal(err)
}
resolved[kopath] = image.String()
}
repr, err := json.Marshal(resolved)
if err != nil {
t.Fatal(err)
}
t.Logf("Images resolved to: %s", string(repr))
environment.WithImages(resolved)
}
5 changes: 5 additions & 0 deletions test/pkg/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func WithKnTest(tb testing.TB, handler func(c *TestContext)) {
it, err := clienttest.NewKnTest()
assert.NilError(tb, err)
tb.Cleanup(func() {
if tb.Failed() {
tb.Logf("Skipping '%s' namespace teardown because '%s' test is failing",
it.Namespace(), tb.Name())
return
}
assert.NilError(tb, it.Teardown())
})
handler(&TestContext{
Expand Down
Loading

0 comments on commit 0414b6a

Please sign in to comment.