Skip to content

Commit

Permalink
Finish unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Parthvi Vala <pvala@redhat.com>
  • Loading branch information
valaparthvi committed Mar 13, 2023
1 parent ef86b7d commit 3199f13
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 37 deletions.
10 changes: 6 additions & 4 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type RegistryClient struct {

var _ Client = (*RegistryClient)(nil)

const (
CONFLICT_DIR_NAME = "CONFLICT_STARTER_PROJECT"
)
func NewRegistryClient(fsys filesystem.Filesystem, preferenceClient preference.Client, kubeClient kclient.ClientInterface) RegistryClient {
return RegistryClient{
fsys: fsys,
Expand Down Expand Up @@ -102,17 +105,16 @@ func (o RegistryClient) DownloadStarterProject(starterProject *devfilev1.Starter
}
} else {
// Case 2
conflictDirName := "CONFLICT_STARTER_PROJECT"
err = o.fsys.MkdirAll(conflictDirName, 0750)
err = o.fsys.MkdirAll(filepath.Join(contextDir, CONFLICT_DIR_NAME), 0750)
if err != nil {
return err
}

copyErr := util.CopyDirWithFS(starterProjectTmpDir, conflictDirName, o.fsys)
copyErr := util.CopyDirWithFS(starterProjectTmpDir, filepath.Join(contextDir, CONFLICT_DIR_NAME), o.fsys)
if copyErr != nil {
return copyErr
}
log.Warningf("\nThere are conflicting files (%s) between starter project and the context directory, hence the starter project has been copied to %s", strings.Join(conflictingFiles, ", "), conflictDirName)
log.Warningf("\nThere are conflicting files (%s) between starter project and the context directory, hence the starter project has been copied to %s", strings.Join(conflictingFiles, ", "), CONFLICT_DIR_NAME)
}
return nil
}
Expand Down
95 changes: 62 additions & 33 deletions pkg/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,32 +617,27 @@ func TestGetRegistryDevfiles(t *testing.T) {
}

func TestRegistryClient_DownloadStarterProject(t *testing.T) {
fakeFS := filesystem.NewFakeFs()
contextDir, ferr := fakeFS.TempDir("", "downloadstarterproject")
if ferr != nil {
t.Errorf("failed to create temp dir; cause: %s", ferr)
}

directoriesToBeCreated := []string{"kubernetes", "docker"}
for _, dirToBeCreated := range directoriesToBeCreated {
dirName := filepath.Join(contextDir, dirToBeCreated)
ferr = fakeFS.MkdirAll(dirName, os.FileMode(0750))
if ferr != nil {
t.Errorf("failed to create %s; cause: %s", dirName, ferr)
setupFS := func(contextDir string, fs filesystem.Filesystem) {
directoriesToBeCreated := []string{"kubernetes", "docker"}
for _, dirToBeCreated := range directoriesToBeCreated {
dirName := filepath.Join(contextDir, dirToBeCreated)
err := fs.MkdirAll(dirName, os.FileMode(0750))
if err != nil {
t.Errorf("failed to create %s; cause: %s", dirName, err)
}
}

}

filesToBeCreated := []string{"devfile.yaml", "kubernetes/deploy.yaml", "docker/Dockerfile"}
for _, fileToBeCreated := range filesToBeCreated {
fileName := filepath.Join(contextDir, fileToBeCreated)
_, ferr = fakeFS.Create(fileName)
if ferr != nil {
t.Errorf("failed to create %s; cause: %s", fileName, ferr)
filesToBeCreated := []string{"devfile.yaml", "kubernetes/deploy.yaml", "docker/Dockerfile"}
for _, fileToBeCreated := range filesToBeCreated {
fileName := filepath.Join(contextDir, fileToBeCreated)
_, err := fs.Create(fileName)
if err != nil {
t.Errorf("failed to create %s; cause: %s", fileName, err)
}
}
}

getFilePath := func(name string) string {
getZipFilePath := func(name string) string {
// filename of this file
_, filename, _, _ := runtime.Caller(0)
// path to the devfile
Expand Down Expand Up @@ -670,7 +665,7 @@ func TestRegistryClient_DownloadStarterProject(t *testing.T) {
{
name: "Starter project has a Devfile",
fields: fields{
fsys: fakeFS,
fsys: filesystem.NewFakeFs(),
},
args: args{
starterProject: &devfilev1.StarterProject{
Expand All @@ -679,33 +674,67 @@ func TestRegistryClient_DownloadStarterProject(t *testing.T) {
SourceType: "",
Zip: &devfilev1.ZipProjectSource{
CommonProjectSource: devfilev1.CommonProjectSource{},
Location: fmt.Sprintf("file://%s", getFilePath("starterproject-with-devfile.zip")),
Location: fmt.Sprintf("file://%s", getZipFilePath("starterproject-with-devfile.zip")),
},
},
},
decryptedToken: "",
contextDir: contextDir,
},
want: []string{"devfile.yaml", "docker", filepath.Join("docker", "Dockerfile"), "README.md", "main.go", "go.mod", "someFile.txt"},
wantErr: false,
},
{
name: "Starter project has conflicting files",
fields: fields{},
args: args{},
want: nil,
name: "Starter project has conflicting files",
fields: fields{
fsys: filesystem.NewFakeFs(),
},
args: args{
starterProject: &devfilev1.StarterProject{
Name: "starter-project-with-conflicting-files",
ProjectSource: devfilev1.ProjectSource{
SourceType: "",
Zip: &devfilev1.ZipProjectSource{
CommonProjectSource: devfilev1.CommonProjectSource{},
Location: fmt.Sprintf("file://%s", getZipFilePath("starterproject-with-conflicts.zip")),
},
},
},
},
want: []string{"devfile.yaml", "docker", filepath.Join("docker", "Dockerfile"), "kubernetes", filepath.Join("kubernetes", "deploy.yaml"),
CONFLICT_DIR_NAME, filepath.Join(CONFLICT_DIR_NAME, "kubernetes"), filepath.Join(CONFLICT_DIR_NAME, "kubernetes", "deploy.yaml"),
filepath.Join(CONFLICT_DIR_NAME, "main.go"), filepath.Join(CONFLICT_DIR_NAME, "go.mod"), filepath.Join(CONFLICT_DIR_NAME, "README.md"), filepath.Join(CONFLICT_DIR_NAME, "someFile.txt")},
wantErr: false,
},
{
name: "Starter project does not have any conflicting files",
fields: fields{},
args: args{},
want: nil,
name: "Starter project does not have any conflicting files",
fields: fields{
fsys: filesystem.NewFakeFs(),
},
args: args{
starterProject: &devfilev1.StarterProject{
Name: "starter-project-with-no-conflicting-files",
ProjectSource: devfilev1.ProjectSource{
SourceType: "",
Zip: &devfilev1.ZipProjectSource{
CommonProjectSource: devfilev1.CommonProjectSource{},
Location: fmt.Sprintf("file://%s", getZipFilePath("starterproject-with-no-conflicts.zip")),
},
},
},
},
want: []string{"devfile.yaml", "docker", filepath.Join("docker", "Dockerfile"), "kubernetes", filepath.Join("kubernetes", "deploy.yaml"), "README.md", "main.go", "go.mod"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
contextDir, ferr := tt.fields.fsys.TempDir("", "downloadstarterproject")
if ferr != nil {
t.Errorf("failed to create temp dir; cause: %s", ferr)
}
tt.args.contextDir = contextDir

setupFS(tt.args.contextDir, tt.fields.fsys)

o := RegistryClient{
fsys: tt.fields.fsys,
preferenceClient: tt.fields.preferenceClient,
Expand Down

0 comments on commit 3199f13

Please sign in to comment.