1/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package scheduling
18
19import v1 "k8s.io/api/core/v1"
20
21// FakeVolumeBinderConfig holds configurations for fake volume binder.
22type FakeVolumeBinderConfig struct {
23	AllBound    bool
24	FindReasons ConflictReasons
25	FindErr     error
26	AssumeErr   error
27	BindErr     error
28}
29
30// NewFakeVolumeBinder sets up all the caches needed for the scheduler to make
31// topology-aware volume binding decisions.
32func NewFakeVolumeBinder(config *FakeVolumeBinderConfig) *FakeVolumeBinder {
33	return &FakeVolumeBinder{
34		config: config,
35	}
36}
37
38// FakeVolumeBinder represents a fake volume binder for testing.
39type FakeVolumeBinder struct {
40	config       *FakeVolumeBinderConfig
41	AssumeCalled bool
42	BindCalled   bool
43}
44
45// GetPodVolumes implements SchedulerVolumeBinder.GetPodVolumes.
46func (b *FakeVolumeBinder) GetPodVolumes(pod *v1.Pod) (boundClaims, unboundClaimsDelayBinding, unboundClaimsImmediate []*v1.PersistentVolumeClaim, err error) {
47	return nil, nil, nil, nil
48}
49
50// FindPodVolumes implements SchedulerVolumeBinder.FindPodVolumes.
51func (b *FakeVolumeBinder) FindPodVolumes(pod *v1.Pod, _, _ []*v1.PersistentVolumeClaim, node *v1.Node) (podVolumes *PodVolumes, reasons ConflictReasons, err error) {
52	return nil, b.config.FindReasons, b.config.FindErr
53}
54
55// AssumePodVolumes implements SchedulerVolumeBinder.AssumePodVolumes.
56func (b *FakeVolumeBinder) AssumePodVolumes(assumedPod *v1.Pod, nodeName string, podVolumes *PodVolumes) (bool, error) {
57	b.AssumeCalled = true
58	return b.config.AllBound, b.config.AssumeErr
59}
60
61// RevertAssumedPodVolumes implements SchedulerVolumeBinder.RevertAssumedPodVolumes
62func (b *FakeVolumeBinder) RevertAssumedPodVolumes(_ *PodVolumes) {}
63
64// BindPodVolumes implements SchedulerVolumeBinder.BindPodVolumes.
65func (b *FakeVolumeBinder) BindPodVolumes(assumedPod *v1.Pod, podVolumes *PodVolumes) error {
66	b.BindCalled = true
67	return b.config.BindErr
68}
69