|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "k8s.io/kubernetes/pkg/scheduler/apis/config" |
| 25 | + "k8s.io/kubernetes/pkg/scheduler/apis/config/scheme" |
| 26 | +) |
| 27 | + |
| 28 | +func TestDecodeInto(t *testing.T) { |
| 29 | + type PluginFooConfig struct { |
| 30 | + FooTest string `json:"foo_test,omitempty"` |
| 31 | + } |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + schedulerConfig string |
| 35 | + expeted PluginFooConfig |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "test decode for JSON config", |
| 39 | + schedulerConfig: `{ |
| 40 | + "kind": "KubeSchedulerConfiguration", |
| 41 | + "apiVersion": "kubescheduler.config.k8s.io/v1alpha1", |
| 42 | + "plugins": { |
| 43 | + "permit": { |
| 44 | + "enabled": [ |
| 45 | + { |
| 46 | + "name": "foo" |
| 47 | + } |
| 48 | + ] |
| 49 | + } |
| 50 | + }, |
| 51 | + "pluginConfig": [ |
| 52 | + { |
| 53 | + "name": "foo", |
| 54 | + "args": { |
| 55 | + "foo_test": "test decode" |
| 56 | + } |
| 57 | + } |
| 58 | + ] |
| 59 | + }`, |
| 60 | + expeted: PluginFooConfig{ |
| 61 | + FooTest: "test decode", |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "test decode for YAML config", |
| 66 | + schedulerConfig: ` |
| 67 | +apiVersion: kubescheduler.config.k8s.io/v1alpha1 |
| 68 | +kind: KubeSchedulerConfiguration |
| 69 | +plugins: |
| 70 | + permit: |
| 71 | + enabled: |
| 72 | + - name: foo |
| 73 | +pluginConfig: |
| 74 | + - name: foo |
| 75 | + args: |
| 76 | + foo_test: "test decode"`, |
| 77 | + expeted: PluginFooConfig{ |
| 78 | + FooTest: "test decode", |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + for i, test := range tests { |
| 83 | + schedulerConf, err := loadConfig([]byte(test.schedulerConfig)) |
| 84 | + if err != nil { |
| 85 | + t.Errorf("Test #%v(%s): failed to load scheduler config: %v", i, test.name, err) |
| 86 | + } |
| 87 | + var pluginFooConf PluginFooConfig |
| 88 | + if err := DecodeInto(&schedulerConf.PluginConfig[0].Args, &pluginFooConf); err != nil { |
| 89 | + t.Errorf("Test #%v(%s): failed to decode args %+v: %v", |
| 90 | + i, test.name, schedulerConf.PluginConfig[0].Args, err) |
| 91 | + } |
| 92 | + if !reflect.DeepEqual(pluginFooConf, test.expeted) { |
| 93 | + t.Errorf("Test #%v(%s): failed to decode plugin config, expected: %+v, got: %+v", |
| 94 | + i, test.name, test.expeted, pluginFooConf) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func loadConfig(data []byte) (*config.KubeSchedulerConfiguration, error) { |
| 100 | + configObj := &config.KubeSchedulerConfiguration{} |
| 101 | + if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), data, configObj); err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + return configObj, nil |
| 106 | +} |
0 commit comments