Пример #1
0
// TestAppV1Put
func TestAppV1Put(t *testing.T) {
	namespace := "containerops"
	repository := "official"
	a := models.ArtifactV1{}

	r, _ := models.NewAppV1(namespace, repository)
	err := r.Put(a)
	assert.Nil(t, err, "Fail to add an artifact to a repository")

	r.Locked = true
	err = r.Put(a)
	assert.NotNil(t, err, "Should not add an artifact to a locked repository")
}
Пример #2
0
// AppDeleteFileV1Handler remove a file from a repo
func AppDeleteFileV1Handler(ctx *macaron.Context) (int, []byte) {
	// setup the repository.
	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")
	r, err := models.NewAppV1(namespace, repository)
	if err != nil {
		log.Errorf("[%s] setup repository error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Setup Repository Error"})
		return http.StatusBadRequest, result
	}

	a := models.ArtifactV1{
		OS:   ctx.Params(":os"),
		Arch: ctx.Params(":arch"),
		App:  ctx.Params(":app"),
		Tag:  ctx.Params(":tag"),
	}

	// Remove from update service
	appV1, err := us.NewUpdateService("appV1", setting.Storage, setting.KeyManager)
	if err != nil {
		log.Errorf("[%s] create update service: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Create Update Service Error"})
		return http.StatusBadRequest, result
	}

	err = appV1.Delete(namespace+"/"+repository, a.GetName())
	if err != nil {
		log.Errorf("[%s] delete from update service error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Delete Update Service Error"})
		return http.StatusBadRequest, result
	}

	err = r.Delete(a)
	if err != nil {
		log.Errorf("[%s] delete artifact error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Delete Artifact Error"})
		return http.StatusBadRequest, result
	}

	return httpRet("AppV1 Delete data", nil, err)
}
Пример #3
0
// TestNewAppV1
func TestNewAppV1(t *testing.T) {
	namespace := "containerops"
	repository := "official"
	_, err := models.NewAppV1(namespace, repository)
	assert.Nil(t, err, "Fail to create/query a repository")
}
Пример #4
0
// AppPutFileV1Handler creates or updates a certain app
func AppPutFileV1Handler(ctx *macaron.Context) (int, []byte) {
	data, err := ctx.Req.Body().Bytes()
	if err != nil {
		log.Errorf("[%s] Req.Body.Bytes error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Req.Body.Bytes Error"})
		return http.StatusBadRequest, result
	}

	// Query or Create the repository.
	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")
	r, err := models.NewAppV1(namespace, repository)
	if err != nil {
		log.Errorf("[%s] query/create repository error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Query/Create Repository Error"})
		return http.StatusBadRequest, result
	}

	reqMethod := ctx.Req.Header.Get("Dockyard-Encrypt-Method")
	encryptMethod := utils.NewEncryptMethod(reqMethod)
	if encryptMethod == utils.EncryptNotSupported {
		log.Errorf("[%s] encrypt method %s is invalid", ctx.Req.RequestURI, reqMethod)

		result, _ := json.Marshal(map[string]string{"Error": "Invalid Encrypt Method"})
		return http.StatusBadRequest, result
	}

	a := models.ArtifactV1{
		OS:            ctx.Params(":os"),
		Arch:          ctx.Params(":arch"),
		App:           ctx.Params(":app"),
		Tag:           ctx.Params(":tag"),
		EncryptMethod: string(encryptMethod),
		Size:          int64(len(data)),
	}

	// Add to update service
	appV1, err := us.NewUpdateService("appV1", setting.Storage, setting.KeyManager)
	if err != nil {
		log.Errorf("[%s] create update service: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Create Update Service Error"})
		return http.StatusBadRequest, result
	}

	tmpPath, err := appV1.Put(namespace+"/"+repository, a.GetName(), data, encryptMethod)
	if err != nil {
		log.Errorf("[%s] put to update service error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Put Update Service Error"})
		return http.StatusBadRequest, result
	}

	// Although we record the local storage path (or object storage key), we do load it by UpdateService.
	a.Path = tmpPath
	err = r.Put(a)
	if err != nil {
		log.Errorf("[%s] put artifact error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "PUT Artifact Error"})
		return http.StatusBadRequest, result
	}

	return httpRet("AppV1 Put data", nil, err)
}