minikube/test/integration/error_spam_test.go

220 lines
6.4 KiB
Go
Raw Normal View History

2020-04-04 19:41:54 +00:00
// +build integration
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 integration
import (
"context"
"fmt"
"os"
2020-04-04 19:41:54 +00:00
"os/exec"
"path/filepath"
2020-04-17 00:42:37 +00:00
"regexp"
2020-04-04 19:41:54 +00:00
"strings"
"testing"
)
// stderrAllow are regular expressions acceptable to find in normal stderr
var stderrAllow = []string{
// kubectl out of date warning
`kubectl`,
// slow docker warning
`slow|long time|Restarting the docker service may improve`,
2020-04-23 19:40:04 +00:00
// don't care if we can't push images to other profiles
`cache_images.go:.*error getting status`,
2020-05-11 19:38:05 +00:00
// don't care if we can't push images to other profiles which are deleted.
`cache_images.go:.*Failed to load profile`,
2021-04-06 19:27:11 +00:00
// "! 'docker' driver reported a issue that could affect the performance."
`docker.*issue.*performance`,
// "* Suggestion: enable overlayfs kernel module on your Linux"
`Suggestion.*overlayfs`,
2021-04-06 19:27:11 +00:00
// "! docker is currently using the btrfs storage driver, consider switching to overlay2 for better performance"
`docker.*btrfs storage driver`,
2021-02-26 18:06:37 +00:00
// jenkins VMs (debian 9) cgoups don't allow setting memory
`Your cgroup does not allow setting memory.`,
2021-04-06 19:27:11 +00:00
// progress bar output
` > .*`,
}
// stderrAllowRe combines rootCauses into a single regex
var stderrAllowRe = regexp.MustCompile(strings.Join(stderrAllow, "|"))
// TestErrorSpam asserts that there are no unexpected errors displayed in minikube command outputs.
2020-04-09 12:00:42 +00:00
func TestErrorSpam(t *testing.T) {
2020-04-04 19:41:54 +00:00
if NoneDriver() {
t.Skip("none driver always shows a warning")
}
2020-04-04 20:24:31 +00:00
profile := UniqueProfileName("nospam")
ctx, cancel := context.WithTimeout(context.Background(), Minutes(25))
2020-04-04 19:41:54 +00:00
defer CleanupWithLogs(t, profile, cancel)
2021-04-06 19:27:11 +00:00
logDir := filepath.Join(os.TempDir(), profile)
if err := os.MkdirAll(logDir, 0755); err != nil {
t.Fatalf("Unable to make logDir %s: %v", logDir, err)
}
defer os.RemoveAll(logDir)
2021-06-08 20:30:23 +00:00
t.Run("setup", func(t *testing.T) {
// This should likely use multi-node once it's ready
// use `--log_dir` flag to run isolated and avoid race condition - ie, failing to clean up (locked) log files created by other concurently-run tests, or counting them in results
args := append([]string{"start", "-p", profile, "-n=1", "--memory=2250", "--wait=false", fmt.Sprintf("--log_dir=%s", logDir)}, StartArgs()...)
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err != nil {
t.Errorf("%q failed: %v", rr.Command(), err)
}
2020-04-04 19:41:54 +00:00
2021-06-08 20:30:23 +00:00
stdout := rr.Stdout.String()
stderr := rr.Stderr.String()
2021-06-08 20:30:23 +00:00
for _, line := range strings.Split(stderr, "\n") {
if stderrAllowRe.MatchString(line) {
t.Logf("acceptable stderr: %q", line)
continue
}
2020-04-04 20:24:31 +00:00
2021-06-08 20:30:23 +00:00
if len(strings.TrimSpace(line)) > 0 {
t.Errorf("unexpected stderr: %q", line)
}
2020-04-04 20:24:31 +00:00
}
2020-04-04 19:41:54 +00:00
2021-06-08 20:30:23 +00:00
for _, line := range strings.Split(stdout, "\n") {
keywords := []string{"error", "fail", "warning", "conflict"}
for _, keyword := range keywords {
if strings.Contains(line, keyword) {
t.Errorf("unexpected %q in stdout: %q", keyword, line)
}
2020-04-04 20:24:31 +00:00
}
2020-04-04 19:41:54 +00:00
}
2021-06-08 20:30:23 +00:00
if t.Failed() {
t.Logf("minikube stdout:\n%s", stdout)
t.Logf("minikube stderr:\n%s", stderr)
}
2021-06-08 20:30:23 +00:00
steps := []string{
"Generating certificates and keys ...",
"Booting up control plane ...",
"Configuring RBAC rules ...",
}
2021-06-08 20:30:23 +00:00
for _, step := range steps {
if !strings.Contains(stdout, step) {
t.Errorf("missing kubeadm init sub-step %q", step)
}
}
})
2021-05-14 08:42:32 +00:00
logTests := []struct {
command string
args []string
2021-05-14 08:42:32 +00:00
}{
{
command: "start",
args: []string{"--dry-run"},
2021-05-14 08:42:32 +00:00
},
{
command: "status",
2021-05-14 08:42:32 +00:00
}, {
command: "pause",
2021-05-14 08:42:32 +00:00
}, {
command: "unpause",
2021-05-14 08:42:32 +00:00
}, {
command: "stop",
2021-05-14 08:42:32 +00:00
},
}
for _, test := range logTests {
t.Run(test.command, func(t *testing.T) {
// before starting the test, ensure no other logs from the current command are written
2021-06-08 02:51:04 +00:00
logFiles, err := getLogFiles(logDir, test.command)
2021-05-14 08:42:32 +00:00
if err != nil {
t.Fatalf("failed to get old log files for command %s : %v", test.command, err)
2021-05-14 08:42:32 +00:00
}
cleanupLogFiles(t, logFiles)
args := []string{"-p", profile, "--log_dir", logDir, test.command}
args = append(args, test.args...)
// run command twice
for i := 0; i < 2; i++ {
2021-05-14 08:42:32 +00:00
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err != nil {
2021-06-08 19:09:28 +00:00
t.Logf("%q failed: %v", rr.Command(), err)
2021-05-14 08:42:32 +00:00
}
}
// check if one log file exists
if err := checkLogFileCount(test.command, logDir, 1); err != nil {
t.Fatal(err)
}
// get log file generated above
2021-06-08 02:51:04 +00:00
logFiles, err = getLogFiles(logDir, test.command)
2021-05-14 08:42:32 +00:00
if err != nil {
t.Fatalf("failed to get new log files for command %s : %v", test.command, err)
2021-05-14 08:42:32 +00:00
}
// make file at least 1024 KB in size
2021-06-08 19:09:28 +00:00
if err := os.Truncate(logFiles[0], 2e7); err != nil {
t.Fatalf("failed to increase file size to 1024KB: %v", err)
}
2021-05-14 08:42:32 +00:00
2021-06-08 02:43:05 +00:00
// run command again
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
if err != nil {
2021-06-08 19:09:28 +00:00
t.Logf("%q failed: %v", rr.Command(), err)
}
// check if two log files exist now
if err := checkLogFileCount(test.command, logDir, 2); err != nil {
t.Fatal(err)
2021-05-14 08:42:32 +00:00
}
})
}
}
2021-06-08 02:51:04 +00:00
func getLogFiles(logDir string, command string) ([]string, error) {
return filepath.Glob(filepath.Join(logDir, fmt.Sprintf("minikube_%s*", command)))
}
func checkLogFileCount(command string, logDir string, expectedNumberOfLogFiles int) error {
// get log files generated above
2021-06-08 02:51:04 +00:00
logFiles, err := getLogFiles(logDir, command)
if err != nil {
return fmt.Errorf("failed to get new log files for command %s : %v", command, err)
}
if len(logFiles) != expectedNumberOfLogFiles {
return fmt.Errorf("Running cmd %q resulted in %d log file(s); expected: %d", command, len(logFiles), expectedNumberOfLogFiles)
}
return nil
}
2021-05-14 08:42:32 +00:00
// cleanupLogFiles removes logfiles generated during testing
func cleanupLogFiles(t *testing.T, logFiles []string) {
t.Logf("Cleaning up %d logfile(s) ...", len(logFiles))
for _, logFile := range logFiles {
if err := os.Remove(logFile); err != nil {
t.Logf("failed to cleanup log file: %s : %v", logFile, err)
}
}
2020-04-04 19:41:54 +00:00
}