Skip to content
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

Shell executor logs through testing.T in upgrade tests #14495

Merged
merged 13 commits into from
Oct 16, 2023
13 changes: 5 additions & 8 deletions test/upgrade/installation/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package installation

import (
"os"
"testing"

"knative.dev/hack/shell"
pkgupgrade "knative.dev/pkg/test/upgrade"
"knative.dev/pkg/test/upgrade/shell"
)

// Head installs Knative Serving from the HEAD of the default branch.
Expand All @@ -36,21 +36,18 @@ func LatestRelease() pkgupgrade.Operation {
func install(installName, shellFunc string) pkgupgrade.Operation {
return pkgupgrade.NewOperation(installName, func(c pkgupgrade.Context) {
c.Log.Info("Running shell function: ", shellFunc)
if err := callShellFunction(shellFunc); err != nil {
if err := callShellFunction(shellFunc, c.T); err != nil {
c.T.Error(err)
}
})
}

func callShellFunction(funcName string) error {
func callShellFunction(funcName string, t *testing.T) error {
loc, err := shell.NewProjectLocation("../../..")
if err != nil {
return err
}
exec := shell.NewExecutor(shell.ExecutorConfig{
ProjectLocation: loc,
Environ: os.Environ(),
})
exec := shell.NewExecutor(t, loc)
fn := shell.Function{
Script: shell.Script{
Label: funcName,
Expand Down
201 changes: 0 additions & 201 deletions third_party/VENDOR-LICENSE/knative.dev/hack/shell/LICENSE

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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

https://www.apache.org/licenses/LICENSE-2.0
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,
Expand All @@ -17,7 +17,7 @@ limitations under the License.
package shell

import (
"errors"
"bytes"
"fmt"
"os"
"os/exec"
Expand All @@ -31,23 +31,32 @@ const (
executeMode = 0700
)

// ErrNoProjectLocation is returned if user didnt provided the project location.
var ErrNoProjectLocation = errors.New("project location isn't provided")

// NewExecutor creates a new executor from given config.
func NewExecutor(config ExecutorConfig) Executor {
configureDefaultValues(&config)
// NewExecutor creates a new executor.
func NewExecutor(t TestingT, loc ProjectLocation, opts ...Option) Executor {
config := &ExecutorConfig{
ProjectLocation: loc,
Streams: testingTStreams(t),
}
for _, opt := range opts {
opt(config)
}
configureDefaultValues(config)
return &streamingExecutor{
ExecutorConfig: config,
ExecutorConfig: *config,
}
}

// testingTStreams returns Streams which writes to test log.
func testingTStreams(t TestingT) Streams {
tWriter := testingWriter{t: t}
return Streams{
Out: tWriter,
Err: tWriter,
}
}

// RunScript executes a shell script with args.
func (s *streamingExecutor) RunScript(script Script, args ...string) error {
err := validate(s.ExecutorConfig)
if err != nil {
return err
}
cnt := script.scriptContent(s.ProjectLocation, args)
return withTempScript(cnt, func(bin string) error {
return stream(bin, s.ExecutorConfig, script.Label)
Expand All @@ -56,10 +65,6 @@ func (s *streamingExecutor) RunScript(script Script, args ...string) error {

// RunFunction executes a shell function with args.
func (s *streamingExecutor) RunFunction(fn Function, args ...string) error {
err := validate(s.ExecutorConfig)
if err != nil {
return err
}
cnt := fn.scriptContent(s.ProjectLocation, args)
return withTempScript(cnt, func(bin string) error {
return stream(bin, s.ExecutorConfig, fn.Label)
Expand All @@ -70,20 +75,7 @@ type streamingExecutor struct {
ExecutorConfig
}

func validate(config ExecutorConfig) error {
if config.ProjectLocation == nil {
return ErrNoProjectLocation
}
return nil
}

func configureDefaultValues(config *ExecutorConfig) {
if config.Out == nil {
config.Out = os.Stdout
}
if config.Err == nil {
config.Err = os.Stderr
}
if config.LabelOut == "" {
config.LabelOut = defaultLabelOut
}
Expand Down Expand Up @@ -186,3 +178,14 @@ func quoteArgs(args []string) string {
}
return strings.Join(quoted, " ")
}

func (w testingWriter) Write(p []byte) (n int, err error) {
n = len(p)

// Strip trailing newline because t.Log always adds one.
p = bytes.TrimRight(p, "\n")

w.t.Logf("%s", p)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a good idea to Log every line of the output. Just for ascetic reasons. Now some lines are skewed in the output:

executor.go:207: Oct 12 07:08:25.738 install_head_reuse_ingress [OUT] ***************************************
    Oct 12 07:08:25.738 install_head_reuse_ingress [OUT] ***         E2E TEST FAILED         ***
    Oct 12 07:08:25.738 install_head_reuse_ingress [OUT] ***    Start of information dump    ***
    Oct 12 07:08:25.738 install_head_reuse_ingress [OUT] ***************************************
executor.go:207: Oct 12 07:08:25.739 install_head_reuse_ingress [OUT] >>> The dump is located at /logs/artifacts/k8s.dump-.txt
executor.go:207: Oct 12 07:08:26.048 install_head_reuse_ingress [OUT] >>> allowlistedv2workloads.auto.gke.io (0 objects)

See: https://prow.knative.dev/view/gs/knative-prow/pr-logs/pull/knative_serving/14495/upgrade-tests_serving_main/1712360626715627520

Something like:

Suggested change
w.t.Logf("%s", p)
for line := range strings.Split(p, "\n") {
w.t.Log(line)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a cosmetic change but why not. I've tested this in https://prow.knative.dev/view/gs/knative-prow/pr-logs/pull/knative_serving/14495/upgrade-tests_serving_main/1713811249294217216.
Anyway, this change would be in knative.dev/pkg so I can do it later. It doesn't affect this PR.

Copy link
Contributor Author

@mgencur mgencur Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this PR and it's ready for review/merging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return n, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
# 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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

https://www.apache.org/licenses/LICENSE-2.0
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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

https://www.apache.org/licenses/LICENSE-2.0
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,
Expand All @@ -31,7 +31,7 @@ var (
// ErrCallerNotAllowed is raised when user tries to use this shell-out package
// outside of allowed places. This package is deprecated from start and was
// introduced to allow rewriting of shell code to Golang in small chunks.
ErrCallerNotAllowed = errors.New("don't try use knative.dev/hack/shell package outside of allowed places")
ErrCallerNotAllowed = errors.New("don't try use knative.dev/pkg/test/upgrade/shell package outside of allowed places")
)

// NewProjectLocation creates a ProjectLocation that is used to calculate
Expand Down Expand Up @@ -68,7 +68,7 @@ type callerLocation struct {
func isCallsiteAllowed(funcName string) error {
validPaths := []string{
"knative.+/test/upgrade",
"knative(:?\\.dev/|-)hack/shell",
"knative(:?\\.dev/|-)pkg/test/upgrade/shell",
}
for _, validPath := range validPaths {
r := regexp.MustCompile(validPath)
Expand Down
Loading
Loading