feat: enhanced and added testcases to increase unit test coverage

Signed-off-by: Animesh Pathak <kurosakiichigo.songoku@gmail.com>
This commit is contained in:
Animesh Pathak 2025-02-13 16:17:33 +05:30
parent 1bd44986dd
commit 6d3d111bf2
7 changed files with 601 additions and 74 deletions

View file

@ -1,63 +1,61 @@
package registry
import (
"errors"
"fmt"
"io"
"log"
"os"
"errors"
"fmt"
"io"
"log"
"os"
)
type LocalRegistry struct {
pathTemplate string
strict bool
debug bool
pathTemplate string
strict bool
debug bool
schemaPathFunc func(pathTemplate, resourceKind, resourceAPIVersion, k8sVersion string, strict bool) (string, error)
}
// NewLocalSchemas creates a new "registry", that will serve schemas from files, given a list of schema filenames
func newLocalRegistry(pathTemplate string, strict bool, debug bool) (*LocalRegistry, error) {
return &LocalRegistry{
pathTemplate,
strict,
debug,
}, nil
return &LocalRegistry{
pathTemplate: pathTemplate,
strict: strict,
debug: debug,
schemaPathFunc: schemaPath,
}, nil
}
// DownloadSchema retrieves the schema from a file for the resource
func (r LocalRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) (string, []byte, error) {
schemaFile, err := schemaPath(r.pathTemplate, resourceKind, resourceAPIVersion, k8sVersion, r.strict)
if err != nil {
return schemaFile, []byte{}, nil
}
f, err := os.Open(schemaFile)
if err != nil {
if os.IsNotExist(err) {
msg := fmt.Sprintf("could not open file %s", schemaFile)
if r.debug {
log.Print(msg)
}
return schemaFile, nil, newNotFoundError(errors.New(msg))
}
schemaFile, err := r.schemaPathFunc(r.pathTemplate, resourceKind, resourceAPIVersion, k8sVersion, r.strict)
if err != nil {
return schemaFile, []byte{}, nil
}
f, err := os.Open(schemaFile)
if err != nil {
if os.IsNotExist(err) {
msg := fmt.Sprintf("could not open file %s", schemaFile)
if r.debug {
log.Print(msg)
}
return schemaFile, nil, newNotFoundError(errors.New(msg))
}
msg := fmt.Sprintf("failed to open schema at %s: %s", schemaFile, err)
if r.debug {
log.Print(msg)
}
return schemaFile, nil, errors.New(msg)
}
defer f.Close()
content, err := io.ReadAll(f)
if err != nil {
msg := fmt.Sprintf("failed to read schema at %s: %s", schemaFile, err)
if r.debug {
log.Print(msg)
}
return schemaFile, nil, err
}
if r.debug {
log.Printf("using schema found at %s", schemaFile)
}
return schemaFile, content, nil
msg := fmt.Sprintf("failed to open schema at %s: %s", schemaFile, err)
if r.debug {
log.Print(msg)
}
return schemaFile, nil, errors.New(msg)
}
defer f.Close()
content, err := io.ReadAll(f)
if err != nil {
msg := fmt.Sprintf("failed to read schema at %s: %s", schemaFile, err)
if r.debug {
log.Print(msg)
}
return schemaFile, nil, err
}
if r.debug {
log.Printf("using schema found at %s", schemaFile)
}
return schemaFile, content, nil
}

109
pkg/registry/local_test.go Normal file
View file

@ -0,0 +1,109 @@
package registry
import (
"fmt"
"testing"
"os"
)
// Test generated using Keploy
func TestDownloadSchema_SchemaPathError(t *testing.T) {
// Arrange
pathTemplate := "./schemas/%s/%s/%s.json"
strict := false
debug := false
registry, err := newLocalRegistry(pathTemplate, strict, debug)
if err != nil {
t.Fatalf("Failed to create LocalRegistry: %v", err)
}
// Mock schemaPathFunc to return an error
registry.schemaPathFunc = func(pathTemplate, resourceKind, resourceAPIVersion, k8sVersion string, strict bool) (string, error) {
return "", fmt.Errorf("mock schemaPath error")
}
// Act
filePath, content, err := registry.DownloadSchema("Pod", "v1", "1.21")
// Assert
if err != nil {
t.Errorf("Expected nil error, got %v", err)
}
if filePath != "" {
t.Errorf("Expected empty filePath, got %s", filePath)
}
if len(content) != 0 {
t.Errorf("Expected empty content, got %v", content)
}
}
// Test generated using Keploy
func TestDownloadSchema_Success(t *testing.T) {
// Arrange
pathTemplate := "./schemas/%s/%s/%s.json"
strict := false
debug := true
registry, err := newLocalRegistry(pathTemplate, strict, debug)
if err != nil {
t.Fatalf("Failed to create LocalRegistry: %v", err)
}
// Mock schemaPathFunc to return a valid file path
registry.schemaPathFunc = func(pathTemplate, resourceKind, resourceAPIVersion, k8sVersion string, strict bool) (string, error) {
return "./valid_schema.json", nil
}
// Create a valid schema file
validFile := "./valid_schema.json"
expectedContent := []byte(`{"type": "object"}`)
os.WriteFile(validFile, expectedContent, 0644)
defer os.Remove(validFile)
// Act
filePath, content, err := registry.DownloadSchema("Pod", "v1", "1.21")
// Assert
if err != nil {
t.Errorf("Expected nil error, got %v", err)
}
if filePath != validFile {
t.Errorf("Expected filePath '%s', got %s", validFile, filePath)
}
if string(content) != string(expectedContent) {
t.Errorf("Expected content '%s', got %s", string(expectedContent), string(content))
}
}
// Test generated using Keploy
func TestDownloadSchema_FileNotFound(t *testing.T) {
// Arrange
pathTemplate := "./schemas/%s/%s/%s.json"
strict := false
debug := true
registry, err := newLocalRegistry(pathTemplate, strict, debug)
if err != nil {
t.Fatalf("Failed to create LocalRegistry: %v", err)
}
// Mock schemaPathFunc to return a valid file path
registry.schemaPathFunc = func(pathTemplate, resourceKind, resourceAPIVersion, k8sVersion string, strict bool) (string, error) {
return "./nonexistent_file.json", nil
}
// Act
filePath, content, err := registry.DownloadSchema("Pod", "v1", "1.21")
// Assert
if err == nil || err.Error() != "could not open file ./nonexistent_file.json" {
t.Errorf("Expected NotFoundError, got %v", err)
}
if filePath != "./nonexistent_file.json" {
t.Errorf("Expected filePath './nonexistent_file.json', got %s", filePath)
}
if content != nil {
t.Errorf("Expected nil content, got %v", content)
}
}