-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In this commit, we introduce unit tests for the `Interpreter` webhook across `Decoder`, `HTTP`, `Injection`, `Response`, and `Webhook` components. Signed-off-by: Mohamed Awnallah <[email protected]>
- Loading branch information
1 parent
0846725
commit f01637c
Showing
5 changed files
with
958 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,234 @@ | ||
/* | ||
Copyright 2024 The Karmada 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 interpreter | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
schema "k8s.io/apimachinery/pkg/runtime/schema" | ||
|
||
configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1" | ||
) | ||
|
||
// MyTestPod represents a simplified version of a Kubernetes Pod for testing purposes. | ||
// It includes basic fields such as API version, kind, and metadata. | ||
type MyTestPod struct { | ||
APIVersion string `json:"apiVersion"` | ||
Kind string `json:"kind"` | ||
Metadata struct { | ||
Name string `json:"name"` | ||
} `json:"metadata"` | ||
} | ||
|
||
// DeepCopyObject creates a deep copy of the MyTestPod instance. | ||
// This method is part of the runtime.Object interface and ensures that modifications | ||
// to the copy do not affect the original object. | ||
func (p *MyTestPod) DeepCopyObject() runtime.Object { | ||
return &MyTestPod{ | ||
APIVersion: p.APIVersion, | ||
Kind: p.Kind, | ||
Metadata: p.Metadata, | ||
} | ||
} | ||
|
||
// GetObjectKind returns the schema.ObjectKind for the MyTestPod instance. | ||
// This method is part of the runtime.Object interface and provides the API version | ||
// and kind of the object, which is used for object identification in Kubernetes. | ||
func (p *MyTestPod) GetObjectKind() schema.ObjectKind { | ||
return &metav1.TypeMeta{ | ||
APIVersion: p.APIVersion, | ||
Kind: p.Kind, | ||
} | ||
} | ||
|
||
func TestNewDecoder(t *testing.T) { | ||
// Create a dummy runtime.Scheme object. | ||
scheme := runtime.NewScheme() | ||
decoder := NewDecoder(scheme) | ||
|
||
// Verify that the Decoder object is created correctly. | ||
if decoder == nil { | ||
t.Errorf("expected decoder to not be nil") | ||
} | ||
} | ||
|
||
func TestDecodeRaw_ValidRaw_Success(t *testing.T) { | ||
// Define values to be used during the test. | ||
const apiVersion = "v1" | ||
const kind = "Pod" | ||
const name = "test-pod" | ||
|
||
// Prepare the necessary inputs. | ||
rawJSON := fmt.Sprintf(`{"apiVersion": "%s", "kind": "%s", "metadata": {"name": "%s"}}`, apiVersion, kind, name) | ||
rawObj := runtime.RawExtension{ | ||
Raw: []byte(rawJSON), | ||
} | ||
into := &unstructured.Unstructured{} | ||
|
||
// Create a dummy runtime.Scheme object. | ||
scheme := runtime.NewScheme() | ||
decoder := NewDecoder(scheme) | ||
|
||
// Call DecodeRaw and expect no errors. | ||
err := decoder.DecodeRaw(rawObj, into) | ||
if err != nil { | ||
t.Errorf("DecodeRaw returned an error: %v", err) | ||
} | ||
|
||
// Verify the contents of the decoded object. | ||
if got := into.GetAPIVersion(); got != apiVersion { | ||
t.Errorf("expected API version '%s', got '%s'", apiVersion, got) | ||
} | ||
if got := into.GetKind(); got != kind { | ||
t.Errorf("expected kind '%s', got '%s'", kind, got) | ||
} | ||
if got := into.GetName(); got != name { | ||
t.Errorf("expected name '%s', got '%s'", name, got) | ||
} | ||
} | ||
|
||
func TestDecodeRaw_EmptyRaw_Error(t *testing.T) { | ||
// Test case for error when RawExtension has no content. | ||
rawObj := runtime.RawExtension{ | ||
Raw: []byte{}, | ||
} | ||
into := &unstructured.Unstructured{} | ||
|
||
// Create a dummy runtime.Scheme object. | ||
scheme := runtime.NewScheme() | ||
decoder := NewDecoder(scheme) | ||
|
||
// Call DecodeRaw and expect an error. | ||
err := decoder.DecodeRaw(rawObj, into) | ||
if err == nil { | ||
t.Errorf("Expected error but got nil") | ||
} | ||
|
||
// Verify the error message. | ||
expectedErrorMessage := "there is no content to decode" | ||
if err.Error() != expectedErrorMessage { | ||
t.Errorf("Expected error message '%s', got '%s'", expectedErrorMessage, err.Error()) | ||
} | ||
} | ||
|
||
func TestDecodeRaw_IntoNonUnstructuredType_Success(t *testing.T) { | ||
// Define values to be used during the test. | ||
const apiVersion = "v1" | ||
const kind = "Pod" | ||
const name = "test-pod" | ||
|
||
// Prepare the necessary inputs. | ||
rawJSON := fmt.Sprintf(`{"apiVersion": "%s", "kind": "%s", "metadata": {"name": "%s"}}`, apiVersion, kind, name) | ||
rawObj := runtime.RawExtension{ | ||
Raw: []byte(rawJSON), | ||
} | ||
|
||
// Create an instance of the non-unstructured type. | ||
into := &MyTestPod{} | ||
|
||
// Create a dummy runtime.Scheme object. | ||
scheme := runtime.NewScheme() | ||
decoder := NewDecoder(scheme) | ||
|
||
// Call DecodeRaw and expect no errors. | ||
err := decoder.DecodeRaw(rawObj, into) | ||
if err != nil { | ||
t.Errorf("DecodeRaw returned an error: %v", err) | ||
} | ||
|
||
// Verify the contents of the decoded object. | ||
if got := into.APIVersion; got != apiVersion { | ||
t.Errorf("expected API version '%s', got '%s'", apiVersion, got) | ||
} | ||
if got := into.Kind; got != kind { | ||
t.Errorf("expected kind '%s', got '%s'", kind, got) | ||
} | ||
if got := into.Metadata.Name; got != name { | ||
t.Errorf("expected name '%s', got '%s'", name, got) | ||
} | ||
} | ||
|
||
func TestDecode_ValidRaw_Success(t *testing.T) { | ||
// Define values to be used during the test. | ||
const apiVersion = "v1" | ||
const kind = "Pod" | ||
const name = "test-pod" | ||
|
||
// Prepare the necessary inputs. | ||
rawJSON := fmt.Sprintf(`{"apiVersion": "%s", "kind": "%s", "metadata": {"name": "%s"}}`, apiVersion, kind, name) | ||
req := Request{ | ||
ResourceInterpreterRequest: configv1alpha1.ResourceInterpreterRequest{ | ||
Object: runtime.RawExtension{ | ||
Raw: []byte(rawJSON), | ||
}, | ||
}, | ||
} | ||
into := &unstructured.Unstructured{} | ||
|
||
// Create a dummy runtime.Scheme object. | ||
scheme := runtime.NewScheme() | ||
decoder := NewDecoder(scheme) | ||
|
||
// Call Decode and expect no errors. | ||
err := decoder.Decode(req, into) | ||
if err != nil { | ||
t.Errorf("DecodeRaw returned an error: %v", err) | ||
} | ||
|
||
// Verify the contents of the decoded object. | ||
if got := into.GetAPIVersion(); got != apiVersion { | ||
t.Errorf("expected API version '%s', got '%s'", apiVersion, got) | ||
} | ||
if got := into.GetKind(); got != kind { | ||
t.Errorf("expected kind '%s', got '%s'", kind, got) | ||
} | ||
if got := into.GetName(); got != name { | ||
t.Errorf("expected name '%s', got '%s'", name, got) | ||
} | ||
} | ||
|
||
func TestDecode_EmptyRaw_Error(t *testing.T) { | ||
// Test case for error when Request object has no content. | ||
req := Request{ | ||
ResourceInterpreterRequest: configv1alpha1.ResourceInterpreterRequest{ | ||
Object: runtime.RawExtension{ | ||
Raw: []byte{}, | ||
}, | ||
}, | ||
} | ||
into := &unstructured.Unstructured{} | ||
|
||
// Create a dummy runtime.Scheme object. | ||
scheme := runtime.NewScheme() | ||
decoder := NewDecoder(scheme) | ||
|
||
// Call Decode and expect an error. | ||
err := decoder.Decode(req, into) | ||
if err == nil { | ||
t.Errorf("Expected error but got nil") | ||
} | ||
|
||
// Verify the error message. | ||
expectedErrorMessage := "there is no content to decode" | ||
if err.Error() != expectedErrorMessage { | ||
t.Errorf("Expected error message '%s', got '%s'", expectedErrorMessage, err.Error()) | ||
} | ||
} |
Oops, something went wrong.