Skip to content

Commit

Permalink
Add new tests to increase coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Maysun J Faisal <[email protected]>
  • Loading branch information
maysunfaisal committed Jan 8, 2024
1 parent 5d0e5e5 commit a1aea32
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/devfile/parser/context/apiVersion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestSetDevfileAPIVersion(t *testing.T) {
concreteSchema = `{"schemaVersion": "2.2.0-latest"}`
emptyJson = "{}"
emptySchemaVersionJson = `{"schemaVersion": ""}`
badJson = `{"name": "Joe", "age": null]`
devfilePath = "/testpath/devfile.yaml"
devfileURL = "http://server/devfile.yaml"
)
Expand Down Expand Up @@ -66,6 +67,12 @@ func TestSetDevfileAPIVersion(t *testing.T) {
want: "",
wantErr: &errPkg.NonCompliantDevfile{Err: fmt.Sprintf("schemaVersion in devfile: %s cannot be empty", devfileURL)},
},
{
name: "unmarshal error",
devfileCtx: DevfileCtx{rawContent: []byte(badJson), url: devfileURL},
want: "",
wantErr: &errPkg.NonCompliantDevfile{Err: "invalid character ']' after object key:value pair"},
},
}

for _, tt := range tests {
Expand Down
149 changes: 149 additions & 0 deletions pkg/devfile/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3350,6 +3350,48 @@ commands:
kind: deploy
isDefault: true`

badDevfile := `schemaVersion: 10.0.0
metadata:
name: nodejs
version: 2.1.1
displayName: Node.js Runtime
description: Stack with Node.js 16
components:
- name: image-build
image:
imageName: nodejs-image:latest
dockerfile:
uri: Dockerfile
buildContext: .
rootRequired: true
- name: kubernetes-deploy
kubernetes:
uri: deploy.yaml
endpoints:
- name: http-3001
targetPort: 3001
path: /
commands:
- id: build-image
apply:
component: image-build
- id: deployk8s
apply:
component: kubernetes-deploy
group:
kind: deploy
- id: deploy
composite:
commands:
- build-image
- deployk8s
group:
kind: deploy
isDefault: true`

badYAML := `
~: a`

tests := []struct {
name string
parserArgs ParserArgs
Expand Down Expand Up @@ -3416,6 +3458,25 @@ commands:
},
wantErr: true,
},
{
name: "error out if parser args have no devfile src",
parserArgs: ParserArgs{},
wantErr: true,
},
{
name: "error out if parser args have bad YAML",
parserArgs: ParserArgs{
Data: []byte(badYAML),
},
wantErr: true,
},
{
name: "error if parser args have a bad Devfile",
parserArgs: ParserArgs{
Data: []byte(badDevfile),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -3623,6 +3684,10 @@ func Test_parseParentFromKubeCRD(t *testing.T) {
},
}

emptyImportReference := v1.ImportReference{
Version: "1",
}

importFromKubeCRD := attributes.Attributes{}.PutString(importSourceAttribute, resolveImportReference(kubeCRDReference))
parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(importSourceAttribute,
resolveImportReference(kubeCRDReference)).PutString(parentOverrideAttribute, "main devfile")
Expand Down Expand Up @@ -3664,6 +3729,8 @@ func Test_parseParentFromKubeCRD(t *testing.T) {
},
}

noParentImportSrcErr := "parent does not define any resources"
noPluginImportSrcErr := "plugin plugin1 does not define any resources"
crdNotFoundErr := "not found"

//override all properties
Expand Down Expand Up @@ -3904,6 +3971,50 @@ func Test_parseParentFromKubeCRD(t *testing.T) {
},
wantErr: &crdNotFoundErr,
},
{
name: "should err out if there is no parent import reference",
mainDevfile: DevfileObj{
Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath),
Data: &v2.DevfileV2{
Devfile: v1.Devfile{
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
Parent: &v1.Parent{
ImportReference: emptyImportReference,
},
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{},
},
},
},
},
wantErr: &noParentImportSrcErr,
},
{
name: "should err out if there is no plugin import reference",
mainDevfile: DevfileObj{
Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath),
Data: &v2.DevfileV2{
Devfile: v1.Devfile{
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
Components: []v1.Component{
{
Name: "plugin1",
ComponentUnion: v1.ComponentUnion{
Plugin: &v1.PluginComponent{
ImportReference: v1.ImportReference{
Version: "2",
},
},
},
},
},
},
},
},
},
},
wantErr: &noPluginImportSrcErr,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -4533,6 +4644,9 @@ func Test_parseFromRegistry(t *testing.T) {
stagingRegistry = "https://registry.stage.devfile.io"
)

badYAML := `
~: a`

parentDevfile := DevfileObj{
Data: &v2.DevfileV2{
Devfile: v1.Devfile{
Expand Down Expand Up @@ -4590,13 +4704,16 @@ func Test_parseFromRegistry(t *testing.T) {
missingRegistryURLErr := "failed to fetch from registry, registry URL is not provided"
invalidRegistryURLErr := "Get .* dial tcp: lookup http: .*"
resourceDownloadErr := "failed to pull stack from registry .*"
badDevfileErr := "error parsing devfile because of non-compliant data"

testServer := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var data []byte
var err error
if strings.Contains(r.URL.Path, "/devfiles/"+registryId) {
if strings.Contains(r.URL.Path, "latest") {
data, err = yaml.Marshal(latestParentDevfile.Data)
} else if strings.Contains(r.URL.Path, "bad") {
data = []byte(badYAML)
} else if strings.Contains(r.URL.Path, "1.1.0") {
data, err = yaml.Marshal(parentDevfile.Data)
} else if r.URL.Path == fmt.Sprintf("/devfiles/%s/", registryId) {
Expand Down Expand Up @@ -4742,6 +4859,17 @@ func Test_parseFromRegistry(t *testing.T) {
},
wantErr: &invalidRegistryURLErr,
},
{
name: "should fail if registry returns a bad devfile content",
importReference: v1.ImportReference{
ImportReferenceUnion: v1.ImportReferenceUnion{
Id: registryId,
},
Version: "bad",
RegistryUrl: httpPrefix + registry,
},
wantErr: &badDevfileErr,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -4786,6 +4914,7 @@ func Test_parseFromKubeCRD(t *testing.T) {
}

crdNotFoundErr := "not found"
noContextErr := "kubernetes client and context are required to parse from Kubernetes CRD"

tests := []struct {
name string
Expand Down Expand Up @@ -4834,6 +4963,20 @@ func Test_parseFromKubeCRD(t *testing.T) {
},
wantErr: &crdNotFoundErr,
},
{
name: "should err out on no context",
wantDevFile: parentDevfile,
importReference: v1.ImportReference{
ImportReferenceUnion: v1.ImportReferenceUnion{
Kubernetes: &v1.KubernetesCustomResourceImportReference{
Name: name,
Namespace: namespace,
},
},
},
devWorkspaceResources: map[string]v1.DevWorkspaceTemplate{},
wantErr: &noContextErr,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -4845,6 +4988,12 @@ func Test_parseFromKubeCRD(t *testing.T) {
k8sClient: testK8sClient,
context: context.Background(),
}
if tt.name == "should err out on no context" {
tool = resolverTools{
k8sClient: testK8sClient,
context: nil,
}
}
got, err := parseFromKubeCRD(tt.importReference, &resolutionContextTree{}, tool)
if (err != nil) != (tt.wantErr != nil) {
t.Errorf("Test_parseFromKubeCRD() unexpected error: %v, wantErr %v", err, tt.wantErr)
Expand Down

0 comments on commit a1aea32

Please sign in to comment.