Skip to content

Commit

Permalink
test: add more UTs
Browse files Browse the repository at this point in the history
Signed-off-by: Ramkumar Chinchani <rchincha@cisco.com>
  • Loading branch information
rchincha committed Feb 15, 2023
1 parent 89709e6 commit 42dbedb
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
74 changes: 74 additions & 0 deletions pkg/lib/dir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package lib_test

import (
"os"
"path"
"testing"

. "github.com/smartystreets/goconvey/convey"
"stackerbuild.io/stacker/pkg/lib"
)

func TestDir(t *testing.T) {
Convey("IsSymLink", t, func() {
src, err := os.CreateTemp("", "src")
So(err, ShouldBeNil)
So(src, ShouldNotBeNil)
defer os.Remove(src.Name())

_, err = src.Write([]byte("hello world!"))
So(err, ShouldBeNil)

ok := lib.IsSymlink(src.Name())
So(ok, ShouldBeFalse)
})

Convey("DirCopy", t, func() {
src, err := os.CreateTemp("", "src")
So(err, ShouldBeNil)
So(src, ShouldNotBeNil)
defer os.Remove(src.Name())

_, err = src.Write([]byte("hello world!"))
So(err, ShouldBeNil)

dest, err := os.MkdirTemp("", "dest")
So(err, ShouldBeNil)
So(dest, ShouldNotBeNil)
defer os.Remove(dest)

err = lib.DirCopy(path.Dir(src.Name()), dest)
So(err, ShouldBeNil)
})

Convey("CopyThing", t, func() {
src, err := os.CreateTemp("", "src")
So(err, ShouldBeNil)
So(src, ShouldNotBeNil)
defer os.Remove(src.Name())

_, err = src.Write([]byte("hello world!"))
So(err, ShouldBeNil)

dest, err := os.CreateTemp("", "dest")
So(err, ShouldBeNil)
So(dest, ShouldNotBeNil)
defer os.Remove(dest.Name())

err = lib.CopyThing(src.Name(), dest.Name())
So(err, ShouldBeNil)
})

Convey("Chmod", t, func() {
src, err := os.CreateTemp("", "src")
So(err, ShouldBeNil)
So(src, ShouldNotBeNil)
defer os.Remove(src.Name())

_, err = src.Write([]byte("hello world!"))
So(err, ShouldBeNil)

err = lib.Chmod("644", src.Name())
So(err, ShouldBeNil)
})
}
47 changes: 47 additions & 0 deletions pkg/lib/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package lib_test

import (
"io/fs"
"os"
"testing"

. "github.com/smartystreets/goconvey/convey"
"stackerbuild.io/stacker/pkg/lib"
)

func TestFile(t *testing.T) {
Convey("FileCopy", t, func() {
src, err := os.CreateTemp("", "src")
So(err, ShouldBeNil)
So(src, ShouldNotBeNil)
defer os.Remove(src.Name())

_, err = src.Write([]byte("hello world!"))
So(err, ShouldBeNil)

Convey("With defaults", func() {
dest, err := os.CreateTemp("", "dest")
So(err, ShouldBeNil)
defer os.Remove(dest.Name())

err = lib.FileCopy(dest.Name(), src.Name(), nil, -1, -1)
So(err, ShouldBeNil)
})

Convey("With non-default mode", func() {
dest, err := os.CreateTemp("", "dest")
So(err, ShouldBeNil)
defer os.Remove(dest.Name())

mode := fs.FileMode(0644)
err = lib.FileCopy(dest.Name(), src.Name(), &mode, -1, -1)
So(err, ShouldBeNil)
})
})

Convey("FindFiles", t, func() {
files, err := lib.FindFiles("/tmp", ".*")
So(err, ShouldBeNil)
So(files, ShouldNotBeEmpty)
})
}
12 changes: 12 additions & 0 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func TestLog(t *testing.T) {
So(func() { log.Debugf("debug msg") }, ShouldNotPanic)
So(func() { log.Infof("info msg") }, ShouldNotPanic)
So(func() { log.Errorf("error msg") }, ShouldNotPanic)

So(func() { log.FilterNonStackerLogs(handler, 1) }, ShouldNotPanic)

So(func() { log.Debugf("debug msg") }, ShouldNotPanic)
So(func() { log.Infof("info msg") }, ShouldNotPanic)
So(func() { log.Errorf("error msg") }, ShouldNotPanic)
})

Convey("Without timestamps", t, func() {
Expand All @@ -26,5 +32,11 @@ func TestLog(t *testing.T) {
So(func() { log.Debugf("debug msg") }, ShouldNotPanic)
So(func() { log.Infof("info msg") }, ShouldNotPanic)
So(func() { log.Errorf("error msg") }, ShouldNotPanic)

So(func() { log.FilterNonStackerLogs(handler, 1) }, ShouldNotPanic)

So(func() { log.Debugf("debug msg") }, ShouldNotPanic)
So(func() { log.Infof("info msg") }, ShouldNotPanic)
So(func() { log.Errorf("error msg") }, ShouldNotPanic)
})
}

0 comments on commit 42dbedb

Please sign in to comment.