This is an automated email from the ASF dual-hosted git repository.

msciabarra pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/openserverless-cli.git


The following commit(s) were added to refs/heads/main by this push:
     new 2eba32b  test and debugging
2eba32b is described below

commit 2eba32b63baa45a71c27f5e3b0f754b45502f42e
Author: Michele Sciabarra <[email protected]>
AuthorDate: Mon Jul 29 17:12:30 2024 +0200

    test and debugging
---
 README.md                                        |  2 +
 main.go                                          | 19 ++++++--
 main_test.go                                     |  1 -
 nuv.go                                           |  4 +-
 prereq.go                                        | 37 ++++++++++----
 prereq_test.go                                   | 30 ++++--------
 tests/prereq.bats                                | 27 +++++++++--
 tests/{prereq.bats => prereq/olaris/nuvfile.yml} | 19 ++++----
 tests/prereq/olaris/nuvroot.json                 |  6 +++
 tests/prereq/olaris/prereq.yml                   | 61 ++++++++++++++++++++++++
 tests/vars.bats                                  |  1 -
 version.txt                                      |  2 +-
 12 files changed, 154 insertions(+), 55 deletions(-)

diff --git a/README.md b/README.md
index f0c5647..fd66cac 100644
--- a/README.md
+++ b/README.md
@@ -52,6 +52,8 @@ The following environment variables allows to ovverride 
certain defaults.
 - `DEBUG` if set enable debugging messages
 - `TRACE` when set gives mode detailes tracing informations, also enable 
DEBUG=1
 - `EXTRA` appends extra arguments to the task invocation - useful when you 
need to set extra variables with a nuvopts active.
+- `OS` overrides the value of the detected operating system - useful to test 
prereq scripts
+-  `ARCH` overrides the value of the detected architecture - useful to test 
prereq scripts
 - `OPS_HOME` is the home dir, defaults to `~/.nuv` if not defined
 - `NUV_ROOT` is the folder where `ops` looks for its tasks. If not defined, if 
