Skip to content

Commit

Permalink
More coverage (#4)
Browse files Browse the repository at this point in the history
* typo

* more unit test

* change to utc
  • Loading branch information
iomarmochtar authored Jul 10, 2022
1 parent 8ba5dc1 commit 015a824
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.*'"
```

Expand Down
14 changes: 14 additions & 0 deletions app/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd_test

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}
54 changes: 54 additions & 0 deletions pkg/helpers/bytes_test.go
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)
})
}
}
41 changes: 41 additions & 0 deletions pkg/helpers/helpers_test.go
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"))
}
2 changes: 1 addition & 1 deletion pkg/helpers/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions pkg/helpers/time_test.go
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)
}
8 changes: 4 additions & 4 deletions pkg/registry/gcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
},
},
Expand All @@ -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(),
},
},
},
Expand Down

0 comments on commit 015a824

Please sign in to comment.