diff --git a/README.md b/README.md index da5c903..192c4f3 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ So this tools can help you create a rotation mechanism for it, by using the powe ``` ./cir-rotator delete --service-account sa.json -ho asia.gcr.io/parent-repo \ --if "Now() - UploadedAt >= Duration('6M') and ImageSize >= SizeStr('100 MiB')" \ - --ef "Repository matches '.*base-image$' and 'latest' in Tag" \ + --ef "Repository matches '.*base-image$' and 'latest' in Tags" \ --ef "Repository matches '.*internal-tools.*'" ``` diff --git a/app/cmd/cmd_test.go b/app/cmd/cmd_test.go index 44eb629..66580c5 100644 --- a/app/cmd/cmd_test.go +++ b/app/cmd/cmd_test.go @@ -1,6 +1,7 @@ package cmd_test import ( + "bytes" "flag" "fmt" "io/ioutil" @@ -11,6 +12,7 @@ import ( "strings" "testing" + "github.com/iomarmochtar/cir-rotator/app/cmd" h "github.com/iomarmochtar/cir-rotator/pkg/helpers" "github.com/stretchr/testify/assert" cli "github.com/urfave/cli/v2" @@ -122,3 +124,15 @@ func runCmdTestCases(name string, command *cli.Command, testCases map[string]cas }) } } + +func TestNew(t *testing.T) { + // get version + buf := new(bytes.Buffer) + app := cmd.New() + app.Writer = buf + err := app.Run([]string{"cir-rotator", "--version"}) + output := buf.String() + + assert.NoError(t, err) + assert.Equal(t, fmt.Sprintf("cir-rotator version %s\n", app.Version), output) +} diff --git a/pkg/helpers/bytes_test.go b/pkg/helpers/bytes_test.go new file mode 100644 index 0000000..58444c0 --- /dev/null +++ b/pkg/helpers/bytes_test.go @@ -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) + }) + } +} diff --git a/pkg/helpers/helpers_test.go b/pkg/helpers/helpers_test.go new file mode 100644 index 0000000..be4c84c --- /dev/null +++ b/pkg/helpers/helpers_test.go @@ -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")) +} diff --git a/pkg/helpers/time.go b/pkg/helpers/time.go index 427c053..d921dfd 100644 --- a/pkg/helpers/time.go +++ b/pkg/helpers/time.go @@ -14,7 +14,7 @@ func ConvertTimeStrToUnix(timeStr string) (time.Time, error) { if err != nil { return time.Time{}, err } - return time.Unix(parsed/unitMilliSecond, 0), nil + return time.Unix(parsed/unitMilliSecond, 0).UTC(), nil } func ConvertTimeStrToReadAble(timeStr string) (string, error) { diff --git a/pkg/helpers/time_test.go b/pkg/helpers/time_test.go new file mode 100644 index 0000000..917b532 --- /dev/null +++ b/pkg/helpers/time_test.go @@ -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) +} diff --git a/pkg/registry/gcr_test.go b/pkg/registry/gcr_test.go index a435e67..47ab9bd 100644 --- a/pkg/registry/gcr_test.go +++ b/pkg/registry/gcr_test.go @@ -186,8 +186,8 @@ func TestGCR_Catalog(t *testing.T) { Name: "sha256:C05ce64163cd2327d364933df75aa4850af425b6cbaec2f6af3b31e5246be0e2", ImageSizeBytes: 530325786, Tag: []string{"latest", "abc"}, - Created: time.Unix(1585800237411/1000, 0), - Uploaded: time.Unix(1585800278141/1000, 0), + Created: time.Unix(1585800237411/1000, 0).UTC(), + Uploaded: time.Unix(1585800278141/1000, 0).UTC(), }, }, }, @@ -198,8 +198,8 @@ func TestGCR_Catalog(t *testing.T) { Name: "sha256:02123554c8d65d241a77dd8e238403ba8cc697afca7208bbe7564b634aa22fee", ImageSizeBytes: 365557176, Tag: []string{"latest", "release-20210624-150000"}, - Created: time.Unix(1624518709150/1000, 0), - Uploaded: time.Unix(1624518770462/1000, 0), + Created: time.Unix(1624518709150/1000, 0).UTC(), + Uploaded: time.Unix(1624518770462/1000, 0).UTC(), }, }, },