Repository: kubernetes/kubernetes
File: staging/src/k8s.io/apiserver/pkg/server/options/audit_test.go (Go)

Code before:
			if tc.expected == "" {
				assert.Nil(t, config.AuditBackend)
			} else {
				assert.Equal(t, tc.expected, fmt.Sprintf("%s", config.AuditBackend))
			}

			w, err := options.LogOptions.getWriter()
			require.NoError(t, err, "Writer creation should not fail.")

			// Don't check writer if logging is disabled.
			if w == nil {
				return
			}

			if options.LogOptions.Path == "-" {
				assert.Equal(t, os.Stdout, w)
				assert.NoFileExists(t, options.LogOptions.Path)
			} else {
				logger, ok := w.(*lumberjack.Logger)
				assert.True(t, ok, "Writer should be of type *lumberjack.Logger")
				assert.FileExists(t, options.LogOptions.Path)

				// Special check for MaxSize behavior
				if options.LogOptions.MaxSize == 0 {
					// Expecting MaxInt32 when MaxSize is 0 to avoid lumberjack default (100MB)
					assert.Equal(t, int(math.MaxInt32), logger.MaxSize)
				} else {
					assert.Equal(t, options.LogOptions.MaxSize, logger.MaxSize)
				}
			}
		})
	}
}

func TestAuditLogMaxSizeZero(t *testing.T) {
	tmpDir := t.TempDir()
	auditPath := filepath.Join(tmpDir, "audit")
	policy := makeTmpPolicy(t)
	defer os.Remove(policy)

	o := NewAuditOptions()
	o.LogOptions.Path = auditPath
	o.PolicyFile = policy
	o.LogOptions.MaxSize = 0

	w, err := o.LogOptions.getWriter()
	require.NoError(t, err)
	require.NotNil(t, w)

	// When MaxSize is 0, we return a raw file writer (os.File) for unlimited size
	file, ok := w.(*os.File)

Review comment (refactor):
remove this `if` block and the MaxInt32 bit, this is no longer how this is implemented, just keep the `assert.Equal(t, options.LogOptions.MaxSize, logger.MaxSize)` line

Code after:
				assert.Nil(t, config.AuditBackend)
			} else {
				assert.Equal(t, tc.expected, fmt.Sprintf("%s", config.AuditBackend))
			}

			w, err := options.LogOptions.getWriter()
			require.NoError(t, err, "Writer creation should not fail.")

			// Don't check writer if logging is disabled.
			if w == nil {
				return
			}

			if options.LogOptions.Path == "-" {
				assert.Equal(t, os.Stdout, w)
				assert.NoFileExists(t, options.LogOptions.Path)
			} else if options.LogOptions.MaxSize == 0 {
				// When MaxSize is 0, we return a raw file writer (os.File) for unlimited size
				file, ok := w.(*os.File)
				assert.True(t, ok, "Writer should be of type *os.File when MaxSize is 0")
				assert.FileExists(t, options.LogOptions.Path)
				assert.NoError(t, file.Close())
			} else {
				logger, ok := w.(*lumberjack.Logger)
				assert.True(t, ok, "Writer should be of type *lumberjack.Logger")
				assert.FileExists(t, options.LogOptions.Path)
				assert.Equal(t, options.LogOptions.MaxSize, logger.MaxSize)
			}
		})
	}
}

func TestAuditLogMaxSizeZero(t *testing.T) {
	tmpDir := t.TempDir()
	auditPath := filepath.Join(tmpDir, "audit")
	policy := makeTmpPolicy(t)
	defer func() { _ = os.Remove(policy) }()

	o := NewAuditOptions()
	o.LogOptions.Path = auditPath
	o.PolicyFile = policy
	o.LogOptions.MaxSize = 0

	w, err := o.LogOptions.getWriter()
	require.NoError(t, err)
	require.NotNil(t, w)

	// When MaxSize is 0, we return a raw file writer (os.File) for unlimited size
	file, ok := w.(*os.File)
	require.True(t, ok, "Should be an os.File when MaxSize is 0")