Skip to content

Conversation

Benjosh95
Copy link
Contributor

@Benjosh95 Benjosh95 commented Sep 9, 2025

Description

Jira-ticket: https://jira.schwarz/browse/STACKITSDK-139

Checklist

  • Issue was linked above
  • No generated code was adjusted manually (check comments in file header)
  • Changelogs
    • Changelog in the root directory was adjusted (see here)
    • Changelog(s) of the service(s) were adjusted (see e.g. here)
  • VERSION file(s) of the service(s) were adjusted
  • Code format was applied: make fmt
  • Examples were added / adjusted (see examples/ directory)
  • Unit tests got implemented or updated
  • Unit tests are passing: make test (will be checked by CI)
  • No linter issues: make lint (will be checked by CI)

@Benjosh95 Benjosh95 force-pushed the bf/userdata-base64-fix branch from 9160675 to 4502061 Compare September 9, 2025 07:14
@@ -37,3 +38,166 @@ func TestContainsInt(t *testing.T) {
t.Fatalf("Should not be contained")
}
}

// Test struct for YAML conversion testing
type TestServer struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type TestServer struct {
type testServer struct {

doesn't need to be exposed I guess

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...or just move the struct definition into the TestConvertByteArraysToBase64 func. You're only using it there if I didn't mess something up here. See the example below for reference.

https://github.com/stackitcloud/stackit-cli/blob/a5438f4cac3a794cb95d04891a83252aa9ae1f1e/internal/pkg/utils/strings_test.go#L10-L13

name: "normal case with byte arrays",
input: TestServer{
Name: "test-server",
UserData: func() *[]byte { b := []byte("hello world"); return &b }(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
UserData: func() *[]byte { b := []byte("hello world"); return &b }(),
UserData: Ptr([]byte("hello world"))

// Ptr Returns the pointer to any type T
func Ptr[T any](v T) *T {
return &v
}

}
}
case string:
if v != "" && v != "hello" && v != "test" && v != "nested" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hardcoded values? 🤔

{"nil", nil, ""},
{"map with []byte", map[string]interface{}{"data": []byte("hello")}, "aGVsbG8="},
{"slice with []byte", []interface{}{[]byte("test")}, "dGVzdA=="},
{"nested map", map[string]interface{}{"level": map[string]interface{}{"data": []byte("nested")}}, "bmVzdGVk"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format the input for the table test properly.

{
  name: "nested map", 
  input: map[string]interface{}{
    "level": map[string]interface{}{
      "data": []byte("nested")
     },
  }, 
  expected: "bmVzdGVk"
},

Maybe it's just me, but I'm having a really hard time to get the goal of this PR in it's current state.

input interface{}
expected string // check if []byte was converted to base64 string
}{
{"nil", nil, ""},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"nil"? Accident or intended?

t.Run(tt.name, func(t *testing.T) {
convertByteArraysToBase64Recursive(tt.input)

if tt.expected == "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this skip of the further checks needed?

name string
input interface{}
expectedFields map[string]interface{}
expectError bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have no test case in your table test where expectError is set to true. Then you don't have to have it here.

I would say either add some test cases which are expected to exit with an error or get rid of this input.

@@ -37,3 +38,166 @@ func TestContainsInt(t *testing.T) {
t.Fatalf("Should not be contained")
}
}

// Test struct for YAML conversion testing
type TestServer struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...or just move the struct definition into the TestConvertByteArraysToBase64 func. You're only using it there if I didn't mess something up here. See the example below for reference.

https://github.com/stackitcloud/stackit-cli/blob/a5438f4cac3a794cb95d04891a83252aa9ae1f1e/internal/pkg/utils/strings_test.go#L10-L13

for fieldName, actualValue := range result {
if _, expected := tt.expectedFields[fieldName]; !expected {
// Allow data field to exist even if not in expectedFields (for empty byte array case)
if fieldName == "data" && tt.name == "empty byte array case" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems weird to me checking for test case names here. Any reason?

t.Run(tt.name, func(t *testing.T) {
result, err := ConvertByteArraysToBase64(tt.input)

if tt.expectError {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if tt.expectError {
if (err != nil) != tt.wantErr {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should handle all cases without needing 3 if conditions 😄

if result != nil {
t.Fatalf("Expected nil result for nil input, got: %v", result)
}
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why stop the test case only because the input is nil?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants