Skip to content

Commit

Permalink
feat(AnalyzerTest) canonical reference for slimming docker test imag…
Browse files Browse the repository at this point in the history
…e and native analyzer testing (#271)
  • Loading branch information
microsoftly authored Sep 26, 2018
1 parent 67c2ff1 commit c90d051
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 16 deletions.
8 changes: 6 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ jobs:
command: |
# Load shell helpers (e.g. sdkman)
source /home/fossa/.bashrc
# Run tests
# ./test.sh > $ARTIFACTS/integration-test-stdout 2> >(tee $ARTIFACTS/integration-test-stderr >&2)
# Run native integration tests
# as long as -short is not being called, integration tests get run
go test ./... -v
#Run shell based tests
# these tests were inherently broken. Keep them for refernce, but comment out their call to speed up move to native
#./test.sh > $ARTIFACTS/integration-test-stdout 2> >(tee $ARTIFACTS/integration-test-stderr >&2)
- store_test_results:
path: /tmp/test-results
- store_artifacts:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ unit-test:

.PHONY: junit-test
junit-test: $(GO_JUNIT_REPORT) $(GOVERALLS)
goveralls -v -service=circle-ci -repotoken=$(COVERALLS_TOKEN) | go-junit-report
goveralls -v -service=circle-ci -repotoken=$(COVERALLS_TOKEN) -flags "-short" | go-junit-report

.PHONY: integration-test
integration-test: docker-test
Expand Down
211 changes: 211 additions & 0 deletions analyzers/nodejs/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package nodejs_test

import (
"flag"
"os"
"path/filepath"
"sync"
"testing"

"github.com/stretchr/testify/assert"

"github.com/fossas/fossa-cli/analyzers"
"github.com/fossas/fossa-cli/exec"
"github.com/fossas/fossa-cli/files"
"github.com/fossas/fossa-cli/module"
"github.com/fossas/fossa-cli/pkg"
"github.com/fossas/fossa-cli/testing/fixtures"
"github.com/fossas/fossa-cli/testing/runfossa"
)

var nodeAnalyzerFixtureDir = filepath.Join(fixtures.Directory(), "nodejs", "analyzer")

func TestMain(m *testing.M) {
// flags are not parsed at this point. In order to have testing.Short() read actually provided values, this must be executed
flag.Parse()
if testing.Short() {
return
}

err := fixtures.Clone(nodeAnalyzerFixtureDir, projects)
if err != nil {
panic(err)
}

err = initializeProjects(nodeAnalyzerFixtureDir)
if err != nil {
panic(err)
}

exitCode := m.Run()
defer os.Exit(exitCode)
// uncomment this if you need test files cleaned up locally
// defer cleanUp(nodeAnalyzerFixtureDir)
}

// While not testing the core functionality, this ensures that the tests have been setup correctly as needed for a prereq to run the analyzer steps
// This test itself does not incur any overhead.
func TestTestSetup(t *testing.T) {
t.Parallel()
assertProjectFixtureExists(t, "puppeteer")
// faker has no deps
// assertProjectFixtureExists(t, "fakerjs")
assertProjectFixtureExists(t, "fastify")
assertProjectFixtureExists(t, "nest")
assertProjectFixtureExists(t, "ohm")
assertProjectFixtureExists(t, "express")
assertProjectFixtureExists(t, "standard")
assertProjectFixtureExists(t, "sodium-encryption")
assertProjectFixtureExists(t, "request")
}

func TestAnalysisOutput(t *testing.T) {
t.Parallel()
for _, proj := range projects {
t.Run(proj.Name, func(t *testing.T) {
module := module.Module{
Dir: filepath.Join(nodeAnalyzerFixtureDir, proj.Name),
Type: pkg.NodeJS,
Name: proj.Name,
Options: map[string]interface{}{"allow-npm-err": proj.Name == "standard"},
BuildTarget: filepath.Join(nodeAnalyzerFixtureDir, proj.Name, "package.json"),
}

analyzer, err := analyzers.New(module)
assert.NoError(t, err)

deps, err := analyzer.Analyze()
assert.NoError(t, err)
// faker has no deps
if proj.Name == "fakerjs" {
assert.Empty(t, deps.Direct)
assert.Empty(t, deps.Transitive)
} else {
assert.NotEmpty(t, deps.Direct)
assert.NotEmpty(t, deps.Transitive)
}
})
}
}

func assertProjectFixtureExists(t *testing.T, name string) {
exists, err := files.ExistsFolder(nodeAnalyzerFixtureDir, name)
assert.NoError(t, err)
assert.True(t, exists, name+" was not properly cloned")

exists, err = files.ExistsFolder(nodeAnalyzerFixtureDir, name, "node_modules")
assert.NoError(t, err)
assert.True(t, exists, name+" did not have its node modules installed")
}

func initializeProjects(testDir string) error {
var waitGroup sync.WaitGroup
waitGroup.Add(len(projects))

for _, project := range projects {
go func(proj fixtures.Project) {
defer waitGroup.Done()

projectDir := filepath.Join(testDir, proj.Name)

_, errOut, err := exec.Run(exec.Cmd{
Name: "npm",
Argv: []string{"install", "--production"},
Dir: projectDir,
WithEnv: proj.Env,
Command: "npm",
})
if err != nil {
println(errOut)
println("failed to run npm install on " + proj.Name)
}

// save time on local
ymlAlreadyExists, err := files.Exists(filepath.Join(projectDir, ".fossa.yml"))
if err != nil {
panic(err)
}
if ymlAlreadyExists {
return
}

// any key will work to prevent the "NEED KEY" error message
err = runfossa.Init(projectDir)
if err != nil {
println("failed to run fossa init on " + proj.Name)
println(err.Error())
panic(err)
}
}(project)
}

waitGroup.Wait()

return nil
}

var projects = []fixtures.Project{
fixtures.Project{
Name: "puppeteer",
URL: "https://github.com/GoogleChrome/puppeteer",
Commit: "b97bddf8e5750d20c6ba82392eebe2a3fd2dd218",
Env: map[string]string{
"PUPPETEER_SKIP_CHROMIUM_DOWNLOAD": "1",
},
},
fixtures.Project{
Name: "fakerjs",
URL: "https://github.com/Marak/faker.js",
Commit: "3a4bb358614c1e1f5d73f4df45c13a1a7aa013d7",
Env: map[string]string{},
},
fixtures.Project{
Name: "fastify",
URL: "https://github.com/fastify/fastify",
Commit: "1b16a4c5e381f9292d3ac2c327c3bda4bd277408",
Env: map[string]string{},
},
fixtures.Project{
Name: "nest",
URL: "https://github.com/nestjs/nest",
Commit: "ce498e86150f7de4a260f0c393d47ec4cc920ea1",
Env: map[string]string{},
},
fixtures.Project{
Name: "ohm",
URL: "https://github.com/harc/ohm",
Commit: "8202eff3723cfa26522134e7b003cf31ab5de445",
Env: map[string]string{},
},
fixtures.Project{
Name: "express",
URL: "https://github.com/expressjs/express",
Commit: "b4eb1f59d39d801d7365c86b04500f16faeb0b1c",
Env: map[string]string{},
},
fixtures.Project{
Name: "standard",
URL: "https://github.com/standard/standard",
Commit: "bc02256fa2c03632e657248483c55a752e63e724",
Env: map[string]string{},
},
fixtures.Project{
Name: "sodium-encryption",
URL: "https://github.com/mafintosh/sodium-encryption",
Commit: "42a7cba0f97718157e8c7a386ef94ba31e16837a",
Env: map[string]string{},
},
fixtures.Project{
Name: "request",
URL: "https://github.com/request/request",
Commit: "8162961dfdb73dc35a5a4bfeefb858c2ed2ccbb7",
Env: map[string]string{},
},
}

func cleanUp(dir string) {
err := os.RemoveAll(dir)
if err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion buildtools/pip/bindata/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions exec/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ type Cmd struct {
WithEnv map[string]string // If set, the command's environment is _added_ to WithEnv.
}

// func RunJSON(v interface{}, cmd Cmd) (stderr string, err error) {}

// Run executes a `Cmd`.
func Run(cmd Cmd) (stdout, stderr string, err error) {
log.WithFields(log.Fields{
Expand Down
12 changes: 2 additions & 10 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,55 +94,48 @@ echo "Testing puppeteer"
cd $HOME/puppeteer
fossa init
cat .fossa.yml
time fossa analyze --output | json
time fossa report licenses
time fossa report dependencies | json

echo "Testing pkg"
cd $HOME/pkg
fossa init
cat .fossa.yml
time fossa analyze --output | json
time fossa report licenses
time fossa report dependencies | json

echo "Testing faker.js"
cd $HOME/faker.js
fossa init
cat .fossa.yml
time fossa analyze --output | json
time fossa report licenses
time fossa report dependencies | json

echo "Testing fastify"
cd $HOME/fastify
fossa init
cat .fossa.yml
time fossa analyze --output | json
time fossa report licenses
time fossa report dependencies | json

echo "Testing nest"
cd $HOME/nest
fossa init
cat .fossa.yml
time fossa analyze --output npm:package.json | json
time fossa report licenses npm:package.json
time fossa report dependencies npm:package.json | json

echo "Testing ohm"
cd $HOME/ohm
fossa init
cat .fossa.yml
time fossa analyze --output | json
time fossa report licenses
time fossa report dependencies | json

echo "Testing express"
cd $HOME/express
fossa init
cat .fossa.yml
time fossa analyze --output | json
time fossa report licenses
time fossa report dependencies | json

Expand All @@ -151,23 +144,22 @@ echo "Testing standard"
cd $HOME/standard
fossa init
cat .fossa.yml
time fossa analyze --output --option allow-npm-err:true | json
# tested natively
# time fossa analyze --output --option allow-npm-err:true | json
time fossa report licenses --option allow-npm-err:true
time fossa report dependencies --option allow-npm-err:true | json

echo "Testing sodium-encryption"
cd $HOME/sodium-encryption
fossa init
cat .fossa.yml
time fossa analyze --output | json
time fossa report licenses
time fossa report dependencies | json

echo "Testing request"
cd $HOME/request
fossa init
cat .fossa.yml
time fossa analyze --output | json
time fossa report licenses
time fossa report dependencies | json

Expand Down
Loading

0 comments on commit c90d051

Please sign in to comment.