-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tests * commit * change main * update docker version * Fixed a flaky test, added more logger tests, fix typos * Add file extractors tests --------- Co-authored-by: Shaked Karta <[email protected]>
- Loading branch information
1 parent
ccc3bda
commit f298e52
Showing
39 changed files
with
2,015 additions
and
70 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
File renamed without changes.
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,126 @@ | ||
package extractors | ||
|
||
import ( | ||
"github.com/CheckmarxDev/containers-resolver/internal/logger" | ||
"github.com/CheckmarxDev/containers-resolver/internal/types" | ||
"testing" | ||
) | ||
|
||
func TestExtractImagesFromDockerComposeFiles(t *testing.T) { | ||
l := logger.NewLogger(false) | ||
|
||
filePaths := []types.FilePath{ | ||
{FullPath: "../../test_files/imageExtraction/dockerCompose/docker-compose.yaml", RelativePath: "docker-compose.yaml"}, | ||
{FullPath: "../../test_files/imageExtraction/dockerCompose/docker-compose-2.yaml", RelativePath: "docker-compose-2.yaml"}, | ||
} | ||
|
||
images, err := ExtractImagesFromDockerComposeFiles(l, filePaths) | ||
if err != nil { | ||
t.Errorf("Error extracting images: %v", err) | ||
} | ||
|
||
expectedImages := map[string]types.ImageLocation{ | ||
"postgres:12.0": {Origin: types.DockerComposeFileOrigin, Path: "docker-compose-2.yaml"}, | ||
"minio/minio:RELEASE.2020-06-22T03-12-50Z": {Origin: types.DockerComposeFileOrigin, Path: "docker-compose-2.yaml"}, | ||
"redis:6.0.10-alpine": {Origin: types.DockerComposeFileOrigin, Path: "docker-compose-2.yaml"}, | ||
"buildimage:latest": {Origin: types.DockerComposeFileOrigin, Path: "docker-compose.yaml"}, | ||
} | ||
|
||
if len(images) != len(expectedImages) { | ||
t.Errorf("Expected %d images, but got %d", len(expectedImages), len(images)) | ||
} | ||
|
||
for _, image := range images { | ||
expectedLocation, ok := expectedImages[image.Name] | ||
if !ok { | ||
t.Errorf("Unexpected image found: %s", image.Name) | ||
continue | ||
} | ||
|
||
if len(image.ImageLocations) != 1 { | ||
t.Errorf("Expected image %s to have exactly one location, but got %d", image.Name, len(image.ImageLocations)) | ||
continue | ||
} | ||
|
||
if image.ImageLocations[0].Path != expectedLocation.Path { | ||
t.Errorf("Expected image %s to have path %s, but got %s", image.Name, expectedLocation.Path, image.ImageLocations[0].Path) | ||
} | ||
|
||
if image.ImageLocations[0].Origin != expectedLocation.Origin { | ||
t.Errorf("Expected image %s to have origin %s, but got %s", image.Name, expectedLocation.Origin, image.ImageLocations[0].Origin) | ||
} | ||
} | ||
} | ||
|
||
func TestExtractImagesFromDockerComposeFile(t *testing.T) { | ||
l := logger.NewLogger(false) | ||
filePath := types.FilePath{FullPath: "../../test_files/imageExtraction/dockerCompose/docker-compose-2.yaml", RelativePath: "docker-compose-2.yaml"} | ||
|
||
images, err := extractImagesFromDockerComposeFile(l, filePath) | ||
if err != nil { | ||
t.Errorf("Error extracting images: %v", err) | ||
} | ||
|
||
expectedImages := map[string]types.ImageLocation{ | ||
"postgres:12.0": {Origin: types.DockerComposeFileOrigin, Path: "docker-compose-2.yaml"}, | ||
"minio/minio:RELEASE.2020-06-22T03-12-50Z": {Origin: types.DockerComposeFileOrigin, Path: "docker-compose-2.yaml"}, | ||
"redis:6.0.10-alpine": {Origin: types.DockerComposeFileOrigin, Path: "docker-compose-2.yaml"}, | ||
} | ||
|
||
if len(images) != len(expectedImages) { | ||
t.Errorf("Expected %d images, but got %d", len(expectedImages), len(images)) | ||
} | ||
|
||
for _, image := range images { | ||
expectedLocation, ok := expectedImages[image.Name] | ||
if !ok { | ||
t.Errorf("Unexpected image found: %s", image.Name) | ||
continue | ||
} | ||
|
||
if len(image.ImageLocations) != 1 { | ||
t.Errorf("Expected image %s to have exactly one location, but got %d", image.Name, len(image.ImageLocations)) | ||
continue | ||
} | ||
|
||
if image.ImageLocations[0].Path != expectedLocation.Path { | ||
t.Errorf("Expected image %s to have path %s, but got %s", image.Name, expectedLocation.Path, image.ImageLocations[0].Path) | ||
} | ||
|
||
if image.ImageLocations[0].Origin != expectedLocation.Origin { | ||
t.Errorf("Expected image %s to have origin %s, but got %s", image.Name, expectedLocation.Origin, image.ImageLocations[0].Origin) | ||
} | ||
} | ||
} | ||
|
||
func TestExtractImagesFromDockerComposeFiles_NoFilesFound(t *testing.T) { | ||
l := logger.NewLogger(false) | ||
|
||
filePaths := []types.FilePath{} // No files provided | ||
|
||
images, err := ExtractImagesFromDockerComposeFiles(l, filePaths) | ||
if err != nil { | ||
t.Errorf("Error extracting images: %v", err) | ||
} | ||
|
||
if len(images) != 0 { | ||
t.Errorf("Expected 0 images, but got %d", len(images)) | ||
} | ||
} | ||
|
||
func TestExtractImagesFromDockerComposeFiles_NoImagesFound(t *testing.T) { | ||
l := logger.NewLogger(false) | ||
|
||
filePaths := []types.FilePath{ | ||
{FullPath: "../../test_files/imageExtraction/dockerCompose/docker-compose-5.yaml", RelativePath: "docker-compose-5.yaml"}, // Empty Docker Compose file | ||
} | ||
|
||
images, err := ExtractImagesFromDockerComposeFiles(l, filePaths) | ||
if err != nil { | ||
t.Errorf("Error extracting images: %v", err) | ||
} | ||
|
||
if len(images) != 0 { | ||
t.Errorf("Expected 0 images, but got %d", len(images)) | ||
} | ||
} |
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,110 @@ | ||
package extractors | ||
|
||
import ( | ||
"github.com/CheckmarxDev/containers-resolver/internal/logger" | ||
"github.com/CheckmarxDev/containers-resolver/internal/types" | ||
"testing" | ||
) | ||
|
||
func TestExtractImagesFromDockerfiles(t *testing.T) { | ||
l := logger.NewLogger(false) | ||
|
||
filePaths := []types.FilePath{ | ||
{FullPath: "../../test_files/imageExtraction/dockerfiles/Dockerfile", RelativePath: "Dockerfile"}, | ||
{FullPath: "../../test_files/imageExtraction/dockerfiles/Dockerfile-2", RelativePath: "Dockerfile-2"}, | ||
} | ||
|
||
images, err := ExtractImagesFromDockerfiles(l, filePaths) | ||
if err != nil { | ||
t.Errorf("Error extracting images: %v", err) | ||
} | ||
|
||
expectedImages := map[string]types.ImageLocation{ | ||
"mcr.microsoft.com/dotnet/sdk:6.0": {Origin: types.DockerFileOrigin, Path: "Dockerfile"}, | ||
"mcr.microsoft.com/dotnet/aspnet:6.0": {Origin: types.DockerFileOrigin, Path: "Dockerfile"}, | ||
"nginx:latest": {Origin: types.DockerFileOrigin, Path: "Dockerfile-2"}, | ||
"mcr.microsoft.com/dotnet/aspnet:4.0": {Origin: types.DockerFileOrigin, Path: "Dockerfile-2"}, | ||
} | ||
|
||
checkResult(t, images, expectedImages) | ||
} | ||
|
||
func TestExtractImagesFromDockerfiles_NoFilesFound(t *testing.T) { | ||
l := logger.NewLogger(false) | ||
|
||
filePaths := []types.FilePath{} // No files provided | ||
|
||
images, err := ExtractImagesFromDockerfiles(l, filePaths) | ||
if err != nil { | ||
t.Errorf("Error extracting images: %v", err) | ||
} | ||
|
||
if len(images) != 0 { | ||
t.Errorf("Expected 0 images, but got %d", len(images)) | ||
} | ||
} | ||
|
||
func TestExtractImagesFromDockerfiles_NoImagesFound(t *testing.T) { | ||
l := logger.NewLogger(false) | ||
|
||
filePaths := []types.FilePath{ | ||
{FullPath: "../../test_files/imageExtraction/dockerfiles/Dockerfile-3", RelativePath: "Dockerfile-3"}, // Empty Dockerfile | ||
} | ||
|
||
images, err := ExtractImagesFromDockerfiles(l, filePaths) | ||
if err != nil { | ||
t.Errorf("Error extracting images: %v", err) | ||
} | ||
|
||
if len(images) != 0 { | ||
t.Errorf("Expected 0 images, but got %d", len(images)) | ||
} | ||
} | ||
|
||
func TestExtractImagesFromDockerfiles_OneValidOneInvalid(t *testing.T) { | ||
l := logger.NewLogger(false) | ||
|
||
filePaths := []types.FilePath{ | ||
{FullPath: "../../test_files/imageExtraction/dockerfiles/Dockerfile", RelativePath: "Dockerfile"}, | ||
{FullPath: "../../test_files/imageExtraction/dockerfiles/InvalidDockerfile", RelativePath: "InvalidDockerfile"}, | ||
} | ||
|
||
images, err := ExtractImagesFromDockerfiles(l, filePaths) | ||
if err != nil { | ||
t.Errorf("Error extracting images: %v", err) | ||
} | ||
|
||
expectedImages := map[string]types.ImageLocation{ | ||
"mcr.microsoft.com/dotnet/sdk:6.0": {Origin: types.DockerFileOrigin, Path: "Dockerfile"}, | ||
"mcr.microsoft.com/dotnet/aspnet:6.0": {Origin: types.DockerFileOrigin, Path: "Dockerfile"}, | ||
} | ||
|
||
checkResult(t, images, expectedImages) | ||
} | ||
|
||
func checkResult(t *testing.T, images []types.ImageModel, expectedImages map[string]types.ImageLocation) { | ||
if len(images) != len(expectedImages) { | ||
t.Errorf("Expected %d images, but got %d", len(expectedImages), len(images)) | ||
} | ||
|
||
for _, image := range images { | ||
expectedLocation, ok := expectedImages[image.Name] | ||
if !ok { | ||
t.Errorf("Unexpected image found: %s", image.Name) | ||
continue | ||
} | ||
|
||
if len(image.ImageLocations) != 1 { | ||
t.Errorf("Expected image %s to have exactly one location, but got %d", image.Name, len(image.ImageLocations)) | ||
continue | ||
} | ||
|
||
if image.ImageLocations[0].Path != expectedLocation.Path { | ||
t.Errorf("Expected image %s to have path %s, but got %s", image.Name, expectedLocation.Path, image.ImageLocations[0].Path) | ||
} | ||
|
||
if image.ImageLocations[0].Origin != expectedLocation.Origin { | ||
t.Errorf("Expected image %s to have origin %s, but got %s", image.Name, expectedLocation.Origin, image.ImageLocations[0].Origin) | ||
} | ||
} | ||
} |
Oops, something went wrong.