Skip to content

Commit

Permalink
fix test error
Browse files Browse the repository at this point in the history
Signed-off-by: Stephanie <[email protected]>
  • Loading branch information
yangcao77 committed Jul 24, 2023
1 parent 2252e02 commit 6316eab
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 95 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ lint:
unit-tests:
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" SKIP_PACT_TESTS=true go test ./... -coverprofile cover.out -v

cdq-analysis-unit-tests:
cd ./cdq-analysis && go test ./... -coverprofile cover.out -v

pact-tests:
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -v --run TestContracts

Expand All @@ -155,6 +158,7 @@ pact: manifests generate fmt vet envtest ## Run just Pact tests.
test: manifests generate fmt vet envtest ## Run tests.
make unit-tests
make pact-tests
make cdq-analysis-unit-tests

##@ Build

Expand Down
116 changes: 66 additions & 50 deletions cdq-analysis/pkg/componentdetectionquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ func TestCloneAndAnalyze(t *testing.T) {
log := ctrl.Log.WithName("TestCloneAndAnalyze")

k8sClient := K8sInfoClient{
Ctx: ctx,
Clientset: clientset,
Log: log,
Ctx: ctx,
Clientset: clientset,
Log: log,
CreateK8sJob: false,
}

compName := "testComponent"
Expand All @@ -52,34 +53,34 @@ func TestCloneAndAnalyze(t *testing.T) {
failedToCloneRepoErr := "failed to clone the repo.*"

springDevfileContext := `
schemaVersion: 2.2.0
metadata:
name: java-springboot
version: 1.2.1
projectType: springboot
provider: Red Hat
language: Java
`
schemaVersion: 2.2.0
metadata:
name: java-springboot
version: 1.2.1
projectType: springboot
provider: Red Hat
language: Java
`

pythonDevfileContext := `
schemaVersion: 2.2.0
metadata:
name: python
version: 1.0.1
projectType: Python
provider: Red Hat
language: Python
`
schemaVersion: 2.2.0
metadata:
name: python
version: 1.0.1
projectType: Python
provider: Red Hat
language: Python
`

nodeJSDevfileContext := `
schemaVersion: 2.2.0
metadata:
name: nodejs
version: 2.1.1
projectType: Node.js
provider: Red Hat
language: JavaScript
`
schemaVersion: 2.2.0
metadata:
name: nodejs
version: 2.1.1
projectType: Node.js
provider: Red Hat
language: JavaScript
`

tests := []struct {
testCase string
Expand All @@ -101,14 +102,17 @@ language: JavaScript
testCase: "repo with devfile - should successfully detect spring component",
URL: springSampleURL,
DevfileRegistryURL: DevfileRegistryEndpoint,
devfilePath: "devfile.yaml",
isDevfilePresent: true,
isDockerfilePresent: false,
wantDevfilesMap: map[string][]byte{},
wantDevfilesURLMap: map[string]string{},
wantDockerfileContextMap: map[string]string{
"./": "https://raw.githubusercontent.com/devfile-samples/devfile-sample-java-springboot-basic/main/docker/Dockerfile",
wantDevfilesMap: map[string][]byte{
"./": []byte(springDevfileContext),
},
wantDevfilesURLMap: map[string]string{
"./": "https://raw.githubusercontent.com/devfile-samples/devfile-sample-java-springboot-basic/main/devfile.yaml",
},
wantComponentsPortMap: map[string][]int{},
wantDockerfileContextMap: map[string]string{},
wantComponentsPortMap: map[string][]int{},
},
{
testCase: "repo without devfile and dockerfile - should successfully detect spring component",
Expand All @@ -120,12 +124,14 @@ language: JavaScript
"./": []byte(springDevfileContext),
},
wantDevfilesURLMap: map[string]string{
"./": "https://registry.devfile.io/devfiles/java-springboot-basic",
"./": "https://raw.githubusercontent.com/devfile-samples/devfile-sample-java-springboot-basic/main/devfile.yaml",
},
wantDockerfileContextMap: map[string]string{
"./": "https://raw.githubusercontent.com/devfile-samples/devfile-sample-java-springboot-basic/main/docker/Dockerfile",
},
wantComponentsPortMap: map[string][]int{},
wantComponentsPortMap: map[string][]int{
"./": {8081},
},
},
{
testCase: "private repo - should error out with no token provided",
Expand Down Expand Up @@ -166,28 +172,37 @@ language: JavaScript
},
wantDevfilesURLMap: map[string]string{
"devfile-sample-java-springboot-basic": "https://raw.githubusercontent.com/maysunfaisal/multi-components-dockerfile/main/devfile-sample-java-springboot-basic/.devfile/.devfile.yaml",
"devfile-sample-nodejs-basic": "https://raw.githubusercontent.com/maysunfaisal/multi-components-dockerfile/main/devfile-sample-nodejs-basic/devfile.yaml",
"devfile-sample-nodejs-basic": "https://raw.githubusercontent.com/nodeshift-starters/devfile-sample/main/devfile.yaml",
"devfile-sample-python-basic": "https://raw.githubusercontent.com/maysunfaisal/multi-components-dockerfile/main/devfile-sample-python-basic/.devfile.yaml",
"python-src-none": "https://registry.devfile.io/devfiles/python-basic",
"python-src-none": "https://raw.githubusercontent.com/devfile-samples/devfile-sample-python-basic/main/devfile.yaml",
},
wantDockerfileContextMap: map[string]string{
"devfile-sample-nodejs-basic": "https://raw.githubusercontent.com/nodeshift-starters/devfile-sample/main/Dockerfile",
"devfile-sample-python-basic": "https://raw.githubusercontent.com/maysunfaisal/multi-components-dockerfile/main/devfile-sample-python-basic/Dockerfile",
"python-src-docker": "python-src-docker/Dockerfile",
"python-src-docker": "Dockerfile",
"python-src-none": "https://raw.githubusercontent.com/devfile-samples/devfile-sample-python-basic/main/docker/Dockerfile",
},
wantComponentsPortMap: map[string][]int{},
wantComponentsPortMap: map[string][]int{
"devfile-sample-nodejs-basic": {3000},
},
},
{
testCase: "should successfully detect single component when context is provided",
context: "devfile-sample-nodejs-basic",
URL: multiComponentRepoURL,
DevfileRegistryURL: DevfileRegistryEndpoint,
isDevfilePresent: true,
isDockerfilePresent: false,
wantDevfilesMap: map[string][]byte{},
wantDevfilesURLMap: map[string]string{},
wantComponentsPortMap: map[string][]int{},
testCase: "should successfully detect single component when context is provided",
context: "devfile-sample-nodejs-basic",
URL: multiComponentRepoURL,
DevfileRegistryURL: DevfileRegistryEndpoint,
devfilePath: "devfile.yaml",
isDevfilePresent: true,
isDockerfilePresent: false,
wantDevfilesMap: map[string][]byte{
"devfile-sample-nodejs-basic": []byte(nodeJSDevfileContext),
},
wantDevfilesURLMap: map[string]string{
"devfile-sample-nodejs-basic": "https://raw.githubusercontent.com/nodeshift-starters/devfile-sample/main/devfile.yaml",
},
wantComponentsPortMap: map[string][]int{
"devfile-sample-nodejs-basic": {3000},
},
wantDockerfileContextMap: map[string]string{
"devfile-sample-nodejs-basic": "https://raw.githubusercontent.com/nodeshift-starters/devfile-sample/main/Dockerfile",
},
Expand Down Expand Up @@ -239,9 +254,10 @@ func TestSendBackDetectionResult(t *testing.T) {
log := ctrl.Log.WithName("TestSendBackDetectionResult")

k8sClient := K8sInfoClient{
Ctx: ctx,
Clientset: clientset,
Log: log,
Ctx: ctx,
Clientset: clientset,
Log: log,
CreateK8sJob: true,
}

compName := "testComponent"
Expand Down
9 changes: 7 additions & 2 deletions cdq-analysis/pkg/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ func AnalyzePath(log logr.Logger, a Alizer, localpath, context, devfileRegistryU
}

dockerfileContextMapFromRepo[context] = link
componentPortsMapFromRepo[context] = detectedPorts
// only set if not empty
if detectedPorts != nil && !reflect.DeepEqual(detectedPorts, []int{}) {
componentPortsMapFromRepo[context] = detectedPorts
}
isDockerfilePresent = true
}
}
Expand All @@ -250,7 +253,9 @@ func AnalyzePath(log logr.Logger, a Alizer, localpath, context, devfileRegistryU
// Still invoke alizer to detect the ports from the component
_, _, _, detectedPorts, err := AnalyzeAndDetectDevfile(a, localpath, devfileRegistryURL)
if err == nil {
componentPortsMapFromRepo[context] = detectedPorts
if detectedPorts != nil && !reflect.DeepEqual(detectedPorts, []int{}) {
componentPortsMapFromRepo[context] = detectedPorts
}
} else {
log.Info(fmt.Sprintf("failed to detect port from context: %v, error: %v", context, err))
}

Check warning on line 261 in cdq-analysis/pkg/detect.go

View check run for this annotation

Codecov / codecov/patch

cdq-analysis/pkg/detect.go#L259-L261

Added lines #L259 - L261 were not covered by tests
Expand Down
10 changes: 0 additions & 10 deletions cdq-analysis/pkg/ioutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os"
"path/filepath"

"github.com/go-logr/logr"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -57,12 +56,3 @@ func IsExisting(fs afero.Fs, path string) (bool, error) {
func CreateTempPath(prefix string, appFs afero.Afero) (string, error) {
return appFs.TempDir(os.TempDir(), prefix)
}

// RemoveFolderAndLogError removes the specified folder. If the delete fails, no error is returned, but an error is logged
// Used in cases where we're cleaning up after encountering an error, but want to return the original error instead.
func RemoveFolderAndLogError(log logr.Logger, fs afero.Fs, path string) {
err := fs.RemoveAll(path)
if err != nil {
log.Error(err, fmt.Sprintf("Unable to delete folder %s", path))
}
}
33 changes: 0 additions & 33 deletions cdq-analysis/pkg/ioutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"strings"
"testing"

"github.com/go-logr/logr"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -154,35 +153,3 @@ func TestCreateTempPath(t *testing.T) {
})
}
}

func TestRemoveFolderAndLogError(t *testing.T) {
fs := NewFilesystem()
var logger logr.Logger
inmemoryFs := NewMemoryFilesystem()
readOnlyFs := NewReadOnlyFs()

tests := []struct {
name string
fs afero.Afero
}{
{
name: "inmemory fs",
fs: inmemoryFs,
},
{
name: "read only fs",
fs: readOnlyFs,
},
{
name: "local fs",
fs: fs,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path, _ := CreateTempPath("TestCreateTempPath", tt.fs)
RemoveFolderAndLogError(logger, tt.fs, path)
})
}
}

0 comments on commit 6316eab

Please sign in to comment.