generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1116 from mohamedasifs123/packagek8s
adding tests for package k8s
- Loading branch information
Showing
4 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package k8s | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetEtcdVersion(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
version int | ||
expected string | ||
}{ | ||
{"Version 8", 8, "3.0.17"}, | ||
{"Version 9", 9, "3.1.12"}, | ||
{"Version 10", 10, "3.1.12"}, | ||
{"Version 11", 11, "3.2.18"}, | ||
{"Version 12", 12, "3.2.24"}, | ||
{"Version 13", 13, "3.2.24"}, | ||
{"Version 14", 14, "3.3.10"}, | ||
{"Version 15", 15, "3.3.10"}, | ||
{"Version 16", 16, "3.3.17-0"}, | ||
{"Version 17", 17, "3.4.3-0"}, | ||
{"Version 18", 18, "3.4.3-0"}, | ||
{"Version 19", 19, "3.4.13-0"}, | ||
{"Version 20", 20, "3.4.13-0"}, | ||
{"Version 21", 21, "3.4.13-0"}, | ||
{"Version 22", 22, "3.5.11-0"}, | ||
{"Version 23", 23, "3.5.11-0"}, | ||
{"Version 24", 24, "3.5.11-0"}, | ||
{"Version 25", 25, "3.5.11-0"}, | ||
{"Version 26", 26, "3.5.11-0"}, | ||
{"Version 27", 27, "3.5.11-0"}, | ||
{"Version 28", 28, "3.5.11-0"}, | ||
{"Version 29", 29, "3.5.11-0"}, | ||
{"Version too low", 7, "3.0.17"}, | ||
{"Version too high", 30, "3.5.11-0"}, | ||
{"Negative version", -1, "3.5.11-0"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := GetEtcdVersion(tt.version) | ||
if result != tt.expected { | ||
t.Errorf("expected %s, got %s", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package k8s | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetFeatureGates(t *testing.T) { | ||
// Mocking the rawData and lockEnabled variables for testing | ||
rawData = []FeatureSpec{ | ||
{Name: "feature1", Stage: GA, Since: 10, Until: 20}, | ||
{Name: "feature2", Stage: Beta, Since: 15, Until: 25}, | ||
{Name: "feature3", Stage: Deprecated, Since: 5, Until: 15}, | ||
} | ||
|
||
lockEnabled = map[string]bool{ | ||
"feature1": true, | ||
"feature2": false, | ||
"feature3": true, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
version int | ||
expected string | ||
}{{ | ||
name: "Version 1", | ||
version: 1, | ||
expected: "", | ||
}, | ||
{ | ||
name: "Version 5", | ||
version: 5, | ||
expected: "", | ||
}, { | ||
name: "Version 10", | ||
version: 10, | ||
expected: "", | ||
}, { | ||
name: "Version 20", | ||
version: 20, | ||
expected: "feature2=false", | ||
}, { | ||
name: "Version 25", | ||
version: 25, | ||
expected: "feature2=false", | ||
}, { | ||
name: "Version 30", | ||
version: 30, | ||
expected: "", | ||
}, { | ||
name: "Negative Version", | ||
version: -1, | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := GetFeatureGates(tt.version) | ||
if result != tt.expected { | ||
t.Errorf("expected %s, got %s", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package k8s | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestBuildKubeApiserverTracingConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
conf BuildKubeApiserverTracingConfigParam | ||
expected string | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "Valid endpoint", | ||
conf: BuildKubeApiserverTracingConfigParam{ | ||
Endpoint: "http://example.com/tracing", | ||
}, | ||
expected: `apiVersion: apiserver.config.k8s.io/v1alpha1 | ||
kind: TracingConfiguration | ||
endpoint: http://example.com/tracing | ||
samplingRatePerMillion: 1000000`, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "Empty endpoint", | ||
conf: BuildKubeApiserverTracingConfigParam{ | ||
Endpoint: "", | ||
}, | ||
expected: `apiVersion: apiserver.config.k8s.io/v1alpha1 | ||
kind: TracingConfiguration | ||
endpoint: | ||
samplingRatePerMillion: 1000000`, | ||
expectedError: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result, err := BuildKubeApiserverTracingConfig(tt.conf) | ||
|
||
if tt.expectedError && err == nil { | ||
t.Errorf("expected an error, but got nil") | ||
} else if !tt.expectedError && err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
|
||
if reflect.DeepEqual(result, tt.expected) { | ||
t.Errorf("unexpected output:\nexpected:\n%s\nactual:\n%s", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package k8s | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetRuntimeConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
version int | ||
expected string | ||
}{ | ||
{"Version less than 17", 16, ""}, | ||
{"Version equal to 17", 17, "api/legacy=false,api/alpha=false"}, | ||
{"Version greater than 17", 18, "api/legacy=false,api/alpha=false"}, | ||
{"Negative version", -1, ""}, | ||
{"Zero version", 0, ""}, | ||
{"High version number", 100, "api/legacy=false,api/alpha=false"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := GetRuntimeConfig(tt.version) | ||
if result != tt.expected { | ||
t.Errorf("expected %s, got %s", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} |