diff --git a/api/pkg/apis/v1alpha1/providers/stage/script/script.go b/api/pkg/apis/v1alpha1/providers/stage/script/script.go index 1d570c051..ab3abf071 100644 --- a/api/pkg/apis/v1alpha1/providers/stage/script/script.go +++ b/api/pkg/apis/v1alpha1/providers/stage/script/script.go @@ -11,7 +11,6 @@ import ( "encoding/json" "errors" "io" - "io/ioutil" "net/http" "net/url" "os" @@ -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 @@ -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) @@ -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) diff --git a/api/pkg/apis/v1alpha1/providers/stage/script/script_test.go b/api/pkg/apis/v1alpha1/providers/stage/script/script_test.go index d7e17acc6..0af81b625 100644 --- a/api/pkg/apis/v1alpha1/providers/stage/script/script_test.go +++ b/api/pkg/apis/v1alpha1/providers/stage/script/script_test.go @@ -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" ) @@ -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) +}