-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid allocating empty strings in the Env slice
Previously, we declared the Env slice with the size of the `c.Services.KubeAPI.ExtraEnv` field, which leads to empty strings at the beginning of the Env slice because we use Golang's append function to add new elements, and Docker is not happy with that. We fix this issue by declaring the Env variable as an empty slice. We also enhance the `getUniqStringList` function to properly trim leading and trailing spaces in each element and to ignore empty strings. We add unit tests for the `getUniqStringList` function and update the integration tests.
- Loading branch information
Showing
3 changed files
with
76 additions
and
1 deletion.
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
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,52 @@ | ||
package cluster | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_getUniqStringList(t *testing.T) { | ||
type args struct { | ||
l []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []string | ||
}{ | ||
{ | ||
"contain strings with only spaces", | ||
args{ | ||
[]string{" ", "key1=value1", " ", "key2=value2"}, | ||
}, | ||
[]string{"key1=value1", "key2=value2"}, | ||
}, | ||
{ | ||
"contain strings with trailing or leading spaces", | ||
args{ | ||
[]string{" key1=value1", "key1=value1 ", " key2=value2 "}, | ||
}, | ||
[]string{"key1=value1", "key2=value2"}, | ||
}, | ||
{ | ||
"contain duplicated strings", | ||
args{ | ||
[]string{"", "key1=value1", "key1=value1", "key2=value2"}, | ||
}, | ||
[]string{"key1=value1", "key2=value2"}, | ||
}, | ||
{ | ||
"contain empty string", | ||
args{ | ||
[]string{"", "key1=value1", "", "key2=value2"}, | ||
}, | ||
[]string{"key1=value1", "key2=value2"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.want, getUniqStringList(tt.args.l), "getUniqStringList(%v)", tt.args.l) | ||
}) | ||
} | ||
} |
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