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

Code before:
			config := &server.Config{}
			require.NoError(t, options.ApplyTo(config))
			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)


Review comment (bug):
this assertion will now fail when MaxSize is 0, because we return an os.File now

need another if block similar to the `if options.LogOptions.Path == "-" {` one that asserts we get a raw file writer back when MaxSize == 0

Code after:
			require.NoError(t, options.ApplyTo(config))
			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 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)