chore(pkg): refactor: convert tests to testify assert/require part 9

refactor: convert tests to testify assert/require in pkg/chart/common/util
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
pull/32424/head
Matthieu MOREL 2 months ago
parent 3f1f55c4c8
commit 13d1edc5f3

@ -141,9 +141,7 @@ func TestCoalesceValues(t *testing.T) {
) )
vals, err := common.ReadValues(testCoalesceValuesYaml) vals, err := common.ReadValues(testCoalesceValuesYaml)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
// taking a copy of the values before passing it // taking a copy of the values before passing it
// to CoalesceValues as argument, so that we can // to CoalesceValues as argument, so that we can
@ -152,9 +150,7 @@ func TestCoalesceValues(t *testing.T) {
maps.Copy(valsCopy, vals) maps.Copy(valsCopy, vals)
v, err := CoalesceValues(c, vals) v, err := CoalesceValues(c, vals)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
j, _ := json.MarshalIndent(v, "", " ") j, _ := json.MarshalIndent(v, "", " ")
t.Logf("Coalesced Values: %s", string(j)) t.Logf("Coalesced Values: %s", string(j))
@ -211,32 +207,26 @@ func TestCoalesceValues(t *testing.T) {
nullKeys := []string{"bottom", "right", "left", "front"} nullKeys := []string{"bottom", "right", "left", "front"}
for _, nullKey := range nullKeys { for _, nullKey := range nullKeys {
if _, ok := v[nullKey]; ok { _, ok := v[nullKey]
t.Errorf("Expected key %q to be removed, still present", nullKey) assert.Falsef(t, ok, "Expected key %q to be removed, still present", nullKey)
}
} }
if _, ok := v["nested"].(map[string]any)["boat"]; ok { _, ok := v["nested"].(map[string]any)["boat"]
t.Error("Expected nested boat key to be removed, still present") assert.False(t, ok, "Expected nested boat key to be removed, still present")
}
subchart := v["pequod"].(map[string]any) subchart := v["pequod"].(map[string]any)
if _, ok := subchart["boat"]; ok { _, ok = subchart["boat"]
t.Error("Expected subchart boat key to be removed, still present") assert.False(t, ok, "Expected subchart boat key to be removed, still present")
}
subsubchart := subchart["ahab"].(map[string]any) subsubchart := subchart["ahab"].(map[string]any)
if _, ok := subsubchart["boat"]; ok { _, ok = subsubchart["boat"]
t.Error("Expected sub-subchart ahab boat key to be removed, still present") assert.False(t, ok, "Expected sub-subchart ahab boat key to be removed, still present")
}
if _, ok := subsubchart["nested"].(map[string]any)["boat"]; ok { _, ok = subsubchart["nested"].(map[string]any)["boat"]
t.Error("Expected sub-subchart nested boat key to be removed, still present") assert.False(t, ok, "Expected sub-subchart nested boat key to be removed, still present")
}
if _, ok := subsubchart["object"]; ok { _, ok = subsubchart["object"]
t.Error("Expected sub-subchart object map to be removed, still present") assert.False(t, ok, "Expected sub-subchart object map to be removed, still present")
}
// CoalesceValues should not mutate the passed arguments // CoalesceValues should not mutate the passed arguments
is.Equal(valsCopy, vals) is.Equal(valsCopy, vals)
@ -306,9 +296,7 @@ func TestMergeValues(t *testing.T) {
) )
vals, err := common.ReadValues(testCoalesceValuesYaml) vals, err := common.ReadValues(testCoalesceValuesYaml)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
// taking a copy of the values before passing it // taking a copy of the values before passing it
// to MergeValues as argument, so that we can // to MergeValues as argument, so that we can
@ -317,9 +305,7 @@ func TestMergeValues(t *testing.T) {
maps.Copy(valsCopy, vals) maps.Copy(valsCopy, vals)
v, err := MergeValues(c, vals) v, err := MergeValues(c, vals)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
j, _ := json.MarshalIndent(v, "", " ") j, _ := json.MarshalIndent(v, "", " ")
t.Logf("Coalesced Values: %s", string(j)) t.Logf("Coalesced Values: %s", string(j))
@ -377,25 +363,20 @@ func TestMergeValues(t *testing.T) {
// removed. // removed.
nullKeys := []string{"bottom", "right", "left", "front"} nullKeys := []string{"bottom", "right", "left", "front"}
for _, nullKey := range nullKeys { for _, nullKey := range nullKeys {
if vv, ok := v[nullKey]; !ok { vv, ok := v[nullKey]
t.Errorf("Expected key %q to be present but it was removed", nullKey) assert.Truef(t, ok, "Expected key %q to be present but it was removed", nullKey)
} else if vv != nil { assert.Nilf(t, vv, "Expected key %q to be null but it has a value of %v", nullKey, vv)
t.Errorf("Expected key %q to be null but it has a value of %v", nullKey, vv)
}
} }
if _, ok := v["nested"].(map[string]any)["boat"]; !ok { _, ok := v["nested"].(map[string]any)["boat"]
t.Error("Expected nested boat key to be present but it was removed") assert.True(t, ok, "Expected nested boat key to be present but it was removed")
}
subchart := v["pequod"].(map[string]any)["ahab"].(map[string]any) subchart := v["pequod"].(map[string]any)["ahab"].(map[string]any)
if _, ok := subchart["boat"]; !ok { _, ok = subchart["boat"]
t.Error("Expected subchart boat key to be present but it was removed") assert.True(t, ok, "Expected subchart boat key to be present but it was removed")
}
if _, ok := subchart["nested"].(map[string]any)["bar"]; !ok { _, ok = subchart["nested"].(map[string]any)["bar"]
t.Error("Expected subchart nested bar key to be present but it was removed") assert.True(t, ok, "Expected subchart nested bar key to be present but it was removed")
}
// CoalesceValues should not mutate the passed arguments // CoalesceValues should not mutate the passed arguments
is.Equal(valsCopy, vals) is.Equal(valsCopy, vals)
@ -433,47 +414,27 @@ func TestCoalesceTables(t *testing.T) {
// otherwise the values are coalesced. // otherwise the values are coalesced.
CoalesceTables(dst, src) CoalesceTables(dst, src)
if dst["name"] != "Ishmael" { assert.Equal(t, "Ishmael", dst["name"], "Unexpected name: %s", dst["name"])
t.Errorf("Unexpected name: %s", dst["name"]) assert.Equal(t, "whaler", dst["occupation"], "Unexpected occupation: %s", dst["occupation"])
}
if dst["occupation"] != "whaler" {
t.Errorf("Unexpected occupation: %s", dst["occupation"])
}
addr, ok := dst["address"].(map[string]any) addr, ok := dst["address"].(map[string]any)
if !ok { require.True(t, ok, "Address went away.")
t.Fatal("Address went away.") assert.Equal(t, "123 Spouter Inn Ct.", addr["street"].(string), "Unexpected address: %v", addr["street"])
} assert.Equal(t, "Nantucket", addr["city"].(string), "Unexpected city: %v", addr["city"])
assert.Equal(t, "MA", addr["state"].(string), "Unexpected state: %v", addr["state"])
if addr["street"].(string) != "123 Spouter Inn Ct." {
t.Errorf("Unexpected address: %v", addr["street"])
}
if addr["city"].(string) != "Nantucket" { _, ok = addr["country"]
t.Errorf("Unexpected city: %v", addr["city"]) assert.False(t, ok, "The country is not left out.")
}
if addr["state"].(string) != "MA" { det, ok := dst["details"].(map[string]any)
t.Errorf("Unexpected state: %v", addr["state"]) require.Truef(t, ok, "Details is the wrong type: %v", dst["details"])
}
if _, ok = addr["country"]; ok { _, ok = det["friends"]
t.Error("The country is not left out.") assert.True(t, ok, "Could not find your friends. Maybe you don't have any. :-(")
} assert.Equal(t, "pequod", dst["boat"].(string), "Expected boat string, got %v", dst["boat"])
if det, ok := dst["details"].(map[string]any); !ok { _, ok = dst["hole"]
t.Fatalf("Details is the wrong type: %v", dst["details"]) assert.False(t, ok, "The hole still exists.")
} else if _, ok := det["friends"]; !ok {
t.Error("Could not find your friends. Maybe you don't have any. :-(")
}
if dst["boat"].(string) != "pequod" {
t.Errorf("Expected boat string, got %v", dst["boat"])
}
if _, ok = dst["hole"]; ok {
t.Error("The hole still exists.")
}
dst2 := map[string]any{ dst2 := map[string]any{
"name": "Ishmael", "name": "Ishmael",
@ -493,40 +454,21 @@ func TestCoalesceTables(t *testing.T) {
// this happens when the --reuse-values flag is set but the chart has no modifications yet // this happens when the --reuse-values flag is set but the chart has no modifications yet
CoalesceTables(dst2, nil) CoalesceTables(dst2, nil)
if dst2["name"] != "Ishmael" { assert.Equal(t, "Ishmael", dst2["name"], "Unexpected name: %s", dst2["name"])
t.Errorf("Unexpected name: %s", dst2["name"])
}
addr2, ok := dst2["address"].(map[string]any) addr2, ok := dst2["address"].(map[string]any)
if !ok { require.True(t, ok, "Address went away.")
t.Fatal("Address went away.") assert.Equal(t, "123 Spouter Inn Ct.", addr2["street"].(string), "Unexpected address: %v", addr2["street"])
} assert.Equal(t, "Nantucket", addr2["city"].(string), "Unexpected city: %v", addr2["city"])
assert.Equal(t, "US", addr2["country"].(string), "Unexpected Country: %v", addr2["country"])
if addr2["street"].(string) != "123 Spouter Inn Ct." {
t.Errorf("Unexpected address: %v", addr2["street"]) det2, ok := dst2["details"].(map[string]any)
} require.Truef(t, ok, "Details is the wrong type: %v", dst2["details"])
if addr2["city"].(string) != "Nantucket" { _, ok = det2["friends"]
t.Errorf("Unexpected city: %v", addr2["city"]) assert.True(t, ok, "Could not find your friends. Maybe you don't have any. :-(")
} assert.Equal(t, "pequod", dst2["boat"].(string), "Expected boat string, got %v", dst2["boat"])
assert.Equal(t, "black", dst2["hole"].(string), "Expected hole string, got %v", dst2["boat"])
if addr2["country"].(string) != "US" {
t.Errorf("Unexpected Country: %v", addr2["country"])
}
if det2, ok := dst2["details"].(map[string]any); !ok {
t.Fatalf("Details is the wrong type: %v", dst2["details"])
} else if _, ok := det2["friends"]; !ok {
t.Error("Could not find your friends. Maybe you don't have any. :-(")
}
if dst2["boat"].(string) != "pequod" {
t.Errorf("Expected boat string, got %v", dst2["boat"])
}
if dst2["hole"].(string) != "black" {
t.Errorf("Expected hole string, got %v", dst2["boat"])
}
} }
func TestMergeTables(t *testing.T) { func TestMergeTables(t *testing.T) {
@ -561,51 +503,31 @@ func TestMergeTables(t *testing.T) {
// otherwise the values are coalesced. // otherwise the values are coalesced.
MergeTables(dst, src) MergeTables(dst, src)
if dst["name"] != "Ishmael" { assert.Equal(t, "Ishmael", dst["name"], "Unexpected name: %s", dst["name"])
t.Errorf("Unexpected name: %s", dst["name"]) assert.Equal(t, "whaler", dst["occupation"], "Unexpected occupation: %s", dst["occupation"])
}
if dst["occupation"] != "whaler" {
t.Errorf("Unexpected occupation: %s", dst["occupation"])
}
addr, ok := dst["address"].(map[string]any) addr, ok := dst["address"].(map[string]any)
if !ok { require.True(t, ok, "Address went away.")
t.Fatal("Address went away.") assert.Equal(t, "123 Spouter Inn Ct.", addr["street"].(string), "Unexpected address: %v", addr["street"])
} assert.Equal(t, "Nantucket", addr["city"].(string), "Unexpected city: %v", addr["city"])
assert.Equal(t, "MA", addr["state"].(string), "Unexpected state: %v", addr["state"])
if addr["street"].(string) != "123 Spouter Inn Ct." {
t.Errorf("Unexpected address: %v", addr["street"])
}
if addr["city"].(string) != "Nantucket" {
t.Errorf("Unexpected city: %v", addr["city"])
}
if addr["state"].(string) != "MA" {
t.Errorf("Unexpected state: %v", addr["state"])
}
// This is one test that is different from CoalesceTables. Because country // This is one test that is different from CoalesceTables. Because country
// is a nil value and it's not removed it's still present. // is a nil value and it's not removed it's still present.
if _, ok = addr["country"]; !ok { _, ok = addr["country"]
t.Error("The country is left out.") assert.True(t, ok, "The country is left out.")
}
if det, ok := dst["details"].(map[string]any); !ok { det, ok := dst["details"].(map[string]any)
t.Fatalf("Details is the wrong type: %v", dst["details"]) require.Truef(t, ok, "Details is the wrong type: %v", dst["details"])
} else if _, ok := det["friends"]; !ok {
t.Error("Could not find your friends. Maybe you don't have any. :-(")
}
if dst["boat"].(string) != "pequod" { _, ok = det["friends"]
t.Errorf("Expected boat string, got %v", dst["boat"]) assert.True(t, ok, "Could not find your friends. Maybe you don't have any. :-(")
} assert.Equal(t, "pequod", dst["boat"].(string), "Expected boat string, got %v", dst["boat"])
// This is one test that is different from CoalesceTables. Because hole // This is one test that is different from CoalesceTables. Because hole
// is a nil value and it's not removed it's still present. // is a nil value and it's not removed it's still present.
if _, ok = dst["hole"]; !ok { _, ok = dst["hole"]
t.Error("The hole no longer exists.") assert.True(t, ok, "The hole no longer exists.")
}
dst2 := map[string]any{ dst2 := map[string]any{
"name": "Ishmael", "name": "Ishmael",
@ -626,44 +548,22 @@ func TestMergeTables(t *testing.T) {
// this happens when the --reuse-values flag is set but the chart has no modifications yet // this happens when the --reuse-values flag is set but the chart has no modifications yet
MergeTables(dst2, nil) MergeTables(dst2, nil)
if dst2["name"] != "Ishmael" { assert.Equal(t, "Ishmael", dst2["name"], "Unexpected name: %s", dst2["name"])
t.Errorf("Unexpected name: %s", dst2["name"])
}
addr2, ok := dst2["address"].(map[string]any) addr2, ok := dst2["address"].(map[string]any)
if !ok { require.True(t, ok, "Address went away.")
t.Fatal("Address went away.") assert.Equal(t, "123 Spouter Inn Ct.", addr2["street"].(string), "Unexpected address: %v", addr2["street"])
} assert.Equal(t, "Nantucket", addr2["city"].(string), "Unexpected city: %v", addr2["city"])
assert.Equal(t, "US", addr2["country"].(string), "Unexpected Country: %v", addr2["country"])
if addr2["street"].(string) != "123 Spouter Inn Ct." {
t.Errorf("Unexpected address: %v", addr2["street"]) det2, ok := dst2["details"].(map[string]any)
} require.Truef(t, ok, "Details is the wrong type: %v", dst2["details"])
if addr2["city"].(string) != "Nantucket" { _, ok = det2["friends"]
t.Errorf("Unexpected city: %v", addr2["city"]) assert.True(t, ok, "Could not find your friends. Maybe you don't have any. :-(")
} assert.Equal(t, "pequod", dst2["boat"].(string), "Expected boat string, got %v", dst2["boat"])
assert.Equal(t, "black", dst2["hole"].(string), "Expected hole string, got %v", dst2["hole"])
if addr2["country"].(string) != "US" { assert.Nil(t, dst2["nilval"], "Expected nilvalue to have nil value but it does not")
t.Errorf("Unexpected Country: %v", addr2["country"])
}
if det2, ok := dst2["details"].(map[string]any); !ok {
t.Fatalf("Details is the wrong type: %v", dst2["details"])
} else if _, ok := det2["friends"]; !ok {
t.Error("Could not find your friends. Maybe you don't have any. :-(")
}
if dst2["boat"].(string) != "pequod" {
t.Errorf("Expected boat string, got %v", dst2["boat"])
}
if dst2["hole"].(string) != "black" {
t.Errorf("Expected hole string, got %v", dst2["boat"])
}
if dst2["nilval"] != nil {
t.Error("Expected nilvalue to have nil value but it does not")
}
} }
func TestCoalesceValuesWarnings(t *testing.T) { func TestCoalesceValuesWarnings(t *testing.T) {
@ -716,9 +616,7 @@ func TestCoalesceValuesWarnings(t *testing.T) {
} }
_, err := coalesce(printf, c, vals, "", false) _, err := coalesce(printf, c, vals, "", false)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
t.Logf("vals: %v", vals) t.Logf("vals: %v", vals)
assert.Contains(t, warnings, "warning: skipped value for level1.level2.level3.boat: Not a table.") assert.Contains(t, warnings, "warning: skipped value for level1.level2.level3.boat: Not a table.")
@ -926,7 +824,6 @@ func TestCoalesceValuesSubchartNilCleanedWhenUserPartiallyOverrides(t *testing.T
keyMapping, ok := childVals["keyMapping"].(map[string]any) keyMapping, ok := childVals["keyMapping"].(map[string]any)
is.True(ok, "keyMapping should be a map") is.True(ok, "keyMapping should be a map")
is.Equal("sha256", keyMapping["format"], "User override should be preserved") is.Equal("sha256", keyMapping["format"], "User override should be preserved")
_, ok = keyMapping["password"] _, ok = keyMapping["password"]

@ -20,75 +20,47 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"helm.sh/helm/v4/pkg/chart/common" "helm.sh/helm/v4/pkg/chart/common"
chart "helm.sh/helm/v4/pkg/chart/v2" chart "helm.sh/helm/v4/pkg/chart/v2"
) )
func TestValidateAgainstSingleSchema(t *testing.T) { func TestValidateAgainstSingleSchema(t *testing.T) {
values, err := common.ReadValuesFile("./testdata/test-values.yaml") values, err := common.ReadValuesFile("./testdata/test-values.yaml")
if err != nil { require.NoError(t, err, "Error reading YAML file")
t.Fatalf("Error reading YAML file: %s", err)
}
schema, err := os.ReadFile("./testdata/test-values.schema.json")
if err != nil {
t.Fatalf("Error reading YAML file: %s", err)
}
if err := ValidateAgainstSingleSchema(values, schema); err != nil { schema, err := os.ReadFile("./testdata/test-values.schema.json")
t.Errorf("Error validating Values against Schema: %s", err) require.NoError(t, err, "Error reading YAML file")
} assert.NoErrorf(t, ValidateAgainstSingleSchema(values, schema), "Error validating Values against Schema")
} }
func TestValidateAgainstInvalidSingleSchema(t *testing.T) { func TestValidateAgainstInvalidSingleSchema(t *testing.T) {
values, err := common.ReadValuesFile("./testdata/test-values.yaml") values, err := common.ReadValuesFile("./testdata/test-values.yaml")
if err != nil { require.NoError(t, err, "Error reading YAML file")
t.Fatalf("Error reading YAML file: %s", err)
}
schema, err := os.ReadFile("./testdata/test-values-invalid.schema.json")
if err != nil {
t.Fatalf("Error reading YAML file: %s", err)
}
var errString string schema, err := os.ReadFile("./testdata/test-values-invalid.schema.json")
if err := ValidateAgainstSingleSchema(values, schema); err == nil { require.NoError(t, err, "Error reading YAML file")
t.Fatal("Expected an error, but got nil")
} else {
errString = err.Error()
}
expectedErrString := `"file:///values.schema.json#" is not valid against metaschema: jsonschema validation failed with 'https://json-schema.org/draft/2020-12/schema#' expectedErrString := `"file:///values.schema.json#" is not valid against metaschema: jsonschema validation failed with 'https://json-schema.org/draft/2020-12/schema#'
- at '': got number, want boolean or object` - at '': got number, want boolean or object`
if errString != expectedErrString { assert.EqualError(t, ValidateAgainstSingleSchema(values, schema), expectedErrString)
t.Errorf("Error string :\n`%s`\ndoes not match expected\n`%s`", errString, expectedErrString)
}
} }
func TestValidateAgainstSingleSchemaNegative(t *testing.T) { func TestValidateAgainstSingleSchemaNegative(t *testing.T) {
values, err := common.ReadValuesFile("./testdata/test-values-negative.yaml") values, err := common.ReadValuesFile("./testdata/test-values-negative.yaml")
if err != nil { require.NoError(t, err, "Error reading YAML file")
t.Fatalf("Error reading YAML file: %s", err)
}
schema, err := os.ReadFile("./testdata/test-values.schema.json")
if err != nil {
t.Fatalf("Error reading JSON file: %s", err)
}
var errString string schema, err := os.ReadFile("./testdata/test-values.schema.json")
if err := ValidateAgainstSingleSchema(values, schema); err == nil { require.NoError(t, err, "Error reading JSON file")
t.Fatal("Expected an error, but got nil")
} else {
errString = err.Error()
}
expectedErrString := `- at '': missing property 'employmentInfo' expectedErrString := `- at '': missing property 'employmentInfo'
- at '/age': minimum: got -5, want 0 - at '/age': minimum: got -5, want 0
` `
if errString != expectedErrString { assert.EqualError(t, ValidateAgainstSingleSchema(values, schema), expectedErrString)
t.Errorf("Error string :\n`%s`\ndoes not match expected\n`%s`", errString, expectedErrString)
}
} }
const subchartSchema = `{ const subchartSchema = `{
@ -145,9 +117,7 @@ func TestValidateAgainstSchema(t *testing.T) {
}, },
} }
if err := ValidateAgainstSchema(chrt, vals); err != nil { assert.NoErrorf(t, ValidateAgainstSchema(chrt, vals), "Error validating Values against Schema")
t.Errorf("Error validating Values against Schema: %s", err)
}
} }
func TestValidateAgainstSchemaNegative(t *testing.T) { func TestValidateAgainstSchemaNegative(t *testing.T) {
@ -170,19 +140,10 @@ func TestValidateAgainstSchemaNegative(t *testing.T) {
"subchart": map[string]any{}, "subchart": map[string]any{},
} }
var errString string
if err := ValidateAgainstSchema(chrt, vals); err == nil {
t.Fatal("Expected an error, but got nil")
} else {
errString = err.Error()
}
expectedErrString := `subchart: expectedErrString := `subchart:
- at '': missing property 'age' - at '': missing property 'age'
` `
if errString != expectedErrString { assert.EqualError(t, ValidateAgainstSchema(chrt, vals), expectedErrString)
t.Errorf("Error string :\n`%s`\ndoes not match expected\n`%s`", errString, expectedErrString)
}
} }
func TestValidateAgainstSchema2020(t *testing.T) { func TestValidateAgainstSchema2020(t *testing.T) {
@ -207,9 +168,7 @@ func TestValidateAgainstSchema2020(t *testing.T) {
}, },
} }
if err := ValidateAgainstSchema(chrt, vals); err != nil { assert.NoErrorf(t, ValidateAgainstSchema(chrt, vals), "Error validating Values against Schema")
t.Errorf("Error validating Values against Schema: %s", err)
}
} }
func TestValidateAgainstSchema2020Negative(t *testing.T) { func TestValidateAgainstSchema2020Negative(t *testing.T) {
@ -234,20 +193,11 @@ func TestValidateAgainstSchema2020Negative(t *testing.T) {
}, },
} }
var errString string
if err := ValidateAgainstSchema(chrt, vals); err == nil {
t.Fatal("Expected an error, but got nil")
} else {
errString = err.Error()
}
expectedErrString := `subchart: expectedErrString := `subchart:
- at '/data': no items match contains schema - at '/data': no items match contains schema
- at '/data/0': got number, want string - at '/data/0': got number, want string
` `
if errString != expectedErrString { assert.EqualError(t, ValidateAgainstSchema(chrt, vals), expectedErrString)
t.Errorf("Error string :\n`%s`\ndoes not match expected\n`%s`", errString, expectedErrString)
}
} }
func TestHTTPURLLoader_Load(t *testing.T) { func TestHTTPURLLoader_Load(t *testing.T) {
@ -262,12 +212,8 @@ func TestHTTPURLLoader_Load(t *testing.T) {
loader := newHTTPURLLoader() loader := newHTTPURLLoader()
result, err := loader.Load(server.URL) result, err := loader.Load(server.URL)
if err != nil { require.NoError(t, err, "Expected no error, got")
t.Fatalf("Expected no error, got: %v", err) require.NotNil(t, result, "Expected result to be non-nil")
}
if result == nil {
t.Fatal("Expected result to be non-nil")
}
}) })
t.Run("HTTP error status", func(t *testing.T) { t.Run("HTTP error status", func(t *testing.T) {
@ -278,12 +224,8 @@ func TestHTTPURLLoader_Load(t *testing.T) {
loader := newHTTPURLLoader() loader := newHTTPURLLoader()
_, err := loader.Load(server.URL) _, err := loader.Load(server.URL)
if err == nil { require.Error(t, err, "Expected error for HTTP 404")
t.Fatal("Expected error for HTTP 404") assert.ErrorContains(t, err, "404", "Expected error message to contain '404'")
}
if !strings.Contains(err.Error(), "404") {
t.Errorf("Expected error message to contain '404', got: %v", err)
}
}) })
} }
@ -295,9 +237,7 @@ func TestValidateAgainstSingleSchema_UnresolvedURN_Ignored(t *testing.T) {
"$ref": "urn:example:helm:schemas:v1:helm-schema-validation-conditions:v1/helmSchemaValidation-true" "$ref": "urn:example:helm:schemas:v1:helm-schema-validation-conditions:v1/helmSchemaValidation-true"
}`) }`)
vals := map[string]any{"any": "value"} vals := map[string]any{"any": "value"}
if err := ValidateAgainstSingleSchema(vals, schema); err != nil { require.NoErrorf(t, ValidateAgainstSingleSchema(vals, schema), "expected no error when URN unresolved is ignored, got")
t.Fatalf("expected no error when URN unresolved is ignored, got: %v", err)
}
} }
// Non-regression tests for https://github.com/helm/helm/issues/31202 // Non-regression tests for https://github.com/helm/helm/issues/31202
@ -323,14 +263,10 @@ func TestValidateAgainstSchema_MissingSubchartValues_NoPanic(t *testing.T) {
} }
defer func() { defer func() {
if r := recover(); r != nil { require.Nilf(t, recover(), "ValidateAgainstSchema panicked (missing subchart values)")
t.Fatalf("ValidateAgainstSchema panicked (missing subchart values): %v", r)
}
}() }()
if err := ValidateAgainstSchema(chrt, vals); err != nil { require.NoErrorf(t, ValidateAgainstSchema(chrt, vals), "expected no error when subchart values are missing, got")
t.Fatalf("expected no error when subchart values are missing, got: %v", err)
}
} }
func TestValidateAgainstSchema_SubchartNil_NoPanic(t *testing.T) { func TestValidateAgainstSchema_SubchartNil_NoPanic(t *testing.T) {
@ -351,14 +287,10 @@ func TestValidateAgainstSchema_SubchartNil_NoPanic(t *testing.T) {
} }
defer func() { defer func() {
if r := recover(); r != nil { require.Nilf(t, recover(), "ValidateAgainstSchema panicked (nil subchart values)")
t.Fatalf("ValidateAgainstSchema panicked (nil subchart values): %v", r)
}
}() }()
if err := ValidateAgainstSchema(chrt, vals); err != nil { require.NoErrorf(t, ValidateAgainstSchema(chrt, vals), "expected no error when subchart values are nil, got")
t.Fatalf("expected no error when subchart values are nil, got: %v", err)
}
} }
func TestValidateAgainstSchema_InvalidSubchartValuesType_NoPanic(t *testing.T) { func TestValidateAgainstSchema_InvalidSubchartValuesType_NoPanic(t *testing.T) {
@ -379,13 +311,9 @@ func TestValidateAgainstSchema_InvalidSubchartValuesType_NoPanic(t *testing.T) {
} }
defer func() { defer func() {
if r := recover(); r != nil { require.Nilf(t, recover(), "ValidateAgainstSchema panicked (invalid subchart values type)")
t.Fatalf("ValidateAgainstSchema panicked (invalid subchart values type): %v", r)
}
}() }()
// We expect a non-nil error (invalid type), but crucially no panic. // We expect a non-nil error (invalid type), but crucially no panic.
if err := ValidateAgainstSchema(chrt, vals); err == nil { require.Error(t, ValidateAgainstSchema(chrt, vals), "expected an error when subchart values have invalid type, got nil")
t.Fatal("expected an error when subchart values have invalid type, got nil")
}
} }

