Repository: kubernetes/kubernetes
File: pkg/scheduler/framework/plugins/dynamicresources/dynamicresources_test.go (Go)

Code before:
		"single-request-only-subrequest-allocated": {
			claims: []*resourceapi.ResourceClaim{
				st.MakeResourceClaim().
					NamedRequestWithPrioritizedList("req-1",
						st.SubRequest("subreq-1", className, 1),
					).
					Obj(),
			},
			allocations: nodeAllocation{
				allocationResults: []resourceapi.AllocationResult{
					{
						Devices: resourceapi.DeviceAllocationResult{
							Results: []resourceapi.DeviceRequestAllocationResult{
								{
									Request: "req-1/subreq-1",
								},
							},
						},
					},
				},
			},
			expectedScore: 8,
		},
		"single-request-last-subrequest-allocated": {
			claims: []*resourceapi.ResourceClaim{
				st.MakeResourceClaim().
					NamedRequestWithPrioritizedList("req-1",
						st.SubRequest("subreq-1", className, 1),
						st.SubRequest("subreq-2", className, 1),
						st.SubRequest("subreq-3", className, 1),
						st.SubRequest("subreq-4", className, 1),
						st.SubRequest("subreq-5", className, 1),
						st.SubRequest("subreq-6", className, 1),
						st.SubRequest("subreq-7", className, 1),
						st.SubRequest("subreq-8", className, 1),
					).
					Obj(),
			},
			allocations: nodeAllocation{
				allocationResults: []resourceapi.AllocationResult{
					{
						Devices: resourceapi.DeviceAllocationResult{
							Results: []resourceapi.DeviceRequestAllocationResult{
								{
									Request: "req-1/subreq-8",
								},
							},
						},
					},
				},
			},

Review comment (suggestion):
The "stable" implementation is now only used if users disable beta on-by-default features. This is expected.

👍 

Code after:

func (m *mockDeviceClassResolver) GetDeviceClass(resourceName v1.ResourceName) *resourceapi.DeviceClass {
	return m.mapping[resourceName]
}

// TestAllocatorSelection covers the selection of a structured allocation implementation
// based on actual Kubernetes feature gates. This test lives here instead of
// k8s.io/dynamic-resource-allocation/structured because that code has no access
// to feature gate definitions.
func TestAllocatorSelection(t *testing.T) {
	for name, tc := range map[string]struct {
		features             string
		expectImplementation string
	}{
		// The most conservative implementation: only used when explicitly asking
		// for the most stable Kubernetes (no alpha or beta features).
		"only-GA": {
			features:             "AllAlpha=false,AllBeta=false",
			expectImplementation: "stable",
		},

		// By default, some beta features are on and the incubating implementation
		// is used.
		"default": {
			features:             "",
			expectImplementation: "incubating",
		},

		// Alpha features need the experimental implementation.
		"alpha": {
			features:             "AllAlpha=true,AllBeta=true",
			expectImplementation: "incubating",
		},
	} {
		t.Run(name, func(t *testing.T) {
			tCtx := ktesting.Init(t)
			featureGate := utilfeature.DefaultFeatureGate.DeepCopy()
			tCtx.ExpectNoError(featureGate.Set(tc.features), "set features")
			fts := feature.NewSchedulerFeaturesFromGates(featureGate)
			features := AllocatorFeatures(fts)

			// Slightly hacky: most arguments are not valid and the constructor
			// is expected to not use them yet.
			allocator, err := structured.NewAllocator(tCtx, features, structured.AllocatedState{}, nil, nil, nil)
			tCtx.ExpectNoError(err, "create allocator")
			allocatorType := fmt.Sprintf("%T", allocator)
			if !strings.Contains(allocatorType, tc.expectImplementation) {
				tCtx.Fatalf("Expected allocator implementation %q, got %s", tc.expectImplementation, allocatorType)
			}
		})
	}