Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cmd flags #10

Merged
merged 8 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ HELM_PLUGIN_DIR = $(HELM_PLUGINS)/$(PLUGIN_SHORTNAME)
build: ## Build the plugin
@echo "Building plugin..."
@${GO_BUILD_ENV_VARS} go build -o $(BINNAME) ${GO_BUILD_ARGS}
@ls -la

install: build ## Install the plugin
@echo "Installing plugin..."
@mkdir -p $(HELM_PLUGIN_DIR)
@cp $(BINNAME)* $(HELM_PLUGIN_DIR)
@cp $(BINNAME) $(HELM_PLUGIN_DIR)
@cp plugin.yaml $(HELM_PLUGIN_DIR)

verify: ## Verify the plugin
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![ci](https://github.com/losisin/helm-values-schema-json/actions/workflows/ci.yaml/badge.svg)](https://github.com/losisin/helm-values-schema-json/actions/workflows/ci.yaml)
[![codecov](https://codecov.io/gh/losisin/helm-values-schema-json/graph/badge.svg?token=0QQVCFJH84)](https://codecov.io/gh/losisin/helm-values-schema-json)
[![Go Report Card](https://goreportcard.com/badge/github.com/losisin/helm-values-schema-json)](https://goreportcard.com/report/github.com/losisin/helm-values-schema-json)
[![Static Badge](https://img.shields.io/badge/licence%20-%20MIT-green)](https://github.com/losisin/helm-values-schema-json/blob/add-Makefile/LICENSE)
[![GitHub release (with filter)](https://img.shields.io/github/v/release/losisin/helm-values-schema-json)](https://github.com/losisin/helm-values-schema-json/releases)

Expand Down
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
comment:
require_changes: true
34 changes: 17 additions & 17 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"encoding/json"
"errors"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -54,29 +55,30 @@
}

func printMap(data *jsonschema.Document, outputPath string) error {
if data == nil {
return errors.New("data is nil")
}
// Use YAML marshaling to format the map
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}

// If outputPath is provided, create or overwrite the specified file
if outputPath != "" {
// Create or overwrite the file
file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()
// Create or overwrite the file
file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()

// Write the new data to the output file
_, err = file.Write(jsonData)
if err != nil {
return err
}
fmt.Printf("Merged data saved to %s\n", outputPath)
// Write the new data to the output file
_, err = file.Write(jsonData)
if err != nil {
return err

Check warning on line 77 in schema.go

View check run for this annotation

Codecov / codecov/patch

schema.go#L77

Added line #L77 was not covered by tests
}

fmt.Printf("Merged data saved to %s\n", outputPath)

return nil
}

Expand All @@ -102,7 +104,7 @@

// Check if the input flag is set
if len(yamlFiles) == 0 {
fmt.Println("Input flag is required. Please provide input yaml files using the -i flag.")
fmt.Println("Input flag is required. Please provide input yaml files using the -input flag.")

Check warning on line 107 in schema.go

View check run for this annotation

Codecov / codecov/patch

schema.go#L107

Added line #L107 was not covered by tests
usage()
return
}
Expand All @@ -124,8 +126,6 @@
os.Exit(1)
}

// fmt.Println("Schema URL:", schemaUrl)

// Declare a map to hold the merged YAML data
mergedMap := make(map[string]interface{})

Expand Down
212 changes: 178 additions & 34 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,207 @@ package main

import (
"os"
"reflect"
"testing"

"github.com/losisin/go-jsonschema-generator"
)

func TestReadAndUnmarshalYAML(t *testing.T) {
yamlContent := []byte("key1: value1\nkey2: value2\n")
yamlFilePath := "test.yaml"
err := os.WriteFile(yamlFilePath, yamlContent, 0644)
if err != nil {
t.Fatalf("Error creating a temporary YAML file: %v", err)
func TestMultiStringFlagString(t *testing.T) {
tests := []struct {
input multiStringFlag
expected string
}{
{
input: multiStringFlag{},
expected: "",
},
{
input: multiStringFlag{"value1"},
expected: "value1",
},
{
input: multiStringFlag{"value1", "value2", "value3"},
expected: "value1, value2, value3",
},
}
defer os.Remove(yamlFilePath)

var target map[string]interface{}
err = readAndUnmarshalYAML(yamlFilePath, &target)
if err != nil {
t.Errorf("Error reading and unmarshaling YAML: %v", err)
for i, test := range tests {
result := test.input.String()
if result != test.expected {
t.Errorf("Test case %d: Expected %q, but got %q", i+1, test.expected, result)
}
}
}

if len(target) != 2 {
t.Errorf("Expected target map length to be 2, but got %d", len(target))
func TestMultiStringFlagSet(t *testing.T) {
tests := []struct {
input string
initial multiStringFlag
expected multiStringFlag
errorFlag bool
}{
{
input: "value1,value2,value3",
initial: multiStringFlag{},
expected: multiStringFlag{"value1", "value2", "value3"},
errorFlag: false,
},
{
input: "",
initial: multiStringFlag{"existingValue"},
expected: multiStringFlag{"existingValue"},
errorFlag: false,
},
{
input: "value1, value2, value3",
initial: multiStringFlag{},
expected: multiStringFlag{"value1", "value2", "value3"},
errorFlag: false,
},
}

if target["key1"] != "value1" {
t.Errorf("target map should contain key1 with value 'value1'")
for i, test := range tests {
err := test.initial.Set(test.input)
if err != nil && !test.errorFlag {
t.Errorf("Test case %d: Expected no error, but got: %v", i+1, err)
} else if err == nil && test.errorFlag {
t.Errorf("Test case %d: Expected an error, but got none", i+1)
}
}
}

if target["key2"] != "value2" {
t.Errorf("target map should contain key2 with value 'value2'")
}
func TestReadAndUnmarshalYAML(t *testing.T) {
t.Run("Valid YAML", func(t *testing.T) {
yamlContent := []byte("key1: value1\nkey2: value2\n")
yamlFilePath := "valid.yaml"

err := os.WriteFile(yamlFilePath, yamlContent, 0644)
if err != nil {
t.Fatalf("Error creating a temporary YAML file: %v", err)
}
defer os.Remove(yamlFilePath)

var target map[string]interface{}
err = readAndUnmarshalYAML(yamlFilePath, &target)

if err != nil {
t.Errorf("Error reading and unmarshaling valid YAML: %v", err)
return
}

if len(target) != 2 {
t.Errorf("Expected target map length to be 2, but got %d", len(target))
}

if target["key1"] != "value1" {
t.Errorf("target map should contain key1 with value 'value1'")
}

if target["key2"] != "value2" {
t.Errorf("target map should contain key2 with value 'value2'")
}
})

t.Run("File Missing", func(t *testing.T) {
// YAML file is assumed to be missing
missingFilePath := "missing.yaml"

var target map[string]interface{}
err := readAndUnmarshalYAML(missingFilePath, &target)

if err == nil {
t.Errorf("Expected an error when the file is missing, but got nil")
}
})
}

func TestMergeMaps(t *testing.T) {
mapA := map[string]interface{}{
"key1": "value1",
"key2": "value2",
tests := []struct {
a, b, expected map[string]interface{}
}{
{
a: map[string]interface{}{"key1": "value1"},
b: map[string]interface{}{"key2": "value2"},
expected: map[string]interface{}{"key1": "value1", "key2": "value2"},
},
{
a: map[string]interface{}{"key1": "value1"},
b: map[string]interface{}{"key1": "value2"},
expected: map[string]interface{}{"key1": "value2"},
},
{
a: map[string]interface{}{
"key1": map[string]interface{}{"subkey1": "value1"},
},
b: map[string]interface{}{
"key1": map[string]interface{}{"subkey2": "value2"},
},
expected: map[string]interface{}{
"key1": map[string]interface{}{"subkey1": "value1", "subkey2": "value2"},
},
},
{
a: map[string]interface{}{
"key1": map[string]interface{}{
"subkey1": "value1",
},
},
b: map[string]interface{}{
"key1": map[string]interface{}{
"subkey2": "value2",
},
},
expected: map[string]interface{}{
"key1": map[string]interface{}{
"subkey1": "value1",
"subkey2": "value2",
},
},
},
}

mapB := map[string]interface{}{
"key2": "newvalue2",
"key3": "value3",
for i, test := range tests {
result := mergeMaps(test.a, test.b)
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("Test case %d failed. Expected: %v, Got: %v", i+1, test.expected, result)
}
}
}

merged := mergeMaps(mapA, mapB)
func TestPrintMap(t *testing.T) {
tmpFile := "test_output.json"
defer os.Remove(tmpFile)

if len(merged) != 3 {
t.Errorf("Expected merged map length to be 3, but got %d", len(merged))
}
var yamlData map[string]interface{}

if merged["key1"] != "value1" {
t.Errorf("Merged map should contain key1 from mapA")
// Test successful data read and schema creation
err := readAndUnmarshalYAML("testdata/values.yaml", &yamlData)
if err != nil {
t.Fatalf("Failed to mock YAML data: %v", err)
}
data := jsonschema.NewDocument("")
data.ReadDeep(&yamlData)

if merged["key2"] != "newvalue2" {
t.Errorf("Merged map should contain key2 from mapB")
cases := []struct {
data *jsonschema.Document
tmpFile string
expectError bool
}{
{data, tmpFile, false},
{data, "", true},
{nil, tmpFile, true},
}

if merged["key3"] != "value3" {
t.Errorf("Merged map should contain key3 from mapB")
for _, c := range cases {
t.Run("PrintMap", func(t *testing.T) {
err := printMap(c.data, c.tmpFile)
switch {
case err == nil && c.expectError:
t.Fatalf("Expected an error, but printMap succeeded")
case err != nil && !c.expectError:
t.Fatalf("Unexpected error: %v", err)
}
})
}
}
14 changes: 14 additions & 0 deletions testdata/values.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"nodeSelector": {
"type": "object",
"properties": {
"kubernetes.io/hostname": {
"type": "string"
}
}
}
}
}
2 changes: 2 additions & 0 deletions testdata/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nodeSelector:
kubernetes.io/hostname: "node1"