@ -21,6 +21,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/require"
chart "helm.sh/helm/v4/pkg/chart/v2" chart "helm.sh/helm/v4/pkg/chart/v2"
"helm.sh/helm/v4/pkg/release/common" "helm.sh/helm/v4/pkg/release/common"
release "helm.sh/helm/v4/pkg/release/v1" release "helm.sh/helm/v4/pkg/release/v1"
@ -44,9 +46,7 @@ func checkFileCompletion(t *testing.T, cmdName string, shouldBePerformed bool) {
testcmd := fmt.Sprintf("__complete %s ''", cmdName) testcmd := fmt.Sprintf("__complete %s ''", cmdName)
_, out, err := executeActionCommandC(storage, testcmd) _, out, err := executeActionCommandC(storage, testcmd)
if err != nil { require.NoError(t, err)
t.Errorf("unexpected error, %s", err)
}
if !strings.Contains(out, "ShellCompDirectiveNoFileComp") != shouldBePerformed { if !strings.Contains(out, "ShellCompDirectiveNoFileComp") != shouldBePerformed {
if shouldBePerformed { if shouldBePerformed {
t.Errorf("Unexpected directive ShellCompDirectiveNoFileComp when completing '%s'", cmdName) t.Errorf("Unexpected directive ShellCompDirectiveNoFileComp when completing '%s'", cmdName)

Loading…
Cancel
Save