Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Rework e2e tests for logstream instrumentation and to be more similar…
Browse files Browse the repository at this point in the history
… to Pipeline's e2e tests

Signed-off-by: Andrew Bayer <andrew.bayer@gmail.com>
  • Loading branch information
abayer committed May 19, 2022
1 parent a225d6e commit 031ee33
Show file tree
Hide file tree
Showing 24 changed files with 1,914 additions and 309 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9
knative.dev/hack v0.0.0-20220328133751-f06773764ce3
knative.dev/pkg v0.0.0-20220329144915-0a1ec2e0d46c
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -142,5 +143,4 @@ require (
k8s.io/klog/v2 v2.60.1-0.20220317184644-43cc75f9ae89 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
75 changes: 75 additions & 0 deletions pkg/names/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2022 The Tekton Authors
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 names

import (
"fmt"
"regexp"

utilrand "k8s.io/apimachinery/pkg/util/rand"
)

// NameGenerator generates names for objects. Some backends may have more information
// available to guide selection of new names and this interface hides those details.
type NameGenerator interface {
// RestrictLengthWithRandomSuffix generates a valid name from the base name, adding a random suffix to the
// the base. If base is valid, the returned name must also be valid. The generator is
// responsible for knowing the maximum valid name length.
RestrictLengthWithRandomSuffix(base string) string

// RestrictLength generates a valid name from the name of a step specified in a Task,
// shortening it to the maximum valid name length if needed.
RestrictLength(base string) string
}

// simpleNameGenerator generates random names.
type simpleNameGenerator struct{}

// SimpleNameGenerator is a generator that returns the name plus a random suffix of five alphanumerics
// when a name is requested. The string is guaranteed to not exceed the length of a standard Kubernetes
// name (63 characters)
var SimpleNameGenerator NameGenerator = simpleNameGenerator{}

const (
// TODO: make this flexible for non-core resources with alternate naming rules.
maxNameLength = 63
randomLength = 5
maxGeneratedNameLength = maxNameLength - randomLength - 1
)

// RestrictLengthWithRandomSuffix takes a base name and returns a potentially shortened version of that name with
// a random suffix, with the whole string no longer than 63 characters.
func (simpleNameGenerator) RestrictLengthWithRandomSuffix(base string) string {
if len(base) > maxGeneratedNameLength {
base = base[:maxGeneratedNameLength]
}
return fmt.Sprintf("%s-%s", base, utilrand.String(randomLength))
}

var alphaNumericRE = regexp.MustCompile(`^[a-zA-Z0-9]+$`)

// RestrictLength takes a base name and returns a potentially shortened version of that name, no longer than 63 characters.
func (simpleNameGenerator) RestrictLength(base string) string {
if len(base) > maxNameLength {
base = base[:maxNameLength]
}

for !alphaNumericRE.MatchString(base[len(base)-1:]) {
base = base[:len(base)-1]
}
return base
}
68 changes: 68 additions & 0 deletions pkg/names/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2022 The Tekton Authors
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 names

import (
"strings"
"testing"

"github.com/tektoncd/resolution/test/names"
)

func TestRestrictLengthWithRandomSuffix(t *testing.T) {
for _, c := range []struct {
in, want string
}{{
in: "hello",
want: "hello-9l9zj",
}, {
in: strings.Repeat("a", 100),
want: strings.Repeat("a", 57) + "-9l9zj",
}} {
t.Run(c.in, func(t *testing.T) {
names.TestingSeed()
got := SimpleNameGenerator.RestrictLengthWithRandomSuffix(c.in)
if got != c.want {
t.Errorf("RestrictLengthWithRandomSuffix:\n got %q\nwant %q", got, c.want)
}
})
}
}

func TestRestrictLength(t *testing.T) {
for _, c := range []struct {
in, want string
}{{
in: "hello",
want: "hello",
}, {
in: strings.Repeat("a", 100),
want: strings.Repeat("a", maxNameLength),
}, {
// Values that don't end with an alphanumeric value are
// trimmed until they do.
in: "abcdefg !@#!$",
want: "abcdefg",
}} {
t.Run(c.in, func(t *testing.T) {
got := SimpleNameGenerator.RestrictLength(c.in)
if got != c.want {
t.Errorf("RestrictLength:\n got %q\nwant %q", got, c.want)
}
})
}
}
85 changes: 85 additions & 0 deletions test/bundles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//go:build e2e

/*
Copyright 2022 The Tekton Authors
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 test

import (
"context"
"os"
"testing"
"time"

"github.com/tektoncd/resolution/pkg/apis/resolution/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
knativetest "knative.dev/pkg/test"
"knative.dev/pkg/test/helpers"
)

// waitInterval is the duration between repeat attempts to check on the
// status of the test's resolution request.
const waitInterval = time.Second

// waitTimeout is the total maximum time the test may spend waiting for
// successful resolution of the test's bundle request.
const waitTimeout = 20 * time.Second

// TestBundlesSmoke creates a resolution request for a bundle and checks
// that it succeeds.
func TestBundlesSmoke(t *testing.T) {
ctx := context.Background()

requestYAML, err := os.ReadFile("./bundles_test/resolution-request.yaml")
if err != nil {
t.Fatalf("unable to read resolution request yaml fixture: %v", err)
}

req := &v1alpha1.ResolutionRequest{}
_, _, err = scheme.Codecs.UniversalDeserializer().Decode(requestYAML, nil, req)
if err != nil {
t.Fatalf("error parsing resolution request yaml fixture: %v", err)
}
req.Name = helpers.ObjectNameForTest(t)

c, ns := setup(ctx, t)
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, ns) }, t.Logf)
defer tearDown(ctx, t, c, ns)

_, err = c.ResolutionRequestClient.Create(ctx, req, metav1.CreateOptions{})
if err != nil {
t.Fatalf("error creating request: %v", err)
}

err = wait.PollImmediate(waitInterval, waitTimeout, func() (bool, error) {
latestResolutionRequest, err := c.ResolutionRequestClient.Get(ctx, req.Name, metav1.GetOptions{})
if err != nil {
return false, err
}
resolvedData := latestResolutionRequest.Status.ResolutionRequestStatusFields.Data
if resolvedData != "" {
return true, nil
}
return false, nil
})

if err != nil {
t.Fatalf("error waiting for completed resolution request: %v", err)
}
}
121 changes: 0 additions & 121 deletions test/bundles_test/bundles_test.go

This file was deleted.

Loading

0 comments on commit 031ee33

Please sign in to comment.