Skip to content

Commit

Permalink
test: add tests for default, source and delete type path mappings (#1020
Browse files Browse the repository at this point in the history
)

Signed-off-by: Harikrishnan Balagopal <[email protected]>
  • Loading branch information
HarikrishnanBalagopal authored Apr 10, 2023
1 parent 4bfde1d commit c2396c4
Show file tree
Hide file tree
Showing 7 changed files with 870 additions and 14 deletions.
46 changes: 34 additions & 12 deletions transformer/processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package transformer

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -35,7 +36,7 @@ func getpair(a, b string) pair {
return pair{A: a, B: b}
}

func processPathMappings(pms []transformertypes.PathMapping, sourcePath, outputPath string) error {
func processPathMappings(pms []transformertypes.PathMapping, sourcePath, outputPath string, failOnFirstError bool) error {
copiedSourceDests := map[pair]bool{}
for _, pm := range pms {
if !strings.EqualFold(string(pm.Type), string(transformertypes.SourcePathMappingType)) || copiedSourceDests[getpair(pm.SrcPath, pm.DestPath)] {
Expand All @@ -47,7 +48,10 @@ func processPathMappings(pms []transformertypes.PathMapping, sourcePath, outputP
}
destPath := filepath.Join(outputPath, pm.DestPath)
if err := filesystem.Merge(srcPath, destPath, true); err != nil {
logrus.Errorf("failed to copy the source path %s the the destination path %s for the path mapping %+v . Error: %q", srcPath, destPath, pm, err)
if failOnFirstError {
return fmt.Errorf("failed to copy the source path '%s' to the destination path '%s' for the path mapping %+v . Error: %w", srcPath, destPath, pm, err)
}
logrus.Errorf("failed to copy the source path '%s' to the destination path '%s' for the path mapping %+v . Error: %q", srcPath, destPath, pm, err)
continue
}
copiedSourceDests[getpair(pm.SrcPath, pm.DestPath)] = true
Expand All @@ -62,24 +66,40 @@ func processPathMappings(pms []transformertypes.PathMapping, sourcePath, outputP
case strings.ToLower(string(transformertypes.SourcePathMappingType)): // skip sources
case strings.ToLower(string(transformertypes.DeletePathMappingType)): // skip deletes
case strings.ToLower(string(transformertypes.ModifiedSourcePathMappingType)):
if err := filesystem.Merge(pm.SrcPath, destPath, false); err != nil {
if err := filesystem.Merge(pm.SrcPath, destPath, failOnFirstError); err != nil {
if failOnFirstError {
return fmt.Errorf("failed to merge for the path mapping %+v . Error: %w", pm, err)
}
logrus.Errorf("Error while copying sourcepath for %+v . Error: %q", pm, err)
}
case strings.ToLower(string(transformertypes.TemplatePathMappingType)):
if err := filesystem.TemplateCopy(pm.SrcPath, destPath,
filesystem.AddOnConfig{Config: pm.TemplateConfig}); err != nil {
if err := filesystem.TemplateCopy(pm.SrcPath, destPath, filesystem.AddOnConfig{Config: pm.TemplateConfig}); err != nil {
if failOnFirstError {
return fmt.Errorf("failed to copy the template for the path mapping %+v . Error: %w", pm, err)
}
logrus.Errorf("Error while copying sourcepath for %+v . Error: %q", pm, err)
}
case strings.ToLower(string(transformertypes.SpecialTemplatePathMappingType)):
if err := filesystem.TemplateCopy(pm.SrcPath, destPath,
filesystem.AddOnConfig{OpeningDelimiter: filesystem.SpecialOpeningDelimiter,
if err := filesystem.TemplateCopy(
pm.SrcPath,
destPath,
filesystem.AddOnConfig{
OpeningDelimiter: filesystem.SpecialOpeningDelimiter,
ClosingDelimiter: filesystem.SpecialClosingDelimiter,
Config: pm.TemplateConfig}); err != nil {
Config: pm.TemplateConfig,
},
); err != nil {
if failOnFirstError {
return fmt.Errorf("failed to copy the special template for the path mapping %+v . Error: %w", pm, err)
}
logrus.Errorf("Error while copying sourcepath for %+v . Error: %q", pm, err)
}
default:
if !copiedDefaultDests[getpair(pm.SrcPath, pm.DestPath)] {
if err := filesystem.Merge(pm.SrcPath, destPath, false); err != nil {
if err := filesystem.Merge(pm.SrcPath, destPath, failOnFirstError); err != nil {
if failOnFirstError {
return fmt.Errorf("failed to merge for the path mapping %+v . Error: %w", pm, err)
}
logrus.Errorf("Error while copying sourcepath for %+v . Error: %q", pm, err)
}
copiedDefaultDests[getpair(pm.SrcPath, pm.DestPath)] = true
Expand All @@ -95,9 +115,11 @@ func processPathMappings(pms []transformertypes.PathMapping, sourcePath, outputP
if !filepath.IsAbs(pm.DestPath) {
destPath = filepath.Join(outputPath, pm.DestPath)
}
err := os.RemoveAll(destPath)
if err != nil {
logrus.Errorf("Path [%s] marked by delete-path-mapping could not been deleted: %q", destPath, err)
if err := os.RemoveAll(destPath); err != nil {
if failOnFirstError {
return fmt.Errorf("failed to remove the destination path '%s' . Error: %w", destPath, err)
}
logrus.Errorf("Path [%s] marked by delete-path-mapping could not be deleted. Error: %q", destPath, err)
continue
}
logrus.Debugf("Path [%s] marked by delete-path-mapping has been deleted", destPath)
Expand Down
199 changes: 199 additions & 0 deletions transformer/processors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Copyright IBM Corporation 2023
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package transformer

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

"github.com/google/go-cmp/cmp"
"github.com/konveyor/move2kube/common"
transformertypes "github.com/konveyor/move2kube/types/transformer"
)

func TestProcessPathMappings(t *testing.T) {
t.Run("no-pathmappings", func(t *testing.T) {
pms := []transformertypes.PathMapping{}
sourcePath := ""
outputPath := ""
if err := processPathMappings(pms, sourcePath, outputPath, true); err != nil {
t.Fatalf("failed to process the path mappings. Error: %q", err)
}
})
t.Run("single-default-pathmapping", func(t *testing.T) {
sourcePath := "./testdata"
pms := []transformertypes.PathMapping{{
Type: transformertypes.DefaultPathMappingType,
SrcPath: filepath.Join(sourcePath, "k8s-yamls"),
DestPath: "yamls",
}}
outputPath := t.TempDir()
if err := processPathMappings(pms, sourcePath, outputPath, true); err != nil {
t.Fatalf("failed to process the path mappings. Error: %q", err)
}
{
fs, err := os.ReadDir(outputPath)
if err != nil {
t.Fatalf("Error: %q", err)
}
for _, f := range fs {
if f.Name() != "yamls" {
t.Fatalf("wrong folder name. f %+v f.Name %s", f, f.Name())
}
}
}
{
fs, err := os.ReadDir(filepath.Join(outputPath, "yamls"))
if err != nil {
t.Fatalf("Error: %q", err)
}
for _, f := range fs {
if f.Name() != "deployment.yaml" {
t.Fatalf("wrong file name. f %+v f.Name %s", f, f.Name())
}
}
}
{
expectedContents, err := os.ReadFile(filepath.Join(sourcePath, "k8s-yamls", "deployment.yaml"))
if err != nil {
t.Fatalf("Error: %q", err)
}
actualContents, err := os.ReadFile(filepath.Join(outputPath, "yamls", "deployment.yaml"))
if err != nil {
t.Fatalf("Error: %q", err)
}
if diff := cmp.Diff(string(actualContents), string(expectedContents)); diff != "" {
t.Fatalf("wrong file contents. Differences: %s", diff)
}
}
})
t.Run("single-source-pathmapping", func(t *testing.T) {
sourcePath := "./testdata"
pms := []transformertypes.PathMapping{{
Type: transformertypes.SourcePathMappingType,
SrcPath: "src",
DestPath: "source",
}}
outputPath := t.TempDir()
if err := processPathMappings(pms, sourcePath, outputPath, true); err != nil {
t.Fatalf("failed to process the path mappings. Error: %q", err)
}
{
fs, err := os.ReadDir(outputPath)
if err != nil {
t.Fatalf("Error: %q", err)
}
for _, f := range fs {
if f.Name() != "source" {
t.Fatalf("wrong folder name. f %+v f.Name %s", f, f.Name())
}
}
}
expectedFilenames := []string{
"index.js",
"package-lock.json",
"package.json",
}
{
fs, err := os.ReadDir(filepath.Join(outputPath, "source"))
if err != nil {
t.Fatalf("Error: %q", err)
}
for i, f := range fs {
if f.Name() != expectedFilenames[i] {
t.Fatalf("wrong file name. expected: '%s' actual: '%s' f %+v", expectedFilenames[i], f.Name(), f)
}
}
}
for _, expectedFilename := range expectedFilenames {
expectedContents, err := os.ReadFile(filepath.Join(sourcePath, "src", expectedFilename))
if err != nil {
t.Fatalf("Error: %q", err)
}
actualContents, err := os.ReadFile(filepath.Join(outputPath, "source", expectedFilename))
if err != nil {
t.Fatalf("Error: %q", err)
}
if diff := cmp.Diff(string(actualContents), string(expectedContents)); diff != "" {
t.Fatalf("wrong file contents for filename '%s' . Differences: %s", expectedFilename, diff)
}
}
})

t.Run("single-delete-pathmapping", func(t *testing.T) {
sourcePath := "./testdata/src"
sourceIndexJSPath := filepath.Join(sourcePath, "index.js")
outputPath := t.TempDir()
outputIndexJSPath := filepath.Join(outputPath, "index.js")
if err := common.CopyFile(outputIndexJSPath, sourceIndexJSPath); err != nil {
t.Fatalf("Error: %q", err)
}
expectedFilenames := []string{"index.js"}
{
fs, err := os.ReadDir(outputPath)
if err != nil {
t.Fatalf("Error: %q", err)
}
for i, f := range fs {
if f.Name() != expectedFilenames[i] {
t.Fatalf("wrong file name. expected: '%s' actual: '%s' f %+v", expectedFilenames[i], f.Name(), f)
}
}
}
for _, expectedFilename := range expectedFilenames {
expectedContents, err := os.ReadFile(filepath.Join(sourcePath, expectedFilename))
if err != nil {
t.Fatalf("Error: %q", err)
}
actualContents, err := os.ReadFile(filepath.Join(outputPath, expectedFilename))
if err != nil {
t.Fatalf("Error: %q", err)
}
if diff := cmp.Diff(string(actualContents), string(expectedContents)); diff != "" {
t.Fatalf("wrong file contents for filename '%s' . Differences: %s", expectedFilename, diff)
}
}
pms := []transformertypes.PathMapping{{
Type: transformertypes.DeletePathMappingType,
DestPath: outputIndexJSPath,
}}
if err := processPathMappings(pms, sourcePath, outputPath, true); err != nil {
t.Fatalf("failed to process the path mappings. Error: %q", err)
}
{
fs, err := os.ReadDir(outputPath)
if err != nil {
t.Fatalf("Error: %q", err)
}
for _, f := range fs {
if f.Name() != "source" {
t.Fatalf("wrong folder name. f %+v f.Name %s", f, f.Name())
}
}
}
{
fs, err := os.ReadDir(outputPath)
if err != nil {
t.Fatalf("Error: %q", err)
}
if len(fs) != 0 {
t.Fatalf("expected an empty output directory. actual: %+v", fs)
}
}
})
}
25 changes: 25 additions & 0 deletions transformer/testdata/k8s-yamls/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
resources:
limits:
cpu: "250m"
memory: "64Mi"
4 changes: 4 additions & 0 deletions transformer/testdata/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('hello!'));
app.listen(8080);
Loading

0 comments on commit c2396c4

Please sign in to comment.