Skip to content

Commit

Permalink
Add response check after trying to download script.
Browse files Browse the repository at this point in the history
  • Loading branch information
lirenjie95 committed Aug 7, 2024
1 parent a5289b6 commit 2956501
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
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 {
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)
assert.IsType(t, v1alpha2.COAError{}, err)
assert.NotEqual(t, http.StatusOK, err.(v1alpha2.COAError).State)
}

0 comments on commit 2956501

Please sign in to comment.