-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
144 additions
and
6 deletions.
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
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,54 @@ | ||
package helpers_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/iomarmochtar/cir-rotator/pkg/helpers" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestByteCountIEC(t *testing.T) { | ||
assert.Equal(t, "984 B", helpers.ByteCountIEC(984), "under 1024") | ||
assert.Equal(t, "505.8 MiB", helpers.ByteCountIEC(530325786), "upper 1024") | ||
} | ||
|
||
func TestSizeUnitStrToFloat(t *testing.T) { | ||
testCase := map[string]struct { | ||
expectedErrMsg string | ||
result float64 | ||
input string | ||
}{ | ||
"unknown pattern": { | ||
input: "1 KB", | ||
expectedErrMsg: "unknown pattern 1 KB", | ||
}, | ||
"invalid number": { | ||
input: "1.32.32 KiB", | ||
expectedErrMsg: "unknown pattern 1.32.32 KiB", | ||
}, | ||
"successfully converted KiB": { | ||
input: "1 KiB", | ||
result: 1024, | ||
}, | ||
"successfully converted MiB": { | ||
input: "1 MiB", | ||
result: 1024 * 1024, | ||
}, | ||
"successfully converted GiB": { | ||
input: "1 GiB", | ||
result: 1024 * 1024 * 1024, | ||
}, | ||
} | ||
|
||
for title, tc := range testCase { | ||
t.Run(title, func(t *testing.T) { | ||
result, err := helpers.SizeUnitStrToFloat(tc.input) | ||
if tc.expectedErrMsg != "" { | ||
assert.EqualError(t, err, tc.expectedErrMsg) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
assert.Equal(t, tc.result, result) | ||
}) | ||
} | ||
} |
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,41 @@ | ||
package helpers_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/iomarmochtar/cir-rotator/pkg/helpers" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsInList(t *testing.T) { | ||
assert.True(t, helpers.IsInList(10, []int{3, 4, 10})) | ||
assert.False(t, helpers.IsInList("adi", []string{"sorry", "not", "found"})) | ||
} | ||
|
||
func TestReadLines(t *testing.T) { | ||
expectedLines := []string{`{"errors":[{"code":"NAME_UNKNOWN","message":"Failed to compute blob liveness for manifest: 'latest'"}]}`} | ||
result, err := helpers.ReadLines("../../testdata/gcr/error_delete_manifest.json") | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedLines, result, "read the contents of file") | ||
|
||
result, err = helpers.ReadLines("/this/path/is/not/found.txt") | ||
assert.True(t, os.IsNotExist(err), "expected error for file not found") | ||
assert.Empty(t, result) | ||
} | ||
|
||
func TestSlachJoin(t *testing.T) { | ||
assert.Equal(t, "hello/world/gogo", helpers.SlashJoin("hello", "world", "gogo")) | ||
} | ||
|
||
func TestCombineMaps(t *testing.T) { | ||
map1 := map[string]string{"name1": "val1", "name2": "val2"} | ||
map2 := map[string]string{"name2": "val3", "name4": "val4"} | ||
expected := map[string]string{"name1": "val1", "name2": "val3", "name4": "val4"} | ||
assert.Equal(t, expected, helpers.CombineMaps(map1, map2)) | ||
} | ||
|
||
func TestFileExist(t *testing.T) { | ||
assert.True(t, helpers.FileExist("../../testdata/gcr/error_delete_manifest.json")) | ||
assert.False(t, helpers.FileExist("/path/not/exists.txt")) | ||
} |
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,29 @@ | ||
package helpers_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/iomarmochtar/cir-rotator/pkg/helpers" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConvertTimeStrToUnix(t *testing.T) { | ||
parsed, err := helpers.ConvertTimeStrToUnix("1585800237411") | ||
assert.NoError(t, err) | ||
assert.Equal(t, time.Date(2020, time.April, 2, 4, 3, 57, 0, time.UTC), parsed) | ||
|
||
parsed, err = helpers.ConvertTimeStrToUnix("unknown") | ||
assert.Error(t, err) | ||
assert.Equal(t, time.Time{}, parsed) | ||
} | ||
|
||
func TestConvertTimeStrToReadAble(t *testing.T) { | ||
parsed, err := helpers.ConvertTimeStrToReadAble("1585800237411") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "2020-04-02T04:03:57Z", parsed, "parsed successfully") | ||
|
||
parsed, err = helpers.ConvertTimeStrToReadAble("2022-06-13") | ||
assert.Error(t, err, "wrong pattern") | ||
assert.Equal(t, "", parsed) | ||
} |
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