Skip to content

Commit

Permalink
Add e2e tests to resolve and docker in ci pipelines. (#140)
Browse files Browse the repository at this point in the history
Additionally, fix some bugs in images with missing dependencies to resolve python manifests
  • Loading branch information
emilwareus authored Oct 31, 2023
1 parent 8729272 commit a0a7184
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 3,697 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ jobs:
path: coverage.html
retention-days: 2

- name: E2E - resolve
if: ${{ matrix.os == 'ubuntu-latest' }}
run: bash scripts/test_e2e.sh resolver

- name: E2E - scan
run: go run cmd/debricked/main.go scan internal/file/testdata/misc -e requirements.txt -t ${{ secrets.DEBRICKED_TOKEN }} -r debricked/cli-test -c E2E-test-${{ github.run_id }}-${{ github.run_number }}-${{ github.run_attempt }}

Expand Down Expand Up @@ -96,6 +100,38 @@ jobs:
- name: Callgraph E2E
run: ./scripts/test_e2e_callgraph_java_version.sh ${{matrix.java}}

docker-resolve:
name: Docker E2E Resolve
runs-on: ubuntu-latest
strategy:
matrix:
docker-os: ['alpine', 'debian']
steps:
- uses: actions/checkout@v3

# Pull from debian and re-tag to match cache
- name: Pull Image; Debian
if: ${{ matrix.docker-os == 'debian' }}
run: docker pull debricked/cli:latest-resolution-debian || true

- name: Pull Image; Alpine
if: ${{ matrix.docker-os == 'alpine' }}
run: docker pull debricked/cli:latest-resolution || true

- name: Build Docker image
if: ${{ matrix.docker-os == 'debian' }}
run: docker build -f build/docker/${{ matrix.docker-os }}.Dockerfile -t debricked/cli:resolution-test --cache-from debricked/cli:latest-resolution-debian --target resolution .

- name: Build Docker image
if: ${{ matrix.docker-os == 'alpine' }}
run: docker build -f build/docker/${{ matrix.docker-os }}.Dockerfile -t debricked/cli:resolution-test --cache-from debricked/cli:latest-resolution --target resolution .

- name: Resolve with Docker
run: docker run -v $(pwd):/root debricked/cli:resolution-test debricked resolve test/resolve

- name: Test if resolved
run: bash test/checkFilesResolved.sh

golangci:
name: Lint
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ test/resolve/testdata/nuget/obj
test/resolve/testdata/nuget/**/obj
debricked.fingerprints.wfp
test/resolve/testdata/gomod/gomod.debricked.lock
test/resolve/testdata/maven/maven.debricked.lock
test/resolve/testdata/gradle/*/**
**.gradle-init-script.debricked.groovy
test/resolve/testdata/gradle/gradle.debricked.lock
/mvnproj/target
4 changes: 3 additions & 1 deletion build/docker/alpine.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ RUN wget https://services.gradle.org/distributions/gradle-$GRADLE_VERSION-bin.zi
unzip gradle-$GRADLE_VERSION-bin.zip -d $GRADLE_HOME && \
rm gradle-$GRADLE_VERSION-bin.zip

# g++ needed to compile python packages with C dependencies (numpy, scipy, etc.)
RUN apk --no-cache --update add \
openjdk11-jre \
python3 \
Expand All @@ -44,7 +45,8 @@ RUN apk --no-cache --update add \
go~=1.20 \
nodejs \
yarn \
dotnet7-sdk
dotnet7-sdk \
g++

RUN dotnet --version

Expand Down
3 changes: 2 additions & 1 deletion build/docker/debian.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ RUN echo "deb http://ftp.us.debian.org/debian testing-updates main" >> /etc/apt/

RUN apt -y update && apt -y upgrade && apt -y install openjdk-11-jre \
python3 \
python3-scipy \
python3-venv \
ca-certificates \
python3-pip && \
apt -y install -t testing golang-1.20 && \
apt -y clean && rm -rf /var/lib/apt/lists/* && \
# Symlink pip3 to pip, we assume that "pip" works in CLI
ln -sf /usr/bin/pip3 /usr/bin/pip && \
ln -sf /usr/bin/python3 /usr/bin/python && \
# Symlink go binary to bin directory which is in path
ln -s /usr/lib/go-1.20/bin/go /usr/bin/go

Expand Down
29 changes: 29 additions & 0 deletions test/checkFilesResolved.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
GREEN='\033[0;32m'
RED='\033[0;31m'

# This script checks if all files in the current directory exist
function check_files_exist() {
isOk=true
for file in "$@"; do
if test -f "$file"; then
echo -e "${GREEN}File $file OK${GREEN}"
else
echo -e "${RED}File $file does not exist!${RED}"
isOk=false
fi
done

if [ "$isOk" = false ]; then
exit 1
fi

}

check_files_exist "test/resolve/testdata/npm/yarn.lock" \
"test/resolve/testdata/pip/requirements.txt.pip.debricked.lock" \
"test/resolve/testdata/nuget/packagesconfig/packages.config.nuget.debricked.lock" \
"test/resolve/testdata/nuget/csproj/packages.lock.json" \
"test/resolve/testdata/gradle/gradle.debricked.lock" \
"test/resolve/testdata/maven/maven.debricked.lock" \
"test/resolve/testdata/gomod/gomod.debricked.lock"
46 changes: 13 additions & 33 deletions test/resolve/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,62 @@ package resolve
import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/debricked/cli/internal/cmd/resolve"
"github.com/debricked/cli/internal/wire"
"github.com/stretchr/testify/assert"
)

func removeLines(input, prefix string) string {
lines := strings.Split(input, "\n")
var result []string

for _, line := range lines {
if !strings.HasPrefix(line, prefix) {
result = append(result, line)
}
}

return strings.Join(result, "\n")
}

func TestResolves(t *testing.T) {
cases := []struct {
name string
manifestFile string
lockFileName string
expectedFile string
packageManager string
}{
{
name: "basic package.json",
manifestFile: "testdata/npm/package.json",
lockFileName: "yarn.lock",
expectedFile: "testdata/npm/yarn-expected.lock",
packageManager: "npm",
},
{
name: "basic requirements.txt",
manifestFile: "testdata/pip/requirements.txt",
lockFileName: "requirements.txt.pip.debricked.lock",
expectedFile: "testdata/pip/expected.lock",
packageManager: "pip",
},
{
name: "basic .csproj",
manifestFile: "testdata/nuget/csproj/basic.csproj",
lockFileName: "packages.lock.json",
expectedFile: "testdata/nuget/csproj/packages-expected.lock.json",
packageManager: "nuget",
},
{
name: "basic packages.config",
manifestFile: "testdata/nuget/packagesconfig/packages.config",
lockFileName: "packages.config.nuget.debricked.lock",
expectedFile: "testdata/nuget/packagesconfig/packages.config.expected.lock",
packageManager: "nuget",
},
{
name: "basic go.mod",
manifestFile: "testdata/gomod/go.mod",
lockFileName: "gomod.debricked.lock",
expectedFile: "testdata/gomod/expected.lock",
packageManager: "gomod",
},
{
name: "basic pom.xml",
manifestFile: "testdata/maven/pom.xml",
lockFileName: "maven.debricked.lock",
packageManager: "maven",
},
{
name: "basic build.gradle",
manifestFile: "testdata/gradle/build.gradle",
lockFileName: "gradle.debricked.lock",
packageManager: "gradle",
},
}

for _, cT := range cases {
Expand All @@ -84,22 +76,10 @@ func TestResolves(t *testing.T) {
lockFileContents, fileErr := os.ReadFile(lockFile)
assert.NoError(t, fileErr)

expectedFileContents, fileErr := os.ReadFile(c.expectedFile)
assert.NoError(t, fileErr)
expectedString := string(expectedFileContents)
actualString := string(lockFileContents)

if c.packageManager == "pip" {
// Remove locations as it is dependent on the machine
expectedString = removeLines(expectedString, "Location: ")
actualString = removeLines(actualString, "Location: ")
assert.Greater(t, len(actualString), 0)

} else if c.packageManager == "nuget" {
// Remove hashes as that is different on different OS
expectedString = removeLines(expectedString, " \"contentHash\":")
actualString = removeLines(actualString, " \"contentHash\":")
}
assert.Equal(t, expectedString, actualString)
})
}
}
Loading

0 comments on commit a0a7184

Please sign in to comment.