Skip to content

Commit

Permalink
Fix relative paths, add CI test (#37)
Browse files Browse the repository at this point in the history
* ✨ minor fixes

Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>

* ✨ add testing workflow

Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>

---------

Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>
  • Loading branch information
pranavgaikwad committed Aug 25, 2023
1 parent 49a4ef7 commit c9aa8af
Show file tree
Hide file tree
Showing 8 changed files with 2,975 additions and 17 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Demo Testing

on: ["push", "pull_request"]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Build image and binary
run: |
podman build -t localhost/kantra:latest -f Dockerfile .
go build -o kantra main.go
- name: Fetch sample applications
run: |
git clone https://github.com/konveyor/example-applications
git clone https://github.com/ivargrimstad/jakartaee-duke
- name: Run analysis test and copy output
run: |
RUNNER_IMG=localhost/kantra:latest ./kantra analyze --input $(pwd)/example-applications/example-1/ --output ./output/ --target cloud-readiness
- name: Fail if analysis output does not match expected
run: |
expected_file=./test-data/analysis-output.yaml
actual_file=./output/output.yaml
sed 's/^[ \t-]*//' $expected_file | sort > /tmp/expected_file
sed 's/^[ \t-]*//' $actual_file | sort > /tmp/actual_file
diff /tmp/expected_file /tmp/actual_file || diff $expected_file $actual_file
- name: Fail if dependencies output does not match expected
run: |
expected_file=./test-data/deps-output.yaml
actual_file=./output/dependencies.yaml
sed 's/^[ \t-]*//' $expected_file | sort > /tmp/expected_file
sed 's/^[ \t-]*//' $actual_file | sort > /tmp/actual_file
diff /tmp/expected_file /tmp/actual_file || diff $expected_file $actual_file
23 changes: 19 additions & 4 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -133,9 +134,16 @@ func (a *analyzeCommand) Validate() error {
}
stat, err := os.Stat(a.output)
if err != nil {
return fmt.Errorf("failed to stat output directory %s", a.output)
if errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(a.output, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create output dir %s", a.output)
}
} else {
return fmt.Errorf("failed to stat output directory %s", a.output)
}
}
if !stat.IsDir() {
if stat != nil && !stat.IsDir() {
return fmt.Errorf("output path %s is not a directory", a.output)
}
stat, err = os.Stat(a.input)
Expand All @@ -157,6 +165,13 @@ func (a *analyzeCommand) Validate() error {
a.mode != string(provider.SourceOnlyAnalysisMode) {
return fmt.Errorf("mode must be one of 'full' or 'source-only'")
}
// try to get abs path, if not, continue with relative path
if absPath, err := filepath.Abs(a.output); err == nil {
a.output = absPath
}
if absPath, err := filepath.Abs(a.input); err == nil {
a.input = absPath
}
return nil
}

Expand Down Expand Up @@ -482,8 +497,8 @@ func (a *analyzeCommand) RunAnalysis(ctx context.Context) error {
}
defer dependencyLog.Close()

a.log.Info("running source code analysis",
"log", analysisLogFilePath, "input", a.input, "output", a.output, "args", strings.Join(args, " "))
a.log.Info("running source code analysis", "log", analysisLogFilePath,
"input", a.input, "output", a.output, "args", strings.Join(args, " "), "volumes", volumes)
// TODO (pgaikwad): run analysis & deps in parallel
err = NewContainer().Run(
ctx,
Expand Down
4 changes: 3 additions & 1 deletion cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
Expand Down Expand Up @@ -163,7 +164,8 @@ func (c *container) Run(ctx context.Context, opts ...Option) error {
}
for sourcePath, destPath := range c.volumes {
args = append(args, "-v")
args = append(args, fmt.Sprintf("%s:%s:Z", sourcePath, destPath))
args = append(args, fmt.Sprintf("%s:%s:Z",
filepath.Clean(sourcePath), filepath.Clean(destPath)))
}
for k, v := range c.env {
args = append(args, "--env")
Expand Down
6 changes: 5 additions & 1 deletion cmd/openrewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -60,7 +61,6 @@ func (o *openRewriteCommand) Validate() error {
if o.listTargets {
return nil
}

stat, err := os.Stat(o.input)
if err != nil {
return fmt.Errorf("failed to stat input directory %s", o.input)
Expand All @@ -76,6 +76,10 @@ func (o *openRewriteCommand) Validate() error {
if _, found := recipes[o.target]; !found {
return fmt.Errorf("unsupported target recipe. use --list-targets to get list of all recipes")
}
// try to get abs path, if not, continue with relative path
if absPath, err := filepath.Abs(o.input); err == nil {
o.input = absPath
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
logrusLog := logrus.New()
logrusLog.SetOutput(os.Stdout)
logrusLog.SetFormatter(&logrus.TextFormatter{})
logrusLog.SetLevel(logrus.Level(logLevel))
logrusLog.SetLevel(logrus.InfoLevel)
log := logrusr.New(logrusLog)

rootCmd.AddCommand(NewTransformCommand(log))
Expand Down
36 changes: 26 additions & 10 deletions cmd/shimconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/apex/log"
"github.com/go-logr/logr"
Expand Down Expand Up @@ -58,25 +60,37 @@ func NewWindupShimCommand(log logr.Logger) *cobra.Command {
func (w *windupShimCommand) Validate() error {
outputStat, err := os.Stat(w.output)
if err != nil {
return err
if errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(w.output, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create output dir %s", w.output)
}
} else {
return fmt.Errorf("failed to stat output directory %s", w.output)
}
}
if !outputStat.IsDir() {
if outputStat != nil && !outputStat.IsDir() {
log.Errorf("output path %s is not a directory", w.output)
return err
}
if w.input == nil || len(w.input) == 0 {
return fmt.Errorf("input for rule file or directory must not be empty")
}
// try to get abs path, if not, continue with relative path
if absPath, err := filepath.Abs(w.output); err == nil {
w.output = absPath
}
for idx := range w.input {
if absPath, err := filepath.Abs(w.input[idx]); err == nil {
w.input[idx] = absPath
}
}
return nil
}

func (w *windupShimCommand) getRulesVolumes(tempRuleDir string) (map[string]string, error) {
rulesVolumes := make(map[string]string)
mountTempDir := false
err := os.Mkdir(tempRuleDir, os.ModePerm)
if err != nil {
return nil, err
}
for _, r := range w.input {
stat, err := os.Stat(r)
if err != nil {
Expand Down Expand Up @@ -104,15 +118,16 @@ func (w *windupShimCommand) getRulesVolumes(tempRuleDir string) (map[string]stri
}

func (w *windupShimCommand) Run(ctx context.Context) error {
wd, err := os.Getwd()
tempDir, err := os.MkdirTemp("", "transform-rules-")
if err != nil {
w.log.V(5).Error(err, "failed to create temp dir for rules")
return err
}
tempXMLRulesDir := filepath.Join(wd, "xmlrules")
defer os.RemoveAll(tempDir)
volumes := map[string]string{
w.output: ShimOutputPath,
}
ruleVols, err := w.getRulesVolumes(tempXMLRulesDir)
ruleVols, err := w.getRulesVolumes(tempDir)
if err != nil {
w.log.V(5).Error(err, "failed to get xml rules for conversion")
return err
Expand All @@ -123,6 +138,8 @@ func (w *windupShimCommand) Run(ctx context.Context) error {
fmt.Sprintf("--outputdir=%v", ShimOutputPath),
XMLRulePath,
}
w.log.Info("running windup-shim convert command",
"args", strings.Join(args, " "), "volumes", volumes, "output", w.output, "inputs", strings.Join(w.input, ","))
err = NewContainer().Run(
ctx,
WithVolumes(volumes),
Expand All @@ -132,6 +149,5 @@ func (w *windupShimCommand) Run(ctx context.Context) error {
if err != nil {
return err
}
defer os.RemoveAll(tempXMLRulesDir)
return nil
}
Loading

0 comments on commit c9aa8af

Please sign in to comment.