go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipkg/base/actions/url_test.go (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package actions
    16  
    17  import (
    18  	"context"
    19  	_ "embed"
    20  	"fmt"
    21  	"io"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"os"
    25  	"path/filepath"
    26  	"testing"
    27  
    28  	"go.chromium.org/luci/cipkg/core"
    29  	"go.chromium.org/luci/cipkg/internal/testutils"
    30  
    31  	. "github.com/smartystreets/goconvey/convey"
    32  )
    33  
    34  func TestProcessURL(t *testing.T) {
    35  	Convey("Test action processor for url", t, func() {
    36  		ap := NewActionProcessor()
    37  		pm := testutils.NewMockPackageManage("")
    38  
    39  		url := &core.ActionURLFetch{
    40  			Url:           "https://host.not.exist/123",
    41  			HashAlgorithm: core.HashAlgorithm_HASH_SHA256,
    42  			HashValue:     "abcdef",
    43  		}
    44  
    45  		pkg, err := ap.Process("", pm, &core.Action{
    46  			Name: "url",
    47  			Spec: &core.Action_Url{Url: url},
    48  		})
    49  		So(err, ShouldBeNil)
    50  
    51  		checkReexecArg(pkg.Derivation.Args, url)
    52  	})
    53  }
    54  
    55  func TestExecuteURL(t *testing.T) {
    56  	Convey("Test execute action url", t, func() {
    57  		ctx := context.Background()
    58  		out := t.TempDir()
    59  
    60  		s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    61  			fmt.Fprint(w, "something")
    62  		}))
    63  		defer s.Close()
    64  
    65  		Convey("Test download file", func() {
    66  			a := &core.ActionURLFetch{
    67  				Url: s.URL,
    68  			}
    69  
    70  			err := ActionURLFetchExecutor(ctx, a, out)
    71  			So(err, ShouldBeNil)
    72  
    73  			{
    74  				f, err := os.Open(filepath.Join(out, "file"))
    75  				So(err, ShouldBeNil)
    76  				defer f.Close()
    77  				b, err := io.ReadAll(f)
    78  				So(err, ShouldBeNil)
    79  				So(string(b), ShouldEqual, "something")
    80  			}
    81  		})
    82  
    83  		Convey("Test download file with hash verify", func() {
    84  			a := &core.ActionURLFetch{
    85  				Url:           s.URL,
    86  				HashAlgorithm: core.HashAlgorithm_HASH_SHA256,
    87  				HashValue:     "3fc9b689459d738f8c88a3a48aa9e33542016b7a4052e001aaa536fca74813cb",
    88  			}
    89  
    90  			err := ActionURLFetchExecutor(ctx, a, out)
    91  			So(err, ShouldBeNil)
    92  
    93  			{
    94  				f, err := os.Open(filepath.Join(out, "file"))
    95  				So(err, ShouldBeNil)
    96  				defer f.Close()
    97  				b, err := io.ReadAll(f)
    98  				So(err, ShouldBeNil)
    99  				So(string(b), ShouldEqual, "something")
   100  			}
   101  		})
   102  
   103  		Convey("Test download file with hash verify failed", func() {
   104  			a := &core.ActionURLFetch{
   105  				Url:           s.URL,
   106  				HashAlgorithm: core.HashAlgorithm_HASH_SHA256,
   107  				HashValue:     "abcdef",
   108  			}
   109  
   110  			err := ActionURLFetchExecutor(ctx, a, out)
   111  			So(err, ShouldNotBeNil)
   112  			So(err.Error(), ShouldContainSubstring, "hash mismatch")
   113  		})
   114  	})
   115  }
   116  
   117  func TestReexecURL(t *testing.T) {
   118  	Convey("Test re-execute action processor for url", t, func() {
   119  		ap := NewActionProcessor()
   120  		pm := testutils.NewMockPackageManage("")
   121  		ctx := context.Background()
   122  		out := t.TempDir()
   123  
   124  		s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   125  			fmt.Fprint(w, "something")
   126  		}))
   127  		defer s.Close()
   128  
   129  		pkg, err := ap.Process("", pm, &core.Action{
   130  			Name: "url",
   131  			Spec: &core.Action_Url{Url: &core.ActionURLFetch{
   132  				Url:           s.URL,
   133  				HashAlgorithm: core.HashAlgorithm_HASH_SHA256,
   134  				HashValue:     "3fc9b689459d738f8c88a3a48aa9e33542016b7a4052e001aaa536fca74813cb",
   135  			}},
   136  		})
   137  		So(err, ShouldBeNil)
   138  
   139  		runWithDrv(ctx, pkg.Derivation, out)
   140  
   141  		{
   142  			f, err := os.Open(filepath.Join(out, "file"))
   143  			So(err, ShouldBeNil)
   144  			defer f.Close()
   145  			b, err := io.ReadAll(f)
   146  			So(err, ShouldBeNil)
   147  			So(string(b), ShouldEqual, "something")
   148  		}
   149  	})
   150  }