Skip to content

Commit

Permalink
Shell executor logs through testing.T in upgrade tests (#7367)
Browse files Browse the repository at this point in the history
* Shell executor logs through testing.T in upgrade tests

Update deps on knative.dev/pkg

* TMP: Induce error

* Fix

* Revert "TMP: Induce error"

This reverts commit 65c557d.

* Update codegen
  • Loading branch information
mgencur authored Oct 23, 2023
1 parent 5848584 commit b5fd264
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 247 deletions.
2 changes: 1 addition & 1 deletion test/upgrade/installation/git_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func GitHead() pkgupgrade.Operation {
}
for _, shellfunc := range ops {
c.Log.Info("Running shell function: ", shellfunc)
err := callShellFunction(shellfunc)
err := callShellFunction(shellfunc, c.T)
if err != nil {
c.T.Error(err)
return
Expand Down
2 changes: 1 addition & 1 deletion test/upgrade/installation/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func LatestStable() pkgupgrade.Operation {
return pkgupgrade.NewOperation("EventingLatestRelease", func(c pkgupgrade.Context) {
shellfunc := "install_latest_release"
c.Log.Info("Running shell function: ", shellfunc)
err := callShellFunction(shellfunc)
err := callShellFunction(shellfunc, c.T)
if err != nil {
c.T.Error(err)
return
Expand Down
10 changes: 5 additions & 5 deletions test/upgrade/installation/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ limitations under the License.
package installation

import (
"knative.dev/hack/shell"
"testing"

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

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,
})
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,16 @@ 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")

for _, line := range strings.Split(string(p), "\n") {
w.t.Logf(line)
}

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

0 comments on commit b5fd264

Please sign in to comment.