This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
520 additions
and
324 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
var testLogFile = path.Join(os.TempDir(), "gammaray-log-test") | ||
|
||
func cleanLogs() { | ||
os.Remove(testLogFile) | ||
} | ||
|
||
func TestHelloWorld(t *testing.T) { | ||
defer cleanLogs() | ||
err := (&Args{ | ||
Image: "", | ||
LogFile: testLogFile, | ||
Path: "test_data/hello-world", | ||
}).Run() | ||
if err != nil { | ||
panic(err) | ||
} | ||
info, error := os.Stat(testLogFile) | ||
if error != nil { | ||
t.Errorf("TestHelloWorld: Could not stat log file <%s>:\n%s", testLogFile, error) | ||
} | ||
log.Println("TestHelloWorld: log fileinfo:\n", info) | ||
|
||
if info.Size() == 0 { | ||
t.Errorf("TestHelloWorld: log size should not be 0!") | ||
} | ||
|
||
} | ||
|
||
func TestInsecureProject(t *testing.T) { | ||
defer cleanLogs() | ||
err := (&Args{ | ||
Image: "", | ||
LogFile: testLogFile, | ||
Path: "test_data/insecure-project", | ||
}).Run() | ||
if err != nil { | ||
panic(err) | ||
} | ||
info, error := os.Stat(testLogFile) | ||
if error != nil { | ||
t.Errorf("TestInsecureProject: Could not stat log file <%s>:\n%s", testLogFile, error) | ||
} | ||
log.Println("TestInsecureProject: log fileinfo:\n", info) | ||
|
||
if info.Size() == 0 { | ||
t.Errorf("TestInsecureProject: log size should not be 0!") | ||
} | ||
|
||
} | ||
|
||
func TestPathDoesNotExist(t *testing.T) { | ||
defer cleanLogs() | ||
err := (&Args{ | ||
Image: "", | ||
LogFile: testLogFile, | ||
Path: "./does-not-exist", | ||
}).Run() | ||
|
||
if err == nil { | ||
t.Errorf("TestPathDoesNotExist: there should be an error when trying to analyze ./does-not-exist") | ||
} | ||
|
||
if diff := cmp.Diff(err.Error(), "stat ./does-not-exist: no such file or directory"); diff != "" { | ||
t.Errorf("TestPathDoesNotExist: error : (-got +want)\n%s", diff) | ||
} | ||
} | ||
|
||
func TestPathIsEmpty(t *testing.T) { | ||
defer cleanLogs() | ||
|
||
os.Args = []string{""} | ||
|
||
err := (&Args{ | ||
Image: "", | ||
LogFile: testLogFile, | ||
Path: "", | ||
}).Run() | ||
|
||
if err == nil { | ||
t.Errorf("TestPathIsEmpty: there should be an error when trying to analyze an empty path/empty image") | ||
} | ||
|
||
if diff := cmp.Diff(err.Error(), "you need to at least properly define a path or a docker image"); diff != "" { | ||
t.Errorf("TestPathIsEmpty: error : (-got +want)\n%s", diff) | ||
} | ||
} | ||
|
||
func TestImageHelloWorld(t *testing.T) { | ||
defer cleanLogs() | ||
err := (&Args{ | ||
Image: "gammaray-test-hello-world:1.0.0", | ||
LogFile: testLogFile, | ||
Path: "", | ||
}).Run() | ||
if err != nil { | ||
panic(err) | ||
} | ||
info, error := os.Stat(testLogFile) | ||
if error != nil { | ||
t.Errorf("TestImageHelloWorld: Could not stat log file <%s>:\n%s", testLogFile, error) | ||
} | ||
log.Println("TestImageHelloWorld: log fileinfo:\n", info) | ||
|
||
if info.Size() == 0 { | ||
t.Errorf("TestImageHelloWorld: log size should not be 0!") | ||
} | ||
|
||
} | ||
|
||
func TestDefaults(t *testing.T) { | ||
expected := Args{ | ||
Path: "", | ||
Image: "", | ||
LogFile: ".gammaray.log", | ||
} | ||
|
||
if diff := cmp.Diff(*Defaults(), expected); diff != "" { | ||
t.Errorf("TestDefaults: error : (-got +want)\n%s", diff) | ||
} | ||
} | ||
|
||
func TestMainHelloWorld(t *testing.T) { | ||
defer cleanLogs() | ||
|
||
os.Args = []string{"./gammaray", "-path", "test_data/hello-world", "-log-file", testLogFile} | ||
|
||
main() | ||
|
||
info, error := os.Stat(testLogFile) | ||
if error != nil { | ||
t.Errorf("TestMainHelloWorld: Could not stat log file <%s>:\n%s", testLogFile, error) | ||
} | ||
log.Println("TestMainHelloWorld: log fileinfo:\n", info) | ||
|
||
if info.Size() == 0 { | ||
t.Errorf("TestMainHelloWorld: log size should not be 0!") | ||
} | ||
} |
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
Oops, something went wrong.