-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #332: Use lru cache to expire entrypoint items #350
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package entrypoint_test | ||
package entrypoint | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually usually prefer to have tests in the same package, so that package-level vars/consts/funcs don't get exported just so they can be called from tests. In some cases it's possible to only call funcs that are exported anyway, but sometimes those funcs get long and need to be split up into smaller focused funcs, and it's usually a good idea to avoid exporting those. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, we should definitely not export var/const/func just for the sake of tests 😉 |
||
|
||
import ( | ||
"context" | ||
|
@@ -10,14 +10,17 @@ import ( | |
"strings" | ||
"testing" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/partial" | ||
"github.com/google/go-containerregistry/pkg/v1/types" | ||
"github.com/knative/build/pkg/apis/build/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/knative/build-pipeline/pkg/reconciler/v1alpha1/taskrun/config" | ||
"github.com/knative/build-pipeline/pkg/reconciler/v1alpha1/taskrun/entrypoint" | ||
) | ||
|
||
const ( | ||
exceedCacheSize = 10 | ||
) | ||
|
||
func TestAddEntrypoint(t *testing.T) { | ||
|
@@ -44,12 +47,12 @@ func TestAddEntrypoint(t *testing.T) { | |
`{"args":["abcd"],"process_log":"/tools/process-log.txt","marker_file":"/tools/marker-file.txt"}`, | ||
`{"args":["abcd","efgh"],"process_log":"/tools/process-log.txt","marker_file":"/tools/marker-file.txt"}`, | ||
} | ||
err := entrypoint.RedirectSteps(inputs) | ||
err := RedirectSteps(inputs) | ||
if err != nil { | ||
t.Errorf("failed to get resources: %v", err) | ||
} | ||
for i, input := range inputs { | ||
if len(input.Command) == 0 || input.Command[0] != entrypoint.BinaryLocation { | ||
if len(input.Command) == 0 || input.Command[0] != BinaryLocation { | ||
t.Errorf("command incorrectly set: %q", input.Command) | ||
} | ||
if len(input.Args) > 0 { | ||
|
@@ -59,13 +62,13 @@ func TestAddEntrypoint(t *testing.T) { | |
t.Error("there should be atleast one envvar") | ||
} | ||
for _, e := range input.Env { | ||
if e.Name == entrypoint.JSONConfigEnvVar && e.Value != envVarStrings[i] { | ||
if e.Name == JSONConfigEnvVar && e.Value != envVarStrings[i] { | ||
t.Errorf("envvar \n%s\n does not match \n%s", e.Value, envVarStrings[i]) | ||
} | ||
} | ||
found := false | ||
for _, vm := range input.VolumeMounts { | ||
if vm.Name == entrypoint.MountName { | ||
if vm.Name == MountName { | ||
found = true | ||
break | ||
} | ||
|
@@ -165,7 +168,11 @@ func TestGetRemoteEntrypoint(t *testing.T) { | |
})) | ||
defer server.Close() | ||
image := path.Join(strings.TrimPrefix(server.URL, "http://"), "image:latest") | ||
ep, err := entrypoint.GetRemoteEntrypoint(entrypoint.NewCache(), image) | ||
entrypointCache, err := NewCache() | ||
if err != nil { | ||
t.Fatalf("couldn't create new entrypoint cache: %v", err) | ||
} | ||
ep, err := GetRemoteEntrypoint(entrypointCache, image) | ||
if err != nil { | ||
t.Errorf("couldn't get entrypoint remote: %v", err) | ||
} | ||
|
@@ -174,6 +181,31 @@ func TestGetRemoteEntrypoint(t *testing.T) { | |
} | ||
} | ||
|
||
func TestEntrypointCacheLRU(t *testing.T) { | ||
entrypoint := []string{"/bin/expected", "entrypoint"} | ||
entrypointCache, err := NewCache() | ||
if err != nil { | ||
t.Fatalf("couldn't create new entrypoint cache: %v", err) | ||
} | ||
|
||
for i := 0; i < cacheSize+exceedCacheSize; i++ { | ||
image := fmt.Sprintf("image%d:latest", i) | ||
entrypointCache.set(image, entrypoint) | ||
} | ||
for i := 0; i < exceedCacheSize; i++ { | ||
image := fmt.Sprintf("image%d:latest", i) | ||
if _, ok := entrypointCache.get(image); ok { | ||
t.Errorf("entrypoint of image %s should be expired", image) | ||
} | ||
} | ||
for i := exceedCacheSize; i < cacheSize+exceedCacheSize; i++ { | ||
image := fmt.Sprintf("image%d:latest", i) | ||
if _, ok := entrypointCache.get(image); !ok { | ||
t.Errorf("entrypoint of image %s shouldn't be expired", image) | ||
} | ||
} | ||
} | ||
|
||
func TestAddCopyStep(t *testing.T) { | ||
cfg := &config.Config{ | ||
Entrypoint: &config.Entrypoint{ | ||
|
@@ -194,11 +226,11 @@ func TestAddCopyStep(t *testing.T) { | |
} | ||
|
||
expectedSteps := len(bs.Steps) + 1 | ||
entrypoint.AddCopyStep(ctx, bs) | ||
AddCopyStep(ctx, bs) | ||
if len(bs.Steps) != 3 { | ||
t.Errorf("BuildSpec has the wrong step count: %d should be %d", len(bs.Steps), expectedSteps) | ||
} | ||
if bs.Steps[0].Name != entrypoint.InitContainerName { | ||
t.Errorf("entrypoint is incorrect: %s should be %s", bs.Steps[0].Name, entrypoint.InitContainerName) | ||
if bs.Steps[0].Name != InitContainerName { | ||
t.Errorf("entrypoint is incorrect: %s should be %s", bs.Steps[0].Name, InitContainerName) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool!