Skip to content

Commit 0a68ca4

Browse files
committed
adding parser and util tests
Signed-off-by: Michael Hoang <[email protected]>
1 parent df97492 commit 0a68ca4

File tree

3 files changed

+164
-11
lines changed

3 files changed

+164
-11
lines changed

pkg/devfile/parser/parse.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"github.com/devfile/api/v2/pkg/attributes"
8+
registryLibrary "github.com/devfile/registry-support/registry-library/library"
89
"io/ioutil"
910
"net/url"
1011
"os"
@@ -23,8 +24,6 @@ import (
2324

2425
"reflect"
2526

26-
registryLibrary "github.com/devfile/registry-support/registry-library/library"
27-
2827
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2928
apiOverride "github.com/devfile/api/v2/pkg/utils/overriding"
3029
"github.com/devfile/api/v2/pkg/validation"
@@ -402,14 +401,17 @@ func parseFromRegistry(importReference v1.ImportReference, resolveCtx *resolutio
402401
id := importReference.Id
403402
registryURL := importReference.RegistryUrl
404403
destDir := "."
405-
tempDir, err := ioutil.TempDir("", "")
404+
405+
// contains the downloaded stack from the registry
406+
tempDir, _ := ioutil.TempDir("", "")
406407
defer os.RemoveAll(tempDir)
407408

408409
if registryURL != "" {
409-
err = registryLibrary.PullStackFromRegistry(registryURL, id, tempDir, registryLibrary.RegistryOptions{})
410-
err = util.CopyAllDirFiles(tempDir, destDir)
411-
412-
devfileContent, err := ioutil.ReadFile(path.Join(tempDir, "devfile.yaml"))
410+
err = getStackFromRegistry(id, registryURL, tempDir, destDir)
411+
if err != nil {
412+
return DevfileObj{}, err
413+
}
414+
devfileContent, err := getDevfileFromDir(tempDir)
413415
if err != nil {
414416
devfileContent, err = getDevfileFromRegistry(id, registryURL, importReference.Version)
415417
if err != nil {
@@ -426,10 +428,11 @@ func parseFromRegistry(importReference v1.ImportReference, resolveCtx *resolutio
426428

427429
} else if tool.registryURLs != nil {
428430
for _, registryURL := range tool.registryURLs {
429-
err = registryLibrary.PullStackFromRegistry(registryURL, id, tempDir, registryLibrary.RegistryOptions{})
430-
err = util.CopyAllDirFiles(tempDir, destDir)
431-
432-
devfileContent, err := ioutil.ReadFile(path.Join(tempDir, "devfile.yaml"))
431+
err = getStackFromRegistry(id, registryURL, tempDir, destDir)
432+
if err != nil {
433+
return DevfileObj{}, err
434+
}
435+
devfileContent, err := getDevfileFromDir(tempDir)
433436
if err != nil {
434437
devfileContent, err = getDevfileFromRegistry(id, registryURL, importReference.Version)
435438
}
@@ -451,6 +454,24 @@ func parseFromRegistry(importReference v1.ImportReference, resolveCtx *resolutio
451454
return DevfileObj{}, fmt.Errorf("failed to get id: %s from registry URLs provided", id)
452455
}
453456

457+
func getStackFromRegistry(id, registryURL, stackDir, destDir string) error {
458+
if !strings.HasPrefix(registryURL, "http://") && !strings.HasPrefix(registryURL, "https://") {
459+
return fmt.Errorf("the provided registryURL: %s is not a valid URL", registryURL)
460+
}
461+
registryLibrary.PullStackFromRegistry(registryURL, id, stackDir, registryLibrary.RegistryOptions{})
462+
util.CopyAllDirFiles(stackDir, destDir)
463+
464+
return nil
465+
}
466+
467+
func getDevfileFromDir(dir string) ([]byte, error) {
468+
devfileContent, err := ioutil.ReadFile(path.Join(dir, "devfile.yaml"))
469+
if err != nil {
470+
return nil, fmt.Errorf("failed to get devfile from stack registry")
471+
}
472+
return devfileContent, err
473+
}
474+
454475
func getDevfileFromRegistry(id, registryURL, version string) ([]byte, error) {
455476
if !strings.HasPrefix(registryURL, "http://") && !strings.HasPrefix(registryURL, "https://") {
456477
return nil, fmt.Errorf("the provided registryURL: %s is not a valid URL", registryURL)

pkg/devfile/parser/parse_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/http/httptest"
2121
"os"
2222
"path"
23+
"path/filepath"
2324
"reflect"
2425
"sigs.k8s.io/yaml"
2526
"strings"
@@ -4317,6 +4318,52 @@ func Test_parseFromRegistry(t *testing.T) {
43174318
}
43184319
}
43194320

4321+
func Test_getDevfileFromDir(t *testing.T) {
4322+
tempDir, err := ioutil.TempDir("", "")
4323+
if err != nil {
4324+
t.Errorf("Failed to create temp dir: %s, error: %v", tempDir, err)
4325+
}
4326+
defer os.RemoveAll(tempDir)
4327+
4328+
devfile := filepath.Join(tempDir, "devfile.yaml")
4329+
if err := ioutil.WriteFile(devfile, []byte(""), 0666); err != nil {
4330+
t.Errorf("Failed to create temp devfile, error: %v", err)
4331+
}
4332+
4333+
missingDevfileDir := "no/devfile/here"
4334+
4335+
tests := []struct {
4336+
name string
4337+
dir string
4338+
wantErr bool
4339+
}{
4340+
{
4341+
name: "should be able to get devfile from dir",
4342+
dir: tempDir,
4343+
wantErr: false,
4344+
},
4345+
{
4346+
name: "should fail if directory doesn't have devfile",
4347+
dir: missingDevfileDir,
4348+
wantErr: true,
4349+
},
4350+
}
4351+
4352+
for _, tt := range tests {
4353+
t.Run(tt.name, func(t *testing.T) {
4354+
gotErr := false
4355+
_, err = getDevfileFromDir(tt.dir)
4356+
if err != nil {
4357+
gotErr = true
4358+
}
4359+
4360+
if !reflect.DeepEqual(gotErr, tt.wantErr) {
4361+
t.Errorf("Got error: %t, want error: %t", gotErr, tt.wantErr)
4362+
}
4363+
})
4364+
}
4365+
}
4366+
43204367
func Test_parseFromKubeCRD(t *testing.T) {
43214368
const (
43224369
namespace = "default"

pkg/util/util_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// Copyright 2021-2022 Red Hat, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package util
17+
18+
import (
19+
"io/ioutil"
20+
"os"
21+
"path/filepath"
22+
"reflect"
23+
"testing"
24+
)
25+
26+
// copied from https://github.com/redhat-developer/odo/blob/main/pkg/util/util_test.go#L1618
27+
func TestCopyFile(t *testing.T) {
28+
// Create temp dir
29+
tempDir, err := ioutil.TempDir("", "")
30+
if err != nil {
31+
t.Errorf("Failed to create temp dir: %s, error: %v", tempDir, err)
32+
}
33+
34+
// Create temp file under temp dir as source file
35+
tempFile, err := ioutil.TempFile(tempDir, "")
36+
if err != nil {
37+
t.Errorf("Failed to create temp file: %s, error: %v", tempFile.Name(), err)
38+
}
39+
defer tempFile.Close()
40+
41+
srcPath := tempFile.Name()
42+
fakePath := "!@#/**"
43+
dstPath := filepath.Join(tempDir, "dstFile")
44+
info, _ := os.Stat(srcPath)
45+
46+
tests := []struct {
47+
name string
48+
srcPath string
49+
dstPath string
50+
wantErr bool
51+
}{
52+
{
53+
name: "should be able to copy file to destination path",
54+
srcPath: srcPath,
55+
dstPath: dstPath,
56+
wantErr: false,
57+
},
58+
{
59+
name: "should fail if source path is invalid",
60+
srcPath: fakePath,
61+
dstPath: dstPath,
62+
wantErr: true,
63+
},
64+
{
65+
name: "should fail if destination path is invalid",
66+
srcPath: srcPath,
67+
dstPath: fakePath,
68+
wantErr: true,
69+
},
70+
}
71+
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
gotErr := false
75+
err = CopyFile(tt.srcPath, tt.dstPath, info)
76+
if err != nil {
77+
gotErr = true
78+
}
79+
80+
if !reflect.DeepEqual(gotErr, tt.wantErr) {
81+
t.Errorf("Got error: %t, want error: %t", gotErr, tt.wantErr)
82+
}
83+
})
84+
}
85+
}

0 commit comments

Comments
 (0)