Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response check after trying to download script. #379

Merged
merged 5 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions api/pkg/apis/v1alpha1/providers/stage/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -112,17 +111,29 @@ func downloadFile(scriptFolder string, script string, stagingFolder string) erro
}
tPath := filepath.Join(stagingFolder, script)

out, err := os.Create(tPath)
resp, err := http.Get(sPath)
if err != nil {
return err
}
defer out.Close()
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
lirenjie95 marked this conversation as resolved.
Show resolved Hide resolved
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return v1alpha2.NewCOAError(
nil,
"Response body content: "+string(body),
v1alpha2.State(resp.StatusCode),
)
}

resp, err := http.Get(sPath)
out, err := os.Create(tPath)
if err != nil {
return err
}
defer resp.Body.Close()
defer out.Close()

_, err = io.Copy(out, resp.Body)
if err != nil {
return err
Expand Down Expand Up @@ -154,7 +165,7 @@ func (i *ScriptStageProvider) Process(ctx context.Context, mgrContext contexts.M

staging := filepath.Join(i.Config.StagingFolder, input)
file, _ := json.MarshalIndent(inputs, "", " ")
_ = ioutil.WriteFile(staging, file, 0644)
_ = os.WriteFile(staging, file, 0644)

abs, _ := filepath.Abs(staging)

Expand All @@ -177,7 +188,7 @@ func (i *ScriptStageProvider) Process(ctx context.Context, mgrContext contexts.M
outputStaging := filepath.Join(i.Config.StagingFolder, output)

var data []byte
data, err = ioutil.ReadFile(outputStaging)
data, err = os.ReadFile(outputStaging)

if err != nil {
sLog.Errorf(" P (Script Stage): failed to parse get script output (expected map[string]interface{}): %+v", err)
Expand Down
16 changes: 16 additions & 0 deletions api/pkg/apis/v1alpha1/providers/stage/script/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ package script

import (
"context"
"net/http"
"os"
"testing"

"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/contexts"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -61,3 +63,17 @@ func TestShellScriptOnline(t *testing.T) {
assert.Nil(t, err)
os.Remove("staging/go1.21.6.src.tar.gz")
}

func TestShellScriptNotFoundOnline(t *testing.T) {
provider := ScriptStageProvider{}
err := provider.Init(ScriptStageProviderConfig{
Name: "test",
Script: "test.ps1",
ScriptEngine: "powershell",
ScriptFolder: "https://bing.com",
StagingFolder: "staging",
})
assert.NotNil(t, err)
lirenjie95 marked this conversation as resolved.
Show resolved Hide resolved
assert.IsType(t, v1alpha2.COAError{}, err)
assert.NotEqual(t, http.StatusOK, err.(v1alpha2.COAError).State)
}
Loading