Merge pull request #60881 from hanxiaoshuai/addut0307

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

add unit test for function FeatureGateSetFromMap and FeatureGateString

**What this PR does / why we need it**:
add unit test for function FeatureGateSetFromMap and FeatureGateString
**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```

Kubernetes-commit: cba7843cb403e1d7864a59949e0ec6c8927ea403
This commit is contained in:
Kubernetes Publisher 2018-03-21 21:17:51 -07:00
commit 5d130da6bf
2 changed files with 1957 additions and 1764 deletions

3592
Godeps/Godeps.json generated

File diff suppressed because it is too large Load Diff

View File

@ -189,3 +189,132 @@ func TestFeatureGateFlagDefaults(t *testing.T) {
t.Errorf("Expected true")
}
}
func TestFeatureGateSetFromMap(t *testing.T) {
// gates for testing
const testAlphaGate Feature = "TestAlpha"
const testBetaGate Feature = "TestBeta"
tests := []struct {
name string
setmap map[string]bool
expect map[Feature]bool
setmapError string
}{
{
name: "set TestAlpha and TestBeta true",
setmap: map[string]bool{
"TestAlpha": true,
"TestBeta": true,
},
expect: map[Feature]bool{
testAlphaGate: true,
testBetaGate: true,
},
},
{
name: "set TestBeta true",
setmap: map[string]bool{
"TestBeta": true,
},
expect: map[Feature]bool{
testAlphaGate: false,
testBetaGate: true,
},
},
{
name: "set TestAlpha false",
setmap: map[string]bool{
"TestAlpha": false,
},
expect: map[Feature]bool{
testAlphaGate: false,
testBetaGate: false,
},
},
{
name: "set TestInvaild true",
setmap: map[string]bool{
"TestInvaild": true,
},
expect: map[Feature]bool{
testAlphaGate: false,
testBetaGate: false,
},
setmapError: "unrecognized key:",
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("SetFromMap %s", test.name), func(t *testing.T) {
f := NewFeatureGate()
f.Add(map[Feature]FeatureSpec{
testAlphaGate: {Default: false, PreRelease: Alpha},
testBetaGate: {Default: false, PreRelease: Beta},
})
err := f.SetFromMap(test.setmap)
if test.setmapError != "" {
if !strings.Contains(err.Error(), test.setmapError) {
t.Errorf("%d: SetFromMap(%#v) Expected err:%v, Got err:%v", i, test.setmap, test.setmapError, err)
}
} else if err != nil {
t.Errorf("%d: SetFromMap(%#v) Expected success, Got err:%v", i, test.setmap, err)
}
for k, v := range test.expect {
if actual := f.Enabled(k); actual != v {
t.Errorf("%d: SetFromMap(%#v) Expected %s=%v, Got %s=%v", i, test.setmap, k, v, k, actual)
}
}
})
}
}
func TestFeatureGateString(t *testing.T) {
// gates for testing
const testAlphaGate Feature = "TestAlpha"
const testBetaGate Feature = "TestBeta"
const testGAGate Feature = "TestGA"
featuremap := map[Feature]FeatureSpec{
testGAGate: {Default: true, PreRelease: GA},
testAlphaGate: {Default: false, PreRelease: Alpha},
testBetaGate: {Default: true, PreRelease: Beta},
}
tests := []struct {
setmap map[string]bool
expect string
}{
{
setmap: map[string]bool{
"TestAlpha": false,
},
expect: "TestAlpha=false",
},
{
setmap: map[string]bool{
"TestAlpha": false,
"TestBeta": true,
},
expect: "TestAlpha=false,TestBeta=true",
},
{
setmap: map[string]bool{
"TestGA": true,
"TestAlpha": false,
"TestBeta": true,
},
expect: "TestAlpha=false,TestBeta=true,TestGA=true",
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("SetFromMap %s", test.expect), func(t *testing.T) {
f := NewFeatureGate()
f.Add(featuremap)
f.SetFromMap(test.setmap)
result := f.String()
if result != test.expect {
t.Errorf("%d: SetFromMap(%#v) Expected %s, Got %s", i, test.setmap, test.expect, result)
}
})
}
}