github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cmd/publish-artifacts/main_test.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package main
    12  
    13  import (
    14  	"os"
    15  	"regexp"
    16  	"testing"
    17  
    18  	"github.com/aws/aws-sdk-go/service/s3"
    19  	"github.com/kr/pretty"
    20  )
    21  
    22  // Whether to run slow tests.
    23  var slow bool
    24  
    25  func init() {
    26  	if err := os.Setenv("AWS_ACCESS_KEY_ID", "testing"); err != nil {
    27  		panic(err)
    28  	}
    29  	if err := os.Setenv("AWS_SECRET_ACCESS_KEY", "hunter2"); err != nil {
    30  		panic(err)
    31  	}
    32  }
    33  
    34  type recorder struct {
    35  	reqs []s3.PutObjectInput
    36  }
    37  
    38  func (r *recorder) PutObject(req *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
    39  	r.reqs = append(r.reqs, *req)
    40  	return &s3.PutObjectOutput{}, nil
    41  }
    42  
    43  func mockPutter(p s3putter) func() {
    44  	origPutter := testableS3
    45  	f := func() {
    46  		testableS3 = origPutter
    47  	}
    48  	testableS3 = func() (s3putter, error) {
    49  		return p, nil
    50  	}
    51  	return f
    52  }
    53  
    54  func TestMain(t *testing.T) {
    55  	if !slow {
    56  		t.Skip("only to be run manually via `./build/builder.sh go test -tags slow -timeout 1h -v ./pkg/cmd/publish-artifacts`")
    57  	}
    58  	r := &recorder{}
    59  	undo := mockPutter(r)
    60  	defer undo()
    61  
    62  	shaPat := regexp.MustCompile(`[a-f0-9]{40}`)
    63  	const shaStub = "<sha>"
    64  
    65  	type testCase struct {
    66  		Bucket, ContentDisposition, Key, WebsiteRedirectLocation, CacheControl string
    67  	}
    68  	exp := []testCase{
    69  		{
    70  			Bucket:             "cockroach",
    71  			ContentDisposition: "attachment; filename=cockroach.darwin-amd64." + shaStub,
    72  			Key:                "/cockroach/cockroach.darwin-amd64." + shaStub,
    73  		},
    74  		{
    75  			Bucket:                  "cockroach",
    76  			CacheControl:            "no-cache",
    77  			Key:                     "cockroach/cockroach.darwin-amd64.LATEST",
    78  			WebsiteRedirectLocation: "/cockroach/cockroach.darwin-amd64." + shaStub,
    79  		},
    80  		{
    81  			Bucket:             "cockroach",
    82  			ContentDisposition: "attachment; filename=cockroach.linux-gnu-amd64." + shaStub,
    83  			Key:                "/cockroach/cockroach.linux-gnu-amd64." + shaStub,
    84  		},
    85  		{
    86  			Bucket:                  "cockroach",
    87  			CacheControl:            "no-cache",
    88  			Key:                     "cockroach/cockroach.linux-gnu-amd64.LATEST",
    89  			WebsiteRedirectLocation: "/cockroach/cockroach.linux-gnu-amd64." + shaStub,
    90  		},
    91  		{
    92  			Bucket:             "cockroach",
    93  			ContentDisposition: "attachment; filename=cockroach.race.linux-gnu-amd64." + shaStub,
    94  			Key:                "/cockroach/cockroach.race.linux-gnu-amd64." + shaStub,
    95  		},
    96  		{
    97  			Bucket:                  "cockroach",
    98  			CacheControl:            "no-cache",
    99  			Key:                     "cockroach/cockroach.race.linux-gnu-amd64.LATEST",
   100  			WebsiteRedirectLocation: "/cockroach/cockroach.race.linux-gnu-amd64." + shaStub,
   101  		},
   102  		{
   103  			Bucket:             "cockroach",
   104  			ContentDisposition: "attachment; filename=cockroach.windows-amd64." + shaStub + ".exe",
   105  			Key:                "/cockroach/cockroach.windows-amd64." + shaStub + ".exe",
   106  		},
   107  		{
   108  			Bucket:                  "cockroach",
   109  			CacheControl:            "no-cache",
   110  			Key:                     "cockroach/cockroach.windows-amd64.LATEST",
   111  			WebsiteRedirectLocation: "/cockroach/cockroach.windows-amd64." + shaStub + ".exe",
   112  		},
   113  		{
   114  			Bucket:             "cockroach",
   115  			ContentDisposition: "attachment; filename=workload." + shaStub,
   116  			Key:                "/cockroach/workload." + shaStub,
   117  		},
   118  		{
   119  			Bucket:                  "cockroach",
   120  			CacheControl:            "no-cache",
   121  			Key:                     "cockroach/workload.LATEST",
   122  			WebsiteRedirectLocation: "/cockroach/workload." + shaStub,
   123  		},
   124  		{
   125  			Bucket: "binaries.cockroachdb.com",
   126  			Key:    "cockroach-v42.42.42.src.tgz",
   127  		},
   128  		{
   129  			Bucket:       "binaries.cockroachdb.com",
   130  			CacheControl: "no-cache",
   131  			Key:          "cockroach-latest.src.tgz",
   132  		},
   133  		{
   134  			Bucket: "binaries.cockroachdb.com",
   135  			Key:    "cockroach-v42.42.42.darwin-10.9-amd64.tgz",
   136  		},
   137  		{
   138  			Bucket:       "binaries.cockroachdb.com",
   139  			CacheControl: "no-cache",
   140  			Key:          "cockroach-latest.darwin-10.9-amd64.tgz",
   141  		},
   142  		{
   143  			Bucket: "binaries.cockroachdb.com",
   144  			Key:    "cockroach-v42.42.42.linux-amd64.tgz",
   145  		},
   146  		{
   147  			Bucket:       "binaries.cockroachdb.com",
   148  			CacheControl: "no-cache",
   149  			Key:          "cockroach-latest.linux-amd64.tgz",
   150  		},
   151  		{
   152  			Bucket: "binaries.cockroachdb.com",
   153  			Key:    "cockroach-v42.42.42.windows-6.2-amd64.zip",
   154  		},
   155  		{
   156  			Bucket:       "binaries.cockroachdb.com",
   157  			CacheControl: "no-cache",
   158  			Key:          "cockroach-latest.windows-6.2-amd64.zip",
   159  		},
   160  	}
   161  
   162  	if err := os.Setenv("TC_BUILD_BRANCH", "master"); err != nil {
   163  		t.Fatal(err)
   164  	}
   165  	main()
   166  
   167  	if err := os.Setenv("TC_BUILD_BRANCH", "v42.42.42"); err != nil {
   168  		t.Fatal(err)
   169  	}
   170  	*isRelease = true
   171  	main()
   172  
   173  	var acts []testCase
   174  	for _, req := range r.reqs {
   175  		var act testCase
   176  		if req.Bucket != nil {
   177  			act.Bucket = *req.Bucket
   178  		}
   179  		if req.ContentDisposition != nil {
   180  			act.ContentDisposition = shaPat.ReplaceAllLiteralString(*req.ContentDisposition, shaStub)
   181  		}
   182  		if req.Key != nil {
   183  			act.Key = shaPat.ReplaceAllLiteralString(*req.Key, shaStub)
   184  		}
   185  		if req.WebsiteRedirectLocation != nil {
   186  			act.WebsiteRedirectLocation = shaPat.ReplaceAllLiteralString(*req.WebsiteRedirectLocation, shaStub)
   187  		}
   188  		if req.CacheControl != nil {
   189  			act.CacheControl = *req.CacheControl
   190  		}
   191  		acts = append(acts, act)
   192  	}
   193  
   194  	for i := len(exp); i < len(acts); i++ {
   195  		exp = append(exp, testCase{})
   196  	}
   197  	for i := len(acts); i < len(exp); i++ {
   198  		acts = append(acts, testCase{})
   199  	}
   200  
   201  	if len(pretty.Diff(acts, exp)) > 0 {
   202  		t.Error("diff(act, exp) is nontrivial")
   203  		pretty.Ldiff(t, acts, exp)
   204  	}
   205  }