[go: up one dir, main page]

Skip to content

Commit

Permalink
Enable verbose output (nytimes#37)
Browse files Browse the repository at this point in the history
* Update go to 1.19

* Add verbosity to config

* Add zap logger

* Make zap logger globally accessible

* Output request and response object

* Update tester.go

* Update main.go

---------

Co-authored-by: Ahmed Bebars <1381372+abebars@users.noreply.github.com>
  • Loading branch information
Sophia Castellarin and abebars authored Sep 28, 2023
1 parent 1f409e6 commit f2a1007
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
10 changes: 10 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {
Host string
PrintFailedTestsOnly bool
TestDirectory string
Verbosity int
}

// FromEnv returns config read from environment variables
Expand All @@ -40,6 +41,14 @@ func FromEnv() (*Config, error) {
return nil, fmt.Errorf("invalid concurrency value: %d", concurrency)
}

verbosity, err := strconv.Atoi(getEnv("TEST_VERBOSITY", "0"))
if err != nil {
return nil, fmt.Errorf("invalid verbosity value: %s", err)
}
if verbosity < 0 {
return nil, fmt.Errorf("invalid verbosity value: %d", verbosity)
}

printFailedOnly := false
if getEnv("TEST_PRINT_FAILED_ONLY", "false") == "true" {
printFailedOnly = true
Expand All @@ -51,6 +60,7 @@ func FromEnv() (*Config, error) {
DNSOverride: getEnv("TEST_DNS_OVERRIDE", ""),
PrintFailedTestsOnly: printFailedOnly,
TestDirectory: getEnv("TEST_DIRECTORY", "tests"),
Verbosity: verbosity,
}, nil
}

Expand Down
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/nytimes/httptest

go 1.15
go 1.19

require (
github.com/drone/envsubst v1.0.2
Expand All @@ -10,3 +10,13 @@ require (
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/tidwall/match v1.1.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 // indirect
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk=
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
48 changes: 47 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//
// 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.
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Expand All @@ -18,6 +18,9 @@ import (
"fmt"
"log"
"os"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var (
Expand All @@ -31,6 +34,45 @@ var (
BuildTime string
)

func buildLogger(logLevel int) *zap.Logger {
zapLevel := zap.FatalLevel
switch logLevel {
case 0:
zapLevel = zap.FatalLevel
case 1:
zapLevel = zap.InfoLevel
default:
zapLevel = zap.DebugLevel
}

config := zap.Config{
Level: zap.NewAtomicLevelAt(zapLevel),
Development: false,
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: zapcore.OmitKey,
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.EpochTimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{
"stderr",
},
ErrorOutputPaths: []string{
"stderr",
},
}
return zap.Must(config.Build())
}

func main() {
// Print version info
fmt.Printf("httptest: %s %s %s\n", BuildCommit, BuildBranch, BuildTime)
Expand All @@ -45,6 +87,10 @@ func main() {
log.Fatalf("error: failed to apply config: %s", err)
}

logger := buildLogger(config.Verbosity)
defer logger.Sync()
zap.ReplaceGlobals(logger)

// Parse and run tests
tests, err := ParseAllTestsInDirectory(config.TestDirectory)
if err != nil {
Expand Down
14 changes: 13 additions & 1 deletion tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"os"
"regexp"
"strings"

"go.uber.org/zap"
)

// TestResult stores results of a single test
Expand All @@ -29,7 +31,7 @@ type TestResult struct {
Errors []error
}

// RunTest runs single test
// RunTest runs a single test
func RunTest(test *Test, defaultHost string) *TestResult {
result := &TestResult{}

Expand Down Expand Up @@ -68,12 +70,22 @@ func RunTest(test *Test, defaultHost string) *TestResult {
SkipCertVerification: test.SkipCertVerification,
}

zap.L().Info("sending request",
zap.Any("request", reqConfig),
)

resp, respBody, err := SendHTTPRequest(reqConfig)
if err != nil {
result.Errors = append(result.Errors, err)
return result
}

zap.L().Info("got response",
zap.ByteString("body", respBody),
zap.String("status", resp.Status),
zap.Any("headers", resp.Header),
)

// Append response validation errors
result.Errors = append(result.Errors, validateResponse(test, resp, respBody)...)

Expand Down

0 comments on commit f2a1007

Please sign in to comment.