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

Code before:
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 options

import (
	"fmt"
	"strings"
	"testing"
	"time"

	utilerrors "k8s.io/apimachinery/pkg/util/errors"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/apimachinery/pkg/util/version"
	utilfeature "k8s.io/apiserver/pkg/util/feature"
	"k8s.io/component-base/featuregate"
	utilversion "k8s.io/component-base/version"
	netutils "k8s.io/utils/net"
)

func TestServerRunOptionsValidate(t *testing.T) {
	testRegistry := featuregate.NewComponentGlobalsRegistry()
	featureGate := utilfeature.DefaultFeatureGate.DeepCopy()
	effectiveVersion := utilversion.NewEffectiveVersion("1.32")
	effectiveVersion.SetEmulationVersion(version.MajorMinor(1, 33))
	testComponent := "test"
	utilruntime.Must(testRegistry.Register(testComponent, effectiveVersion, featureGate))

	testCases := []struct {
		name        string
		testOptions *ServerRunOptions
		expectErr   string
	}{
		{
			name: "Test when MaxRequestsInFlight is negative value",
			testOptions: &ServerRunOptions{
				AdvertiseAddress:            netutils.ParseIPSloppy("192.168.10.10"),
				CorsAllowedOriginList:       []string{"10.10.10.100", "10.10.10.200"},
				MaxRequestsInFlight:         -400,
				MaxMutatingRequestsInFlight: 200,
				RequestTimeout:              time.Duration(2) * time.Minute,
				MinRequestTimeout:           1800,
				JSONPatchMaxCopyBytes:       10 * 1024 * 1024,
				MaxRequestBodyBytes:         10 * 1024 * 1024,
				ComponentGlobalsRegistry:    featuregate.DefaultComponentGlobalsRegistry,
			},
			expectErr: "--max-requests-inflight can not be negative value",
		},
		{

Review comment (suggestion):
Like Ben suggested, we can subtract the real maximum and clamp it based on the lowest version to every support this (1.31), so max(binaryVersion.SubtractMinor(MAX_SKEW), v1.31.0), and set the test case to test for binary version 1.34, so that we don't need to change it going forward.

Code after:
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 options

import (
	"fmt"
	"strings"
	"testing"
	"time"

	utilerrors "k8s.io/apimachinery/pkg/util/errors"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/apimachinery/pkg/util/version"
	utilfeature "k8s.io/apiserver/pkg/util/feature"
	"k8s.io/component-base/featuregate"
	utilversion "k8s.io/component-base/version"
	netutils "k8s.io/utils/net"
)

func TestServerRunOptionsValidate(t *testing.T) {
	testRegistry := featuregate.NewComponentGlobalsRegistry()
	featureGate := utilfeature.DefaultFeatureGate.DeepCopy()
	effectiveVersion := utilversion.NewEffectiveVersion("1.35")
	effectiveVersion.SetEmulationVersion(version.MajorMinor(1, 31))
	testComponent := "test"
	utilruntime.Must(testRegistry.Register(testComponent, effectiveVersion, featureGate))

	testCases := []struct {
		name        string
		testOptions *ServerRunOptions
		expectErr   string
	}{
		{
			name: "Test when MaxRequestsInFlight is negative value",
			testOptions: &ServerRunOptions{
				AdvertiseAddress:            netutils.ParseIPSloppy("192.168.10.10"),
				CorsAllowedOriginList:       []string{"10.10.10.100", "10.10.10.200"},
				MaxRequestsInFlight:         -400,
				MaxMutatingRequestsInFlight: 200,
				RequestTimeout:              time.Duration(2) * time.Minute,
				MinRequestTimeout:           1800,
				JSONPatchMaxCopyBytes:       10 * 1024 * 1024,
				MaxRequestBodyBytes:         10 * 1024 * 1024,
				ComponentGlobalsRegistry:    featuregate.DefaultComponentGlobalsRegistry,
			},
			expectErr: "--max-requests-inflight can not be negative value",
		},
		{