示例#1
0
func newUploader(opts *Options, log *logrus.Logger) *uploader {
	var provider uploadProvider

	if opts.CacheControl == "" {
		opts.CacheControl = defaultPublicCacheControl
	}

	if opts.Provider == "" {
		opts.Provider = "s3"
	}

	switch opts.Provider {
	case "artifacts":
		provider = newArtifactsProvider(opts, log)
	case "s3":
		provider = newS3Provider(opts, log)
	case "null":
		provider = newNullProvider([]string{})
	default:
		log.WithFields(logrus.Fields{
			"provider": opts.Provider,
		}).Warn("unrecognized provider, using s3 instead")
		provider = newS3Provider(opts, log)
	}

	u := &uploader{
		Opts:     opts,
		Paths:    path.NewPathSet(),
		Provider: provider,

		log: log,
	}

	for _, s := range opts.Paths {
		parts := strings.SplitN(s, ":", 2)
		if len(parts) < 2 {
			parts = append(parts, "")
		}

		p := path.New(opts.WorkingDir, parts[0], parts[1])
		log.WithFields(logrus.Fields{"path": p}).Debug("adding path")
		u.Paths.Add(p)
	}

	return u
}
示例#2
0
func TestArtifactContentType(t *testing.T) {
	for filepath, expectedCtype := range testArtifactPaths {
		p := path.New("whatever", filepath, "somewhere")
		a := New(p, "bucket", "linux/foo", &Options{
			Perm:     s3.PublicRead,
			RepoSlug: "owner/foo",
		})
		if a == nil {
			t.Errorf("new artifact is nil")
		}

		actualCtype := a.ContentType()
		if expectedCtype != actualCtype {
			t.Errorf("%v != %v", expectedCtype, actualCtype)
		}
	}
}
示例#3
0
func TestArtifactReader(t *testing.T) {
	for filepath := range testArtifactPaths {
		p := path.New("whatever", filepath, "somewhere")
		a := New(p, "bucket", "linux/foo", &Options{
			Perm:     s3.PublicRead,
			RepoSlug: "owner/foo",
		})
		if a == nil {
			t.Errorf("new artifact is nil")
		}

		reader, err := a.Reader()
		if err != nil {
			t.Error(err)
		}

		_, err = ioutil.ReadAll(reader)
		if err != nil {
			t.Error(err)
		}
	}
}
示例#4
0
func TestNewArtifact(t *testing.T) {
	p := path.New("/", "foo", "bar")
	a := New(p, "bucket", "linux/foo", &Options{
		Perm:     s3.PublicRead,
		RepoSlug: "owner/foo",
	})
	if a == nil {
		t.Errorf("new artifact is nil")
	}

	if a.Path != p {
		t.Errorf("path not set correctly: %v", a.Path)
	}

	if a.Prefix != "bucket" {
		t.Errorf("prefix not set correctly: %v", a.Prefix)
	}

	if a.Destination != "linux/foo" {
		t.Errorf("destination not set correctly: %v", a.Destination)
	}

	if a.Perm != s3.PublicRead {
		t.Errorf("s3 perm not set correctly: %v", a.Perm)
	}

	if a.UploadResult == nil {
		t.Errorf("result not initialized")
	}

	if a.UploadResult.OK {
		t.Errorf("result initialized with OK as true")
	}

	if a.UploadResult.Err != nil {
		t.Errorf("result initialized with non-nil Err")
	}
}