will first look in current directory for an `olaris` folder otherwise download 
it from githut fron the `NUV_REPO` with a git clone
 - `OPS_BIN` is the folder where `ops` looks for binaries (external command 
line tools). If not defined, it defaults to `~/.nuv/<os>-<arch>/bin`. All the 
prerequisites are downloaded in this directory
diff --git a/main.go b/main.go
index ab0cff7..08621d3 100644
--- a/main.go
+++ b/main.go
@@ -74,9 +74,8 @@ func setupCmd(me string) (string, error) {
 
 func setupBinPath() error {
        // initialize tools (used by the shell to find myself)
-
        bindir, err := EnsureBindir()
-       if err != nil {
+       if err == nil {
                os.Setenv("PATH", fmt.Sprintf("%s%c%s", bindir, 
os.PathListSeparator, os.Getenv("PATH")))
                debugf("PATH=%s", os.Getenv("PATH"))
        }
@@ -151,6 +150,10 @@ func executeEmbeddedToolsAndExit(cmd string, args 
[]string, nuvHome string) int
                if err := setNuvOlarisHash(dir); err != nil {
                        log.Fatal("unable to set NUV_OLARIS...", err.Error())
                }
+               err = ensurePrereq(dir)
+               if err != nil {
+                       log.Fatalf("cannot download prerequisites: %v", err)
+               }
 
        case "retry":
                if err := tools.ExpBackoffRetry(args[1:]); err != nil {
@@ -301,6 +304,11 @@ func Main() {
                log.Fatalf("cannot apply env vars from configs: %s", 
err.Error())
        }
 
+       // preflight checks - we need at least ssh curl to proceed
+       if err := preflightChecks(); err != nil {
+               log.Fatalf("failed preflight check: %s", err.Error())
+       }
+
        // in case args[1] is a wsk wrapper command invoke it and exit
        if len(args) > 1 {
                if cmd, ok := IsWskWrapperCommand(args[1]); ok {
@@ -333,9 +341,10 @@ func Main() {
 
        // *************** executing tasks
 
-       // preflight checks - we need at least ssh and curl to proceed
-       if err := preflightChecks(); err != nil {
-               log.Fatalf("failed preflight check: %s", err.Error())
+       // ensure prerequisites
+       err = ensurePrereq(nuvRootDir)
+       if err != nil {
+               log.Fatalf("cannot ensure prerequisites: %s", err.Error())
        }
 
        if err := runNuv(nuvRootDir, args); err != nil {
diff --git a/main_test.go b/main_test.go
index c1b6cdd..79568b5 100644
--- a/main_test.go
+++ b/main_test.go
@@ -73,7 +73,6 @@ func TestMain(m *testing.M) {
        workDir, _ = filepath.Abs(wd)
        homeDir, _ = homedir.Dir()
        taskDryRun = true
-       ensurePrereq()
        os.Exit(m.Run())
 }
 
diff --git a/nuv.go b/nuv.go
index 8433880..47212f6 100644
--- a/nuv.go
+++ b/nuv.go
@@ -167,7 +167,7 @@ func Nuv(base string, args []string) error {
 
        isSubCmd := false
 
-       err = ensurePrereq()
+       err = ensurePrereq(base)
        if err != nil {
                fmt.Println("ERROR: cannot ensure prerequisites: " + 
err.Error())
                os.Exit(1)
@@ -191,7 +191,7 @@ func Nuv(base string, args []string) error {
                        if err := os.Chdir(taskName); err != nil {
                                return err
                        }
-                       err = ensurePrereq()
+                       err = ensurePrereq(pwd)
                        if err != nil {
                                fmt.Println("ERROR: cannot ensure 
prerequisites" + err.Error())
                                os.Exit(1)
diff --git a/prereq.go b/prereq.go
index b120c28..ae9ce12 100644
--- a/prereq.go
+++ b/prereq.go
@@ -50,12 +50,8 @@ func execPrereqTask(bindir string, name string) error {
 }
 
 // load prerequisites in current dir
-func loadPrereq() (Prereq, error) {
+func loadPrereq(dir string) (Prereq, error) {
        var prereq Prereq = Prereq{}
-       dir, err := os.Getwd()
-       if err != nil {
-               return prereq, err
-       }
 
        if !exists(dir, PREREQ) {
                return prereq, fmt.Errorf("not found %s", dir)
@@ -80,10 +76,18 @@ func loadPrereq() (Prereq, error) {
 // otherwise setup one in ~/nuv/<os>-<arch>/bin
 // and sets OPS_BIN
 func EnsureBindir() (string, error) {
+       os_ := os.Getenv("OS")
+       if os_ == "" {
+               os_ = runtime.GOOS
+       }
+       arch := os.Getenv("ARCH")
+       if arch == "" {
+               arch = runtime.GOARCH
+       }
        var err error = nil
        bindir := os.Getenv("OPS_BIN")
        if bindir == "" {
-               bindir, err = homedir.Expand(fmt.Sprintf("~/.nuv/%s-%s/bin", 
runtime.GOOS, runtime.GOARCH))
+               bindir, err = homedir.Expand(fmt.Sprintf("~/.nuv/%s-%s/bin", 
os_, arch))
                os.Setenv("OPS_BIN", bindir)
        }
        if err != nil {
@@ -164,8 +168,19 @@ func downloadPrereq(name string, task PrereqTask) error {
                touch(bindir, name)
                touchAndClean(bindir, name, version)
        } else {
+               fmt.Printf("ensuring prerequisite %s %s\n", name, version)
                execPrereqTask(bindir, name)
                // check if file and version exists
+
+               // fixing the name for windows adding .exe
+               os_ := os.Getenv("OS")
+               if os_ == "" {
+                       os_ = runtime.GOOS
+               }
+               if os_ == "windows" {
+                       name = name + ".exe"
+               }
+
                if !exists(bindir, name) {
                        return fmt.Errorf("failed to download %s %s", name, 
version)
                }
@@ -176,13 +191,17 @@ func downloadPrereq(name string, task PrereqTask) error {
 }
 
 // ensure prereq are satified looking at the prereq.yml
-func ensurePrereq() error {
+func ensurePrereq(root string) error {
        // skip prereq - useful for tests
        if os.Getenv("OPS_NO_PREREQ") != "" {
                return nil
        }
-       trace("ensurePrereq")
-       prereq, err := loadPrereq()
+       err := os.Chdir(root)
+       if err != nil {
+               return err
+       }
+       trace("ensurePrereq in", root)
+       prereq, err := loadPrereq(root)
        for task := range prereq.Tasks {
                err = downloadPrereq(task, prereq.Tasks[task])
                if err != nil {
diff --git a/prereq_test.go b/prereq_test.go
index 7e34ab5..ecd15d7 100644
--- a/prereq_test.go
+++ b/prereq_test.go
@@ -3,8 +3,6 @@ package openserverless
 import (
        "fmt"
        "os"
-
-       "github.com/mitchellh/go-homedir"
 )
 
 func Example_execPrereqTask() {
@@ -16,12 +14,10 @@ func Example_execPrereqTask() {
 
 func Example_loadPrereq() {
        //downloadPrereq("")
-       os.Chdir(workDir)
-       os.Chdir("tests")
-       _, err := loadPrereq()
+       dir := joinpath(workDir, "tests")
+       _, err := loadPrereq(dir)
        fmt.Println(npath(err.Error()))
-       os.Chdir("prereq")
-       prq, err := loadPrereq()
+       prq, err := loadPrereq(joinpath(dir, "prereq"))
        //fmt.Println(prq)
        fmt.Println(err, *prq.Tasks["bun"].Description, 
prq.Tasks["bun"].Vars["VERSION"])
        // Output:
@@ -57,18 +53,15 @@ func Example_touchAndClean() {
 }
 
 func Example_downloadPrereq() {
-       bindir, _ := homedir.Expand("~/.nuv/bin")
+       bindir, _ := EnsureBindir()
        os.RemoveAll(bindir)
-       os.Chdir(workDir)
-       os.Chdir("tests")
-       os.Chdir("prereq")
 
-       prq, _ := loadPrereq()
+       prqdir := joinpath(joinpath(workDir, "tests"), "prereq")
+       prq, _ := loadPrereq(prqdir)
        fmt.Println("1", downloadPrereq("bun", prq.Tasks["bun"]))
        fmt.Println("2", downloadPrereq("bun", prq.Tasks["bun"]))
 
-       os.Chdir("sub")
-       prq, _ = loadPrereq()
+       prq, _ = loadPrereq(joinpath(prqdir, "sub"))
        //fmt.Println(prq)
        //fmt.Println(PrereqSeenMap)
        fmt.Println("3", downloadPrereq("bun", prq.Tasks["bun"]))
@@ -83,12 +76,9 @@ func Example_downloadPrereq() {
 func Example_ensurePrereq() {
        bindir, _ := EnsureBindir()
        os.RemoveAll(bindir)
-       os.Chdir(workDir)
-       os.Chdir("tests")
-       os.Chdir("prereq")
-       fmt.Println(ensurePrereq())
-       os.Chdir("sub")
-       fmt.Println(ensurePrereq())
+       dir := joinpath(joinpath(workDir, "tests"), "prereq")
+       fmt.Println(ensurePrereq(dir))
+       fmt.Println(ensurePrereq(joinpath(dir, "sub")))
        // Output:
        // downloading bun v1.11.20
        // downloading coreutils 0.0.27
diff --git a/tests/prereq.bats b/tests/prereq.bats
index 36d8cd1..6725b3e 100644
--- a/tests/prereq.bats
+++ b/tests/prereq.bats
@@ -20,10 +20,27 @@ setup() {
     load 'test_helper/bats-assert/load'
     export NO_COLOR=1
     export NUV_BRANCH="0.1.0-testing"
-    rm -Rvf ~/.nuv
+    cd prereq
 }
 
-#@test "ops prereq" {
-#    #rm -Rvf ~/.nuv/bin
-#    #TRACE=1 ops
-#}
+@test "download others" {
+    rm -Rvf ~/.nuv/
+    for o in linux darwin
+    do for a in arm64 amd64
+       do  
+          run env OS=$o ARCH=$a ops
+          assert_line "ensuring prerequisite 7zz" --partial
+          assert test -e ~/.nuv/$o-$a/bin/7zz
+       done
+    done 
+    run env OS=windows ops
+    assert test -e ~/.nuv/windows-*/bin/7zz.exe
+}
+
+@test "ops prereq" {
+    rm -Rvf ~/.nuv/
+    run ops
+    assert_line "ensuring prerequisite 7zz" --partial
+    run ops info
+    assert_line "7-˜Zip" --partial
+}
diff --git a/tests/prereq.bats b/tests/prereq/olaris/nuvfile.yml
similarity index 76%
copy from tests/prereq.bats
copy to tests/prereq/olaris/nuvfile.yml
index 36d8cd1..dbe7828 100644
--- a/tests/prereq.bats
+++ b/tests/prereq/olaris/nuvfile.yml
@@ -14,16 +14,13 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
+#
+
+version: "3"
+tasks:
 
-setup() {
-    load 'test_helper/bats-support/load'
-    load 'test_helper/bats-assert/load'
-    export NO_COLOR=1
-    export NUV_BRANCH="0.1.0-testing"
-    rm -Rvf ~/.nuv
-}
+  info:
+    desc: info
+    cmds:
+    - 7zz | grep 7-Zip
 
-#@test "ops prereq" {
-#    #rm -Rvf ~/.nuv/bin
-#    #TRACE=1 ops
-#}
diff --git a/tests/prereq/olaris/nuvroot.json b/tests/prereq/olaris/nuvroot.json
new file mode 100644
index 0000000..417f7fe
--- /dev/null
+++ b/tests/prereq/olaris/nuvroot.json
@@ -0,0 +1,6 @@
+{
+    "version": "0.1.0-testing",
+    "config": {
+        "key": "value"
+    }
+}
\ No newline at end of file
diff --git a/tests/prereq/olaris/prereq.yml b/tests/prereq/olaris/prereq.yml
new file mode 100644
index 0000000..22263d6
--- /dev/null
+++ b/tests/prereq/olaris/prereq.yml
@@ -0,0 +1,61 @@
+version: 3
+
+vars:
+  OS: "{{or .OS OS}}"
+  ARCH: "{{or .ARCH ARCH}}"
+
+tasks:
+
+  7zz:
+    desc: 7zr
+    vars:
+      VERSION: "2407"
+      SUFFIX:
+         sh: |
+             case "{{.OS}}-{{.ARCH}}" in
+              linux-amd64) echo "7z{{.VERSION}}-linux-x64.tar.xz" ;;
+              linux-arm64) echo "7z{{.VERSION}}-linux-arm64.tar.xz" ;;
+              darwin-*) echo "7z{{.VERSION}}-mac.tar.xz" ;;
+              windows-*) echo "7zr.exe" ;;
+              *) echo "unknown" ;;
+             esac
+      URL: "https://7-zip.org/a/{{.SUFFIX}}";
+      FILE: "{{base .URL}}"
+    cmds:
+    - curl -sL "{{.URL}}"  -o "{{.FILE}}"
+    - |
+      if test "{{.OS}}" == "windows" 
+      then mv 7zr.exe 7zz.exe
+      else tar xJvf "{{.FILE}}" 7zz
+           rm "{{.FILE}}"
+      fi
+ 
+
+  bun:
+    description: bun
+    vars:
+      XVERSION: "v1.11.20"
+    cmds:
+    - task: _bun
+
+
+  coreutils:
+    desc: coreutils
+    vars:
+       XVERSION: "0.0.27"
+       SUFFIX:
+         sh: |
+             case "{{ARCH}}-{{OS}}" in
+              amd64-linux) echo "i686-unknown-linux-gnu.tar.gz" ;;
+              arm64-linux) echo "aarch64-unknown-linux-gnu.tar.gz" ;;
+              amd64-darwin) echo "x86_64-apple-darwin.tar.gz" ;;
+              arm64-darwin) echo "aarch64-apple-darwin.tar.gz" ;; 
+              amd64-windows) echo "x86_64-pc-windows-gnu.zip" ;;
+              *) echo "unknown" ;;
+             esac
+       URL: 
"https://github.com/uutils/coreutils/releases/download/{{.VERSION}}/coreutils-{{.VERSION}}-{{.SUFFIX}}";
 
+    cmds:
+     - echo "{{.URL}}"
+     #- curl -sL "{{.URL}}" -o coreutils.tar.gz
+
+
diff --git a/tests/vars.bats b/tests/vars.bats
index 44f9735..679ee14 100644
--- a/tests/vars.bats
+++ b/tests/vars.bats
@@ -21,7 +21,6 @@ setup() {
     export NO_COLOR=1
 }
 
-
 @test "simple" {
     run nuv sub vars simple
     # use top level
diff --git a/version.txt b/version.txt
index ab6e780..25d5a22 100644
--- a/version.txt
+++ b/version.txt
@@ -1 +1 @@
-0.1.0-testing.2407291118
+0.1.0-testing.2407291424

Reply via email to