k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/framework/log_test.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package framework_test
    18  
    19  import (
    20  	"errors"
    21  	"os"
    22  	"path"
    23  	"testing"
    24  
    25  	"github.com/onsi/ginkgo/v2"
    26  	"github.com/onsi/ginkgo/v2/reporters"
    27  	"github.com/onsi/gomega"
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  
    31  	"k8s.io/klog/v2"
    32  	"k8s.io/kubernetes/test/e2e/framework"
    33  	"k8s.io/kubernetes/test/e2e/framework/internal/output"
    34  )
    35  
    36  // The line number of the following code is checked in TestFailureOutput below.
    37  // Be careful when moving it around or changing the import statements above.
    38  // Here are some intentionally blank lines that can be removed to compensate
    39  // for future additional import statements.
    40  // This must be line #40.
    41  
    42  // This is included in a stack backtrace.
    43  func failHelper(msg string) {
    44  	framework.Fail(msg)
    45  }
    46  
    47  var _ = ginkgo.Describe("log", func() {
    48  	ginkgo.BeforeEach(func() {
    49  		framework.Logf("before")
    50  	})
    51  	ginkgo.AfterEach(func() {
    52  		framework.Logf("after")
    53  		gomega.Expect(true).To(gomega.BeFalse(), "true is never false either")
    54  	})
    55  	ginkgo.It("fails", func() {
    56  		func() {
    57  			framework.Failf("I'm failing.")
    58  		}()
    59  	})
    60  	ginkgo.It("asserts", func() {
    61  		gomega.Expect(false).To(gomega.BeTrue(), "false is never true")
    62  	})
    63  	ginkgo.It("error", func() {
    64  		err := errors.New("an error with a long, useless description")
    65  		framework.ExpectNoError(err, "hard-coded error")
    66  	})
    67  	ginkgo.It("equal", func() {
    68  		gomega.Expect(0).To(gomega.Equal(1), "of course it's not equal...")
    69  	})
    70  	ginkgo.It("fails with helper", func() {
    71  		failHelper("I'm failing with helper.")
    72  	})
    73  	ginkgo.It("redirects klog", func() {
    74  		klog.Info("hello world")
    75  		klog.Error(nil, "not really an error")
    76  	})
    77  })
    78  
    79  func TestFailureOutput(t *testing.T) {
    80  	expected := output.TestResult{
    81  		Suite: reporters.JUnitTestSuite{
    82  			Tests:    6,
    83  			Failures: 6,
    84  			Errors:   0,
    85  			Disabled: 0,
    86  			Skipped:  0,
    87  
    88  			TestCases: []reporters.JUnitTestCase{
    89  				{
    90  					Name:   "[It] log fails",
    91  					Status: "failed",
    92  					Failure: &reporters.JUnitFailure{
    93  						Type: "failed",
    94  						Description: `[FAILED] I'm failing.
    95  In [It] at: log_test.go:57 <time>
    96  
    97  There were additional failures detected after the initial failure. These are visible in the timeline
    98  `,
    99  					},
   100  					SystemErr: `> Enter [BeforeEach] log - log_test.go:48 <time>
   101  <klog> log_test.go:49] before
   102  < Exit [BeforeEach] log - log_test.go:48 <time>
   103  > Enter [It] fails - log_test.go:55 <time>
   104  [FAILED] I'm failing.
   105  In [It] at: log_test.go:57 <time>
   106  < Exit [It] fails - log_test.go:55 <time>
   107  > Enter [AfterEach] log - log_test.go:51 <time>
   108  <klog> log_test.go:52] after
   109  [FAILED] true is never false either
   110  Expected
   111      <bool>: true
   112  to be false
   113  In [AfterEach] at: log_test.go:53 <time>
   114  < Exit [AfterEach] log - log_test.go:51 <time>
   115  `,
   116  				},
   117  				{
   118  					Name:   "[It] log asserts",
   119  					Status: "failed",
   120  					Failure: &reporters.JUnitFailure{
   121  						Type: "failed",
   122  						Description: `[FAILED] false is never true
   123  Expected
   124      <bool>: false
   125  to be true
   126  In [It] at: log_test.go:61 <time>
   127  
   128  There were additional failures detected after the initial failure. These are visible in the timeline
   129  `,
   130  					},
   131  					SystemErr: `> Enter [BeforeEach] log - log_test.go:48 <time>
   132  <klog> log_test.go:49] before
   133  < Exit [BeforeEach] log - log_test.go:48 <time>
   134  > Enter [It] asserts - log_test.go:60 <time>
   135  [FAILED] false is never true
   136  Expected
   137      <bool>: false
   138  to be true
   139  In [It] at: log_test.go:61 <time>
   140  < Exit [It] asserts - log_test.go:60 <time>
   141  > Enter [AfterEach] log - log_test.go:51 <time>
   142  <klog> log_test.go:52] after
   143  [FAILED] true is never false either
   144  Expected
   145      <bool>: true
   146  to be false
   147  In [AfterEach] at: log_test.go:53 <time>
   148  < Exit [AfterEach] log - log_test.go:51 <time>
   149  `,
   150  				},
   151  				{
   152  					Name:   "[It] log error",
   153  					Status: "failed",
   154  					Failure: &reporters.JUnitFailure{
   155  						Type: "failed",
   156  						Description: `[FAILED] hard-coded error: an error with a long, useless description
   157  In [It] at: log_test.go:65 <time>
   158  
   159  There were additional failures detected after the initial failure. These are visible in the timeline
   160  `,
   161  					},
   162  					SystemErr: `> Enter [BeforeEach] log - log_test.go:48 <time>
   163  <klog> log_test.go:49] before
   164  < Exit [BeforeEach] log - log_test.go:48 <time>
   165  > Enter [It] error - log_test.go:63 <time>
   166  <klog> log_test.go:65] Unexpected error: hard-coded error: 
   167      <*errors.errorString>: 
   168      an error with a long, useless description
   169      {
   170          s: "an error with a long, useless description",
   171      }
   172  [FAILED] hard-coded error: an error with a long, useless description
   173  In [It] at: log_test.go:65 <time>
   174  < Exit [It] error - log_test.go:63 <time>
   175  > Enter [AfterEach] log - log_test.go:51 <time>
   176  <klog> log_test.go:52] after
   177  [FAILED] true is never false either
   178  Expected
   179      <bool>: true
   180  to be false
   181  In [AfterEach] at: log_test.go:53 <time>
   182  < Exit [AfterEach] log - log_test.go:51 <time>
   183  `,
   184  				},
   185  				{
   186  					Name:   "[It] log equal",
   187  					Status: "failed",
   188  					Failure: &reporters.JUnitFailure{
   189  						Type: "failed",
   190  						Description: `[FAILED] of course it's not equal...
   191  Expected
   192      <int>: 0
   193  to equal
   194      <int>: 1
   195  In [It] at: log_test.go:68 <time>
   196  
   197  There were additional failures detected after the initial failure. These are visible in the timeline
   198  `,
   199  					},
   200  					SystemErr: `> Enter [BeforeEach] log - log_test.go:48 <time>
   201  <klog> log_test.go:49] before
   202  < Exit [BeforeEach] log - log_test.go:48 <time>
   203  > Enter [It] equal - log_test.go:67 <time>
   204  [FAILED] of course it's not equal...
   205  Expected
   206      <int>: 0
   207  to equal
   208      <int>: 1
   209  In [It] at: log_test.go:68 <time>
   210  < Exit [It] equal - log_test.go:67 <time>
   211  > Enter [AfterEach] log - log_test.go:51 <time>
   212  <klog> log_test.go:52] after
   213  [FAILED] true is never false either
   214  Expected
   215      <bool>: true
   216  to be false
   217  In [AfterEach] at: log_test.go:53 <time>
   218  < Exit [AfterEach] log - log_test.go:51 <time>
   219  `,
   220  				},
   221  				{
   222  					Name:   "[It] log fails with helper",
   223  					Status: "failed",
   224  					Failure: &reporters.JUnitFailure{
   225  						Type: "failed",
   226  						Description: `[FAILED] I'm failing with helper.
   227  In [It] at: log_test.go:44 <time>
   228  
   229  There were additional failures detected after the initial failure. These are visible in the timeline
   230  `,
   231  					},
   232  					SystemErr: `> Enter [BeforeEach] log - log_test.go:48 <time>
   233  <klog> log_test.go:49] before
   234  < Exit [BeforeEach] log - log_test.go:48 <time>
   235  > Enter [It] fails with helper - log_test.go:70 <time>
   236  [FAILED] I'm failing with helper.
   237  In [It] at: log_test.go:44 <time>
   238  < Exit [It] fails with helper - log_test.go:70 <time>
   239  > Enter [AfterEach] log - log_test.go:51 <time>
   240  <klog> log_test.go:52] after
   241  [FAILED] true is never false either
   242  Expected
   243      <bool>: true
   244  to be false
   245  In [AfterEach] at: log_test.go:53 <time>
   246  < Exit [AfterEach] log - log_test.go:51 <time>
   247  `,
   248  				},
   249  				{
   250  					Name:   "[It] log redirects klog",
   251  					Status: "failed",
   252  					Failure: &reporters.JUnitFailure{
   253  						Type: "failed",
   254  						Description: `[FAILED] true is never false either
   255  Expected
   256      <bool>: true
   257  to be false
   258  In [AfterEach] at: log_test.go:53 <time>
   259  `,
   260  					},
   261  					SystemErr: `> Enter [BeforeEach] log - log_test.go:48 <time>
   262  <klog> log_test.go:49] before
   263  < Exit [BeforeEach] log - log_test.go:48 <time>
   264  > Enter [It] redirects klog - log_test.go:73 <time>
   265  <klog> log_test.go:74] hello world
   266  <klog> log_test.go:75] <nil>not really an error
   267  < Exit [It] redirects klog - log_test.go:73 <time>
   268  > Enter [AfterEach] log - log_test.go:51 <time>
   269  <klog> log_test.go:52] after
   270  [FAILED] true is never false either
   271  Expected
   272      <bool>: true
   273  to be false
   274  In [AfterEach] at: log_test.go:53 <time>
   275  < Exit [AfterEach] log - log_test.go:51 <time>
   276  `,
   277  				},
   278  			},
   279  		},
   280  	}
   281  
   282  	// Simulate the test setup as in a normal e2e test which uses the
   283  	// framework, but remember to restore klog settings when we are done.
   284  	state := klog.CaptureState()
   285  	defer state.Restore()
   286  	var testContext framework.TestContextType
   287  	framework.AfterReadingAllFlags(&testContext)
   288  
   289  	oldStderr := os.Stderr
   290  	tmp := t.TempDir()
   291  	filename := path.Join(tmp, "stderr.log")
   292  	f, err := os.Create(filename)
   293  	require.NoError(t, err, "create temporary file")
   294  	os.Stderr = f
   295  	defer func() {
   296  		os.Stderr = oldStderr
   297  
   298  		err := f.Close()
   299  		require.NoError(t, err, "close temporary file")
   300  		actual, err := os.ReadFile(filename)
   301  		require.NoError(t, err, "read temporary file")
   302  		assert.Empty(t, string(actual), "no output on stderr")
   303  	}()
   304  
   305  	output.TestGinkgoOutput(t, expected)
   306  }