| 1 |
/* |
| 2 |
Copyright 2015 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 storage |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
|
| 22 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 23 |
) |
| 24 |
|
| 25 |
func TestDisabledVersion(t *testing.T) { |
| 26 |
g1v1 := schema.GroupVersion{Group: "group1", Version: "version1"} |
| 27 |
g1v2 := schema.GroupVersion{Group: "group1", Version: "version2"} |
| 28 |
g2v1 := schema.GroupVersion{Group: "group2", Version: "version1"} |
| 29 |
|
| 30 |
config := NewResourceConfig() |
| 31 |
|
| 32 |
config.DisableVersions(g1v1) |
| 33 |
config.EnableVersions(g1v2, g2v1) |
| 34 |
|
| 35 |
if config.versionEnabled(g1v1) { |
| 36 |
t.Errorf("expected disabled for %v, from %v", g1v1, config) |
| 37 |
} |
| 38 |
if !config.versionEnabled(g1v2) { |
| 39 |
t.Errorf("expected enabled for %v, from %v", g1v1, config) |
| 40 |
} |
| 41 |
if !config.versionEnabled(g2v1) { |
| 42 |
t.Errorf("expected enabled for %v, from %v", g1v1, config) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func TestDisabledResource(t *testing.T) { |
| 47 |
g1v1 := schema.GroupVersion{Group: "group1", Version: "version1"} |
| 48 |
g1v1rUnspecified := g1v1.WithResource("unspecified") |
| 49 |
g1v1rEnabled := g1v1.WithResource("enabled") |
| 50 |
g1v1rDisabled := g1v1.WithResource("disabled") |
| 51 |
g1v2 := schema.GroupVersion{Group: "group1", Version: "version2"} |
| 52 |
g1v2rUnspecified := g1v2.WithResource("unspecified") |
| 53 |
g1v2rEnabled := g1v2.WithResource("enabled") |
| 54 |
g1v2rDisabled := g1v2.WithResource("disabled") |
| 55 |
g2v1 := schema.GroupVersion{Group: "group2", Version: "version1"} |
| 56 |
g2v1rUnspecified := g2v1.WithResource("unspecified") |
| 57 |
g2v1rEnabled := g2v1.WithResource("enabled") |
| 58 |
g2v1rDisabled := g2v1.WithResource("disabled") |
| 59 |
|
| 60 |
config := NewResourceConfig() |
| 61 |
|
| 62 |
config.DisableVersions(g1v1) |
| 63 |
config.EnableVersions(g1v2, g2v1) |
| 64 |
|
| 65 |
config.EnableResources(g1v1rEnabled, g1v2rEnabled, g2v1rEnabled) |
| 66 |
config.DisableResources(g1v1rDisabled, g1v2rDisabled, g2v1rDisabled) |
| 67 |
|
| 68 |
// all resources not explicitly enabled under g1v1 are disabled because the group-version is disabled |
| 69 |
if config.ResourceEnabled(g1v1rUnspecified) { |
| 70 |
t.Errorf("expected disabled for %v, from %v", g1v1rUnspecified, config) |
| 71 |
} |
| 72 |
if !config.ResourceEnabled(g1v1rEnabled) { |
| 73 |
t.Errorf("expected enabled for %v, from %v", g1v1rEnabled, config) |
| 74 |
} |
| 75 |
if config.ResourceEnabled(g1v1rDisabled) { |
| 76 |
t.Errorf("expected disabled for %v, from %v", g1v1rDisabled, config) |
| 77 |
} |
| 78 |
if config.ResourceEnabled(g1v1rUnspecified) { |
| 79 |
t.Errorf("expected disabled for %v, from %v", g1v1rUnspecified, config) |
| 80 |
} |
| 81 |
|
| 82 |
// explicitly disabled resources in enabled group-versions are disabled |
| 83 |
if config.ResourceEnabled(g1v2rDisabled) { |
| 84 |
t.Errorf("expected disabled for %v, from %v", g1v2rDisabled, config) |
| 85 |
} |
| 86 |
if config.ResourceEnabled(g2v1rDisabled) { |
| 87 |
t.Errorf("expected disabled for %v, from %v", g2v1rDisabled, config) |
| 88 |
} |
| 89 |
|
| 90 |
// unspecified and explicitly enabled resources in enabled group-versions are enabled |
| 91 |
if !config.ResourceEnabled(g1v2rUnspecified) { |
| 92 |
t.Errorf("expected enabled for %v, from %v", g1v2rUnspecified, config) |
| 93 |
} |
| 94 |
if !config.ResourceEnabled(g1v2rEnabled) { |
| 95 |
t.Errorf("expected enabled for %v, from %v", g1v2rEnabled, config) |
| 96 |
} |
| 97 |
if !config.ResourceEnabled(g2v1rUnspecified) { |
| 98 |
t.Errorf("expected enabled for %v, from %v", g2v1rUnspecified, config) |
| 99 |
} |
| 100 |
if !config.ResourceEnabled(g2v1rEnabled) { |
| 101 |
t.Errorf("expected enabled for %v, from %v", g2v1rEnabled, config) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestAnyVersionForGroupEnabled(t *testing.T) { |
| 106 |
tests := []struct { |
| 107 |
name string |
| 108 |
creator func() APIResourceConfigSource |
| 109 |
testGroup string |
| 110 |
|
| 111 |
expectedResult bool |
| 112 |
}{ |
| 113 |
{ |
| 114 |
name: "empty", |
| 115 |
creator: func() APIResourceConfigSource { |
| 116 |
return NewResourceConfig() |
| 117 |
}, |
| 118 |
testGroup: "one", |
| 119 |
|
| 120 |
expectedResult: false, |
| 121 |
}, |
| 122 |
{ |
| 123 |
name: "present, but disabled", |
| 124 |
creator: func() APIResourceConfigSource { |
| 125 |
ret := NewResourceConfig() |
| 126 |
ret.DisableVersions(schema.GroupVersion{Group: "one", Version: "version1"}) |
| 127 |
return ret |
| 128 |
}, |
| 129 |
testGroup: "one", |
| 130 |
|
| 131 |
expectedResult: false, |
| 132 |
}, |
| 133 |
{ |
| 134 |
name: "present, and one version enabled", |
| 135 |
creator: func() APIResourceConfigSource { |
| 136 |
ret := NewResourceConfig() |
| 137 |
ret.DisableVersions(schema.GroupVersion{Group: "one", Version: "version1"}) |
| 138 |
ret.EnableVersions(schema.GroupVersion{Group: "one", Version: "version2"}) |
| 139 |
return ret |
| 140 |
}, |
| 141 |
testGroup: "one", |
| 142 |
|
| 143 |
expectedResult: true, |
| 144 |
}, |
| 145 |
{ |
| 146 |
name: "present, and one resource enabled", |
| 147 |
creator: func() APIResourceConfigSource { |
| 148 |
ret := NewResourceConfig() |
| 149 |
ret.DisableVersions(schema.GroupVersion{Group: "one", Version: "version1"}) |
| 150 |
ret.EnableResources(schema.GroupVersionResource{Group: "one", Version: "version2", Resource: "foo"}) |
| 151 |
return ret |
| 152 |
}, |
| 153 |
testGroup: "one", |
| 154 |
|
| 155 |
expectedResult: true, |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "present, and one resource under disabled version enabled", |
| 159 |
creator: func() APIResourceConfigSource { |
| 160 |
ret := NewResourceConfig() |
| 161 |
ret.DisableVersions(schema.GroupVersion{Group: "one", Version: "version1"}) |
| 162 |
ret.EnableResources(schema.GroupVersionResource{Group: "one", Version: "version1", Resource: "foo"}) |
| 163 |
return ret |
| 164 |
}, |
| 165 |
testGroup: "one", |
| 166 |
|
| 167 |
expectedResult: true, |
| 168 |
}, |
| 169 |
} |
| 170 |
|
| 171 |
for _, tc := range tests { |
| 172 |
t.Run(tc.name, func(t *testing.T) { |
| 173 |
if e, a := tc.expectedResult, tc.creator().AnyResourceForGroupEnabled(tc.testGroup); e != a { |
| 174 |
t.Errorf("expected %v, got %v", e, a) |
| 175 |
} |
| 176 |
}) |
| 177 |
} |
| 178 |
} |